Some more easy target refactors

EmptyMatcher should extend StandardMatcher.
This commit is contained in:
Michael Miller 2019-08-08 15:45:34 -06:00
parent 17aa472d92
commit badccad2fa
7 changed files with 106 additions and 192 deletions

View file

@ -3,7 +3,7 @@ require "./matcher"
module Spectator::Matchers
# Matcher that tests whether a collection is empty.
# The values are checked with the `empty?` method.
struct EmptyMatcher < Matcher
struct EmptyMatcher < StandardMatcher
private def match?(actual)
actual.value.empty?
end
@ -19,9 +19,5 @@ module Spectator::Matchers
private def failure_message_when_negated(actual)
"#{actual.label} is empty"
end
private def values(actual)
{actual: actual.value.inspect}
end
end
end

View file

@ -4,43 +4,34 @@ module Spectator::Matchers
# Matcher that tests whether two values do not equal each other.
# The values are compared with the != operator.
struct InequalityMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual)
actual != expected
expected.value != actual.value
end
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
def description
"is not equal to #{expected.label}"
end
# Match data specific to this matcher.
private struct MatchData(ExpectedType, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end
private def failure_message(actual)
"#{actual.label} is equal to #{expected.label}"
end
# Information about the match.
def named_tuple
{
expected: NegatablePrefixedMatchDataValue.new("Not", "", @values.expected),
actual: @values.actual,
}
end
private def failure_message_when_negated(actual)
"#{actual.label} is not equal to #{expected.label}"
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} is not #{@values.expected_label} (using !=)"
end
private def values(actual)
{
expected: "Not #{expected.value.inspect}",
actual: actual.value.inspect,
}
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@values.actual_label} is #{@values.expected_label} (using !=)"
end
private def negated_values(actual)
{
expected: expected.value.inspect,
actual: actual.value.inspect,
}
end
end
end

View file

@ -3,45 +3,21 @@ require "./matcher"
module Spectator::Matchers
# Common matcher that tests whether a value is nil.
# The `Object#nil?` method is used for this.
struct NilMatcher < Matcher
# Textual representation of what the matcher expects.
def label
"nil?"
struct NilMatcher < StandardMatcher
private def match?(actual)
actual.value.nil?
end
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
actual = partial.actual
matched = actual.nil?
MatchData.new(matched, actual, partial.label)
def description
"is nil"
end
# Match data specific to this matcher.
private struct MatchData(T) < MatchData
# Creates the match data.
def initialize(matched, @actual : T, @actual_label : String)
super(matched)
end
private def failure_message(actual)
"#{actual.label} is not nil"
end
# Information about the match.
def named_tuple
{
expected: NegatableMatchDataValue.new(nil),
actual: @actual,
}
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@actual_label} is nil"
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 nil"
end
private def failure_message_when_negated(actual)
"#{actual.label} is nil"
end
end
end

View file

@ -4,43 +4,20 @@ module Spectator::Matchers
# Matcher that tests whether two references are the same.
# The values are compared with the `Reference#same?` method.
struct ReferenceMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual)
actual.same?(expected)
expected.value.same?(actual.value)
end
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
def description
"is #{expected.label}"
end
# Match data specific to this matcher.
private struct MatchData(ExpectedType, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end
private def failure_message(actual)
"#{actual.label} is not #{expected.label}"
end
# Information about the match.
def named_tuple
{
expected: NegatableMatchDataValue.new(@values.expected),
actual: @values.actual,
}
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} is #{@values.expected_label}"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@values.actual_label} is not #{@values.expected_label}"
end
private def failure_message_when_negated(actual)
"#{actual.label} is #{expected.label}"
end
end
end

View file

@ -4,39 +4,34 @@ module Spectator::Matchers
# Matcher that tests whether a set has a specified number of elements.
# The set's `#size` method is used for this check.
struct SizeMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
actual = partial.actual.size
values = ExpectedActual.new(expected, label, actual, partial.label)
MatchData.new(actual == expected, values)
private def match?(actual)
expected.value == actual.value.size
end
# Match data specific to this matcher.
private struct MatchData(ExpectedType, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end
def description
"has size #{expected.label}"
end
# Information about the match.
def named_tuple
{
expected: NegatableMatchDataValue.new(@values.expected),
actual: @values.actual,
}
end
private def failure_message(actual)
"#{actual.label} does not have #{expected.label} elements"
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} has #{@values.expected_label} elements"
end
private def failure_message_when_negated(actual)
"#{actual.label} has #{expected.label} elements"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@values.actual_label} does not have #{@values.expected_label} elements"
end
private def values(actual)
{
expected: expected.value.inspect,
actual: actual.value.size.inspect,
}
end
private def negated_values(actual)
{
expected: "Not #{expected.value.inspect}",
actual: actual.value.size.inspect,
}
end
end
end

View file

@ -4,40 +4,34 @@ module Spectator::Matchers
# Matcher that tests whether a set has the same number of elements as another set.
# The set's `#size` method is used for this check.
struct SizeOfMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
actual = partial.actual.size
size = expected.size
values = ExpectedActual.new(size, label, actual, partial.label)
MatchData.new(actual == size, values)
private def match?(actual)
expected.value.size == actual.value.size
end
# Match data specific to this matcher.
private struct MatchData(ExpectedType, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end
def description
"is the same size as #{expected.label}"
end
# Information about the match.
def named_tuple
{
expected: NegatableMatchDataValue.new(@values.expected),
actual: @values.actual,
}
end
private def failure_message(actual)
"#{actual.label} is not the same size as #{expected.label}"
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} has the same number of elements as #{@values.expected_label}"
end
private def failure_message_when_negated(actual)
"#{actual.label} is the same size as #{expected.label}"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@values.actual_label} has a different number of elements than #{@values.expected_label}"
end
private def values(actual)
{
expected: expected.value.size.inspect,
actual: actual.value.size.inspect,
}
end
private def negated_values(actual)
{
expected: "Not #{expected.value.size.inspect}",
actual: actual.value.size.inspect,
}
end
end
end

View file

@ -3,50 +3,35 @@ require "./matcher"
module Spectator::Matchers
# Matcher that tests a value is of a specified type.
# The values are compared with the `Object#is_a?` method.
struct TypeMatcher(Expected) < Matcher
# Textual representation of what the matcher expects.
# The `Expected` type param will be used to populate the label.
def label
Expected.to_s
end
# Determines whether the matcher is satisfied with the value given to it.
struct TypeMatcher(Expected) < StandardMatcher
private def match?(actual)
actual.is_a?(Expected)
actual.value.is_a?(Expected)
end
# Determines whether the matcher is satisfied with the partial given to it.
def match(partial, negated = false)
actual = partial.actual
MatchData(Expected, typeof(actual)).new(match?(actual), actual, partial.label)
def description
"is as #{Expected}"
end
# Match data specific to this matcher.
private struct MatchData(ExpectedType, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @actual : ActualType, @actual_label : String)
super(matched)
end
private def failure_message(actual)
"#{actual.label} is not a #{Expected}"
end
# Information about the match.
def named_tuple
{
expected: NegatableMatchDataValue.new(ExpectedType),
actual: @actual.class,
}
end
private def failure_message_when_negated(actual)
"#{actual.label} is a #{Expected}"
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@actual_label} is a #{ExpectedType}"
end
private def values(actual)
{
expected: Expected.to_s,
actual: actual.value.class.inspect,
}
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 a #{ExpectedType}"
end
private def negated_values(actual)
{
expected: "Not #{Expected}",
actual: actual.value.class.inspect,
}
end
end
end