Skip to content

Commit

Permalink
Make BeBetween composable
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jan 4, 2014
1 parent cf2d0a5 commit 308ccce
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/rspec/matchers.rb
Expand Up @@ -330,6 +330,7 @@ def be_a_kind_of(expected)
def be_between(min, max)
BuiltIn::BeBetween.new(min, max)
end
alias_matcher :a_value_between, :be_between

# Passes if actual == expected +/- delta
#
Expand Down
22 changes: 21 additions & 1 deletion lib/rspec/matchers/built_in/be_between.rb
Expand Up @@ -2,18 +2,38 @@ 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
@actual.between?(@min, @max)
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
Expand Down
5 changes: 2 additions & 3 deletions lib/rspec/matchers/built_in/be_within.rb
Expand Up @@ -33,7 +33,7 @@ def failure_message
end

def failure_message_when_negated
"expected #{@actual} not to #{description}"
"expected #{@actual.inspect} not to #{description}"
end

def description
Expand All @@ -51,8 +51,7 @@ def needs_expected
end

def not_numeric_clause
return "" if numeric?
", but it could not be treated as a numeric value"
", but it could not be treated as a numeric value" unless numeric?
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/matchers/aliases_spec.rb
Expand Up @@ -80,6 +80,14 @@ module RSpec
).with_description("a kind of Integer")
end

specify do
expect(
a_value_between(1, 10)
).to be_aliased_to(
be_between(1, 10)
).with_description("a value between 1 and 10 (inclusive)")
end

specify do
expect(
a_value_within(0.1).of(3)
Expand Down
17 changes: 17 additions & 0 deletions spec/rspec/matchers/built_in/be_between_spec.rb
Expand Up @@ -55,3 +55,20 @@ def inspect
}.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

0 comments on commit 308ccce

Please sign in to comment.