Update StartWithMatcher to use MatchData

This commit is contained in:
Michael Miller 2019-03-06 09:33:36 -07:00
parent 76d09a22ff
commit a78b80ed1f
2 changed files with 417 additions and 310 deletions

View file

@ -1,7 +1,9 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::StartWithMatcher do describe Spectator::Matchers::StartWithMatcher 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::StartWithMatcher do
start = "foo" start = "foo"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at start" do context "not at start" do
@ -18,7 +21,8 @@ describe Spectator::Matchers::StartWithMatcher do
start = "bar" start = "bar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher do
start = "baz" start = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher do
start = 'f' start = 'f'
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at start" do context "not at start" do
@ -48,7 +54,8 @@ describe Spectator::Matchers::StartWithMatcher do
start = 'b' start = 'b'
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher do
start = 'z' start = 'z'
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher do
start = /foo/i start = /foo/i
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at start" do context "not at start" do
@ -78,7 +87,8 @@ describe Spectator::Matchers::StartWithMatcher do
start = /bar/i start = /bar/i
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher do
start = /baz/i start = /baz/i
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher do
start = :a start = :a
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at start" do context "not at start" do
@ -110,7 +122,8 @@ describe Spectator::Matchers::StartWithMatcher do
start = :b start = :b
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher do
start = :z start = :z
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(Symbol) matcher = Spectator::Matchers::StartWithMatcher.new(Symbol)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at start" do context "not at start" do
@ -138,7 +153,8 @@ describe Spectator::Matchers::StartWithMatcher 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::StartWithMatcher.new(Symbol) matcher = Spectator::Matchers::StartWithMatcher.new(Symbol)
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::StartWithMatcher do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(Int32) matcher = Spectator::Matchers::StartWithMatcher.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::StartWithMatcher do
start = /foo/i start = /foo/i
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
context "not at start" do context "not at start" do
@ -167,7 +185,8 @@ describe Spectator::Matchers::StartWithMatcher do
start = /bar/i start = /bar/i
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
end end
@ -178,7 +197,69 @@ describe Spectator::Matchers::StartWithMatcher do
start = /qux/i start = /qux/i
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
end
describe "#values" do
context "with a String" do
context "expected" do
it "is the expected value" do
value = "FOOBAR"
first = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data.values[:expected].should eq(first)
end
end
context "actual" do
it "is the actual value" do
value = "FOOBAR"
first = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data.values[:actual].should eq(value)
end
end
end
context "with an Indexable" do
context "expected" do
it "is the expected value" do
array = %w[FOO BAR BAZ]
first = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data.values[:expected].should eq(first)
end
end
context "actual" do
it "is the first element" do
array = %w[FOO BAR BAZ]
first = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data.values[:actual].should eq(array.first)
end
end
context "list" do
it "is the full actual list" do
array = %w[FOO BAR BAZ]
first = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data.values[:list].should eq(array)
end end
end end
end end
@ -191,7 +272,8 @@ describe Spectator::Matchers::StartWithMatcher do
start = "baz" start = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.message(partial).should contain("#starts_with?") match_data = matcher.match(partial)
match_data.message.should contain("#starts_with?")
end end
end end
@ -200,14 +282,16 @@ describe Spectator::Matchers::StartWithMatcher do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first) matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
matcher.message(partial).should contain("===") match_data = matcher.match(partial)
match_data.message.should contain("===")
end end
it "mentions first" do it "mentions first" do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first) matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
matcher.message(partial).should contain("first") match_data = matcher.match(partial)
match_data.message.should contain("first")
end end
end end
@ -217,7 +301,8 @@ describe Spectator::Matchers::StartWithMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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 +311,8 @@ describe Spectator::Matchers::StartWithMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start, label) matcher = Spectator::Matchers::StartWithMatcher.new(start, 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 +321,8 @@ describe Spectator::Matchers::StartWithMatcher do
start = "baz" start = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.message(partial).should contain(start) match_data = matcher.match(partial)
match_data.message.should contain(start)
end end
end end
end end
@ -247,7 +334,8 @@ describe Spectator::Matchers::StartWithMatcher do
start = "baz" start = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.negated_message(partial).should contain("#starts_with?") match_data = matcher.match(partial)
match_data.negated_message.should contain("#starts_with?")
end end
end end
@ -256,14 +344,16 @@ describe Spectator::Matchers::StartWithMatcher do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first) matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
matcher.negated_message(partial).should contain("===") match_data = matcher.match(partial)
match_data.negated_message.should contain("===")
end end
it "mentions first" do it "mentions first" do
array = %i[a b c] array = %i[a b c]
partial = new_partial(array) partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first) matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
matcher.negated_message(partial).should contain("first") match_data = matcher.match(partial)
match_data.negated_message.should contain("first")
end end
end end
@ -273,7 +363,8 @@ describe Spectator::Matchers::StartWithMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
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 +373,8 @@ describe Spectator::Matchers::StartWithMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start, label) matcher = Spectator::Matchers::StartWithMatcher.new(start, 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 +383,10 @@ describe Spectator::Matchers::StartWithMatcher do
start = "baz" start = "baz"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start) matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.negated_message(partial).should contain(start) match_data = matcher.match(partial)
match_data.negated_message.should contain(start)
end
end
end end
end end
end end

View file

@ -6,72 +6,84 @@ module Spectator::Matchers
# Otherwise, it is treated as an `Enumerable` and the `first` value is compared against. # Otherwise, it is treated as an `Enumerable` and the `first` value is compared against.
struct StartWithMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct StartWithMatcher(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_starts_with?(actual)
def match?(partial) actual.starts_with?(expected)
actual = partial.actual end
compare_method(actual, &.eval(actual, expected))
# Determines whether the matcher is satisfied with the value given to it.
private def match_first?(actual)
expected === actual
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)
actual = values.actual
if actual.responds_to?(:starts_with?)
StartsWithMatchData.new(match_starts_with?(actual), values)
else
first = actual.first
FirstMatchData.new(match_first?(first), values, first)
end
end
# Match data specific to this matcher.
# This type is used when the actual value responds to `starts_with?`.
private struct StartsWithMatchData(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 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
method_string = compare_method(partial.actual, &.to_s) "#{@values.actual_label} starts with #{@values.expected_label} (using #starts_with?)"
"Expected #{partial.label} to start with #{label} (using #{method_string})"
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
method_string = compare_method(partial.actual, &.to_s) "#{@values.actual_label} does not start with #{@values.expected_label} (using #starts_with?)"
"Expected #{partial.label} to not start with #{label} (using #{method_string})"
end
# Returns the method that should be used for comparison.
# Call `eval(actual, expected)` on the returned value.
private macro compare_method(actual, &block)
# If the actual type defines `starts_with?`,
# then use that for the comparison.
# Otherwise, treat the actual type as an `Enumerable`,
# and retrieve the first value to compare with.
# FIXME: Is there a better way to do this?
if {{actual}}.responds_to?(:starts_with?)
{{block.args.first}} = StartsWithCompareMethod.new
{{block.body}}
else
{{block.args.first}} = EnumerableCompareMethod.new
{{block.body}}
end end
end end
# Comparison method for types that define the `starts_with?` method. # Match data specific to this matcher.
private struct StartsWithCompareMethod # This type is used when the actual value does not respond to `ends_with?`.
# Evaluates the condition to determine whether the matcher is satisfied. private struct FirstMatchData(ExpectedType, ActualType, FirstType) < MatchData
def eval(actual, expected) # Creates the match data.
actual.starts_with?(expected) def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType), @first : FirstType)
super(matched)
end end
# String representation for end-user output. # Information about the match.
def to_s def values
"#starts_with?" {
end expected: @values.expected,
actual: @first,
list: @values.actual,
}
end end
# Comparison method for `Enumerable` types. # Describes the condition that satisfies the matcher.
private struct EnumerableCompareMethod # This is informational and displayed to the end-user.
# Evaluates the condition to determine whether the matcher is satisfied. def message
def eval(actual, expected) "#{@values.actual_label} starts with #{@values.expected_label} (using expected === actual.first)"
expected === actual.first
end end
# String representation for end-user output. # Describes the condition that won't satsify the matcher.
def to_s # This is informational and displayed to the end-user.
"expected === actual.first" def negated_message
"#{@values.actual_label} does not start with #{@values.expected_label} (using expected === actual.first)"
end end
end end
end end