Update HaveMatcher to use MatchData

This commit is contained in:
Michael Miller 2019-03-06 10:29:00 -07:00
parent a78b80ed1f
commit 6ab2aad532
2 changed files with 576 additions and 482 deletions

View file

@ -1,16 +1,18 @@
require "../spec_helper"
describe Spectator::Matchers::HaveMatcher do
describe "#match?" do
describe "#match" do
it "uses ===" do
array = %i[a b c]
spy = SpySUT.new
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({spy})
matcher.match?(partial).should be_true
matcher.match(partial)
spy.case_eq_call_count.should be > 0
end
context "returned MatchData" do
describe "#matched?" do
context "with a String" do
context "one argument" do
context "against a matching string" do
@ -19,7 +21,8 @@ describe Spectator::Matchers::HaveMatcher do
search = "bar"
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.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
@ -28,7 +31,8 @@ describe Spectator::Matchers::HaveMatcher do
search = "foo"
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -38,7 +42,8 @@ describe Spectator::Matchers::HaveMatcher do
search = "bar"
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
@ -49,7 +54,8 @@ describe Spectator::Matchers::HaveMatcher do
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -59,7 +65,8 @@ describe Spectator::Matchers::HaveMatcher do
search = 'o'
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.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
@ -68,7 +75,8 @@ describe Spectator::Matchers::HaveMatcher do
search = 'f'
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -78,7 +86,8 @@ describe Spectator::Matchers::HaveMatcher do
search = 'r'
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
@ -89,7 +98,8 @@ describe Spectator::Matchers::HaveMatcher do
search = 'z'
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
@ -101,7 +111,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {"foo", "bar", "baz"}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -111,7 +122,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {"foo", "qux"}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -121,7 +133,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {"baz", "qux"}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -131,7 +144,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {'f', 'b', 'z'}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -141,7 +155,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {'f', 'c', 'd'}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -151,7 +166,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {'c', 'd', 'e'}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -161,7 +177,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {"foo", 'z'}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -171,7 +188,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {"foo", 'c'}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -181,7 +199,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {"qux", 'f'}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -191,7 +210,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {"qux", 'c'}
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
@ -205,7 +225,8 @@ describe Spectator::Matchers::HaveMatcher do
search = :b
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.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
@ -214,7 +235,8 @@ describe Spectator::Matchers::HaveMatcher do
search = :a
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -224,7 +246,8 @@ describe Spectator::Matchers::HaveMatcher do
search = :c
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
@ -235,7 +258,8 @@ describe Spectator::Matchers::HaveMatcher do
search = :z
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -244,7 +268,8 @@ describe Spectator::Matchers::HaveMatcher do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({Symbol})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "at the beginning" do
@ -252,7 +277,8 @@ describe Spectator::Matchers::HaveMatcher do
array = [:a, 1, 2]
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({Symbol})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -261,7 +287,8 @@ describe Spectator::Matchers::HaveMatcher do
array = [0, 1, :c]
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({Symbol})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
@ -271,7 +298,8 @@ describe Spectator::Matchers::HaveMatcher do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({Int32})
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -281,7 +309,8 @@ describe Spectator::Matchers::HaveMatcher do
search = /bar/i
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.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
@ -290,7 +319,8 @@ describe Spectator::Matchers::HaveMatcher do
search = /foo/i
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -300,7 +330,8 @@ describe Spectator::Matchers::HaveMatcher do
search = /baz/i
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
@ -311,7 +342,8 @@ describe Spectator::Matchers::HaveMatcher do
search = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
@ -323,7 +355,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {:a, :b}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "matching type" do
@ -333,7 +366,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {:a, Int32, /foo/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -343,7 +377,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {:a, Int32, /bar/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
@ -355,7 +390,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {:a, Float32, /foo/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -365,7 +401,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {:a, Float32, /bar/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
@ -377,7 +414,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {:a, :d}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -387,7 +425,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {:d, :e}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -397,7 +436,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {Symbol, String}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -407,7 +447,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {Symbol, Float32}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -417,7 +458,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {Float32, Bytes}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -427,7 +469,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {/foo/i, /bar/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
@ -437,7 +480,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {/foo/i, /qux/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -447,7 +491,8 @@ describe Spectator::Matchers::HaveMatcher do
search = {/baz/i, /qux/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -457,13 +502,38 @@ describe Spectator::Matchers::HaveMatcher do
search = {:a, Int32, /foo/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
end
end
describe "#values" do
context "subset" do
it "has the expected value" do
array = [:a, 42, "FOO"]
search = {:a, Int32, /foo/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
match_data = matcher.match(partial)
match_data.values[:subset].should eq(search)
end
end
context "superset" do
it "has the actual value" do
array = [:a, 42, "FOO"]
search = {:a, Int32, /foo/i}
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.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"
@ -471,7 +541,8 @@ describe Spectator::Matchers::HaveMatcher do
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::HaveMatcher.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
@ -480,7 +551,8 @@ describe Spectator::Matchers::HaveMatcher do
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.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
@ -489,7 +561,8 @@ describe Spectator::Matchers::HaveMatcher do
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.new({search})
matcher.message(partial).should contain(search)
match_data = matcher.match(partial)
match_data.message.should contain(search)
end
end
end
@ -501,7 +574,8 @@ describe Spectator::Matchers::HaveMatcher do
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::HaveMatcher.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
@ -510,7 +584,8 @@ describe Spectator::Matchers::HaveMatcher do
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.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
@ -519,7 +594,10 @@ describe Spectator::Matchers::HaveMatcher do
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::HaveMatcher.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

@ -7,8 +7,7 @@ module Spectator::Matchers
struct HaveMatcher(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)
actual = partial.actual
private def match?(actual)
if actual.is_a?(String)
match_string?(actual)
else
@ -16,12 +15,6 @@ module Spectator::Matchers
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")
end
# Checks if a `String` matches the expected values.
# The `includes?` method is used for this check.
private def match_string?(actual)
@ -41,16 +34,39 @@ module Spectator::Matchers
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)
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 include #{label}"
def message
"#{@values.actual_label} includes #{@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 include #{label}"
def negated_message
"#{@values.actual_label} does not include #{@values.expected_label}"
end
end
end
end