Sha256: 1a24870a62d3fd1e7e72716309e92af7bfeae5ad2fb8bc719c2361bfd524b0e7

Contents?: true

Size: 1.98 KB

Versions: 41

Compression:

Stored size: 1.98 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      # @api private
      # Provides the implementation for `be_between`.
      # Not intended to be instantiated directly.
      class BeBetween < BaseMatcher
        def initialize(min, max)
          @min, @max = min, max
          inclusive
        end

        # @api public
        # Makes the between comparison inclusive.
        #
        # @example
        #   expect(3).to be_between(2, 3).inclusive
        #
        # @note The matcher is inclusive by default; this simply provides
        #       a way to be more explicit about it.
        def inclusive
          @less_than_operator = :<=
          @greater_than_operator = :>=
          @mode = :inclusive
          self
        end

        # @api public
        # Makes the between comparison exclusive.
        #
        # @example
        #   expect(3).to be_between(2, 4).exclusive
        def exclusive
          @less_than_operator = :<
          @greater_than_operator = :>
          @mode = :exclusive
          self
        end

        # @api private
        # @return [Boolean]
        def matches?(actual)
          @actual = actual
          comparable? && compare
        rescue ArgumentError
          false
        end

        # @api private
        # @return [String]
        def failure_message
          "#{super}#{not_comparable_clause}"
        end

        # @api private
        # @return [String]
        def description
          "be between #{@min.inspect} and #{@max.inspect} (#{@mode})"
        end

      private

        def comparable?
          @actual.respond_to?(@less_than_operator) && @actual.respond_to?(@greater_than_operator)
        end

        def not_comparable_clause
          ", but it does not respond to `#{@less_than_operator}` and `#{@greater_than_operator}`" unless comparable?
        end

        def compare
          @actual.__send__(@greater_than_operator, @min) && @actual.__send__(@less_than_operator, @max)
        end
      end
    end
  end
end

Version data entries

41 entries across 38 versions & 10 rubygems

Version Path
opal-rspec-0.8.0 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.8.0.alpha3 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.8.0.alpha2 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.8.0.alpha1 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.7.1 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.7.0 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.6.2 rspec-expectations/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.7.0.rc.2 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.6.1 rspec-expectations/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.6.0 rspec-expectations/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.6.0.beta1 rspec-expectations/lib/rspec/matchers/built_in/be_between.rb
opal-connect-rspec-0.5.0 rspec-expectations/lib/rspec/matchers/built_in/be_between.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/rspec-expectations-3.1.2/lib/rspec/matchers/built_in/be_between.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/rspec-expectations-3.1.0/lib/rspec/matchers/built_in/be_between.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/rspec-expectations-3.1.0/lib/rspec/matchers/built_in/be_between.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/rspec-expectations-3.1.0/lib/rspec/matchers/built_in/be_between.rb
opal-rspec-0.5.0 rspec-expectations/lib/rspec/matchers/built_in/be_between.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/rspec-expectations-3.1.2/lib/rspec/matchers/built_in/be_between.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/rspec-expectations-3.1.2/lib/rspec/matchers/built_in/be_between.rb
logstash-codec-json-2.0.3 vendor/gems/rspec-expectations-3.1.2/lib/rspec/matchers/built_in/be_between.rb