Skip to content

Commit

Permalink
Remove unnecessary splatting in Invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabigeek authored and floehopper committed Sep 10, 2022
1 parent c26be5f commit 24d3cfb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/mocha/invocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Mocha
class Invocation
attr_reader :method_name, :block

def initialize(mock, method_name, *arguments, &block)
def initialize(mock, method_name, arguments = [], block = nil)
@mock = mock
@method_name = method_name
@arguments = arguments
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def all_expectations
def method_missing(symbol, *arguments, &block) # rubocop:disable Style/MethodMissingSuper
check_expiry
check_responder_responds_to(symbol)
invocation = Invocation.new(self, symbol, *arguments, &block)
invocation = Invocation.new(self, symbol, arguments, block)
if (matching_expectation_allowing_invocation = all_expectations.match_allowing_invocation(invocation))
matching_expectation_allowing_invocation.invoke(invocation)
elsif (matching_expectation = all_expectations.match(invocation)) || (!matching_expectation && !@everything_stubbed)
Expand Down
6 changes: 3 additions & 3 deletions test/unit/expectation_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_should_find_matching_expectation
expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
expectation_list.add(expectation1)
expectation_list.add(expectation2)
assert_same expectation1, expectation_list.match(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
assert_same expectation1, expectation_list.match(Invocation.new(:irrelevant, :my_method, [:argument1, :argument2]))
end

def test_should_remove_all_expectations_matching_method_name
Expand All @@ -44,7 +44,7 @@ def test_should_find_most_recent_matching_expectation
expectation2 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
expectation_list.add(expectation1)
expectation_list.add(expectation2)
assert_same expectation2, expectation_list.match(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
assert_same expectation2, expectation_list.match(Invocation.new(:irrelevant, :my_method, [:argument1, :argument2]))
end

def test_should_find_matching_expectation_allowing_invocation
Expand All @@ -55,7 +55,7 @@ def test_should_find_matching_expectation_allowing_invocation
define_instance_method(expectation2, :invocations_allowed?) { true }
expectation_list.add(expectation1)
expectation_list.add(expectation2)
assert_same expectation1, expectation_list.match_allowing_invocation(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
assert_same expectation1, expectation_list.match_allowing_invocation(Invocation.new(:irrelevant, :my_method, [:argument1, :argument2]))
end

def test_should_find_most_recent_matching_expectation_allowing_invocation
Expand Down
20 changes: 10 additions & 10 deletions test/unit/expectation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,54 @@ def new_expectation
end

def invoke(expectation, &block)
expectation.invoke(Invocation.new(:irrelevant, :expected_method, &block))
expectation.invoke(Invocation.new(:irrelevant, :expected_method, [], block))
end

def test_should_match_calls_to_same_method_with_any_parameters
assert new_expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
assert new_expectation.match?(Invocation.new(:irrelevant, :expected_method, [1, 2, 3]))
end

def test_should_match_calls_to_same_method_with_exactly_zero_parameters
assert new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method))
end

def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
assert !new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
assert !new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method, [1, 2, 3]))
end

def test_should_match_calls_to_same_method_with_expected_parameter_values
assert new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
assert new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, [1, 2, 3]))
end

def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
expectation = new_expectation.with { |x, y, z| x + y == z }
assert expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
assert expectation.match?(Invocation.new(:irrelevant, :expected_method, [1, 2, 3]))
end

def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
expectation = new_expectation.with { |x, y, z| x + y == z }
assert !expectation.match?(Invocation.new(:irrelevant, :different_method, 1, 2, 3))
assert !expectation.match?(Invocation.new(:irrelevant, :different_method, [1, 2, 3]))
end

def test_should_not_match_calls_to_different_methods_with_no_parameters
assert !new_expectation.match?(Invocation.new(:irrelevant, :unexpected_method))
end

def test_should_not_match_calls_to_same_method_with_too_few_parameters
assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 2))
assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, [1, 2]))
end

def test_should_not_match_calls_to_same_method_with_too_many_parameters
assert !new_expectation.with(1, 2).match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
assert !new_expectation.with(1, 2).match?(Invocation.new(:irrelevant, :expected_method, [1, 2, 3]))
end

def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 0, 3))
assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, [1, 0, 3]))
end

def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
expectation = new_expectation.with { |x, y, z| x + y == z }
assert !expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 0, 3))
assert !expectation.match?(Invocation.new(:irrelevant, :expected_method, [1, 0, 3]))
end

def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
Expand Down

0 comments on commit 24d3cfb

Please sign in to comment.