diff --git a/spec/rspec/expectations/be_between_matcher_spec.cr b/spec/rspec/expectations/be_between_matcher_spec.cr new file mode 100644 index 0000000..913db0b --- /dev/null +++ b/spec/rspec/expectations/be_between_matcher_spec.cr @@ -0,0 +1,17 @@ +require "../../spec_helper" + +Spectator.describe "`be_between` matcher" do + context "basic usage" do + describe 7 do + it { is_expected.to be_between(1, 10) } + it { is_expected.to be_between(0.2, 27.1) } + it { is_expected.not_to be_between(1.5, 4) } + it { is_expected.not_to be_between(8, 9) } + + # boundaries check + it { is_expected.to be_between(0, 7) } + it { is_expected.to be_between(7, 10) } + it { is_expected.not_to (be_between(0, 7).exclusive) } + end + end +end diff --git a/src/spectator/dsl/matchers.cr b/src/spectator/dsl/matchers.cr index 2108a88..b533c5a 100644 --- a/src/spectator/dsl/matchers.cr +++ b/src/spectator/dsl/matchers.cr @@ -342,10 +342,10 @@ module Spectator # expect(100).to be_between(97, 101).exclusive # 97, 98, 99, or 100 (not 101) # ``` macro be_between(min, max) - %range = Range.new({{min}}, {{max}})) + %range = Range.new({{min}}, {{max}}) %label = [{{min.stringify}}, {{max.stringify}}].join(" to ") %test_value = ::Spectator::TestValue.new(%range, %label) - :Spectator::Matchers::RangeMatcher.new(%test_value) + ::Spectator::Matchers::RangeMatcher.new(%test_value) end # Indicates that some value should be within a delta of an expected value. diff --git a/src/spectator/matchers/range_matcher.cr b/src/spectator/matchers/range_matcher.cr index 32d491f..fe54b7e 100644 --- a/src/spectator/matchers/range_matcher.cr +++ b/src/spectator/matchers/range_matcher.cr @@ -13,6 +13,7 @@ module Spectator::Matchers # Returns a new matcher, with the same bounds, but uses an inclusive range. def inclusive + label = expected.label new_range = Range.new(range.begin, range.end, exclusive: false) expected = TestValue.new(new_range, label) RangeMatcher.new(expected) @@ -20,6 +21,7 @@ module Spectator::Matchers # Returns a new matcher, with the same bounds, but uses an exclusive range. def exclusive + label = expected.label new_range = Range.new(range.begin, range.end, exclusive: true) expected = TestValue.new(new_range, label) RangeMatcher.new(expected)