Remove regex matcher - use case matcher

This commit is contained in:
Michael Miller 2019-05-08 16:39:00 -06:00
parent e4ec47f413
commit 7168b26218
5 changed files with 31 additions and 216 deletions

View file

@ -155,17 +155,20 @@ module Spectator::DSL
end
# Indicates that some value should match another.
# The =~ operator is used for this check.
# Typically a regular expression is used,
# but any type that has the =~ operator will work.
# The === (case equality) operator is used for this check.
# Typically a regular expression is used.
# This has identical behavior as a "when" condition in a case block.
#
# Examples:
# ```
# expect("foo").to match(/foo|bar/)
# expect("BAR").to match(/foo|bar/i)
# expect(1 + 2).to match(3)
# expect(5).to match(Int32) # Using `#be_a` instead is recommened here.
# expect({:foo, 5}).to match({Symbol, Int32})
# ```
macro match(expected)
::Spectator::Matchers::RegexMatcher.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::CaseMatcher.new({{expected}}, {{expected.stringify}})
end
# Indicates that some value should be true.

View file

@ -34,13 +34,13 @@ module Spectator::Matchers
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} equals #{@values.expected_label} (using ===)"
"#{@values.actual_label} matches #{@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} does not equal #{@values.expected_label} (using ===)"
"#{@values.actual_label} does not match #{@values.expected_label}"
end
end
end

View file

@ -1,47 +0,0 @@
require "./value_matcher"
module Spectator::Matchers
# Matcher that tests whether a value matches a regular expression.
# The value is compared with the =~ operator.
struct RegexMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
private def match?(actual)
!!(actual =~ expected)
end
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
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
# 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} matches #{@values.expected_label} (using =~)"
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 match #{@values.expected_label} (using =~)"
end
end
end
end