Repository: freerange/mocha (Ruby)

Issue:
Parameter matching fails on keyword arguments
Pretty basic but surprising bug that I ran into just now, where parameter matching on `expect(...).with(...)` doesn't work properly with keyword arguments.

```ruby
        class Foo
          def self.bar(a:, b:, c:)
            a + b + c
          end
        end

        def test_mocha_mocking_kw_args
          mock = mock("foo")
          mock.expects(:bar).with(a: 1, b: 2, c: is_a(Integer)).returns(6)

          mock.bar(a: 1, b: 2, c: 3)
        end
```

This results in the following error:

```
unexpected invocation: #<Mock:foo>.bar(:a => 1, :b => 2, :c => 3)
unsatisfied expectations:
- expected exactly once, invoked never: #<Mock:foo>.bar(:a => 1, :b => 2, :c => is_a(Integer))
```

The positional argument equivalent works just fine:

```ruby
        class Foo
          def self.bar(a, b, c)
            a + b + c
          end
        end

        def test_mocha_mocking_positional_args
          mock = mock("foo")
          mock.expects(:bar).with(1, 2, is_a(Integer)).returns(6)

          mock.bar(1, 2, 3)
        end
```

Mocha: v2.2.0
Ruby: 3.3.1


Discussion:
@CodingAnarchy 

Thanks for reporting this. It looks as if you've made a mistake in your examples - I imagine you intended to mock & call `Foo.bar` from the testx, but instead you are mocking and calling `Mock#bar`. Anyway, that doesn't take away from the fact that there seems to be an issue with the nested `is_a` parameter matcher - "nested", because I believe the kwargs are treated as a `Hash` internally. I've tried to more clearly illustrate the problem below - let me know if I've missed the mark:

```ruby
require "bundler/setup"
require "minitest/autorun"
require "mocha/minitest"
require "mocha/mock"

class FooTest < Minitest::Test
  def setup
    @mock = mock("foo")
  end

  def test_mocking_positional_args
    @mock.expects(:bar).with(1, 2, 3)

    @mock.bar(1, 2, 3)
  end

  def test_mocking_positional_args_with_nested_matcher
    @mock.expects(:bar).with(1, 2, is_a(Integer))

    @mock.bar(1, 2, 3)
  end

  def test_mocha_mocking_kw_args
    @mock.expects(:bar).with(a: 1, b: 2, c: 3)

    @mock.bar(a: 1, b: 2, c: 3)
  end

  def test_mocha_mocking_kw_args_with_nested_matcher
    @mock.expects(:bar).with(a: 1, b: 2, c: is_a(Integer))

    @mock.bar(a: 1, b: 2, c: 3)
    # unexpected invocation: #<Mock:foo>.bar(:a => 1, :b => 2, :c => 3)
    # unsatisfied expectations:
    # - expected exactly once, invoked never: #<Mock:foo>.bar(:a => 1, :b => 2, :c => is_a(Integer))
  end
end
```

@floehopper Yeah, I meant to mock `Foo#bar` there. Thanks for catching that.

Your example does demonstrate the issue I ran into though, so I think you've got it spot on in the replication.
I think I've worked out what the problem is. I'm in a bit of a rush (on a train!), but I've pushed up a tentative fix in [this branch](https://github.com/freerange/mocha/tree/fix-nested-parameter-matching-for-keyword-arguments). If you're in a position to try this out, that would be brilliant. I'm in meetings most of today, but I'll try to work up a full fix as soon as I can. Thanks so much for reporting the problem - I'm somewhat surprised nobody has spotted it sooner!
I just opened this PR to fix this: https://github.com/freerange/mocha/pull/648
Released in [v2.3.0](https://rubygems.org/gems/mocha/versions/2.3.0).



Patch:
diff --git a/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb b/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb
index 63ca83188..d9b77c428 100644
--- a/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb
+++ b/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb
@@ -13,7 +13,8 @@ def initialize(value, expectation)
 
       def matches?(available_parameters)
         parameter, is_last_parameter = extract_parameter(available_parameters)
-        return false unless parameter == @value
+
+        return false unless HasEntries.new(@value).matches?([parameter])
 
         if is_last_parameter && !same_type_of_hash?(parameter, @value)
           return false if Mocha.configuration.strict_keyword_argument_matching?


Test patch:
diff --git a/test/acceptance/keyword_argument_matching_test.rb b/test/acceptance/keyword_argument_matching_test.rb
index f1b28461a..ffdcd751d 100644
--- a/test/acceptance/keyword_argument_matching_test.rb
+++ b/test/acceptance/keyword_argument_matching_test.rb
@@ -189,6 +189,15 @@ def test_should_match_positional_and_keyword_args_with_hash_matcher
     assert_passed(test_result)
   end
 
+  def test_should_match_keyword_args_with_nested_matcher
+    test_result = run_as_test do
+      mock = mock()
+      mock.expects(:method).with(key: is_a(Integer))
+      mock.method(key: 42)
+    end
+    assert_passed(test_result)
+  end
+
   def test_should_match_last_positional_hash_with_hash_matcher
     test_result = run_as_test do
       mock = mock()
diff --git a/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb b/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb
index b5cfd3008..f3e3ae9db 100644
--- a/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb
+++ b/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb
@@ -35,6 +35,11 @@ def test_should_match_keyword_args_with_keyword_args
     assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })]) # rubocop:disable Style/BracesAroundHashParameters
   end
 
+  def test_should_match_keyword_args_with_matchers_using_keyword_args
+    matcher = Hash.ruby2_keywords_hash({ key_1: is_a(String), key_2: is_a(Integer) }).to_matcher # rubocop:disable Style/BracesAroundHashParameters
+    assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 'foo', key_2: 2 })]) # rubocop:disable Style/BracesAroundHashParameters
+  end
+
   def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning_if_appropriate
     expectation = Mocha::Expectation.new(self, :foo); execution_point = ExecutionPoint.current
     matcher = Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }).to_matcher(expectation) # rubocop:disable Style/BracesAroundHashParameters