Use RegexMatcher to force used =~ operator

This commit is contained in:
Michael Miller 2021-09-22 17:57:56 -06:00
parent 64b9c13436
commit 2f1ac1374b
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 41 additions and 1 deletions

View file

@ -0,0 +1,40 @@
require "./value_matcher"
module Spectator::Matchers
# Common matcher that tests whether a value matches another.
# The values are compared with the =~ operator.
struct RegexMatcher(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 : String
"matches #{expected.label}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : Expression(T)) : Bool forall T
!!(actual.value =~ expected.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) : String
"#{actual.label} does not match #{expected.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) : String
"#{actual.label} matched #{expected.label}"
end
end
end

View file

@ -99,7 +99,7 @@ module Spectator::Matchers
# ```
def =~(value)
expected = Value.new(value)
PatternMatcher.new(expected)
RegexMatcher.new(expected)
end
# Checks whether the matcher is satisifed with the expression given to it.