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

View file

@ -5,29 +5,45 @@ module Spectator::Matchers
# The values are checked with the `includes?` method.
struct ContainMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise.
def match?(partial)
private def match?(actual)
expected.all? do |item|
partial.actual.includes?(item)
actual.includes?(item)
end
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) : MatchData
raise NotImplementedError.new("#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 values
{
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(partial)
"Expected #{partial.label} to contain #{label}"
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(partial)
"Expected #{partial.label} to not contain #{label}"
def negated_message
"#{@values.actual_label} does not contain #{@values.expected_label}"
end
end
end
end