Update LessThanEqualMatcher to use MatchData

This commit is contained in:
Michael Miller 2019-03-05 16:37:51 -07:00
parent 36d3bd0a70
commit f846408848
2 changed files with 196 additions and 103 deletions

View file

@ -1,22 +1,25 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::LessThanEqualMatcher do describe Spectator::Matchers::LessThanEqualMatcher do
describe "#match?" do describe "#match" do
it "compares using #<=" do it "compares using #<=" do
spy = SpySUT.new spy = SpySUT.new
partial = new_partial(spy) partial = new_partial(spy)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(42) matcher = Spectator::Matchers::LessThanEqualMatcher.new(42)
matcher.match?(partial).should be_true matcher.match(partial)
spy.le_call_count.should be > 0 spy.le_call_count.should be > 0
end end
context "returned MatchData" do
describe "#matched?" do
context "with a larger value" do context "with a larger value" do
it "is true" do it "is true" do
actual = 42 actual = 42
expected = 777 expected = 777
partial = new_partial(actual) partial = new_partial(actual)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected) matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
end end
@ -26,7 +29,8 @@ describe Spectator::Matchers::LessThanEqualMatcher do
expected = 42 expected = 42
partial = new_partial(actual) partial = new_partial(actual)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected) matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
end end
@ -35,7 +39,65 @@ describe Spectator::Matchers::LessThanEqualMatcher do
value = 42 value = 42
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
end
describe "#values" do
context "expected" do
it "is the expected value" do
actual = 42
expected = 777
partial = new_partial(actual)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected)
match_data = matcher.match(partial)
match_data.values[:expected].value.should eq(expected)
end
it "is prefixed with <=" do
actual = 42
expected = 777
partial = new_partial(actual)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected)
match_data = matcher.match(partial)
match_data.values[:expected].to_s.should start_with("<=")
end
end
context "actual" do
it "is the actual value" do
actual = 42
expected = 777
partial = new_partial(actual)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected)
match_data = matcher.match(partial)
match_data.values[:actual].value.should eq(actual)
end
context "when #matched? is true" do
it "is prefixed with <=" do
actual = 42
expected = 777
partial = new_partial(actual)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected)
match_data = matcher.match(partial)
match_data.matched?.should be_true # Sanity check.
match_data.values[:actual].to_s.should start_with("<=")
end
end
context "when #matched? is false" do
it "is prefixed with >" do
actual = 777
expected = 42
partial = new_partial(actual)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(expected)
match_data = matcher.match(partial)
match_data.matched?.should be_false # Sanity check.
match_data.values[:actual].to_s.should start_with(">")
end
end end
end end
end end
@ -45,7 +107,8 @@ describe Spectator::Matchers::LessThanEqualMatcher do
value = 42 value = 42
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value)
matcher.message(partial).should contain("<=") match_data = matcher.match(partial)
match_data.message.should contain("<=")
end end
it "contains the actual label" do it "contains the actual label" do
@ -53,7 +116,8 @@ describe Spectator::Matchers::LessThanEqualMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value)
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
@ -61,7 +125,8 @@ describe Spectator::Matchers::LessThanEqualMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value, label) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value, 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
@ -70,7 +135,8 @@ describe Spectator::Matchers::LessThanEqualMatcher do
value2 = 777 value2 = 777
partial = new_partial(value1) partial = new_partial(value1)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value2) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value2)
matcher.message(partial).should contain(value2.to_s) match_data = matcher.match(partial)
match_data.message.should contain(value2.to_s)
end end
end end
end end
@ -80,7 +146,8 @@ describe Spectator::Matchers::LessThanEqualMatcher do
value = 42 value = 42
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value)
matcher.negated_message(partial).should contain("<=") match_data = matcher.match(partial)
match_data.negated_message.should contain("<=")
end end
it "contains the actual label" do it "contains the actual label" do
@ -88,7 +155,8 @@ describe Spectator::Matchers::LessThanEqualMatcher do
label = "everything" label = "everything"
partial = new_partial(value, label) partial = new_partial(value, label)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value)
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
@ -96,7 +164,8 @@ describe Spectator::Matchers::LessThanEqualMatcher do
label = "everything" label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value, label) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value, 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
@ -105,7 +174,10 @@ describe Spectator::Matchers::LessThanEqualMatcher do
value2 = 777 value2 = 777
partial = new_partial(value1) partial = new_partial(value1)
matcher = Spectator::Matchers::LessThanEqualMatcher.new(value2) matcher = Spectator::Matchers::LessThanEqualMatcher.new(value2)
matcher.negated_message(partial).should contain(value2.to_s) match_data = matcher.match(partial)
match_data.negated_message.should contain(value2.to_s)
end
end
end end
end end
end end

View file

@ -5,27 +5,48 @@ module Spectator::Matchers
# The values are compared with the <= operator. # The values are compared with the <= operator.
struct LessThanEqualMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct LessThanEqualMatcher(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 <= expected
partial.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)
MatchData.new(match?(values.actual), values)
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: PrefixedValue.new("<=", @values.expected),
actual: PrefixedValue.new(actual_operator, @values.actual),
}
end
# Textual operator for the actual value.
private def actual_operator
matched? ? "<=" : ">"
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
"Expected #{partial.label} to be less than or equal to #{label} (using <=)" "#{@values.actual_label} is less than or equal to #{@values.expected_label} (using <=)"
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
"Expected #{partial.label} to not be less than or equal to #{label} (using <=)" "#{@values.actual_label} is greater than #{@values.expected_label} (using <=)"
end
end end
end end
end end