Refactor TruthyMatcher

This commit is contained in:
Michael Miller 2019-08-08 15:59:31 -06:00
parent badccad2fa
commit a3fa522bd4

View file

@ -1,4 +1,4 @@
require "./value_matcher"
require "./standard_matcher"
module Spectator::Matchers
# Matcher that tests whether a value is truthy or falsey.
@ -8,28 +8,51 @@ module Spectator::Matchers
#
# Additionally, different matchers can be created
# by using the `#<`, `#<=`, `#>`, `#>=`, `#==`, and `#!=` operators.
struct TruthyMatcher < Matcher
struct TruthyMatcher < StandardMatcher
# Creates the truthy matcher.
# The *truthy* argument should be true to match "truthy" values,
# and false to match "falsey" values.
def initialize(@truthy : Bool)
def initialize(@truthy : Bool = true)
end
# Textual representation of what the matcher expects.
def label
private def label
@truthy ? "truthy" : "falsey"
end
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual)
# Cast value to truthy value and compare.
@truthy == !!actual
private def negated_label
@truthy ? "falsey" : "truthy"
end
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
actual = partial.actual
MatchData.new(match?(actual), @truthy, actual, partial.label)
private def match?(actual)
@truthy == !!actual.value
end
def description
"is #{label}"
end
private def failure_message(actual)
"#{actual.label} is #{negated_label}"
end
private def failure_message_when_negated(actual)
"#{actual.label} is #{label}"
end
private def values(actual)
{
expected: @truthy ? "Not false or nil" : "false or nil",
actual: actual.value.inspect,
truthy: !!actual.value.inspect,
}
end
private def negated_values(actual)
{
expected: @truthy ? "false or nil" : "Not false or nil",
actual: actual.value.inspect,
truthy: !!actual.value.inspect,
}
end
# Creates a matcher that checks if a value is less than an expected value.
@ -37,7 +60,8 @@ module Spectator::Matchers
# ```
# expect(0).to be < 1
# ```
def <(expected : ExpectedType) forall ExpectedType
def <(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
LessThanMatcher.new(expected)
end
@ -46,7 +70,8 @@ module Spectator::Matchers
# ```
# expect(0).to be <= 1
# ```
def <=(expected : ExpectedType) forall ExpectedType
def <=(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
LessThanEqualMatcher.new(expected)
end
@ -55,7 +80,8 @@ module Spectator::Matchers
# ```
# expect(2).to be > 1
# ```
def >(expected : ExpectedType) forall ExpectedType
def >(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
GreaterThanMatcher.new(expected)
end
@ -64,7 +90,8 @@ module Spectator::Matchers
# ```
# expect(2).to be >= 1
# ```
def >=(expected : ExpectedType) forall ExpectedType
def >=(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
GreaterThanEqualMatcher.new(expected)
end
@ -73,7 +100,8 @@ module Spectator::Matchers
# ```
# expect(0).to be == 0
# ```
def ==(expected : ExpectedType) forall ExpectedType
def ==(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
EqualityMatcher.new(expected)
end
@ -82,44 +110,9 @@ module Spectator::Matchers
# ```
# expect(0).to be != 1
# ```
def !=(expected : ExpectedType) forall ExpectedType
def !=(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
InequalityMatcher.new(expected)
end
# Match data specific to this matcher.
private struct MatchData(ActualType) < MatchData
# Creates the match data.
def initialize(matched, @truthy : Bool, @actual : ActualType, @actual_label : String)
super(matched)
end
# Information about the match.
def named_tuple
truthy = "Not false or nil"
falsey = "false or nil"
{
expected: AlternativeMatchDataValue.new(@truthy ? truthy : falsey, @truthy ? falsey : truthy),
actual: @actual,
truthy: !!@actual,
}
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@actual_label} is #{expected_label}"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@actual_label} is not #{expected_label}"
end
# Textual representation of what the matcher expects.
private def expected_label
@truthy ? "truthy" : "falsey"
end
end
end
end