-
-
Notifications
You must be signed in to change notification settings - Fork 397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RSpec::Matchers::BuiltIn::BeBetween #405
Changes from all commits
1bf592a
596dc0a
0ef2bd5
cf2d0a5
308ccce
7bbe151
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,15 @@ | |
module RSpec | ||
module Matchers | ||
module BuiltIn | ||
autoload :BeAKindOf, 'rspec/matchers/built_in/be_kind_of' | ||
autoload :BeAnInstanceOf, 'rspec/matchers/built_in/be_instance_of' | ||
autoload :BeBetween, 'rspec/matchers/built_in/be_between' | ||
autoload :Be, 'rspec/matchers/built_in/be' | ||
autoload :BeTruthy, 'rspec/matchers/built_in/be' | ||
autoload :BeComparedTo, 'rspec/matchers/built_in/be' | ||
autoload :BeFalsey, 'rspec/matchers/built_in/be' | ||
autoload :BeNil, 'rspec/matchers/built_in/be' | ||
autoload :BeComparedTo, 'rspec/matchers/built_in/be' | ||
autoload :BePredicate, 'rspec/matchers/built_in/be' | ||
autoload :BeAKindOf, 'rspec/matchers/built_in/be_kind_of' | ||
autoload :BeTruthy, 'rspec/matchers/built_in/be' | ||
autoload :BeWithin, 'rspec/matchers/built_in/be_within' | ||
autoload :Change, 'rspec/matchers/built_in/change' | ||
autoload :Compound, 'rspec/matchers/built_in/compound' | ||
|
@@ -33,11 +34,9 @@ module BuiltIn | |
autoload :StartWith, 'rspec/matchers/built_in/start_and_end_with' | ||
autoload :ThrowSymbol, 'rspec/matchers/built_in/throw_symbol' | ||
autoload :YieldControl, 'rspec/matchers/built_in/yield' | ||
autoload :YieldSuccessiveArgs, 'rspec/matchers/built_in/yield' | ||
autoload :YieldWithArgs, 'rspec/matchers/built_in/yield' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for organizing these -- I thought they were already in alphabetical order but I guess not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the alphabetization into a separate commit, to keep things clean. |
||
autoload :YieldWithNoArgs, 'rspec/matchers/built_in/yield' | ||
autoload :YieldSuccessiveArgs, 'rspec/matchers/built_in/yield' | ||
end | ||
end | ||
end | ||
|
||
|
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This made me laugh :). |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,9 +45,20 @@ | |
expect(RSpec::Matchers.generated_description).to eq "should be > 3" | ||
end | ||
|
||
it "expect(...).to be between min and max" do | ||
expect(10).to be_between(0, 10) | ||
expect(RSpec::Matchers.generated_description).to eq "should be between 0 and 10 (inclusive)" | ||
end | ||
|
||
it "expect(...).to be predicate arg1, arg2 and arg3" do | ||
expect(5.0).to be_between(0,10) | ||
expect(RSpec::Matchers.generated_description).to eq "should be between 0 and 10" | ||
class Parent; end | ||
class Child < Parent | ||
def child_of?(*parents) | ||
parents.all? { |parent| self.is_a?(parent) } | ||
end | ||
end | ||
expect(Child.new).to be_a_child_of(Parent, Object) | ||
expect(RSpec::Matchers.generated_description).to eq "should be a child of Parent and Object" | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to keep an example for the generated description of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totes. |
||
|
||
it "expect(...).to equal" do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a matcher alias for this:
...which allows the matcher to read correctly in composed matcher expressions:
It also makes the failure message read better. You can add a spec to
spec/rspec/matchers/aliases_spec.rb
.