Secondary initializer for missing label

Label is set to actual/expected stringified value if omitted.
This commit is contained in:
Michael Miller 2018-10-18 21:52:00 -06:00
parent 9a77f8b7fd
commit 7f4690b042
5 changed files with 41 additions and 11 deletions

View file

@ -8,14 +8,17 @@ module Spectator::Expectations
getter actual
# Creates the expectation partial.
# The label should be a string representation of the actual value.
# The actual value is stored for later use.
protected def initialize(label : String, @actual : ActualType)
super(label)
end
# Returns the actual value as a string
# if there's no label available.
def label
super.empty? ? actual.to_s : super
# Creates the expectation partial.
# The label is generated by calling `#to_s` on the actual value.
# The actual value is stored for later use.
protected def initialize(@actual : ActualType)
super(@actual.to_s)
end
# Asserts that the `#actual` value matches some criteria.

View file

@ -17,6 +17,13 @@ module Spectator::Matchers
super(label)
end
# Creates the value matcher.
# The label is generated by calling `#to_s` on the expected value.
# The expected value is stored for later use.
def initialize(@expected : ExpectedType)
super(@expected.to_s)
end
# Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise.
abstract def match?(partial : ValueExpectationPartial(ActualType)) : Bool forall ActualType

View file

@ -20,7 +20,7 @@ class Object
# First argument of the `Expectation` initializer is the expression label.
# However, since this isn't a macro and we can't "look behind" this method call
# to see what it was invoked on, the argument is an empty string.
expectation = ::Spectator::Expectation.new("", self)
expectation = ::Spectator::Expectation.new(self)
unless matcher.match?(expectation)
raise ::Spectator::ExpectationFailed.new(matcher.message(expectation))
end
@ -29,7 +29,7 @@ class Object
# Works the same as `#should` except the condition is inverted.
# When `#should` succeeds, this method will fail, and vice-versa.
def should_not(matcher : ::Spectator::Matchers::Matcher)
expectation = ::Spectator::Expectation.new("", self)
expectation = ::Spectator::Expectation.new(self)
if matcher.match?(expectation)
raise ::Spectator::ExpectationFailed.new(matcher.message(expectation))
end