Reorder methods to be in a more standard layout

This commit is contained in:
Michael Miller 2019-08-10 11:10:48 -06:00
parent 898ddcb616
commit ede691dd6a
26 changed files with 254 additions and 255 deletions

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Common matcher that tests whether two values semantically equal each other.
# The values are compared with the === operator.
struct CaseMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value === actual.value
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"matches #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value === actual.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -5,11 +5,6 @@ require "./value_matcher"
module Spectator::Matchers
# Matcher for checking that a value is in a collection of other values.
struct CollectionMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.includes?(actual.value)
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -17,6 +12,11 @@ module Spectator::Matchers
"is in #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.includes?(actual.value)
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -4,13 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether a value, such as a `String` or `Array`, contains one or more values.
# The values are checked with the `includes?` method.
struct ContainMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.all? do |item|
actual.value.includes?(item)
end
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -18,6 +11,13 @@ module Spectator::Matchers
"contains #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.all? do |item|
actual.value.includes?(item)
end
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.
@ -38,7 +38,7 @@ module Spectator::Matchers
private def failure_message_when_negated(actual)
"#{actual.label} contains #{expected.label}"
end
# Additional information about the match failure.
# The return value is a NamedTuple with Strings for each value.
private def values(actual)

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether a collection is empty.
# The values are checked with the `empty?` method.
struct EmptyMatcher < StandardMatcher
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.empty?
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"is empty"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.empty?
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -30,6 +30,16 @@ module Spectator::Matchers
end
end
# Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if actual.value.responds_to?(:ends_with?)
negated_match_ends_with(actual)
else
negated_match_last(actual)
end
end
# Checks whether the actual value ends with the expected value.
# This method expects (and uses) the `#ends_with?` method on the value.
private def match_ends_with(actual_value, actual_label)
@ -60,16 +70,6 @@ module Spectator::Matchers
end
end
# Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if actual.value.responds_to?(:ends_with?)
negated_match_ends_with(actual)
else
negated_match_last(actual)
end
end
# Checks whether the actual value does not end with the expected value.
# This method expects (and uses) the `#ends_with?` method on the value.
private def negated_match_ends_with(actual)

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Common matcher that tests whether two values equal each other.
# The values are compared with the == operator.
struct EqualityMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value == actual.value
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"equals #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value == actual.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether one value is greater than or equal to another.
# The values are compared with the >= operator.
struct GreaterThanEqualMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value >= expected.value
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"greater than or equal to #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value >= expected.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether one value is greater than another.
# The values are compared with the > operator.
struct GreaterThanMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value > expected.value
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"greater than #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value > expected.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.
@ -45,7 +45,7 @@ module Spectator::Matchers
actual: actual.value.inspect,
}
end
# Additional information about the match failure when negated.
# The return value is a NamedTuple with Strings for each value.
private def negated_values(actual)

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether a `Hash` (or similar type) has a given key.
# The set is checked with the `has_key?` method.
struct HaveKeyMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.has_key?(expected.value)
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"has key #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.has_key?(expected.value)
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -5,6 +5,13 @@ module Spectator::Matchers
# For a `String`, the `includes?` method is used.
# Otherwise, it expects an `Enumerable` and iterates over each item until === is true.
struct HaveMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
def description
"includes #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
if (value = actual.value).is_a?(String)
@ -33,13 +40,6 @@ module Spectator::Matchers
end
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
def description
"includes #{expected.label}"
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -75,17 +75,6 @@ module Spectator::Matchers
{% end %}
end
# Produces the tuple for the failed match data from a snapshot of the predicate methods.
private def values(snapshot)
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: snapshot[{{attribute.symbolize}}].inspect,
{% end %}
}
{% end %}
end
# Checks if all predicate methods from the snapshot of them are satisified.
private def match?(snapshot)
# Test each predicate and immediately return false if one is false.
@ -96,5 +85,16 @@ module Spectator::Matchers
# All checks passed if this point is reached.
true
end
# Produces the tuple for the failed match data from a snapshot of the predicate methods.
private def values(snapshot)
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: snapshot[{{attribute.symbolize}}].inspect,
{% end %}
}
{% end %}
end
end
end

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether a `Hash` (or similar type) has a given value.
# The set is checked with the `has_value?` method.
struct HaveValueMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.has_value?(expected.value)
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"has value #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.has_value?(expected.value)
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -4,11 +4,6 @@ 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)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value != actual.value
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"is not equal to #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value != actual.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether one value is less than or equal to another.
# The values are compared with the <= operator.
struct LessThanEqualMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value <= expected.value
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"less than or equal to #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value <= expected.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether one value is less than another.
# The values are compared with the < operator.
struct LessThanMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value < expected.value
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"less than #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value < expected.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Common matcher that tests whether a value is nil.
# The `Object#nil?` method is used for this.
struct NilMatcher < StandardMatcher
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.nil?
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"is nil"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.nil?
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -74,17 +74,6 @@ module Spectator::Matchers
{% end %}
end
# Produces the tuple for the failed match data from a snapshot of the predicate methods.
private def values(snapshot)
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: snapshot[{{attribute.symbolize}}].inspect,
{% end %}
}
{% end %}
end
# Checks if all predicate methods from the snapshot of them are satisified.
private def match?(snapshot)
# Test each predicate and immediately return false if one is false.
@ -95,5 +84,16 @@ module Spectator::Matchers
# All checks passed if this point is reached.
true
end
# Produces the tuple for the failed match data from a snapshot of the predicate methods.
private def values(snapshot)
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: snapshot[{{attribute.symbolize}}].inspect,
{% end %}
}
{% end %}
end
end
end

