Update EndWithMatcher to use MatchData

This commit is contained in:
Michael Miller 2019-03-04 20:16:21 -07:00
parent 1090a7f2f3
commit 7b0f607a6a
2 changed files with 331 additions and 278 deletions

View file

@ -1,7 +1,9 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::EndWithMatcher do describe Spectator::Matchers::EndWithMatcher 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 "against a matching string" do context "against a matching string" do
it "is true" do it "is true" do
@ -9,7 +11,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = "bar" last = "bar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at end" do context "not at end" do
@ -18,7 +21,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = "foo" last = "foo"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -29,7 +33,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = "baz" last = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -39,7 +44,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = 'r' last = 'r'
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at end" do context "not at end" do
@ -48,7 +54,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = 'b' last = 'b'
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -59,7 +66,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = 'z' last = 'z'
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -69,7 +77,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = /bar/i last = /bar/i
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at end" do context "not at end" do
@ -78,7 +87,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = /foo/i last = /foo/i
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -89,7 +99,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = /baz/i last = /baz/i
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -101,7 +112,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = :c last = :c
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at end" do context "not at end" do
@ -110,7 +122,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = :b last = :b
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -121,7 +134,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = :z last = :z
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -130,7 +144,8 @@ describe Spectator::Matchers::EndWithMatcher do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Symbol) matcher = Spectator::Matchers::EndWithMatcher.new(Symbol)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at end" do context "not at end" do
@ -138,7 +153,8 @@ describe Spectator::Matchers::EndWithMatcher do
array = [1, 2, 3, :a, :b, :c] array = [1, 2, 3, :a, :b, :c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Int32) matcher = Spectator::Matchers::EndWithMatcher.new(Int32)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -148,7 +164,8 @@ describe Spectator::Matchers::EndWithMatcher do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Int32) matcher = Spectator::Matchers::EndWithMatcher.new(Int32)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -158,7 +175,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = /baz/i last = /baz/i
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at end" do context "not at end" do
@ -167,7 +185,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = /bar/i last = /bar/i
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -178,12 +197,17 @@ describe Spectator::Matchers::EndWithMatcher do
last = /qux/i last = /qux/i
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
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
describe "#values" do
end
describe "#message" do describe "#message" do
context "with a String" do context "with a String" do
it "mentions #ends_with?" do it "mentions #ends_with?" do
@ -191,7 +215,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = "baz" last = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.message(partial).should contain("#ends_with?") match_data = matcher.match(partial)
match_data.message.should contain("#ends_with?")
end end
end end
@ -200,14 +225,16 @@ describe Spectator::Matchers::EndWithMatcher do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last) matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
matcher.message(partial).should contain("===") match_data = matcher.match(partial)
match_data.message.should contain("===")
end end
it "mentions last" do it "mentions last" do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last) matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
matcher.message(partial).should contain("last") match_data = matcher.match(partial)
match_data.message.should contain("last")
end end
end end
@ -217,7 +244,8 @@ describe Spectator::Matchers::EndWithMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
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
@ -226,7 +254,8 @@ describe Spectator::Matchers::EndWithMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last, label) matcher = Spectator::Matchers::EndWithMatcher.new(last, 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
@ -235,7 +264,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = "baz" last = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.message(partial).should contain(last) match_data = matcher.match(partial)
match_data.message.should contain(last)
end end
end end
end end
@ -247,7 +277,8 @@ describe Spectator::Matchers::EndWithMatcher do
last = "baz" last = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.negated_message(partial).should contain("#ends_with?") match_data = matcher.match(partial)
match_data.negated_message.should contain("#ends_with?")
end end
end end
@ -256,14 +287,16 @@ describe Spectator::Matchers::EndWithMatcher do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last) matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
matcher.negated_message(partial).should contain("===") match_data = matcher.match(partial)
match_data.negated_message.should contain("===")
end end
it "mentions last" do it "mentions last" do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last) matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
matcher.negated_message(partial).should contain("last") match_data = matcher.match(partial)
match_data.negated_message.should contain("last")
end end
end end
@ -273,7 +306,8 @@ describe Spectator::Matchers::EndWithMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
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
@ -282,7 +316,8 @@ describe Spectator::Matchers::EndWithMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last, label) matcher = Spectator::Matchers::EndWithMatcher.new(last, 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
@ -291,7 +326,10 @@ describe Spectator::Matchers::EndWithMatcher do
last = "baz" last = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.negated_message(partial).should contain(last) match_data = matcher.match(partial)
match_data.negated_message.should contain(last)
end
end
end end
end end
end end

View file

@ -6,30 +6,15 @@ module Spectator::Matchers
# Otherwise, it is treated as an `Indexable` and the `last` value is compared against. # Otherwise, it is treated as an `Indexable` and the `last` value is compared against.
struct EndWithMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct EndWithMatcher(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
compare_method(actual, &.eval(actual, expected)) compare_method(actual, &.eval(actual, expected))
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)
end MatchData.new(match?(values.actual), values)
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message(partial)
method_string = compare_method(partial.actual, &.to_s)
"Expected #{partial.label} to end with #{label} (using #{method_string})"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message(partial)
method_string = compare_method(partial.actual, &.to_s)
"Expected #{partial.label} to not end with #{label} (using #{method_string})"
end end
# Returns the method that should be used for comparison. # Returns the method that should be used for comparison.
@ -49,6 +34,36 @@ module Spectator::Matchers
end 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
{
expected: @values.expected,
actual: @values.actual,
}
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
method_string = compare_method(partial.actual, &.to_s)
"#{values.actual_label} ends with #{values.expected_label} (using #{method_string})"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
method_string = compare_method(partial.actual, &.to_s)
"#{values.actual_label} does not end with #{values.expected_label} (using #{method_string})"
end
end
# Comparison method for types that define the `ends_with?` method. # Comparison method for types that define the `ends_with?` method.
private struct EndsWithCompareMethod private struct EndsWithCompareMethod
# Evaluates the condition to determine whether the matcher is satisfied. # Evaluates the condition to determine whether the matcher is satisfied.