Skip to content

Commit

Permalink
Ruby 3.0 args don't drag kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakilon committed Feb 2, 2021
1 parent a4a50fd commit 3fc22e7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ def backtrace # :nodoc:

def message # :nodoc:
bt = Minitest.filter_backtrace(self.backtrace)
bt = Minitest.filter_backtrace(self.cause.cause.backtrace) + bt if self.cause.cause
bt = Minitest.filter_backtrace(self.cause.cause.backtrace) + bt if self.cause && self.cause.cause
"#{self.error.class}: #{self.error.message}#{bt.map{ |s| "\n #{s}" }.join}"
end

Expand Down
4 changes: 2 additions & 2 deletions lib/minitest/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ def stub name, val_or_callable, *block_args

metaclass.send :alias_method, new_name, name

metaclass.send :define_method, name do |*args, &blk|
metaclass.send :define_method, name do |*args, **kwargs, &blk|
if val_or_callable.respond_to? :call then
val_or_callable.call(*args, &blk)
val_or_callable.call(*args, **kwargs, &blk)
else
blk.call(*block_args) if blk
val_or_callable
Expand Down

5 comments on commit 3fc22e7

@TSMMark
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes kwargs for me. Will you submit this as a PR to https://github.com/seattlerb/minitest?

@Nakilon
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TSMMark the proper PR would need some tests. When I did take a look at them there was some complex system of combining different types of args to cover all the cases and stuff was too complex for me to figure it out. This edit is trivial, just two lines long so one who can figure out how those minitest tests are organized can just copy it, no need in crediting.

@zenspider
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  class Keywords
    def self.args req, kw1:, kw2:24
      [req, kw1, kw2]
    end
  end

  def test_stub_callable_keyword_args
    Keywords.stub :args, ->(*args, **kws) { 42 } do
      @tc.assert_equal 42, Keywords.args("woot", kw1:314)
    end
  end

seems to cover this case. Am I missing anything?

@Nakilon
Copy link
Owner Author

@Nakilon Nakilon commented on 3fc22e7 Feb 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zenspider I think rather:

  class Keywords
    def self.args req, kw1:, kw2:24
      [req, kw1, kw2]
    end
  end

  def test_stub_callable_keyword_args
    Keywords.stub :args, ->(*args, **kws) { kws } do
      @tc.assert_equal {kw1: 314}, Keywords.args("woot", kw1:314)
    end
  end

Or:

  def test_stub_callable_keyword_args
    Thread.stub :new, ->(*args, **kwargs) { [args, kwargs] } do
      @tc.assert_equal [["woot"], {kw: 42}], Keywords.args("woot", kw: 42)
    end
  end

because the issue was in that it was [["woot", {kw: 42}], {}] instead of [["woot"], {kw: 42}].

P.S.: it's weird that it broke anything else as I see in your commit history. I don't remember if I ran the tests of minitest itself on other versions of Ruby but the tests of my software using minitests passed on all versions of Ruby.

@zenspider
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to roll out this patch because it broke in several other versions (all but 2.7?) .

Went with https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html

I've improved the test. Thank you.

Please sign in to comment.