Use shorter string when stub is treated as a message

This commit is contained in:
Michael Miller 2022-11-04 22:55:12 -06:00
parent e2cdc9e08e
commit 318e4eba89
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
3 changed files with 26 additions and 14 deletions

View file

@ -296,7 +296,7 @@ Spectator.describe Spectator::Matchers::ReceiveMatcher do
end end
it "has the expected call listed" do it "has the expected call listed" do
is_expected.to contain({:expected, "Not #{stub}"}) is_expected.to contain({:expected, "Not #{stub.message}"})
end end
it "has the list of called methods" do it "has the list of called methods" do

View file

@ -81,7 +81,7 @@ module Spectator::Matchers
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
def description : String def description : String
"received #{@stub} #{humanize_count}" "received #{@stub.message} #{humanize_count}"
end end
# Actually performs the test against the expression (value or block). # Actually performs the test against the expression (value or block).
@ -89,10 +89,10 @@ module Spectator::Matchers
stubbed = actual.value stubbed = actual.value
calls = relevant_calls(stubbed) calls = relevant_calls(stubbed)
if @count.includes?(calls.size) if @count.includes?(calls.size)
SuccessfulMatchData.new("#{actual.label} received #{@stub} #{humanize_count}") SuccessfulMatchData.new("#{actual.label} received #{@stub.message} #{humanize_count}")
else else
FailedMatchData.new("#{actual.label} received #{@stub} #{humanize_count}", FailedMatchData.new("#{actual.label} received #{@stub.message} #{humanize_count}",
"#{actual.label} did not receive #{@stub}", values(actual).to_a) "#{actual.label} did not receive #{@stub.message}", values(actual).to_a)
end end
end end
@ -106,9 +106,9 @@ module Spectator::Matchers
stubbed = actual.value stubbed = actual.value
calls = relevant_calls(stubbed) calls = relevant_calls(stubbed)
if @count.includes?(calls.size) if @count.includes?(calls.size)
FailedMatchData.new("#{actual.label} did not receive #{@stub}", "#{actual.label} received #{@stub}", negated_values(actual).to_a) FailedMatchData.new("#{actual.label} did not receive #{@stub.message}", "#{actual.label} received #{@stub.message}", negated_values(actual).to_a)
else else
SuccessfulMatchData.new("#{actual.label} did not receive #{@stub} #{humanize_count}") SuccessfulMatchData.new("#{actual.label} did not receive #{@stub.message} #{humanize_count}")
end end
end end
@ -120,7 +120,7 @@ module Spectator::Matchers
# Additional information about the match failure. # Additional information about the match failure.
private def values(actual : Expression(T)) forall T private def values(actual : Expression(T)) forall T
{ {
expected: @stub.to_s, expected: @stub.message,
actual: method_call_list(actual.value), actual: method_call_list(actual.value),
} }
end end
@ -128,7 +128,7 @@ module Spectator::Matchers
# Additional information about the match failure when negated. # Additional information about the match failure when negated.
private def negated_values(actual : Expression(T)) forall T private def negated_values(actual : Expression(T)) forall T
{ {
expected: "Not #{@stub}", expected: "Not #{@stub.message}",
actual: method_call_list(actual.value), actual: method_call_list(actual.value),
} }
end end

View file

@ -22,6 +22,23 @@ module Spectator
def initialize(@method : Symbol, @constraint : AbstractArguments? = nil, @location : Location? = nil) def initialize(@method : Symbol, @constraint : AbstractArguments? = nil, @location : Location? = nil)
end end
# String representation of the stub, formatted as a method call.
def message(io : IO) : Nil
io << "#" << method << (constraint || "(any args)")
end
# String representation of the stub, formatted as a method call.
def message
String.build do |str|
message(str)
end
end
# String representation of the stub, formatted as a method definition.
def to_s(io : IO) : Nil
message(io)
end
# Checks if a method call should receive the response from this stub. # Checks if a method call should receive the response from this stub.
def ===(call : MethodCall) def ===(call : MethodCall)
return false if method != call.method return false if method != call.method
@ -29,10 +46,5 @@ module Spectator
constraint === call.arguments constraint === call.arguments
end end
# String representation of the stub, formatted as a method call.
def to_s(io : IO) : Nil
io << "#" << method << (constraint || "(any args)")
end
end end
end end