diff --git a/src/spectator/matchers/case_matcher.cr b/src/spectator/matchers/case_matcher.cr index 278431f..3ba55f3 100644 --- a/src/spectator/matchers/case_matcher.cr +++ b/src/spectator/matchers/case_matcher.cr @@ -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. diff --git a/src/spectator/matchers/collection_matcher.cr b/src/spectator/matchers/collection_matcher.cr index dd84b4e..bfa88f2 100644 --- a/src/spectator/matchers/collection_matcher.cr +++ b/src/spectator/matchers/collection_matcher.cr @@ -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. diff --git a/src/spectator/matchers/contain_matcher.cr b/src/spectator/matchers/contain_matcher.cr index 5b4da9e..53389fc 100644 --- a/src/spectator/matchers/contain_matcher.cr +++ b/src/spectator/matchers/contain_matcher.cr @@ -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) diff --git a/src/spectator/matchers/empty_matcher.cr b/src/spectator/matchers/empty_matcher.cr index 897f5d1..a8ccfe9 100644 --- a/src/spectator/matchers/empty_matcher.cr +++ b/src/spectator/matchers/empty_matcher.cr @@ -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. diff --git a/src/spectator/matchers/end_with_matcher.cr b/src/spectator/matchers/end_with_matcher.cr index 448d1e6..ed76381 100644 --- a/src/spectator/matchers/end_with_matcher.cr +++ b/src/spectator/matchers/end_with_matcher.cr @@ -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) diff --git a/src/spectator/matchers/equality_matcher.cr b/src/spectator/matchers/equality_matcher.cr index 76e39ce..5d58454 100644 --- a/src/spectator/matchers/equality_matcher.cr +++ b/src/spectator/matchers/equality_matcher.cr @@ -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. diff --git a/src/spectator/matchers/greater_than_equal_matcher.cr b/src/spectator/matchers/greater_than_equal_matcher.cr index 39e8a41..5f4e20b 100644 --- a/src/spectator/matchers/greater_than_equal_matcher.cr +++ b/src/spectator/matchers/greater_than_equal_matcher.cr @@ -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. diff --git a/src/spectator/matchers/greater_than_matcher.cr b/src/spectator/matchers/greater_than_matcher.cr index 555ffbf..c51303b 100644 --- a/src/spectator/matchers/greater_than_matcher.cr +++ b/src/spectator/matchers/greater_than_matcher.cr @@ -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) diff --git a/src/spectator/matchers/have_key_matcher.cr b/src/spectator/matchers/have_key_matcher.cr index 3ac5168..e3f2560 100644 --- a/src/spectator/matchers/have_key_matcher.cr +++ b/src/spectator/matchers/have_key_matcher.cr @@ -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. diff --git a/src/spectator/matchers/have_matcher.cr b/src/spectator/matchers/have_matcher.cr index 3aafe98..5dd0151 100644 --- a/src/spectator/matchers/have_matcher.cr +++ b/src/spectator/matchers/have_matcher.cr @@ -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. diff --git a/src/spectator/matchers/have_predicate_matcher.cr b/src/spectator/matchers/have_predicate_matcher.cr index 8b9ff1f..dedc70e 100644 --- a/src/spectator/matchers/have_predicate_matcher.cr +++ b/src/spectator/matchers/have_predicate_matcher.cr @@ -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 diff --git a/src/spectator/matchers/have_value_matcher.cr b/src/spectator/matchers/have_value_matcher.cr index 1e8a83c..9c722f5 100644 --- a/src/spectator/matchers/have_value_matcher.cr +++ b/src/spectator/matchers/have_value_matcher.cr @@ -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. diff --git a/src/spectator/matchers/inequality_matcher.cr b/src/spectator/matchers/inequality_matcher.cr index b793510..7f175f1 100644 --- a/src/spectator/matchers/inequality_matcher.cr +++ b/src/spectator/matchers/inequality_matcher.cr @@ -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. diff --git a/src/spectator/matchers/less_than_equal_matcher.cr b/src/spectator/matchers/less_than_equal_matcher.cr index 0369031..900c7a4 100644 --- a/src/spectator/matchers/less_than_equal_matcher.cr +++ b/src/spectator/matchers/less_than_equal_matcher.cr @@ -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. diff --git a/src/spectator/matchers/less_than_matcher.cr b/src/spectator/matchers/less_than_matcher.cr index 1824d63..5f2ec2c 100644 --- a/src/spectator/matchers/less_than_matcher.cr +++ b/src/spectator/matchers/less_than_matcher.cr @@ -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. diff --git a/src/spectator/matchers/nil_matcher.cr b/src/spectator/matchers/nil_matcher.cr index bd4234b..343fe79 100644 --- a/src/spectator/matchers/nil_matcher.cr +++ b/src/spectator/matchers/nil_matcher.cr @@ -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. diff --git a/src/spectator/matchers/predicate_matcher.cr b/src/spectator/matchers/predicate_matcher.cr index 9d58073..46d3d94 100644 --- a/src/spectator/matchers/predicate_matcher.cr +++ b/src/spectator/matchers/predicate_matcher.cr @@ -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 diff --git a/src/spectator/matchers/range_matcher.cr b/src/spectator/matchers/range_matcher.cr index d03482a..6eba568 100644 --- a/src/spectator/matchers/range_matcher.cr +++ b/src/spectator/matchers/range_matcher.cr @@ -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 diff --git a/src/spectator/matchers/reference_matcher.cr b/src/spectator/matchers/reference_matcher.cr index 06898af..633e619 100644 --- a/src/spectator/matchers/reference_matcher.cr +++ b/src/spectator/matchers/reference_matcher.cr @@ -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. diff --git a/src/spectator/matchers/respond_matcher.cr b/src/spectator/matchers/respond_matcher.cr index a3869b4..d842167 100644 --- a/src/spectator/matchers/respond_matcher.cr +++ b/src/spectator/matchers/respond_matcher.cr @@ -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 diff --git a/src/spectator/matchers/size_matcher.cr b/src/spectator/matchers/size_matcher.cr index f02a2d8..1ab6e3d 100644 --- a/src/spectator/matchers/size_matcher.cr +++ b/src/spectator/matchers/size_matcher.cr @@ -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. diff --git a/src/spectator/matchers/size_of_matcher.cr b/src/spectator/matchers/size_of_matcher.cr index 244528d..3e9f1cf 100644 --- a/src/spectator/matchers/size_of_matcher.cr +++ b/src/spectator/matchers/size_of_matcher.cr @@ -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. diff --git a/src/spectator/matchers/standard_matcher.cr b/src/spectator/matchers/standard_matcher.cr index 18753bb..414e491 100644 --- a/src/spectator/matchers/standard_matcher.cr +++ b/src/spectator/matchers/standard_matcher.cr @@ -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 diff --git a/src/spectator/matchers/start_with_matcher.cr b/src/spectator/matchers/start_with_matcher.cr index 15219c7..067f52a 100644 --- a/src/spectator/matchers/start_with_matcher.cr +++ b/src/spectator/matchers/start_with_matcher.cr @@ -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) diff --git a/src/spectator/matchers/truthy_matcher.cr b/src/spectator/matchers/truthy_matcher.cr index 9eda876..34aea3b 100644 --- a/src/spectator/matchers/truthy_matcher.cr +++ b/src/spectator/matchers/truthy_matcher.cr @@ -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 diff --git a/src/spectator/matchers/type_matcher.cr b/src/spectator/matchers/type_matcher.cr index 09af7ed..2b44296 100644 --- a/src/spectator/matchers/type_matcher.cr +++ b/src/spectator/matchers/type_matcher.cr @@ -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.