Skip to content

Commit

Permalink
Remove ParametersMatcher from Invocation#call_description
Browse files Browse the repository at this point in the history
This is a somewhat odd usage of `ParametersMatcher` which unnecessarily
couples it to `Invocation#call_description`.

The `mocha_inspect` logic is duplicated in `Invocation#argument_description`
for now because it ought to be possible for them to work differently,
specifically because an `Invocation` should never contain matchers.

The main motivation behind this change is that it allows `Invocation` and
`ParametersMatcher` to have different APIs - currently
`ParametersMatcher#initialize` needs an array of args or matchers, which
`Invocation#arguments` can fit into. The hope is that `Invocation` can have
separate `#positional_arguments` and `#keyword_arguments` (see #544), and we
don't want to expose a public `#arguments` method just to fit into
`ParametersMatcher`'s API.

We might even be able to build the descriptions completely from
`#positional_arguments` and `#keyword_arguments`, which would change the
logic here and make it no longer duplicated.

It's also worth mentioning that duplication notwithstanding, the changes
in this commit remove a fair amount of indirection:
- `Invocation#arguments` passes an Array of objects into
  `ParametersMatcher#initialize`.
- `ParametersMatcher#mocha_inspect` will first convert that Array into
  an Array of `ParameterMatchers::Equals` with the original object as
  the underlying value (the assumption here is that
  `Invocation#arguments` would not include any matchers instances, so
  all elements would be converted to `ParameterMatchers::Equals`).
- `Array#mocha_inspect` will then call
  `ParameterMatchers::Equals#mocha_inspect`, which then calls
  `mocha_inspect` on the underlying value.

The changes in this commit mean that `mocha_inspect` is called on the
original objects immediately instead.

Closes #543.
  • Loading branch information
wasabigeek authored and floehopper committed Sep 3, 2022
1 parent 5dfa49f commit c26be5f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/mocha/invocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def arguments
end

def call_description
description = "#{@mock.mocha_inspect}.#{@method_name}#{ParametersMatcher.new(@arguments).mocha_inspect}"
description = "#{@mock.mocha_inspect}.#{@method_name}#{argument_description}"
description << ' { ... }' unless @block.nil?
description
end
Expand All @@ -73,5 +73,14 @@ def result_description
def full_description
"\n - #{call_description} #{result_description}"
end

private

def argument_description
signature = arguments.mocha_inspect
signature = signature.gsub(/^\[|\]$/, '')
signature = signature.gsub(/^\{|\}$/, '') if arguments.length == 1
"(#{signature})"
end
end
end

0 comments on commit c26be5f

Please sign in to comment.