Update AttributesMatcher to use MatchData

This is a glorious mess.
This commit is contained in:
Michael Miller 2019-03-04 16:06:20 -07:00
parent df663d30d9
commit e7441df159
2 changed files with 354 additions and 242 deletions

View file

@ -1,16 +1,18 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::AttributesMatcher do describe Spectator::Matchers::AttributesMatcher do
describe "#match?" do describe "#match" do
it "uses ===" do it "uses ===" do
array = %i[a b c] array = %i[a b c]
spy = SpySUT.new spy = SpySUT.new
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new({spy}) matcher = Spectator::Matchers::AttributesMatcher.new({first: spy})
matcher.match?(partial).should be_true matcher.match(partial)
spy.case_eq_call_count.should be > 0 spy.case_eq_call_count.should be > 0
end end
context "returned MatchData" do
describe "#matched?" do
context "one argument" do context "one argument" do
context "against an equal value" do context "against an equal value" do
it "is true" do it "is true" do
@ -18,7 +20,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: :a} attributes = {first: :a}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -28,7 +31,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: :z} attributes = {first: :z}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -38,7 +42,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Symbol} attributes = {first: Symbol}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -48,7 +53,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Int32} attributes = {first: Int32}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -58,7 +64,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: /foo/i} attributes = {first: /foo/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -68,7 +75,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: /qux/i} attributes = {first: /qux/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -80,7 +88,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: :a, last: :c} attributes = {first: :a, last: :c}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "matching type" do context "matching type" do
@ -90,7 +99,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Symbol, last: /foo/i} attributes = {first: Symbol, last: /foo/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -100,7 +110,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Symbol, last: /bar/i} attributes = {first: Symbol, last: /bar/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -112,7 +123,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Float32, last: /foo/i} attributes = {first: Float32, last: /foo/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -122,7 +134,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Float32, last: /bar/i} attributes = {first: Float32, last: /bar/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -134,7 +147,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: :a, last: :d} attributes = {first: :a, last: :d}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -144,7 +158,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: :d, last: :e} attributes = {first: :d, last: :e}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -154,7 +169,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Symbol, last: String} attributes = {first: Symbol, last: String}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -164,7 +180,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Symbol, last: Float32} attributes = {first: Symbol, last: Float32}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -174,7 +191,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Float32, last: Bytes} attributes = {first: Float32, last: Bytes}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -184,7 +202,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: /foo/i, last: /baz/i} attributes = {first: /foo/i, last: /baz/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -194,7 +213,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: /foo/i, last: /qux/i} attributes = {first: /foo/i, last: /qux/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -204,7 +224,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: /baz/i, last: /qux/i} attributes = {first: /baz/i, last: /qux/i}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -214,12 +235,63 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {first: Symbol, last: /foo/i, size: 3} attributes = {first: Symbol, last: /foo/i, size: 3}
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
end end
end end
describe "#values" do
it "contains a key for each expected attribute" do
array = [:a, 42, "FOO"]
attributes = {first: Symbol, last: /foo/i, size: 3}
partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
match_data = matcher.match(partial)
values = match_data.values
values.has_key?(:"expected first").should be_true
values.has_key?(:"expected last").should be_true
values.has_key?(:"expected size").should be_true
end
it "contains a key for each actual value" do
array = [:a, 42, "FOO"]
attributes = {first: Symbol, last: /foo/i, size: 3}
partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
match_data = matcher.match(partial)
values = match_data.values
values.has_key?(:"actual first").should be_true
values.has_key?(:"actual last").should be_true
values.has_key?(:"actual size").should be_true
end
it "has the expected values" do
array = [:a, 42, "FOO"]
attributes = {first: Symbol, last: /foo/i, size: 3}
partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
match_data = matcher.match(partial)
values = match_data.values
values[:"expected first"].should eq(attributes[:first])
values[:"expected last"].should eq(attributes[:last])
values[:"expected size"].should eq(attributes[:size])
end
it "has the actual values" do
array = [:a, 42, "FOO"]
attributes = {first: Symbol, last: /foo/i, size: 3}
partial = new_partial(array)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
match_data = matcher.match(partial)
values = match_data.values
values[:"actual first"].should eq(array.first)
values[:"actual last"].should eq(array.last)
values[:"actual size"].should eq(array.size)
end
end
describe "#message" do describe "#message" do
it "contains the actual label" do it "contains the actual label" do
value = "foobar" value = "foobar"
@ -227,7 +299,8 @@ describe Spectator::Matchers::AttributesMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
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
@ -236,7 +309,8 @@ describe Spectator::Matchers::AttributesMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes, label) matcher = Spectator::Matchers::AttributesMatcher.new(attributes, 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
@ -245,7 +319,8 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {size: 6} attributes = {size: 6}
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.message(partial).should contain(attributes.to_s) match_data = matcher.match(partial)
match_data.message.should contain(attributes.to_s)
end end
end end
end end
@ -257,7 +332,8 @@ describe Spectator::Matchers::AttributesMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
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
@ -266,7 +342,8 @@ describe Spectator::Matchers::AttributesMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes, label) matcher = Spectator::Matchers::AttributesMatcher.new(attributes, 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
@ -275,7 +352,10 @@ describe Spectator::Matchers::AttributesMatcher do
attributes = {size: 6} attributes = {size: 6}
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::AttributesMatcher.new(attributes) matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
matcher.negated_message(partial).should contain(attributes.to_s) match_data = matcher.match(partial)
match_data.negated_message.should contain(attributes.to_s)
end
end
end end
end end
end end

View file

@ -6,13 +6,10 @@ module Spectator::Matchers
# The `ExpectedType` type param should be a `NamedTuple`. # The `ExpectedType` type param should be a `NamedTuple`.
struct AttributesMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct AttributesMatcher(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)
actual = partial.actual
# Test each attribute and immediately return false if a comparison fails. # Test each attribute and immediately return false if a comparison fails.
{% for attribute in ExpectedType.keys %} {% for attribute in ExpectedType.keys %}
return false unless expected[{{attribute.symbolize}}] === actual.{{attribute}} return false unless expected[{{attribute.symbolize}}] === actual[{{attribute.symbolize}}]
{% end %} {% end %}
# All checks passed if this point is reached. # All checks passed if this point is reached.
@ -21,20 +18,55 @@ module Spectator::Matchers
# 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") actual_values = snapshot_values(partial.actual)
values = ExpectedActual.new(expected, label, actual_values, partial.label)
MatchData.new(match?(actual_values), values)
end
# Captures all of the actual values.
# A `NamedTuple` is returned,
# with each key being the attribute.
private def snapshot_values(actual)
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: actual.{{attribute}},
{% end %}
}
{% end %}
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
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{"expected " + attribute.stringify}}: @values.expected[{{attribute.symbolize}}],
{{"actual " + attribute.stringify}}: @values.actual[{{attribute.symbolize}}],
{% end %}
}
{% end %}
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 have #{label}" "#{@values.actual_label} has attributes #{@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 have #{label}" "#{@values.actual_label} does not have attributes #{@values.expected_label}"
end
end end
end end
end end