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,7 +1,9 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::ContainMatcher do describe Spectator::Matchers::ContainMatcher do
describe "#match?" do describe "#match" do
context "returned MatchData" do
describe "#matched?" do
context "with a String" do context "with a String" do
context "one argument" do context "one argument" do
context "against a matching string" do context "against a matching string" do
@ -10,7 +12,8 @@ describe Spectator::Matchers::ContainMatcher do
search = "bar" search = "bar"
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 beginning" do context "at the beginning" do
@ -19,7 +22,8 @@ describe Spectator::Matchers::ContainMatcher do
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
end end
@ -29,7 +33,8 @@ describe Spectator::Matchers::ContainMatcher do
search = "bar" search = "bar"
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
end end
end end
@ -40,7 +45,8 @@ describe Spectator::Matchers::ContainMatcher do
search = "baz" search = "baz"
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -50,7 +56,8 @@ describe Spectator::Matchers::ContainMatcher do
search = 'o' search = 'o'
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 beginning" do context "at the beginning" do
@ -59,7 +66,8 @@ describe Spectator::Matchers::ContainMatcher do
search = 'f' search = 'f'
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
end end
@ -69,7 +77,8 @@ describe Spectator::Matchers::ContainMatcher do
search = 'r' search = 'r'
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
end end
end end
@ -80,7 +89,8 @@ describe Spectator::Matchers::ContainMatcher do
search = 'z' search = 'z'
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -92,7 +102,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {"foo", "bar", "baz"} search = {"foo", "bar", "baz"}
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
end end
@ -102,7 +113,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {"foo", "qux"} search = {"foo", "qux"}
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -112,7 +124,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {"baz", "qux"} search = {"baz", "qux"}
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -122,7 +135,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {'f', 'b', 'z'} search = {'f', 'b', 'z'}
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
end end
@ -132,7 +146,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {'f', 'c', 'd'} search = {'f', 'c', 'd'}
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -142,7 +157,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {'c', 'd', 'e'} search = {'c', 'd', 'e'}
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -152,7 +168,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {"foo", 'z'} search = {"foo", 'z'}
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
end end
@ -162,7 +179,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {"foo", 'c'} search = {"foo", 'c'}
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -172,7 +190,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {"qux", 'f'} search = {"qux", 'f'}
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -182,7 +201,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {"qux", 'c'} search = {"qux", 'c'}
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_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -196,7 +216,8 @@ describe Spectator::Matchers::ContainMatcher do
search = :b search = :b
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_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "at the beginning" do context "at the beginning" do
@ -205,7 +226,8 @@ describe Spectator::Matchers::ContainMatcher do
search = :a search = :a
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_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -215,7 +237,8 @@ describe Spectator::Matchers::ContainMatcher do
search = :c search = :c
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_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
end end
@ -226,7 +249,8 @@ describe Spectator::Matchers::ContainMatcher do
search = :z search = :z
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.matched?.should be_false
end end
end end
end end
@ -238,7 +262,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {:a, :b} search = {:a, :b}
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_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -248,7 +273,8 @@ describe Spectator::Matchers::ContainMatcher do
search = {:a, :d} search = {:a, :d}
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.matched?.should be_false
end end
end end
@ -258,13 +284,38 @@ describe Spectator::Matchers::ContainMatcher do
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.matched?.should be_false
end end
end end
end end
end end
end end
describe "#values" do
describe "subset" do
it "is the expected 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[: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 describe "#message" do
it "contains the actual label" do it "contains the actual label" do
value = "foobar" value = "foobar"
@ -272,7 +323,8 @@ describe Spectator::Matchers::ContainMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::ContainMatcher.new({search}) matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.message(partial).should contain(label) match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
it "contains the expected label" do it "contains the expected label" do
@ -281,7 +333,8 @@ describe Spectator::Matchers::ContainMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}, label) matcher = Spectator::Matchers::ContainMatcher.new({search}, label)
matcher.message(partial).should contain(label) match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
context "when expected label is omitted" do context "when expected label is omitted" do
@ -290,7 +343,8 @@ describe Spectator::Matchers::ContainMatcher do
search = "baz" search = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}) matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.message(partial).should contain(search) match_data = matcher.match(partial)
match_data.message.should contain(search)
end end
end end
end end
@ -302,7 +356,8 @@ describe Spectator::Matchers::ContainMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::ContainMatcher.new({search}) matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.negated_message(partial).should contain(label) match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end end
it "contains the expected label" do it "contains the expected label" do
@ -311,7 +366,8 @@ describe Spectator::Matchers::ContainMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}, label) matcher = Spectator::Matchers::ContainMatcher.new({search}, label)
matcher.negated_message(partial).should contain(label) match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end end
context "when expected label is omitted" do context "when expected label is omitted" do
@ -320,7 +376,10 @@ describe Spectator::Matchers::ContainMatcher do
search = "baz" search = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}) matcher = Spectator::Matchers::ContainMatcher.new({search})
matcher.negated_message(partial).should contain(search) match_data = matcher.match(partial)
match_data.negated_message.should contain(search)
end
end
end 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
# 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 values
{
subset: @values.expected,
superset: @values.actual,
}
end end
# Describes the condition that satisfies the matcher. # Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user. # This is informational and displayed to the end-user.
def message(partial) def message
"Expected #{partial.label} to contain #{label}" "#{@values.actual_label} contains #{@values.expected_label}"
end end
# Describes the condition that won't satsify the matcher. # Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user. # This is informational and displayed to the end-user.
def negated_message(partial) def negated_message
"Expected #{partial.label} to not contain #{label}" "#{@values.actual_label} does not contain #{@values.expected_label}"
end
end end
end end
end end