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,298 +1,393 @@
require "../spec_helper"
describe Spectator::Matchers::StartWithMatcher do
describe "#match?" do
context "with a String" do
context "against a matching string" do
it "is true" do
value = "foobar"
start = "foo"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true
describe "#match" do
context "returned MatchData" do
describe "#matched?" do
context "with a String" do
context "against a matching string" do
it "is true" do
value = "foobar"
start = "foo"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
value = "foobar"
start = "bar"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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"
start = 'f'
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
value = "foobar"
start = 'b'
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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"
start = 'z'
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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"
start = /foo/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
value = "FOOBAR"
start = /bar/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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"
start = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "not at start" do
it "is false" do
value = "foobar"
start = "bar"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false
context "with an Enumberable" do
context "against an equal value" do
it "is true" do
array = %i[a b c]
start = :a
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
array = %i[a b c]
start = :b
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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]
start = :z
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher.new(Symbol)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
array = [1, 2, 3, :a, :b, :c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(Symbol)
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::StartWithMatcher.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]
start = /foo/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
array = %w[FOO BAR BAZ]
start = /bar/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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]
start = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
end
context "against a different string" do
it "is false" do
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
describe "#message" do
context "with a String" do
it "mentions #starts_with?" do
value = "foobar"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.message.should contain("#starts_with?")
end
end
context "with an Enumerable" do
it "mentions ===" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
match_data = matcher.match(partial)
match_data.message.should contain("===")
end
it "mentions first" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
match_data = matcher.match(partial)
match_data.message.should contain("first")
end
end
it "contains the actual label" do
value = "foobar"
start = "baz"
partial = new_partial(value)
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
end
context "against a matching character" do
it "is true" do
it "contains the expected label" do
value = "foobar"
start = 'f'
start = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true
matcher = Spectator::Matchers::StartWithMatcher.new(start, label)
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
context "not at start" do
it "is false" do
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
start = 'b'
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.message.should contain(start)
end
end
end
context "against a different character" do
it "is false" do
value = "foobar"
start = 'z'
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false
end
end
context "against a matching regex" do
it "is true" do
value = "FOOBAR"
start = /foo/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true
end
context "not at start" do
it "is false" do
value = "FOOBAR"
start = /bar/i
describe "#negated_message" do
context "with a String" do
it "mentions #starts_with?" do
value = "foobar"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.negated_message.should contain("#starts_with?")
end
end
end
context "against a non-matching regex" do
it "is false" do
value = "FOOBAR"
start = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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]
start = :a
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true
end
context "not at start" do
it "is false" do
context "with an Enumerable" do
it "mentions ===" do
array = %i[a b c]
start = :b
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
match_data = matcher.match(partial)
match_data.negated_message.should contain("===")
end
it "mentions first" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
match_data = matcher.match(partial)
match_data.negated_message.should contain("first")
end
end
it "contains the actual label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
it "contains the expected label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start, 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"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.negated_message.should contain(start)
end
end
end
context "against a different value" do
it "is false" do
array = %i[a b c]
start = :z
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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::StartWithMatcher.new(Symbol)
matcher.match?(partial).should be_true
end
context "not at start" do
it "is false" do
array = [1, 2, 3, :a, :b, :c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(Symbol)
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::StartWithMatcher.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]
start = /foo/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_true
end
context "not at start" do
it "is false" do
array = %w[FOO BAR BAZ]
start = /bar/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
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]
start = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.match?(partial).should be_false
end
end
end
end
describe "#message" do
context "with a String" do
it "mentions #starts_with?" do
value = "foobar"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.message(partial).should contain("#starts_with?")
end
end
context "with an Enumerable" do
it "mentions ===" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
matcher.message(partial).should contain("===")
end
it "mentions first" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
matcher.message(partial).should contain("first")
end
end
it "contains the actual label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.message(partial).should contain(label)
end
it "contains the expected label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start, 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"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.message(partial).should contain(start)
end
end
end
describe "#negated_message" do
context "with a String" do
it "mentions #starts_with?" do
value = "foobar"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.negated_message(partial).should contain("#starts_with?")
end
end
context "with an Enumerable" do
it "mentions ===" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
matcher.negated_message(partial).should contain("===")
end
it "mentions first" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
matcher.negated_message(partial).should contain("first")
end
end
it "contains the actual label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.negated_message(partial).should contain(label)
end
it "contains the expected label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start, 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"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
matcher.negated_message(partial).should contain(start)
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.
struct StartWithMatcher(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
compare_method(actual, &.eval(actual, expected))
private def match_starts_with?(actual)
actual.starts_with?(expected)
end
# Determines whether the matcher is satisfied with the value given to it.
private def match_first?(actual)
expected === actual
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
# 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 start 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 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}}
def match(partial)
values = ExpectedActual.new(partial, self)
actual = values.actual
if actual.responds_to?(:starts_with?)
StartsWithMatchData.new(match_starts_with?(actual), values)
else
{{block.args.first}} = EnumerableCompareMethod.new
{{block.body}}
first = actual.first
FirstMatchData.new(match_first?(first), values, first)
end
end
# Comparison method for types that define the `starts_with?` method.
private struct StartsWithCompareMethod
# Evaluates the condition to determine whether the matcher is satisfied.
def eval(actual, expected)
actual.starts_with?(expected)
# 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
# String representation for end-user output.
def to_s
"#starts_with?"
# 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
"#{@values.actual_label} starts with #{@values.expected_label} (using #starts_with?)"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@values.actual_label} does not start with #{@values.expected_label} (using #starts_with?)"
end
end
# Comparison method for `Enumerable` types.
private struct EnumerableCompareMethod
# Evaluates the condition to determine whether the matcher is satisfied.
def eval(actual, expected)
expected === actual.first
# Match data specific to this matcher.
# This type is used when the actual value does not respond to `ends_with?`.
private struct FirstMatchData(ExpectedType, ActualType, FirstType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType), @first : FirstType)
super(matched)
end
# String representation for end-user output.
def to_s
"expected === actual.first"
# Information about the match.
def values
{
expected: @values.expected,
actual: @first,
list: @values.actual,
}
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@values.actual_label} starts with #{@values.expected_label} (using expected === actual.first)"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@values.actual_label} does not start with #{@values.expected_label} (using expected === actual.first)"
end
end
end