View file

@ -4,11 +4,6 @@ module Spectator::Matchers
# Matcher that tests whether a value is in a given range.
# The `Range#includes?` method is used for this check.
struct RangeMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.includes?(actual.value)
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,25 @@ module Spectator::Matchers
"is in #{expected.label}"
end
# Returns a new matcher, with the same bounds, but uses an inclusive range.
def inclusive
new_range = Range.new(range.begin, range.end, exclusive: false)
expected = TestValue.new(new_range, label)
RangeMatcher.new(expected)
end
# Returns a new matcher, with the same bounds, but uses an exclusive range.
def exclusive
new_range = Range.new(range.begin, range.end, exclusive: true)
expected = TestValue.new(new_range, label)
RangeMatcher.new(expected)
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.includes?(actual.value)
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.
@ -57,20 +71,6 @@ module Spectator::Matchers
}
end
# Returns a new matcher, with the same bounds, but uses an inclusive range.
def inclusive
new_range = Range.new(range.begin, range.end, exclusive: false)
expected = TestValue.new(new_range, label)
RangeMatcher.new(expected)
end
# Returns a new matcher, with the same bounds, but uses an exclusive range.
def exclusive
new_range = Range.new(range.begin, range.end, exclusive: true)
expected = TestValue.new(new_range, label)
RangeMatcher.new(expected)
end
# Gets the expected range.
private def range
expected.value

View file

@ -4,11 +4,6 @@ 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)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.same?(actual.value)
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"is #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.same?(actual.value)
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -13,12 +13,6 @@ module Spectator::Matchers
"responds to #{label}"
end
# Generated, user-friendly, string for the expected value.
private def label
# Prefix every method name with # and join them with commas.
{{ExpectedType.keys.map { |e| "##{e}".id }.splat.stringify}}
end
# Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value)
@ -52,6 +46,13 @@ module Spectator::Matchers
{% end %}
end
# Checks if all results from the snapshot are satisified.
private def match?(snapshot)
# The snapshot did the hard work.
# Here just check if all values are true.
snapshot.values.all?
end
# Produces the tuple for the failed match data from a snapshot of the results.
private def values(snapshot)
{% begin %}
@ -63,11 +64,10 @@ module Spectator::Matchers
{% end %}
end
# Checks if all results from the snapshot are satisified.
private def match?(snapshot)
# The snapshot did the hard work.
# Here just check if all values are true.
snapshot.values.all?
# Generated, user-friendly, string for the expected value.
private def label
# Prefix every method name with # and join them with commas.
{{ExpectedType.keys.map { |e| "##{e}".id }.splat.stringify}}
end
end
end

View file

@ -4,11 +4,6 @@ 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)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value == actual.value.size
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"has size #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value == actual.value.size
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -4,11 +4,6 @@ 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)
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.size == actual.value.size
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"is the same size as #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
expected.value.size == actual.value.size
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -17,6 +17,35 @@ module Spectator::Matchers
# then the `#values` and `#negated_values` methods can be overriden.
# Finally, define a `#description` message that can be used for the one-liner "it" syntax.
abstract struct StandardMatcher < Matcher
# Actually performs the test against the expression (value or block).
#
# This method calls the abstract `#match?` method.
# If it returns true, then a `SuccessfulMatchData` instance is returned.
# Otherwise, a `FailedMatchData` instance is returned.
# Additionally, `#failure_message` and `#values` are called for a failed match.
def match(actual : TestExpression(T)) : MatchData forall T
if match?(actual)
SuccessfulMatchData.new
else
FailedMatchData.new(failure_message(actual), **values(actual))
end
end
# Performs the test against the expression (value or block), but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
#
# This method calls the abstract `#does_not_match?` method.
# If it returns true, then a `SuccessfulMatchData` instance is returned.
# Otherwise, a `FailedMatchData` instance is returned.
# Additionally, `#failure_message_when_negated` and `#negated_values` are called for a failed match.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if does_not_match?(actual)
SuccessfulMatchData.new
else
FailedMatchData.new(failure_message_when_negated(actual), **negated_values(actual))
end
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.
@ -96,34 +125,5 @@ module Spectator::Matchers
private def negated_values(actual : TestExpression(T)) forall T
values(actual)
end
# Actually performs the test against the expression (value or block).
#
# This method calls the abstract `#match?` method.
# If it returns true, then a `SuccessfulMatchData` instance is returned.
# Otherwise, a `FailedMatchData` instance is returned.
# Additionally, `#failure_message` and `#values` are called for a failed match.
def match(actual : TestExpression(T)) : MatchData forall T
if match?(actual)
SuccessfulMatchData.new
else
FailedMatchData.new(failure_message(actual), **values(actual))
end
end
# Performs the test against the expression (value or block), but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
#
# This method calls the abstract `#does_not_match?` method.
# If it returns true, then a `SuccessfulMatchData` instance is returned.
# Otherwise, a `FailedMatchData` instance is returned.
# Additionally, `#failure_message_when_negated` and `#negated_values` are called for a failed match.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if does_not_match?(actual)
SuccessfulMatchData.new
else
FailedMatchData.new(failure_message_when_negated(actual), **negated_values(actual))
end
end
end
end

