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,15 +1,17 @@
require "../spec_helper"
describe Spectator::Matchers::RangeMatcher do
describe "#match?" do
describe "#match" do
it "compares using #includes?" do
spy = SpySUT.new
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(spy)
matcher.match?(partial).should be_true
matcher.match(partial)
spy.includes_call_count.should be > 0
end
context "returned MatchData" do
describe "#matched?" do
context "given a Range" do
context "inclusive" do
it "is true for lower-bound" do
@ -19,7 +21,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for lower-bound minus 1" do
@ -29,7 +32,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
it "is true for mid-range" do
@ -39,7 +43,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is true for upper-bound" do
@ -49,7 +54,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for upper-bound plus 1" do
@ -59,7 +65,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
@ -71,7 +78,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for lower-bound minus 1" do
@ -81,7 +89,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
it "is true for mid-range" do
@ -91,7 +100,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for upper-bound" do
@ -101,7 +111,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
it "is false for upper-bound plus 1" do
@ -111,7 +122,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.match?(partial).should be_false
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
@ -122,7 +134,8 @@ describe Spectator::Matchers::RangeMatcher do
value = :b
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(array)
matcher.match?(partial).should be_true
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
it "is false for a non-existing item" do
@ -130,7 +143,95 @@ describe Spectator::Matchers::RangeMatcher do
value = :z
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(array)
matcher.match?(partial).should be_false
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
context "given an Enumerable" do
context "set" do
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
@ -142,7 +243,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.message(partial).should contain(label)
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
it "contains the expected label" do
@ -151,7 +253,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label)
matcher.message(partial).should contain(label)
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
context "when expected label is omitted" do
@ -160,7 +263,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.message(partial).should contain(range.to_s)
match_data = matcher.match(partial)
match_data.message.should contain(range.to_s)
end
end
end
@ -172,7 +276,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.negated_message(partial).should contain(label)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
it "contains the expected label" do
@ -181,7 +286,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range, label)
matcher.negated_message(partial).should contain(label)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
context "when expected label is omitted" do
@ -190,7 +296,10 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
matcher.negated_message(partial).should contain(range.to_s)
match_data = matcher.match(partial)
match_data.negated_message.should contain(range.to_s)
end
end
end
end
end
@ -203,7 +312,8 @@ describe Spectator::Matchers::RangeMatcher do
value = lower
partial = new_partial(value)
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
it "is false for lower-bound minus 1" do
@ -213,7 +323,8 @@ describe Spectator::Matchers::RangeMatcher do
value = lower - 1
partial = new_partial(value)
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
it "is true for mid-range" do
@ -222,7 +333,8 @@ describe Spectator::Matchers::RangeMatcher do
value = center
partial = new_partial(value)
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
it "is true for upper-bound" do
@ -232,7 +344,8 @@ describe Spectator::Matchers::RangeMatcher do
value = upper
partial = new_partial(value)
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
it "is false for upper-bound plus 1" do
@ -242,7 +355,8 @@ describe Spectator::Matchers::RangeMatcher do
value = upper + 1
partial = new_partial(value)
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
describe "#message" do
@ -253,7 +367,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
it "contains the center" do
@ -262,7 +377,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 3
partial = new_partial(value)
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
it "contains the diff" do
@ -271,7 +387,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 3
partial = new_partial(value)
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
@ -283,7 +400,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
it "contains the center" do
@ -292,7 +410,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 3
partial = new_partial(value)
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
it "contains the diff" do
@ -301,7 +420,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 3
partial = new_partial(value)
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
@ -315,7 +435,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
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
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)
partial = new_partial(value)
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
it "is true for mid-range" do
@ -335,7 +457,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
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
it "is true for upper-bound" do
@ -345,7 +468,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
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
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)
partial = new_partial(value)
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
describe "#message" do
@ -364,7 +489,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "does not mention exclusive" do
@ -372,7 +498,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "contains the original label" do
@ -381,7 +508,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
@ -391,7 +519,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "does not mention exclusive" do
@ -399,7 +528,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "contains the original label" do
@ -408,7 +538,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
@ -421,7 +552,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
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
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)
partial = new_partial(value)
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
it "is true for mid-range" do
@ -441,7 +574,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
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
it "is true for upper-bound" do
@ -451,7 +585,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
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
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)
partial = new_partial(value)
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
describe "#message" do
@ -470,7 +606,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "contains the original label" do
@ -479,7 +616,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
@ -489,7 +627,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "contains the original label" do
@ -498,7 +637,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
@ -513,7 +653,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
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
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)
partial = new_partial(value)
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
it "is true for mid-range" do
@ -533,7 +675,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
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
it "is false for upper-bound" do
@ -543,7 +686,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: false)
partial = new_partial(value)
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
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)
partial = new_partial(value)
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
describe "#message" do
@ -562,7 +707,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "does not mention inclusive" do
@ -570,7 +716,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "contains the original label" do
@ -579,7 +726,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
@ -589,7 +737,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "does not mention inclusive" do
@ -597,7 +746,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "contains the original label" do
@ -606,7 +756,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
@ -619,7 +770,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
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
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)
partial = new_partial(value)
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
it "is true for mid-range" do
@ -639,7 +792,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
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
it "is false for upper-bound" do
@ -649,7 +803,8 @@ describe Spectator::Matchers::RangeMatcher do
range = Range.new(lower, upper, exclusive: true)
partial = new_partial(value)
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
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)
partial = new_partial(value)
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
describe "#message" do
@ -668,7 +824,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "contains the original label" do
@ -677,7 +834,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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
@ -687,7 +845,8 @@ describe Spectator::Matchers::RangeMatcher do
value = 5
partial = new_partial(value)
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
it "contains the original label" do
@ -696,7 +855,8 @@ describe Spectator::Matchers::RangeMatcher do
label = "foobar"
partial = new_partial(value)
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

View File

@ -7,27 +7,21 @@ module Spectator::Matchers
# but any type that implements the `includes?` method is supported.
struct RangeMatcher(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)
@expected.includes?(partial.actual)
private def match?(actual)
expected.includes?(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")
def match(partial)
actual = partial.actual
matched = match?(actual)
expected_value = @expected
if expected_value.is_a?(Range)
RangeMatchData.new(matched, ExpectedActual.new(expected_value, label, actual, partial.label))
else
SetMatchData.new(matched, ExpectedActual.new(partial, self))
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message(partial)
"Expected #{partial.label} to be in #{label}"
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
# Creates a new range matcher with bounds based off of *center*.
@ -37,10 +31,10 @@ module Spectator::Matchers
# ```
# 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*
# 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.
def of(center)
diff = @expected
@ -53,13 +47,87 @@ module Spectator::Matchers
# Returns a new matcher, with the same bounds, but uses an inclusive range.
def inclusive
range = Range.new(@expected.begin, @expected.end, exclusive: false)
RangeMatcher.new(range, label + " (inclusive)")
RangeMatcher.new(range, label)
end
# Returns a new matcher, with the same bounds, but uses an exclusive range.
def exclusive
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