Skip to content

Commit 6142eaf

Browse files
authored
Merge pull request #35 from doudou/fix_kw_args_comparison
fix: compare expected and actual keyword argument values with both === and ==
2 parents bebdac7 + 0d0d006 commit 6142eaf

5 files changed

Lines changed: 64 additions & 3 deletions

File tree

lib/flexmock/argument_matchers.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,28 @@ def initialize(expected)
7575
end
7676
def ===(target)
7777
return false unless target.kind_of?(Hash)
78-
return false unless @expected.all? { |k, v| v === target[k] }
78+
matching = @expected.all? do |k, v|
79+
v === target[k] || v == target[k]
80+
end
81+
return false unless matching
7982

8083
@expected.size == target.size
8184
end
8285
def inspect
83-
"kw(#{@expected.inspect})"
86+
args = @expected.map do |k, v|
87+
k_s = case k
88+
when Symbol
89+
"#{k}: "
90+
else
91+
"#{k.inspect} => "
92+
end
93+
94+
v_s = FlexMock.forbid_mocking("<recursive call to mocked method in #inspect>") do
95+
v.inspect
96+
end
97+
"#{k_s}#{v_s}"
98+
end
99+
args.join(", ")
84100
end
85101
end
86102

lib/flexmock/core_class_methods.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ def format_args(args)
9797
end
9898
end
9999

100+
# Class method to format a list of args (the part between the
101+
# parenthesis).
102+
def format_kw_args(args)
103+
if args
104+
FlexMock.forbid_mocking("<recursive call to mocked method in #inspect>") do
105+
args.inspect
106+
end
107+
else
108+
"**args"
109+
end
110+
end
111+
100112
# Check will assert the block returns true. If it doesn't, an
101113
# assertion failure is triggered with the given message.
102114
def check(msg, &block) # :nodoc:

lib/flexmock/expectation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def to_s
6565
def description
6666
result = ["should_receive(#{@sym.inspect})"]
6767
result << ".with(#{FlexMock.format_args(@expected_args)})" if @expected_args
68+
result << ".with_kw_args(#{FlexMock.format_kw_args(@expected_kw_args)})" if @expected_kw_args
6869
@count_validators.each do |validator|
6970
result << validator.describe
7071
end

test/expectation_description_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ def test_with_at_least_1_at_most_10
7878
assert_equal "should_receive(:foo).at_least.once.at_most.times(10)", @exp.description
7979
end
8080

81+
def test_with_kw_args
82+
@exp.at_least.once.with_kw_args(a: 10, "b" => 20)
83+
84+
description = <<~EOD
85+
should_receive(:foo).with_kw_args(a: 10, "b" => 20).at_least.once
86+
EOD
87+
assert_equal description.chomp, @exp.description
88+
end
89+
8190
def test_with_signature
8291
@exp.at_least.once.with_signature(required_arguments: 2, optional_arguments: 3,
8392
required_keyword_arguments: [:test],

test/should_receive_test.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,36 @@ def test_with_hash_non_matching
439439
end
440440
end
441441

442-
def test_with_kw_args_matching
442+
def test_with_kw_args_matching_equality
443443
FlexMock.use('greeter') do |m|
444444
m.should_receive(:hi).with_kw_args(some: 10)
445445
m.hi(some: 10)
446446
end
447447
end
448448

449+
def test_with_kw_args_matching_with_case_operator
450+
FlexMock.use('greeter') do |m|
451+
m.should_receive(:hi).with_kw_args(some: 9..11)
452+
m.hi(some: 10)
453+
end
454+
end
455+
456+
def test_with_kw_args_matching_with_equality_operator
457+
FlexMock.use('greeter') do |m|
458+
m.should_receive(:hi).with_kw_args(some: 9..11)
459+
m.hi(some: 9..11)
460+
end
461+
end
462+
463+
def test_with_kw_args_matching_strictly_with_equality_with_the_eq_operator
464+
assert_raises(FlexMock::CheckFailedError) do
465+
FlexMock.use('greeter') do |m|
466+
m.should_receive(:hi).with_kw_args(some: eq(9..11))
467+
m.hi(some: 10)
468+
end
469+
end
470+
end
471+
449472
def test_with_kw_args_matches_values_separately
450473
FlexMock.use('greeter') do |m|
451474
m.should_receive(:hi).with_kw_args(a: on { |v| v == 1 }, b: 2).once

0 commit comments

Comments
 (0)