Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/flexmock/argument_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,28 @@ def initialize(expected)
end
def ===(target)
return false unless target.kind_of?(Hash)
return false unless @expected.all? { |k, v| v === target[k] }
matching = @expected.all? do |k, v|
v === target[k] || v == target[k]
end
return false unless matching

@expected.size == target.size
end
def inspect
"kw(#{@expected.inspect})"
args = @expected.map do |k, v|
k_s = case k
when Symbol
"#{k}: "
else
"#{k.inspect} => "
end

v_s = FlexMock.forbid_mocking("<recursive call to mocked method in #inspect>") do
v.inspect
end
"#{k_s}#{v_s}"
end
args.join(", ")
end
end

Expand Down
12 changes: 12 additions & 0 deletions lib/flexmock/core_class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ def format_args(args)
end
end

# Class method to format a list of args (the part between the
# parenthesis).
def format_kw_args(args)
if args
FlexMock.forbid_mocking("<recursive call to mocked method in #inspect>") do
args.inspect
end
else
"**args"
end
end

# Check will assert the block returns true. If it doesn't, an
# assertion failure is triggered with the given message.
def check(msg, &block) # :nodoc:
Expand Down
1 change: 1 addition & 0 deletions lib/flexmock/expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def to_s
def description
result = ["should_receive(#{@sym.inspect})"]
result << ".with(#{FlexMock.format_args(@expected_args)})" if @expected_args
result << ".with_kw_args(#{FlexMock.format_kw_args(@expected_kw_args)})" if @expected_kw_args
@count_validators.each do |validator|
result << validator.describe
end
Expand Down
9 changes: 9 additions & 0 deletions test/expectation_description_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ def test_with_at_least_1_at_most_10
assert_equal "should_receive(:foo).at_least.once.at_most.times(10)", @exp.description
end

def test_with_kw_args
@exp.at_least.once.with_kw_args(a: 10, "b" => 20)

description = <<~EOD
should_receive(:foo).with_kw_args(a: 10, "b" => 20).at_least.once
EOD
assert_equal description.chomp, @exp.description
end

def test_with_signature
@exp.at_least.once.with_signature(required_arguments: 2, optional_arguments: 3,
required_keyword_arguments: [:test],
Expand Down
25 changes: 24 additions & 1 deletion test/should_receive_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,36 @@ def test_with_hash_non_matching
end
end

def test_with_kw_args_matching
def test_with_kw_args_matching_equality
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with_kw_args(some: 10)
m.hi(some: 10)
end
end

def test_with_kw_args_matching_with_case_operator
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with_kw_args(some: 9..11)
m.hi(some: 10)
end
end

def test_with_kw_args_matching_with_equality_operator
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with_kw_args(some: 9..11)
m.hi(some: 9..11)
end
end

def test_with_kw_args_matching_strictly_with_equality_with_the_eq_operator
assert_raises(FlexMock::CheckFailedError) do
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with_kw_args(some: eq(9..11))
m.hi(some: 10)
end
end
end

def test_with_kw_args_matches_values_separately
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with_kw_args(a: on { |v| v == 1 }, b: 2).once
Expand Down