Update RangeMatcher to use MatchData

There is a compilation error.
It appears that since the last Matcher#match method has been
implemented,
Crystal is finally grouping all of the NamedTuples together.
This commit is contained in:
Michael Miller 2019-03-06 15:01:00 -07:00
parent acf52e1553
commit 018e3232cd
2 changed files with 466 additions and 238 deletions

View file

@ -1,196 +1,305 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Matchers::RangeMatcher do describe Spectator::Matchers::RangeMatcher do
describe "#match?" do describe "#match" do
it "compares using #includes?" do it "compares using #includes?" do
spy = SpySUT.new spy = SpySUT.new
partial = new_partial(5) partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(spy) matcher = Spectator::Matchers::RangeMatcher.new(spy)
matcher.match?(partial).should be_true matcher.match(partial)
spy.includes_call_count.should be > 0 spy.includes_call_count.should be > 0
end end
context "given a Range" do context "returned MatchData" do
context "inclusive" do describe "#matched?" do
it "is true for lower-bound" do context "given a Range" do
lower = 3 context "inclusive" do
upper = 9 it "is true for lower-bound" do
value = lower lower = 3
range = Range.new(lower, upper, exclusive: false) upper = 9
partial = new_partial(value) value = lower
matcher = Spectator::Matchers::RangeMatcher.new(range) range = Range.new(lower, upper, exclusive: false)
matcher.match?(partial).should be_true partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for lower-bound minus 1" do
lower = 3
upper = 9
value = lower - 1
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
it "is true for mid-range" do
lower = 3
upper = 9
value = 5
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is true for upper-bound" do
lower = 3
upper = 9
value = upper
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for upper-bound plus 1" do
lower = 3
upper = 9
value = upper + 1
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "exclusive" do
it "is true for lower-bound" do
lower = 3
upper = 9
value = lower
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for lower-bound minus 1" do
lower = 3
upper = 9
value = lower - 1
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
it "is true for mid-range" do
lower = 3
upper = 9
value = 5
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for upper-bound" do
lower = 3
upper = 9
value = upper
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
it "is false for upper-bound plus 1" do
lower = 3
upper = 9
value = upper + 1
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end end
it "is false for lower-bound minus 1" do context "given an Enumerable" do
lower = 3 it "is true for an existing item" do
upper = 9 array = %i[a b c]
value = lower - 1 value = :b
range = Range.new(lower, upper, exclusive: false) partial = new_partial(value)
partial = new_partial(value) matcher = Spectator::Matchers::RangeMatcher.new(array)
matcher = Spectator::Matchers::RangeMatcher.new(range) match_data = matcher.match(partial)
matcher.match?(partial).should be_false match_data.matched?.should be_true
end
it "is false for a non-existing item" do
array = %i[a b c]
value = :z
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(array)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
describe "#values" do
context "given a Range" do
context "lower" do
it "is #begin from the expected range" do
range = Range.new(3, 9)
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.values[:lower].value.should eq(range.begin)
end
it "is prefixed with >=" do
range = Range.new(3, 9)
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.values[:lower].to_s.should start_with(">=")
end
end
context "upper" do
it "is #end from the expected range" do
range = Range.new(3, 9)
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.values[:upper].value.should eq(range.end)
end
context "when inclusive" do
it "is prefixed with <=" do
range = Range.new(3, 9, exclusive: false)
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.values[:upper].to_s.should start_with("<=")
end
end
context "when exclusive" do
it "is prefixed with <" do
range = Range.new(3, 9, exclusive: false)
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.values[:upper].to_s.should start_with("<")
end
end
end
context "actual" do
it "is the actual value" do
value = 5
range = Range.new(3, 9)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data.values[:actual].should eq(value)
end
end
end end
it "is true for mid-range" do context "given an Enumerable" do
lower = 3 context "set" do
upper = 9 it "is the expected value" do
array = %i[a b c]
value = :z
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(array)
match_data = matcher.match(partial)
match_data.values[:set].should eq(array)
end
end
context "actual" do
it "is the actual value" do
array = %i[a b c]
value = :z
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(array)
match_data = matcher.match(partial)
match_data.values[:actual].should eq(value)
end
end
end
end
describe "#message" do
it "contains the actual label" do
range = 1..10
value = 5 value = 5
range = Range.new(lower, upper, exclusive: false) label = "everything"
partial = new_partial(value) partial = new_partial(value, label)
matcher = Spectator::Matchers::RangeMatcher.new(range) matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
it "is true for upper-bound" do it "contains the expected label" do
lower = 3 range = 1..10
upper = 9
value = upper
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_true
end
it "is false for upper-bound plus 1" do
lower = 3
upper = 9
value = upper + 1
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_false
end
end
context "exclusive" do
it "is true for lower-bound" do
lower = 3
upper = 9
value = lower
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_true
end
it "is false for lower-bound minus 1" do
lower = 3
upper = 9
value = lower - 1
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_false
end
it "is true for mid-range" do
lower = 3
upper = 9
value = 5 value = 5
range = Range.new(lower, upper, exclusive: true) label = "everything"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range) matcher = Spectator::Matchers::RangeMatcher.new(range, label)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
it "is false for upper-bound" do context "when expected label is omitted" do
lower = 3 it "contains stringified form of expected value" do
upper = 9 range = 1..10
value = upper value = 5
range = Range.new(lower, upper, exclusive: true) partial = new_partial(value)
partial = new_partial(value) matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher = Spectator::Matchers::RangeMatcher.new(range) match_data = matcher.match(partial)
matcher.match?(partial).should be_false match_data.message.should contain(range.to_s)
end end
it "is false for upper-bound plus 1" do
lower = 3
upper = 9
value = upper + 1
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_false
end end
end end
end
context "given an Enumerable" do describe "#negated_message" do
it "is true for an existing item" do it "contains the actual label" do
array = %i[a b c] range = 1..10
value = :b value = 5
partial = new_partial(value) label = "everything"
matcher = Spectator::Matchers::RangeMatcher.new(array) partial = new_partial(value, label)
matcher.match?(partial).should be_true matcher = Spectator::Matchers::RangeMatcher.new(range)
end match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
it "is false for a non-existing item" do it "contains the expected label" do
array = %i[a b c] range = 1..10
value = :z value = 5
partial = new_partial(value) label = "everything"
matcher = Spectator::Matchers::RangeMatcher.new(array) partial = new_partial(value)
matcher.match?(partial).should be_false matcher = Spectator::Matchers::RangeMatcher.new(range, label)
end match_data = matcher.match(partial)
end match_data.negated_message.should contain(label)
end end
describe "#message" do context "when expected label is omitted" do
it "contains the actual label" do it "contains stringified form of expected value" do
range = 1..10 range = 1..10
value = 5 value = 5
label = "everything" partial = new_partial(value)
partial = new_partial(value, label) matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher = Spectator::Matchers::RangeMatcher.new(range) match_data = matcher.match(partial)
matcher.message(partial).should contain(label) match_data.negated_message.should contain(range.to_s)
end end
end
it "contains the expected label" do
range = 1..10
value = 5
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label)
matcher.message(partial).should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
range = 1..10
value = 5
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.message(partial).should contain(range.to_s)
end
end
end
describe "#negated_message" do
it "contains the actual label" do
range = 1..10
value = 5
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.negated_message(partial).should contain(label)
end
it "contains the expected label" do
range = 1..10
value = 5
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label)
matcher.negated_message(partial).should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
range = 1..10
value = 5
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.negated_message(partial).should contain(range.to_s)
end end
end end
end end
@ -203,7 +312,8 @@ describe Spectator::Matchers::RangeMatcher do
value = lower value = lower
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for lower-bound minus 1" do it "is false for lower-bound minus 1" do
@ -213,7 +323,8 @@ describe Spectator::Matchers::RangeMatcher do
value = lower - 1 value = lower - 1
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
it "is true for mid-range" do it "is true for mid-range" do
@ -222,7 +333,8 @@ describe Spectator::Matchers::RangeMatcher do
value = center value = center
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is true for upper-bound" do it "is true for upper-bound" do
@ -232,7 +344,8 @@ describe Spectator::Matchers::RangeMatcher do
value = upper value = upper
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for upper-bound plus 1" do it "is false for upper-bound plus 1" do
@ -242,7 +355,8 @@ describe Spectator::Matchers::RangeMatcher do
value = upper + 1 value = upper + 1
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
describe "#message" do describe "#message" do
@ -253,7 +367,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff, label).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff, label).of(center)
matcher.message(partial).should contain(label) match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
it "contains the center" do it "contains the center" do
@ -262,7 +377,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 3 value = 3
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.message(partial).should contain(center.to_s) match_data = matcher.match(partial)
match_data.message.should contain(center.to_s)
end end
it "contains the diff" do it "contains the diff" do
@ -271,7 +387,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 3 value = 3
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.message(partial).should contain(diff.to_s) match_data = matcher.match(partial)
match_data.message.should contain(diff.to_s)
end end
end end
@ -283,7 +400,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff, label).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff, label).of(center)
matcher.negated_message(partial).should contain(label) match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end end
it "contains the center" do it "contains the center" do
@ -292,7 +410,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 3 value = 3
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.negated_message(partial).should contain(center.to_s) match_data = matcher.match(partial)
match_data.negated_message.should contain(center.to_s)
end end
it "contains the diff" do it "contains the diff" do
@ -301,7 +420,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 3 value = 3
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center) matcher = Spectator::Matchers::RangeMatcher.new(diff).of(center)
matcher.negated_message(partial).should contain(diff.to_s) match_data = matcher.match(partial)
match_data.negated_message.should contain(diff.to_s)
end end
end end
end end
@ -315,7 +435,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for lower-bound minus 1" do it "is false for lower-bound minus 1" do
@ -325,7 +446,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
it "is true for mid-range" do it "is true for mid-range" do
@ -335,7 +457,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is true for upper-bound" do it "is true for upper-bound" do
@ -345,7 +468,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for upper-bound plus 1" do it "is false for upper-bound plus 1" do
@ -355,7 +479,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
describe "#message" do describe "#message" do
@ -364,7 +489,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.message(partial).should contain("inclusive") match_data = matcher.match(partial)
match_data.message.should contain("inclusive")
end end
it "does not mention exclusive" do it "does not mention exclusive" do
@ -372,7 +498,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.message(partial).should_not contain("exclusive") match_data = matcher.match(partial)
match_data.message.should_not contain("exclusive")
end end
it "contains the original label" do it "contains the original label" do
@ -381,7 +508,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range, label).inclusive
matcher.message(partial).should contain(label) match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
end end
@ -391,7 +519,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.negated_message(partial).should contain("inclusive") match_data = matcher.match(partial)
match_data.negated_message.should contain("inclusive")
end end
it "does not mention exclusive" do it "does not mention exclusive" do
@ -399,7 +528,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.negated_message(partial).should_not contain("exclusive") match_data = matcher.match(partial)
match_data.negated_message.should_not contain("exclusive")
end end
it "contains the original label" do it "contains the original label" do
@ -408,7 +538,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range, label).inclusive
matcher.negated_message(partial).should contain(label) match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end end
end end
end end
@ -421,7 +552,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for lower-bound minus 1" do it "is false for lower-bound minus 1" do
@ -431,7 +563,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
it "is true for mid-range" do it "is true for mid-range" do
@ -441,7 +574,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is true for upper-bound" do it "is true for upper-bound" do
@ -451,7 +585,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for upper-bound plus 1" do it "is false for upper-bound plus 1" do
@ -461,7 +596,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
describe "#message" do describe "#message" do
@ -470,7 +606,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.message(partial).should contain("inclusive") match_data = matcher.match(partial)
match_data.message.should contain("inclusive")
end end
it "contains the original label" do it "contains the original label" do
@ -479,7 +616,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range, label).inclusive
matcher.message(partial).should contain(label) match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
end end
@ -489,7 +627,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range).inclusive
matcher.negated_message(partial).should contain("inclusive") match_data = matcher.match(partial)
match_data.negated_message.should contain("inclusive")
end end
it "contains the original label" do it "contains the original label" do
@ -498,7 +637,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label).inclusive matcher = Spectator::Matchers::RangeMatcher.new(range, label).inclusive
matcher.negated_message(partial).should contain(label) match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end end
end end
end end
@ -513,7 +653,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for lower-bound minus 1" do it "is false for lower-bound minus 1" do
@ -523,7 +664,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
it "is true for mid-range" do it "is true for mid-range" do
@ -533,7 +675,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for upper-bound" do it "is false for upper-bound" do
@ -543,7 +686,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
it "is false for upper-bound plus 1" do it "is false for upper-bound plus 1" do
@ -553,7 +697,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false) range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
describe "#message" do describe "#message" do
@ -562,7 +707,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.message(partial).should contain("exclusive") match_data = matcher.match(partial)
match_data.message.should contain("exclusive")
end end
it "does not mention inclusive" do it "does not mention inclusive" do
@ -570,7 +716,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.message(partial).should_not contain("inclusive") match_data = matcher.match(partial)
match_data.message.should_not contain("inclusive")
end end
it "contains the original label" do it "contains the original label" do
@ -579,7 +726,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range, label).exclusive
matcher.message(partial).should contain(label) match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
end end
@ -589,7 +737,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.negated_message(partial).should contain("exclusive") match_data = matcher.match(partial)
match_data.negated_message.should contain("exclusive")
end end
it "does not mention inclusive" do it "does not mention inclusive" do
@ -597,7 +746,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.negated_message(partial).should_not contain("inclusive") match_data = matcher.match(partial)
match_data.negated_message.should_not contain("inclusive")
end end
it "contains the original label" do it "contains the original label" do
@ -606,7 +756,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range, label).exclusive
matcher.negated_message(partial).should contain(label) match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end end
end end
end end
@ -619,7 +770,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for lower-bound minus 1" do it "is false for lower-bound minus 1" do
@ -629,7 +781,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
it "is true for mid-range" do it "is true for mid-range" do
@ -639,7 +792,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_true match_data = matcher.match(partial)
match_data.matched?.should be_true
end end
it "is false for upper-bound" do it "is false for upper-bound" do
@ -649,7 +803,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
it "is false for upper-bound plus 1" do it "is false for upper-bound plus 1" do
@ -659,7 +814,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true) range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.match?(partial).should be_false match_data = matcher.match(partial)
match_data.matched?.should be_false
end end
describe "#message" do describe "#message" do
@ -668,7 +824,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.message(partial).should contain("exclusive") match_data = matcher.match(partial)
match_data.message.should contain("exclusive")
end end
it "contains the original label" do it "contains the original label" do
@ -677,7 +834,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range, label).exclusive
matcher.message(partial).should contain(label) match_data = matcher.match(partial)
match_data.message.should contain(label)
end end
end end
@ -687,7 +845,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5 value = 5
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range).exclusive
matcher.negated_message(partial).should contain("exclusive") match_data = matcher.match(partial)
match_data.negated_message.should contain("exclusive")
end end
it "contains the original label" do it "contains the original label" do
@ -696,7 +855,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar" label = "foobar"
partial = new_partial(value) partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label).exclusive matcher = Spectator::Matchers::RangeMatcher.new(range, label).exclusive
matcher.negated_message(partial).should contain(label) match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end end
end end
end end

