Update ContainMatcher to use MatchData

This commit is contained in:
Michael Miller 2019-03-03 20:03:20 -07:00
parent 1db5341f4b
commit be3218bcd8
2 changed files with 384 additions and 309 deletions

View file

@ -1,327 +1,386 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::ContainMatcher do describe Spectator::Matchers::ContainMatcher do
describe "#match?" do describe "#match" do
context "with a String" do context "returned MatchData" do
context "one argument" do describe "#matched?" do
context "against a matching string" do context "with a String" do
it "is true" do context "one argument" do
value = "foobarbaz" context "against a matching string" do
search = "bar" it "is true" do
partial = new_partial(value) value = "foobarbaz"
matcher = Spectator::Matchers::ContainMatcher.new({search}) search = "bar"
matcher.match?(partial).should be_true partial = new_partial(value)
end matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "at the beginning" do context "at the beginning" do
it "is true" do it "is true" do
value = "foobar" value = "foobar"
search = "foo" search = "foo"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}) matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "at the end" do
it "is true" do
value = "foobar"
search = "bar"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
context "against a different string" do
it "is false" do
value = "foobar"
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a matching character" do
it "is true" do
value = "foobar"
search = 'o'
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "at the beginning" do
it "is true" do
value = "foobar"
search = 'f'
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "at the end" do
it "is true" do
value = "foobar"
search = 'r'
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
context "against a different character" do
it "is false" do
value = "foobar"
search = 'z'
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end end
end end
context "at the end" do context "multiple arguments" do
it "is true" do context "against matching strings" do
value = "foobar" it "is true" do
search = "bar" value = "foobarbaz"
partial = new_partial(value) search = {"foo", "bar", "baz"}
matcher = Spectator::Matchers::ContainMatcher.new({search}) partial = new_partial(value)
matcher.match?(partial).should be_true matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "against one matching string" do
it "is false" do
value = "foobarbaz"
search = {"foo", "qux"}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against no matching strings" do
it "is false" do
value = "foobar"
search = {"baz", "qux"}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against matching characters" do
it "is true" do
value = "foobarbaz"
search = {'f', 'b', 'z'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "against one matching character" do
it "is false" do
value = "foobarbaz"
search = {'f', 'c', 'd'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against no matching characters" do
it "is false" do
value = "foobarbaz"
search = {'c', 'd', 'e'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a matching string and character" do
it "is true" do
value = "foobarbaz"
search = {"foo", 'z'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "against a matching string and non-matching character" do
it "is false" do
value = "foobarbaz"
search = {"foo", 'c'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a non-matching string and matching character" do
it "is false" do
value = "foobarbaz"
search = {"qux", 'f'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a non-matching string and character" do
it "is false" do
value = "foobarbaz"
search = {"qux", 'c'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end end
end end
end end
context "against a different string" do context "with an Enumberable" do
it "is false" do context "one argument" do
value = "foobar" context "against an equal value" do
search = "baz" it "is true" do
partial = new_partial(value) array = %i[a b c]
matcher = Spectator::Matchers::ContainMatcher.new({search}) search = :b
matcher.match?(partial).should be_false partial = new_partial(array)
end matcher = Spectator::Matchers::ContainMatcher.new({search})
end match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "against a matching character" do context "at the beginning" do
it "is true" do it "is true" do
value = "foobar" array = %i[a b c]
search = 'o' search = :a
partial = new_partial(value) partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search}) matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.match?(partial).should be_true match_data = matcher.match(partial)
end match_data.matched?.should be_true
end
end
context "at the beginning" do context "at the end" do
it "is true" do it "is true" do
value = "foobar" array = %i[a b c]
search = 'f' search = :c
partial = new_partial(value) partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search}) matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
context "against a different value" do
it "is false" do
array = %i[a b c]
search = :z
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end end
end end
context "at the end" do context "multiple arguments" do
it "is true" do context "against equal values" do
value = "foobar" it "is true" do
search = 'r' array = %i[a b c]
partial = new_partial(value) search = {:a, :b}
matcher = Spectator::Matchers::ContainMatcher.new({search}) partial = new_partial(array)
matcher.match?(partial).should be_true matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end end
end
end
context "against a different character" do context "against one equal value" do
it "is false" do it "is false" do
value = "foobar" array = %i[a b c]
search = 'z' search = {:a, :d}
partial = new_partial(value) partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search}) matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against no equal values" do
it "is false" do
array = %i[a b c]
search = {:d, :e}
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end end
end end
end end
context "multiple arguments" do describe "#values" do
context "against matching strings" do describe "subset" do
it "is true" do it "is the expected set" do
value = "foobarbaz"
search = {"foo", "bar", "baz"}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_true
end
end
context "against one matching string" do
it "is false" do
value = "foobarbaz"
search = {"foo", "qux"}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false
end
end
context "against no matching strings" do
it "is false" do
value = "foobar"
search = {"baz", "qux"}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false
end
end
context "against matching characters" do
it "is true" do
value = "foobarbaz"
search = {'f', 'b', 'z'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_true
end
end
context "against one matching character" do
it "is false" do
value = "foobarbaz"
search = {'f', 'c', 'd'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false
end
end
context "against no matching characters" do
it "is false" do
value = "foobarbaz"
search = {'c', 'd', 'e'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false
end
end
context "against a matching string and character" do
it "is true" do
value = "foobarbaz"
search = {"foo", 'z'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_true
end
end
context "against a matching string and non-matching character" do
it "is false" do
value = "foobarbaz"
search = {"foo", 'c'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false
end
end
context "against a non-matching string and matching character" do
it "is false" do
value = "foobarbaz"
search = {"qux", 'f'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false
end
end
context "against a non-matching string and character" do
it "is false" do
value = "foobarbaz"
search = {"qux", 'c'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false
end
end
end
end
context "with an Enumberable" do
context "one argument" do
context "against an equal value" do
it "is true" do
array = %i[a b c]
search = :b
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.match?(partial).should be_true
end
context "at the beginning" do
it "is true" do
array = %i[a b c]
search = :a
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.match?(partial).should be_true
end
end
context "at the end" do
it "is true" do
array = %i[a b c]
search = :c
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.match?(partial).should be_true
end
end
end
context "against a different value" do
it "is false" do
array = %i[a b c]
search = :z
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.match?(partial).should be_false
end
end
end
context "multiple arguments" do
context "against equal values" do
it "is true" do
array = %i[a b c]
search = {:a, :b}
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_true
end
end
context "against one equal value" do
it "is false" do
array = %i[a b c]
search = {:a, :d}
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false
end
end
context "against no equal values" do
it "is false" do
array = %i[a b c] array = %i[a b c]
search = {:d, :e} search = {:d, :e}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search) matcher = Spectator::Matchers::ContainMatcher.new(search)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.values[:subset].should eq(search)
end
end
describe "superset" do
it "is the actual set" do
array = %i[a b c]
search = {:d, :e}
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.values[:superset].should eq(array)
end
end
end
describe "#message" do
it "contains the actual label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
it "contains the expected label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}, 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"
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.message.should contain(search)
end
end
end
describe "#negated_message" do
it "contains the actual label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
it "contains the expected label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}, 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"
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.negated_message.should contain(search)
end end
end end
end end
end end
end end
describe "#message" do
it "contains the actual label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.message(partial).should contain(label)
end
it "contains the expected label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}, label)
matcher.message(partial).should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.message(partial).should contain(search)
end
end
end
describe "#negated_message" do
it "contains the actual label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.negated_message(partial).should contain(label)
end
it "contains the expected label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}, label)
matcher.negated_message(partial).should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.negated_message(partial).should contain(search)
end
end
end
end end

View file

@ -5,29 +5,45 @@ module Spectator::Matchers
# The values are checked with the `includes?` method. # The values are checked with the `includes?` method.
struct ContainMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct ContainMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it. # Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise. private def match?(actual)
def match?(partial)
expected.all? do |item| expected.all? do |item|
partial.actual.includes?(item) actual.includes?(item)
end end
end end
# Determines whether the matcher is satisfied with the partial given to it. # Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match. # `MatchData` is returned that contains information about the match.
def match(partial) : MatchData def match(partial)
raise NotImplementedError.new("#match") values = ExpectedActual.new(partial, self)
MatchData.new(match?(values.actual), values)
end end
# Describes the condition that satisfies the matcher. # Match data specific to this matcher.
# This is informational and displayed to the end-user. private struct MatchData(ExpectedType, ActualType) < MatchData
def message(partial) # Creates the match data.
"Expected #{partial.label} to contain #{label}" def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
end super(matched)
end
# Describes the condition that won't satsify the matcher. # Information about the match.
# This is informational and displayed to the end-user. def values
def negated_message(partial) {
"Expected #{partial.label} to not contain #{label}" subset: @values.expected,
superset: @values.actual,
}
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} contains #{@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 contain #{@values.expected_label}"
end
end end
end end
end end