TruthyMatcher doesn't need to store a label

Extend from Matcher instead of ValueMatcher to bypass storing the label.
The "expected" value is still used, but is renamed to "truthy" and used
for the label.
This commit is contained in:
Michael Miller 2019-02-28 14:52:15 -07:00
parent 226708cb82
commit f3e50c6432

View file

@ -8,19 +8,23 @@ module Spectator::Matchers
#
# Additionally, different matchers can be created
# by using the `#<`, `#<=`, `#>`, `#>=`, `#==`, and `#!=` operators.
struct TruthyMatcher < ValueMatcher(Bool)
struct TruthyMatcher < Matcher
# Creates the truthy matcher.
# The *truthy* argument should be true to match "truthy" values,
# and false to match "falsey" values.
def initialize(truthy : Bool)
super(truthy ? "truthy" : "falsey", truthy)
def initialize(@truthy : Bool)
end
# Textual representation of what the matcher expects.
def label
@truthy ? "truthy" : "falsey"
end
# Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise.
def match?(partial)
# Cast value to truthy value and compare.
@expected == !!partial.actual
@truthy == !!partial.actual
end
# Determines whether the matcher is satisfied with the partial given to it.