Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #405 from sferik/be_between
Add RSpec::Matchers::BuiltIn::BeBetween
- Loading branch information
Showing
8 changed files
with
172 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module RSpec | ||
module Matchers | ||
module BuiltIn | ||
class BeBetween < BaseMatcher | ||
include Composable | ||
|
||
def initialize(min, max) | ||
@min, @max = min, max | ||
end | ||
|
||
def matches?(actual) | ||
@actual = actual | ||
comparable? and @actual.between?(@min, @max) | ||
end | ||
|
||
def failure_message | ||
"expected #{@actual.inspect} to #{description}#{not_comparable_clause}" | ||
end | ||
|
||
def failure_message_when_negated | ||
"expected #{@actual.inspect} not to #{description}" | ||
end | ||
|
||
def description | ||
"be between #{@min.inspect} and #{@max.inspect} (inclusive)" | ||
end | ||
|
||
private | ||
|
||
def comparable? | ||
@actual.respond_to?(:between?) | ||
end | ||
|
||
def not_comparable_clause | ||
", but #{@actual.inspect} does not respond to `between?`" unless comparable? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'spec_helper' | ||
|
||
describe "expect(...).to be_between(min, max)" do | ||
it_behaves_like "an RSpec matcher", :valid_value => (10), :invalid_value => (11) do | ||
let(:matcher) { be_between(1, 10) } | ||
end | ||
|
||
it "passes if target is between min and max" do | ||
expect(10).to be_between(1, 10) | ||
end | ||
|
||
it "fails if target is not between min and max" do | ||
expect { | ||
# It does not go to 11 | ||
expect(11).to be_between(1, 10) | ||
}.to fail_with("expected 11 to be between 1 and 10 (inclusive)") | ||
end | ||
|
||
it 'works with strings' do | ||
expect("baz").to be_between("bar", "foo") | ||
expect { | ||
expect("foo").to be_between("bar", "baz") | ||
}.to fail_with("expected \"foo\" to be between \"bar\" and \"baz\" (inclusive)") | ||
end | ||
|
||
it 'works with other Comparable objects' do | ||
class SizeMatters | ||
include Comparable | ||
attr :str | ||
def <=>(other) | ||
str.size <=> other.str.size | ||
end | ||
def initialize(str) | ||
@str = str | ||
end | ||
def inspect | ||
@str | ||
end | ||
end | ||
expect(SizeMatters.new("--")).to be_between(SizeMatters.new("-"), SizeMatters.new("---")) | ||
expect { | ||
expect(SizeMatters.new("---")).to be_between(SizeMatters.new("-"), SizeMatters.new("--")) | ||
}.to fail_with("expected --- to be between - and -- (inclusive)") | ||
end | ||
end | ||
|
||
describe "expect(...).not_to be_between(min, max)" do | ||
it "passes if target is not between min and max" do | ||
expect(11).not_to be_between(1, 10) | ||
end | ||
|
||
it "fails if target is between min and max" do | ||
expect { | ||
expect(10).not_to be_between(1, 10) | ||
}.to fail_with("expected 10 not to be between 1 and 10 (inclusive)") | ||
end | ||
end | ||
|
||
describe "composing with other matchers" do | ||
it "passes when the matchers both match" do | ||
expect([0.1, 2]).to include(a_value_between(2, 4), an_instance_of(Float)) | ||
end | ||
|
||
it "provides a description" do | ||
description = include(a_value_between(2, 4), an_instance_of(Float)).description | ||
expect(description).to eq("include (a value between 2 and 4 (inclusive)) and (an instance of Float)") | ||
end | ||
|
||
it "fails with a clear error message when the matchers do not match" do | ||
expect { | ||
expect([0.1, 1]).to include(a_value_between(2, 4), an_instance_of(Float)) | ||
}.to fail_with("expected [0.1, 1] to include (a value between 2 and 4 (inclusive)) and (an instance of Float)") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters