Skip to content

Commit

Permalink
- In #expect, pop Hash class from args if $MT_KWARGS_HACK. (casperisf…
Browse files Browse the repository at this point in the history
…ine)

- In above scenario, set expected kwargs (as Objects) based on actual kwargs.

[git-p4: depot-paths = "//src/minitest/dev/": change = 13456]
  • Loading branch information
zenspider committed Jun 24, 2022
1 parent 0b4c429 commit 2cc04bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/minitest/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def expect name, retval, args = [], **kwargs, &blk
else
raise ArgumentError, "args must be an array" unless Array === args

if ENV["MT_KWARGS_HAC\K"] && Hash === args.last then
if ENV["MT_KWARGS_HAC\K"] && (Hash === args.last ||
Hash == args.last) then
if kwargs.empty? then
kwargs = args.pop
else
Expand Down Expand Up @@ -167,6 +168,9 @@ def method_missing sym, *args, **kwargs, &block # :nodoc:
expected_args, expected_kwargs, retval, val_block =
expected_call.values_at(:args, :kwargs, :retval, :block)

expected_kwargs = kwargs.map { |ak, av| [ak, Object] }.to_h if
Hash == expected_kwargs

if val_block then
# keep "verify" happy
@actual_calls[sym] << expected_call
Expand Down
16 changes: 16 additions & 0 deletions test/minitest/test_minitest_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,22 @@ def with_kwargs_env
ENV.delete "MT_KWARGS_HAC\K"
end

def test_mock_allow_all_kwargs__old_style_env
with_kwargs_env do
mock = Minitest::Mock.new
mock.expect :foo, true, [Hash]
assert_equal true, mock.foo(bar: 42)
end
end

def test_mock_allow_all_kwargs__old_style_env__rewrite
with_kwargs_env do
mock = Minitest::Mock.new
mock.expect :foo, true, [], bar: Integer
assert_equal true, mock.foo(bar: 42)
end
end

def test_mock_block_is_passed_keyword_args__args__old_style_bad
arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
mock = Minitest::Mock.new
Expand Down

0 comments on commit 2cc04bf

Please sign in to comment.