Update TypeMatcher to use MatchData

This commit is contained in:
Michael Miller 2019-03-06 11:19:57 -07:00
parent 81e20b13ec
commit 6e62ccdfc5
2 changed files with 134 additions and 83 deletions

View file

@ -1,82 +1,117 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::TypeMatcher do describe Spectator::Matchers::TypeMatcher do
describe "#match?" do describe "#match" do
context "with the same type" do context "returned MatchData" do
it "is true" do describe "#matched?" do
value = "foobar" context "with the same type" do
partial = new_partial(value) it "is true" do
matcher = Spectator::Matchers::TypeMatcher(String).new value = "foobar"
matcher.match?(partial).should be_true partial = new_partial(value)
matcher = Spectator::Matchers::TypeMatcher(String).new
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "with a different type" do
it "is false" do
value = "foobar"
partial = new_partial(value)
matcher = Spectator::Matchers::TypeMatcher(Int32).new
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "with a parent type" do
it "is true" do
value = IO::Memory.new
partial = new_partial(value)
matcher = Spectator::Matchers::TypeMatcher(IO).new
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "with a child type" do
it "is false" do
value = Exception.new("foobar")
partial = new_partial(value)
matcher = Spectator::Matchers::TypeMatcher(ArgumentError).new
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "with a mix-in" do
it "is true" do
value = %i[a b c]
partial = new_partial(value)
matcher = Spectator::Matchers::TypeMatcher(Enumerable(Symbol)).new
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end end
end
context "with a different type" do describe "#values" do
it "is false" do context "expected" do
value = "foobar" it "is the expected type name" do
partial = new_partial(value) value = %i[a b c]
matcher = Spectator::Matchers::TypeMatcher(Int32).new partial = new_partial(value)
matcher.match?(partial).should be_false matcher = Spectator::Matchers::TypeMatcher(String).new
match_data = matcher.match(partial)
match_data.values[:expected].should eq(String)
end
end
context "actual" do
it "is the actual type name" do
value = %i[a b c]
partial = new_partial(value)
matcher = Spectator::Matchers::TypeMatcher(String).new
match_data = matcher.match(partial)
match_data.values[:actual].should eq(typeof(value))
end
end
end end
end
context "with a parent type" do describe "#message" do
it "is true" do it "contains the actual label" do
value = IO::Memory.new value = 42
partial = new_partial(value) label = "everything"
matcher = Spectator::Matchers::TypeMatcher(IO).new partial = new_partial(value, label)
matcher.match?(partial).should be_true matcher = Spectator::Matchers::TypeMatcher(String).new
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
it "contains the expected type" do
partial = new_partial(42)
matcher = Spectator::Matchers::TypeMatcher(String).new
match_data = matcher.match(partial)
match_data.message.should contain("String")
end
end end
end
context "with a child type" do describe "#negated_message" do
it "is false" do it "contains the actual label" do
value = Exception.new("foobar") value = 42
partial = new_partial(value) label = "everything"
matcher = Spectator::Matchers::TypeMatcher(ArgumentError).new partial = new_partial(value, label)
matcher.match?(partial).should be_false matcher = Spectator::Matchers::TypeMatcher(String).new
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
it "contains the expected type" do
partial = new_partial(42)
matcher = Spectator::Matchers::TypeMatcher(String).new
match_data = matcher.match(partial)
match_data.negated_message.should contain("String")
end
end end
end end
context "with a mix-in" do
it "is true" do
value = %i[a b c]
partial = new_partial(value)
matcher = Spectator::Matchers::TypeMatcher(Enumerable(Symbol)).new
matcher.match?(partial).should be_true
end
end
end
describe "#message" do
it "contains the actual label" do
value = 42
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::TypeMatcher(String).new
matcher.message(partial).should contain(label)
end
it "contains the expected type" do
partial = new_partial(42)
matcher = Spectator::Matchers::TypeMatcher(String).new
matcher.message(partial).should contain("String")
end
end
describe "#negated_message" do
it "contains the actual label" do
value = 42
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::TypeMatcher(String).new
matcher.negated_message(partial).should contain(label)
end
it "contains the expected type" do
partial = new_partial(42)
matcher = Spectator::Matchers::TypeMatcher(String).new
matcher.negated_message(partial).should contain("String")
end
end end
end end

View file

@ -11,27 +11,43 @@ module Spectator::Matchers
end end
# 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.is_a?(Expected)
partial.actual.is_a?(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") actual = partial.actual
MatchData(Expected, typeof(actual)).new(match?(actual), partial.label)
end end
# Describes the condition that satisfies the matcher. # Match data specific to this matcher.
# This is informational and displayed to the end-user. private struct MatchData(ExpectedType, ActualType) < MatchData
def message(partial) # Creates the match data.
"Expected #{partial.label} to be a #{label}" def initialize(matched, @actual_label : String)
end super(matched)
end
# Describes the condition that won't satsify the matcher. # Information about the match.
# This is informational and displayed to the end-user. def values
def negated_message(partial) {
"Expected #{partial.label} to not be a #{label}" expected: ExpectedType,
actual: ActualType,
}
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message
"#{@actual_label} is a #{ExpectedType}"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message
"#{@actual_label} is not a #{ExpectedType}"
end
end end
end end
end end