Skip to content

Commit

Permalink
- Fix kwargs for expectations. (bobmazanec, blowmage)
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/minitest/dev/": change = 13586]
  • Loading branch information
zenspider committed Dec 3, 2022
1 parent 0b816d3 commit 87604fc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/minitest/spec.rb
Expand Up @@ -6,6 +6,10 @@ def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
dont_flip = false if block
target_obj = block ? '_{obj.method}' : '_(obj)'

# https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html
# Drop this when we can drop ruby 2.6 (aka after rails 6.1 EOL, ~2024-06)
kw_extra = "ruby2_keywords %p" % [new_name] if respond_to?(:ruby2_keywords, true)

# warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
self.class_eval <<-EOM, __FILE__, __LINE__ + 1
def #{new_name} *args
Expand All @@ -14,6 +18,7 @@ def #{new_name} *args
Kernel.warn "DEPRECATED: global use of #{new_name} from #\{where}. Use #{target_obj}.#{new_name} instead. This will fail in Minitest 6."
Minitest::Expectation.new(self, Minitest::Spec.current).#{new_name}(*args)
end
#{kw_extra}
EOM

Minitest::Expectation.class_eval <<-EOM, __FILE__, __LINE__ + 1
Expand All @@ -28,6 +33,7 @@ def #{new_name} *args
ctx.#{meth}(args.first, target, *args[1..-1])
end
end
#{kw_extra}
EOM
end
end
Expand Down
35 changes: 35 additions & 0 deletions test/minitest/test_minitest_spec.rb
Expand Up @@ -1068,3 +1068,38 @@ def test_value_monad_expect_alias
assert_equal "c", struct.expect
end
end

describe Minitest::Spec, :infect_an_assertion do
class << self
attr_accessor :infect_mock
end

def assert_infects exp, act, msg = nil, foo: nil, bar: nil
self.class.infect_mock.assert_infects exp, act, msg, foo: foo, bar: bar
end

infect_an_assertion :assert_infects, :must_infect
infect_an_assertion :assert_infects, :must_infect_without_flipping, :dont_flip

it "infects assertions with kwargs" do
mock = Minitest::Mock.new
mock.expect :assert_infects, true, [:exp, :act, nil], foo: :foo, bar: :bar

self.class.infect_mock = mock

_(:act).must_infect :exp, foo: :foo, bar: :bar

assert_mock mock
end

it "infects assertions with kwargs (dont_flip)" do
mock = Minitest::Mock.new
mock.expect :assert_infects, true, [:act, :exp, nil], foo: :foo, bar: :bar

self.class.infect_mock = mock

_(:act).must_infect_without_flipping :exp, foo: :foo, bar: :bar

assert_mock mock
end
end

0 comments on commit 87604fc

Please sign in to comment.