Allow specifying receive count

Updated checks and failure output to support this.
This commit is contained in:
Michael Miller 2019-11-03 13:22:00 -07:00
parent b107511c9e
commit 2048267eef

View file

@ -3,7 +3,7 @@ require "./standard_matcher"
module Spectator::Matchers module Spectator::Matchers
struct ReceiveMatcher < StandardMatcher struct ReceiveMatcher < StandardMatcher
def initialize(@expected : TestExpression(Symbol)) def initialize(@expected : TestExpression(Symbol), @count : Int32? = nil)
end end
def description : String def description : String
@ -13,7 +13,11 @@ module Spectator::Matchers
def match?(actual : TestExpression(T)) : Bool forall T def match?(actual : TestExpression(T)) : Bool forall T
double = actual.value.as(Mocks::Double) double = actual.value.as(Mocks::Double)
calls = double.spectator_stub_calls(@expected.value) calls = double.spectator_stub_calls(@expected.value)
!calls.empty? if (count = @count)
count == calls.size
else
!calls.empty?
end
end end
def failure_message(actual : TestExpression(T)) : String forall T def failure_message(actual : TestExpression(T)) : String forall T
@ -21,9 +25,11 @@ module Spectator::Matchers
end end
def values(actual : TestExpression(T)) forall T def values(actual : TestExpression(T)) forall T
double = actual.value.as(Mocks::Double)
calls = double.spectator_stub_calls(@expected.value)
{ {
expected: "1 time with any arguments", expected: "#{@count ? "#{@count} time(s)" : "At least once"} with any arguments",
received: "0 times with any arguments", received: "#{calls.size} time(s) with any arguments",
} }
end end
end end