View file

@ -7,27 +7,21 @@ module Spectator::Matchers
# but any type that implements the `includes?` method is supported. # but any type that implements the `includes?` method is supported.
struct RangeMatcher(ExpectedType) < ValueMatcher(ExpectedType) struct RangeMatcher(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) expected.includes?(actual)
@expected.includes?(partial.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") actual = partial.actual
end matched = match?(actual)
expected_value = @expected
# Describes the condition that satisfies the matcher. if expected_value.is_a?(Range)
# This is informational and displayed to the end-user. RangeMatchData.new(matched, ExpectedActual.new(expected_value, label, actual, partial.label))
def message(partial) else
"Expected #{partial.label} to be in #{label}" SetMatchData.new(matched, ExpectedActual.new(partial, self))
end end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message(partial)
"Expected #{partial.label} to not be in #{label}"
end end
# Creates a new range matcher with bounds based off of *center*. # Creates a new range matcher with bounds based off of *center*.
@ -37,10 +31,10 @@ module Spectator::Matchers
# ``` # ```
# RangeMatcher.new(diff).of(center) # RangeMatcher.new(diff).of(center)
# ``` # ```
# This implies that the `match?` method would not work on the original matcher. # This implies that the `#match` method would not work on the original matcher.
# #
# The new range will be centered at *center* # The new range will be centered at *center*
# and have upper and lower bounds equal to *center* plus and minux diff. # and have upper and lower bounds equal to *center* plus and minus diff.
# The range will be inclusive. # The range will be inclusive.
def of(center) def of(center)
diff = @expected diff = @expected
@ -53,13 +47,87 @@ module Spectator::Matchers
# Returns a new matcher, with the same bounds, but uses an inclusive range. # Returns a new matcher, with the same bounds, but uses an inclusive range.
def inclusive def inclusive
range = Range.new(@expected.begin, @expected.end, exclusive: false) range = Range.new(@expected.begin, @expected.end, exclusive: false)
RangeMatcher.new(range, label + " (inclusive)") RangeMatcher.new(range, label)
end end
# Returns a new matcher, with the same bounds, but uses an exclusive range. # Returns a new matcher, with the same bounds, but uses an exclusive range.
def exclusive def exclusive
range = Range.new(@expected.begin, @expected.end, exclusive: true) range = Range.new(@expected.begin, @expected.end, exclusive: true)
RangeMatcher.new(range, label + " (exclusive)") RangeMatcher.new(range, label)
end
# Match data specific to this matcher.
# This is used when the expected type is a `Range`.
private struct RangeMatchData(B, E, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(Range(B, E), ActualType))
super(matched)
end
# Information about the match.
def values
{
lower: PrefixedValue.new(">=", range.begin),
upper: PrefixedValue.new(exclusive? ? "<" : "<=", range.end),
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} is in #{@values.expected_label} (#{exclusivity})"
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} is not in #{@values.expected_label} (#{exclusivity})"
end
# Gets the expected range.
private def range
@values.expected
end
# Indicates whether the range is inclusive or exclusive.
private def exclusive?
range.exclusive?
end
# Produces a string "inclusive" or "exclusive" based on the range.
private def exclusivity
exclusive? ? "exclusive" : "inclusive"
end
end
# Match data specific to this matcher.
# This is used when the expected type is not a `Range`.
private struct SetMatchData(ExpectedType, ActualType) < MatchData
# Creates the match data.
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
super(matched)
end
# Information about the match.
def values
{
set: @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} is in #{@values.expected_label}"
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} is not in #{@values.expected_label}"
end
end end
end end
end end