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

View file

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

View file

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

View file

@ -4,43 +4,20 @@ module Spectator::Matchers
# Matcher that tests whether two references are the same. # Matcher that tests whether two references are the same.
# The values are compared with the `Reference#same?` method. # The values are compared with the `Reference#same?` method.
struct ReferenceMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct ReferenceMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual) private def match?(actual)
actual.same?(expected) expected.value.same?(actual.value)
end end
# Determines whether the matcher is satisfied with the partial given to it. def description
def match(partial, negated = false) "is #{expected.label}"
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end end
# Match data specific to this matcher. private def failure_message(actual)
private struct MatchData(ExpectedType, ActualType) < MatchData "#{actual.label} is not #{expected.label}"
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end end
# Information about the match. private def failure_message_when_negated(actual)
def named_tuple "#{actual.label} is #{expected.label}"
{
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
end end
end end
end end

View file

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

View file

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