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

@ -88,6 +88,28 @@ describe Spectator::Matchers::CaseMatcher do
match_data.matched?.should be_false
end
end
context "with a matching regex" do
it "is true" do
value = "foobar"
pattern = /foo/
partial = new_partial(value)
matcher = Spectator::Matchers::CaseMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "with a non-matching regex" do
it "is false" do
value = "foo"
pattern = /bar/
partial = new_partial(value)
matcher = Spectator::Matchers::CaseMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
end
@ -116,14 +138,6 @@ describe Spectator::Matchers::CaseMatcher do
end
describe "#message" do
it "mentions ===" do
value = 42
partial = new_partial(value)
matcher = Spectator::Matchers::CaseMatcher.new(value)
match_data = matcher.match(partial)
match_data.message.should contain("===")
end
it "contains the actual label" do
value = 42
label = "everything"
@ -155,14 +169,6 @@ describe Spectator::Matchers::CaseMatcher do
end
describe "#negated_message" do
it "mentions ===" do
value = 42
partial = new_partial(value)
matcher = Spectator::Matchers::CaseMatcher.new(value)
match_data = matcher.match(partial)
match_data.negated_message.should contain("===")
end
it "contains the actual label" do
value = 42
label = "everything"

View file

@ -1,147 +0,0 @@
require "../spec_helper"
describe Spectator::Matchers::RegexMatcher do
describe "#match" do
it "compares using #=~" do
spy = SpySUT.new
partial = new_partial(spy)
matcher = Spectator::Matchers::RegexMatcher.new(/foobar/)
matcher.match(partial)
spy.match_call_count.should be > 0
end
context "returned MatchData" do
describe "#matched?" do
context "with a matching pattern" do
it "is true" do
value = "foobar"
pattern = /foo/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "with a non-matching pattern" do
it "is false" do
value = "foo"
pattern = /bar/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
describe "#values" do
context "expected" do
it "is the expected value" do
value = "foo"
pattern = /bar/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data.values, :expected)[:value].should eq(pattern)
end
end
context "actual" do
it "is the actual value" do
value = "foo"
pattern = /bar/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data.values, :actual)[:value].should eq(value)
end
end
end
describe "#message" do
it "mentions =~" do
value = "foobar"
pattern = /foo/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.message.should contain("=~")
end
it "contains the actual label" do
value = "foobar"
label = "different"
pattern = /foo/
partial = new_partial(value, label)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
it "contains the expected label" do
value = "foobar"
label = "different"
pattern = /foo/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern, label)
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
pattern = /foo/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.message.should contain(pattern.to_s)
end
end
end
describe "#negated_message" do
it "mentions =~" do
value = "foobar"
pattern = /foo/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.negated_message.should contain("=~")
end
it "contains the actual label" do
value = "foobar"
label = "different"
pattern = /foo/
partial = new_partial(value, label)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
it "contains the expected label" do
value = "foobar"
label = "different"
pattern = /foo/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern, label)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
pattern = /foo/
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.negated_message.should contain(pattern.to_s)
end
end
end
end
end
end

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