diff --git a/spec/matchers/range_matcher_spec.cr b/spec/matchers/range_matcher_spec.cr index fe223eb..ac8e1c9 100644 --- a/spec/matchers/range_matcher_spec.cr +++ b/spec/matchers/range_matcher_spec.cr @@ -1,196 +1,305 @@ 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 "given a Range" do - context "inclusive" do - it "is true for lower-bound" do - lower = 3 - upper = 9 - value = lower - range = Range.new(lower, upper, exclusive: false) - partial = new_partial(value) - matcher = Spectator::Matchers::RangeMatcher.new(range) - matcher.match?(partial).should be_true + context "returned MatchData" do + describe "#matched?" do + context "given a Range" do + context "inclusive" do + it "is true for lower-bound" do + lower = 3 + upper = 9 + value = lower + 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 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 - 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) - matcher.match?(partial).should be_false + context "given an Enumerable" do + it "is true for an existing item" do + array = %i[a b c] + value = :b + partial = new_partial(value) + matcher = Spectator::Matchers::RangeMatcher.new(array) + match_data = matcher.match(partial) + 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 - it "is true for mid-range" do - lower = 3 - upper = 9 + 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 + + describe "#message" do + it "contains the actual label" do + range = 1..10 value = 5 - range = Range.new(lower, upper, exclusive: false) - partial = new_partial(value) + label = "everything" + partial = new_partial(value, label) matcher = Spectator::Matchers::RangeMatcher.new(range) - matcher.match?(partial).should be_true + match_data = matcher.match(partial) + match_data.message.should contain(label) 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) - 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 + it "contains the expected label" do + range = 1..10 value = 5 - range = Range.new(lower, upper, exclusive: true) + label = "everything" partial = new_partial(value) - matcher = Spectator::Matchers::RangeMatcher.new(range) - matcher.match?(partial).should be_true + matcher = Spectator::Matchers::RangeMatcher.new(range, label) + match_data = matcher.match(partial) + match_data.message.should contain(label) 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) - matcher.match?(partial).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) - matcher.match?(partial).should be_false + 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) + match_data = matcher.match(partial) + match_data.message.should contain(range.to_s) + end end end - end - context "given an Enumerable" do - it "is true for an existing item" do - array = %i[a b c] - value = :b - partial = new_partial(value) - matcher = Spectator::Matchers::RangeMatcher.new(array) - matcher.match?(partial).should be_true - 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) + match_data = matcher.match(partial) + match_data.negated_message.should contain(label) + 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) - matcher.match?(partial).should be_false - 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) + match_data = matcher.match(partial) + match_data.negated_message.should contain(label) + end - describe "#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.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.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) + 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) + 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 diff --git a/src/spectator/matchers/range_matcher.cr b/src/spectator/matchers/range_matcher.cr index b328ae7..9d31516 100644 --- a/src/spectator/matchers/range_matcher.cr +++ b/src/spectator/matchers/range_matcher.cr @@ -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") - 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}" + 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 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