Skip to content

Commit

Permalink
feat(delegate_to): add failure message for and_return
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Mar 3, 2024
1 parent 0fb98d5 commit eebfcb4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def expected_arguments
#
def expected_arguments=(arguments)
Utils::Object.instance_variable_delete(self, :@delegation_value)
Utils::Object.instance_variable_delete(self, :@custom_return_value)

@expected_arguments = arguments
end
Expand Down Expand Up @@ -275,6 +276,17 @@ def delegation_value
end
end

##
# @return [Object] Can be any type.
#
# @internal
# IMPORTANT: Must be refreshed when `expected_arguments` are reset.
# TODO: Specs.
#
def custom_return_value
Utils::Object.memoize_including_falsy_values(self, :@custom_return_value) { expected_return_value_block.call(delegation_value) }
end

##
# @return [ConvenientService::RSpec::PrimitiveMatchers::Classes::DelegateTo::Entities::Matcher::Entities::Chainings]
def chainings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,27 @@ def matches?(block_expectation_value)
# .and_return { |delegation_value| delegation_value.to_s }
# end
#
block_expectation_value == matcher.expected_return_value_block.call(matcher.delegation_value)
block_expectation_value == matcher.custom_return_value
end

##
# @return [String]
#
def failure_message_when_negated
"expected `#{matcher.printable_block_expectation}` NOT to delegate to `#{matcher.printable_method}` and return custom value `#{matcher.custom_return_value}`, but it did."
end

private

##
# @return [String]
#
def failure_message_permanent_part
<<~MESSAGE.chomp
expected `#{matcher.printable_block_expectation}` to delegate to `#{matcher.printable_method}` and return custom value `#{matcher.custom_return_value}`, but it didn't.
`#{matcher.printable_block_expectation}` returns `#{block_expectation_value.inspect}`, but delegation returns `#{matcher.delegation_value.inspect}`.
MESSAGE
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ def matches?(block_expectation_value)
#
block_expectation_value == matcher.delegation_value
end

##
# @return [String]
#
def failure_message_when_negated
"expected `#{matcher.printable_block_expectation}` NOT to delegate to `#{matcher.printable_method}` and return its value, but it did."
end

private

##
# @return [String]
#
def failure_message_permanent_part
<<~MESSAGE.chomp
expected `#{matcher.printable_block_expectation}` to delegate to `#{matcher.printable_method}` and return its value, but it didn't.
`#{matcher.printable_block_expectation}` returns `#{block_expectation_value.inspect}`, but delegation returns `#{matcher.delegation_value.inspect}`.
MESSAGE
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,8 @@ def failure_message
[failure_message_permanent_part, same_visual_output_note].reject(&:empty?).join("\n\n")
end

##
# @return [String]
#
def failure_message_when_negated
"expected `#{matcher.printable_block_expectation}` NOT to delegate to `#{matcher.printable_method}` and return its value, but it did."
end

private

def failure_message_permanent_part
<<~MESSAGE.chomp
expected `#{matcher.printable_block_expectation}` to delegate to `#{matcher.printable_method}` and return its value, but it didn't.
`#{matcher.printable_block_expectation}` returns `#{block_expectation_value.inspect}`, but delegation returns `#{matcher.delegation_value.inspect}`.
MESSAGE
end

##
# @internal
# NOTE: Early return is harder to understand in this particular case, that is why a casual if is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,26 @@ def bar(...)
expect(matcher.expected_arguments = arguments).to eq(arguments)
end

it "delegates to `ConvenientService::Utils::Object`" do
allow(ConvenientService::Utils::Object).to receive(:instance_variable_delete).with(matcher, :@delegation_value).and_call_original
example_group "resets" do
before do
allow(ConvenientService::Utils::Object).to receive(:instance_variable_delete).and_call_original
end

matcher.expected_arguments = arguments
it "delegates to `ConvenientService::Utils::Object` to reset `delegation_value`" do
allow(ConvenientService::Utils::Object).to receive(:instance_variable_delete).with(matcher, :@delegation_value).and_call_original

expect(ConvenientService::Utils::Object).to have_received(:instance_variable_delete).with(matcher, :@delegation_value)
matcher.expected_arguments = arguments

expect(ConvenientService::Utils::Object).to have_received(:instance_variable_delete).with(matcher, :@delegation_value)
end

it "delegates to `ConvenientService::Utils::Object` to reset `custom_return_value`" do
allow(ConvenientService::Utils::Object).to receive(:instance_variable_delete).with(matcher, :@custom_return_value).and_call_original

matcher.expected_arguments = arguments

expect(ConvenientService::Utils::Object).to have_received(:instance_variable_delete).with(matcher, :@custom_return_value)
end
end
end

Expand Down

0 comments on commit eebfcb4

Please sign in to comment.