Class: RSpec::Mocks::ArgumentListMatcher
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/argument_list_matcher.rb
Overview
Wrapper for matching arguments against a list of expected values. Used by the ‘with` method on a `MessageExpectation`:
expect(object).to receive(:message).with(:a, 'b', 3)
object.(:a, 'b', 3)
Values passed to ‘with` can be literal values or argument matchers that match against the real objects .e.g.
expect(object).to receive(:message).with(hash_including(:a => 'b'))
Can also be used directly to match the contents of any ‘Array`. This enables 3rd party mocking libs to take advantage of rspec’s argument matching without using the rest of rspec-mocks.
require 'rspec/mocks/argument_list_matcher'
include RSpec::Mocks::ArgumentMatchers
arg_list_matcher = RSpec::Mocks::ArgumentListMatcher.new(123, hash_including(:a => 'b'))
arg_list_matcher.args_match?(123, :a => 'b')
This class is immutable.
Instance Attribute Summary collapse
- #expected_args ⇒ Object readonly
Instance Method Summary collapse
-
#args_match?(*actual_args) ⇒ Boolean
Matches each element in the ‘expected_args` against the element in the same position of the arguments passed to `new`.
-
#initialize(*expected_args) ⇒ ArgumentListMatcher
constructor
Initializes an ‘ArgumentListMatcher` with a collection of literal values and/or argument matchers.
-
#resolve_expected_args_based_on(actual_args) ⇒ Object
Resolves abstract arg placeholders like ‘no_args` and `any_args` into a more concrete arg list based on the provided `actual_args`.
Constructor Details
#initialize(*expected_args) ⇒ ArgumentListMatcher
Initializes an ‘ArgumentListMatcher` with a collection of literal values and/or argument matchers.
45 46 47 48 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/argument_list_matcher.rb', line 45 def initialize(*expected_args) @expected_args = expected_args ensure_expected_args_valid! end |
Instance Attribute Details
#expected_args ⇒ Object (readonly)
35 36 37 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/argument_list_matcher.rb', line 35 def expected_args @expected_args end |
Instance Method Details
#args_match?(*actual_args) ⇒ Boolean
Matches each element in the ‘expected_args` against the element in the same position of the arguments passed to `new`.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/argument_list_matcher.rb', line 58 def args_match?(*actual_args) expected_args = resolve_expected_args_based_on(actual_args) return false if expected_args.size != actual_args.size if RUBY_VERSION >= "3" # If the expectation was set with keywords, while the actual method was called with a positional hash argument, they don't match. # If the expectation was set without keywords, e.g., with({a: 1}), then it fine to call it with either foo(a: 1) or foo({a: 1}). # This corresponds to Ruby semantics, as if the method was def foo(options). if Hash === expected_args.last && Hash === actual_args.last if !Hash.ruby2_keywords_hash?(actual_args.last) && Hash.ruby2_keywords_hash?(expected_args.last) return false end end end Support::FuzzyMatcher.values_match?(expected_args, actual_args) end |
#resolve_expected_args_based_on(actual_args) ⇒ Object
Resolves abstract arg placeholders like ‘no_args` and `any_args` into a more concrete arg list based on the provided `actual_args`.
81 82 83 84 85 86 87 88 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/argument_list_matcher.rb', line 81 def resolve_expected_args_based_on(actual_args) return [] if [ArgumentMatchers::NoArgsMatcher::INSTANCE] == expected_args any_args_index = expected_args.index { |a| ArgumentMatchers::AnyArgsMatcher::INSTANCE == a } return expected_args unless any_args_index replace_any_args_with_splat_of_anything(any_args_index, actual_args.count) end |