View file

@ -1,5 +1,4 @@
# Checks whether the last element of the value is the expected value.
# Checks whether the last element of the value is the expected value.
# This method expects that the actual value is a set (enumerable).require "./value_matcher"
module Spectator::Matchers
@ -30,6 +29,16 @@ module Spectator::Matchers
end
end
# Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if (value = actual.value).responds_to?(:starts_with?)
negated_match_starts_with(value, actual.label)
else
negated_match_first(value, actual.label)
end
end
# Checks whether the actual value starts with the expected value.
# This method expects (and uses) the `#starts_with?` method on the value.
private def match_starts_with(actual_value, actual_label)
@ -60,16 +69,6 @@ module Spectator::Matchers
end
end
# Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if (value = actual.value).responds_to?(:starts_with?)
negated_match_starts_with(value, actual.label)
else
negated_match_first(value, actual.label)
end
end
# Checks whether the actual value does not start with the expected value.
# This method expects (and uses) the `#starts_with?` method on the value.
private def negated_match_starts_with(actual_value, actual_label)

View file

@ -15,21 +15,6 @@ module Spectator::Matchers
def initialize(@truthy : Bool = true)
end
# Generated, user-friendly, string for the expected value.
private def label
@truthy ? "truthy" : "falsey"
end
# Generated, user-friendly, string for the unexpected value.
private def negated_label
@truthy ? "falsey" : "truthy"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
@truthy == !!actual.value
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -37,47 +22,6 @@ module Spectator::Matchers
"is #{label}"
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.
#
# The message should typically only contain the test expression labels.
# Actual values should be returned by `#values`.
private def failure_message(actual)
"#{actual.label} is #{negated_label}"
end
# Message displayed when the matcher isn't satisifed and is negated.
# This is essentially what would satisfy the matcher if it wasn't negated.
#
# This is only called when `#does_not_match?` returns false.
#
# The message should typically only contain the test expression labels.
# Actual values should be returned by `#values`.
private def failure_message_when_negated(actual)
"#{actual.label} is #{label}"
end
# Additional information about the match failure.
# The return value is a NamedTuple with Strings for each value.
private def values(actual)
{
expected: @truthy ? "Not false or nil" : "false or nil",
actual: actual.value.inspect,
truthy: !!actual.value.inspect,
}
end
# Additional information about the match failure when negated.
# The return value is a NamedTuple with Strings for each value.
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.
# The spec would look like:
# ```
@ -137,5 +81,61 @@ module Spectator::Matchers
expected = TestValue.new(value)
InequalityMatcher.new(expected)
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
@truthy == !!actual.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.
#
# The message should typically only contain the test expression labels.
# Actual values should be returned by `#values`.
private def failure_message(actual)
"#{actual.label} is #{negated_label}"
end
# Message displayed when the matcher isn't satisifed and is negated.
# This is essentially what would satisfy the matcher if it wasn't negated.
#
# This is only called when `#does_not_match?` returns false.
#
# The message should typically only contain the test expression labels.
# Actual values should be returned by `#values`.
private def failure_message_when_negated(actual)
"#{actual.label} is #{label}"
end
# Additional information about the match failure.
# The return value is a NamedTuple with Strings for each value.
private def values(actual)
{
expected: @truthy ? "Not false or nil" : "false or nil",
actual: actual.value.inspect,
truthy: !!actual.value.inspect,
}
end
# Additional information about the match failure when negated.
# The return value is a NamedTuple with Strings for each value.
private def negated_values(actual)
{
expected: @truthy ? "false or nil" : "Not false or nil",
actual: actual.value.inspect,
truthy: !!actual.value.inspect,
}
end
# Generated, user-friendly, string for the expected value.
private def label
@truthy ? "truthy" : "falsey"
end
# Generated, user-friendly, string for the unexpected value.
private def negated_label
@truthy ? "falsey" : "truthy"
end
end
end

View file

@ -4,11 +4,6 @@ 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) < StandardMatcher
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.is_a?(Expected)
end
# Short text about the matcher's purpose.
# This explains what condition satisfies the matcher.
# The description is used when the one-liner syntax is used.
@ -16,6 +11,11 @@ module Spectator::Matchers
"is as #{Expected}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) forall T
actual.value.is_a?(Expected)
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.