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 module Spectator::Matchers
# Matcher that tests whether a value is truthy or falsey. # Matcher that tests whether a value is truthy or falsey.
@ -8,28 +8,51 @@ module Spectator::Matchers
# #
# Additionally, different matchers can be created # Additionally, different matchers can be created
# by using the `#<`, `#<=`, `#>`, `#>=`, `#==`, and `#!=` operators. # by using the `#<`, `#<=`, `#>`, `#>=`, `#==`, and `#!=` operators.
struct TruthyMatcher < Matcher struct TruthyMatcher < StandardMatcher
# Creates the truthy matcher. # Creates the truthy matcher.
# The *truthy* argument should be true to match "truthy" values, # The *truthy* argument should be true to match "truthy" values,
# and false to match "falsey" values. # and false to match "falsey" values.
def initialize(@truthy : Bool) def initialize(@truthy : Bool = true)
end end
# Textual representation of what the matcher expects. private def label
def label
@truthy ? "truthy" : "falsey" @truthy ? "truthy" : "falsey"
end end
# Determines whether the matcher is satisfied with the value given to it. private def negated_label
private def match?(actual) @truthy ? "falsey" : "truthy"
# Cast value to truthy value and compare.
@truthy == !!actual
end end
# Determines whether the matcher is satisfied with the partial given to it. private def match?(actual)
def match(partial, negated = false) @truthy == !!actual.value
actual = partial.actual end
MatchData.new(match?(actual), @truthy, actual, partial.label)
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 end
# Creates a matcher that checks if a value is less than an expected value. # 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 # expect(0).to be < 1
# ``` # ```
def <(expected : ExpectedType) forall ExpectedType def <(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
LessThanMatcher.new(expected) LessThanMatcher.new(expected)
end end
@ -46,7 +70,8 @@ module Spectator::Matchers
# ``` # ```
# expect(0).to be <= 1 # expect(0).to be <= 1
# ``` # ```
def <=(expected : ExpectedType) forall ExpectedType def <=(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
LessThanEqualMatcher.new(expected) LessThanEqualMatcher.new(expected)
end end
@ -55,7 +80,8 @@ module Spectator::Matchers
# ``` # ```
# expect(2).to be > 1 # expect(2).to be > 1
# ``` # ```
def >(expected : ExpectedType) forall ExpectedType def >(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
GreaterThanMatcher.new(expected) GreaterThanMatcher.new(expected)
end end
@ -64,7 +90,8 @@ module Spectator::Matchers
# ``` # ```
# expect(2).to be >= 1 # expect(2).to be >= 1
# ``` # ```
def >=(expected : ExpectedType) forall ExpectedType def >=(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
GreaterThanEqualMatcher.new(expected) GreaterThanEqualMatcher.new(expected)
end end
@ -73,7 +100,8 @@ module Spectator::Matchers
# ``` # ```
# expect(0).to be == 0 # expect(0).to be == 0
# ``` # ```
def ==(expected : ExpectedType) forall ExpectedType def ==(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
EqualityMatcher.new(expected) EqualityMatcher.new(expected)
end end
@ -82,44 +110,9 @@ module Spectator::Matchers
# ``` # ```
# expect(0).to be != 1 # expect(0).to be != 1
# ``` # ```
def !=(expected : ExpectedType) forall ExpectedType def !=(value : ExpectedType) forall ExpectedType
expected = TestValue.new(value)
InequalityMatcher.new(expected) InequalityMatcher.new(expected)
end 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
end end