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,298 +1,336 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::EndWithMatcher do describe Spectator::Matchers::EndWithMatcher do
describe "#match?" do describe "#match" do
context "with a String" do context "returned MatchData" do
context "against a matching string" do describe "#matched?" do
it "is true" do context "with a String" do
value = "foobar" context "against a matching string" do
last = "bar" it "is true" do
partial = new_partial(value) value = "foobar"
matcher = Spectator::Matchers::EndWithMatcher.new(last) last = "bar"
matcher.match?(partial).should be_true partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at end" do
it "is false" do
value = "foobar"
last = "foo"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a different string" do
it "is false" do
value = "foobar"
last = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a matching character" do
it "is true" do
value = "foobar"
last = 'r'
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at end" do
it "is false" do
value = "foobar"
last = 'b'
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a different character" do
it "is false" do
value = "foobar"
last = 'z'
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a matching regex" do
it "is true" do
value = "FOOBAR"
last = /bar/i
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at end" do
it "is false" do
value = "FOOBAR"
last = /foo/i
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a non-matching regex" do
it "is false" do
value = "FOOBAR"
last = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end end
context "not at end" do context "with an Enumberable" do
it "is false" do context "against an equal value" do
value = "foobar" it "is true" do
last = "foo" array = %i[a b c]
partial = new_partial(value) last = :c
matcher = Spectator::Matchers::EndWithMatcher.new(last) partial = new_partial(array)
matcher.match?(partial).should be_false matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at end" do
it "is false" do
array = %i[a b c]
last = :b
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a different value" do
it "is false" do
array = %i[a b c]
last = :z
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against matching element type" do
it "is true" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Symbol)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at end" do
it "is false" do
array = [1, 2, 3, :a, :b, :c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Int32)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against different element type" do
it "is false" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Int32)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a matching regex" do
it "is true" do
array = %w[FOO BAR BAZ]
last = /baz/i
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at end" do
it "is false" do
array = %w[FOO BAR BAZ]
last = /bar/i
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a non-matching regex" do
it "is false" do
array = %w[FOO BAR BAZ]
last = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end end
end end
end end
context "against a different string" do describe "#values" do
it "is false" do
end
describe "#message" do
context "with a String" do
it "mentions #ends_with?" do
value = "foobar"
last = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.message.should contain("#ends_with?")
end
end
context "with an Enumerable" do
it "mentions ===" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
match_data = matcher.match(partial)
match_data.message.should contain("===")
end
it "mentions last" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
match_data = matcher.match(partial)
match_data.message.should contain("last")
end
end
it "contains the actual label" do
value = "foobar" value = "foobar"
last = "baz" last = "baz"
partial = new_partial(value) label = "everything"
partial = new_partial(value, label)
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.message.should contain(label)
end end
end
context "against a matching character" do it "contains the expected label" do
it "is true" do
value = "foobar" value = "foobar"
last = 'r' last = "baz"
label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last) matcher = Spectator::Matchers::EndWithMatcher.new(last, label)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
context "not at end" do context "when expected label is omitted" do
it "is false" do it "contains stringified form of expected value" do
value = "foobar" value = "foobar"
last = 'b' 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.message.should contain(last)
end end
end end
end end
context "against a different character" do describe "#negated_message" do
it "is false" do context "with a String" do
value = "foobar" it "mentions #starts_with?" do
last = 'z' value = "foobar"
partial = new_partial(value) last = "baz"
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false
end
end
context "against a matching regex" do
it "is true" do
value = "FOOBAR"
last = /bar/i
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_true
end
context "not at end" do
it "is false" do
value = "FOOBAR"
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.negated_message.should contain("#ends_with?")
end end
end end
end
context "against a non-matching regex" do context "with an Enumerable" do
it "is false" do it "mentions ===" do
value = "FOOBAR"
last = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false
end
end
end
context "with an Enumberable" do
context "against an equal value" do
it "is true" do
array = %i[a b c]
last = :c
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_true
end
context "not at end" do
it "is false" do
array = %i[a b c] array = %i[a b c]
last = :b
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
match_data = matcher.match(partial)
match_data.negated_message.should contain("===")
end
it "mentions last" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
match_data = matcher.match(partial)
match_data.negated_message.should contain("last")
end
end
it "contains the actual label" do
value = "foobar"
last = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
it "contains the expected label" do
value = "foobar"
last = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last, label)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
last = "baz"
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.negated_message.should contain(last)
end end
end end
end end
context "against a different value" do
it "is false" do
array = %i[a b c]
last = :z
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false
end
end
context "against matching element type" do
it "is true" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Symbol)
matcher.match?(partial).should be_true
end
context "not at end" do
it "is false" do
array = [1, 2, 3, :a, :b, :c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Int32)
matcher.match?(partial).should be_false
end
end
end
context "against different element type" do
it "is false" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(Int32)
matcher.match?(partial).should be_false
end
end
context "against a matching regex" do
it "is true" do
array = %w[FOO BAR BAZ]
last = /baz/i
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_true
end
context "not at end" do
it "is false" do
array = %w[FOO BAR BAZ]
last = /bar/i
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false
end
end
end
context "against a non-matching regex" do
it "is false" do
array = %w[FOO BAR BAZ]
last = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.match?(partial).should be_false
end
end
end
end
describe "#message" do
context "with a String" do
it "mentions #ends_with?" do
value = "foobar"
last = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.message(partial).should contain("#ends_with?")
end
end
context "with an Enumerable" do
it "mentions ===" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
matcher.message(partial).should contain("===")
end
it "mentions last" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
matcher.message(partial).should contain("last")
end
end
it "contains the actual label" do
value = "foobar"
last = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.message(partial).should contain(label)
end
it "contains the expected label" do
value = "foobar"
last = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last, label)
matcher.message(partial).should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
last = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.message(partial).should contain(last)
end
end
end
describe "#negated_message" do
context "with a String" do
it "mentions #starts_with?" do
value = "foobar"
last = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.negated_message(partial).should contain("#ends_with?")
end
end
context "with an Enumerable" do
it "mentions ===" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
matcher.negated_message(partial).should contain("===")
end
it "mentions last" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::EndWithMatcher.new(array.last)
matcher.negated_message(partial).should contain("last")
end
end
it "contains the actual label" do
value = "foobar"
last = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.negated_message(partial).should contain(label)
end
it "contains the expected label" do
value = "foobar"
last = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last, label)
matcher.negated_message(partial).should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
last = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
matcher.negated_message(partial).should contain(last)
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.