Sha256: 47b9a2f8c3983596d37047f596a3d314cb7c0103e05a8b2a93fd5a6058a60dd4

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

module RSpec
  module Matchers
    # Decorator that wraps a matcher and overrides `description`
    # using the provided block in order to support an alias
    # of a matcher. This is intended for use when composing
    # matchers, so that you can use an expression like
    # `include( a_value_within(0.1).of(3) )` rather than
    # `include( be_within(0.1).of(3) )`, and have the corresponding
    # description read naturally.
    #
    # @api private
    class AliasedMatcher < MatcherDelegator
      def initialize(base_matcher, description_block)
        @description_block = description_block
        super(base_matcher)
      end

      # Forward messages on to the wrapped matcher.
      # Since many matchers provide a fluent interface
      # (e.g. `a_value_within(0.1).of(3)`), we need to wrap
      # the returned value if it responds to `description`,
      # so that our override can be applied when it is eventually
      # used.
      def method_missing(*)
        return_val = super
        return return_val unless return_val.respond_to?(:description)
        AliasedMatcher.new(return_val, @description_block)
      end

      # Provides the description of the aliased matcher. Aliased matchers
      # are designed to behave identically to the original matcher except
      # for this method. The description is different to reflect the aliased
      # name.
      #
      # @api private
      def description
        @description_block.call(super)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
rspec-expectations-3.0.4 lib/rspec/matchers/aliased_matcher.rb
rspec-expectations-3.0.3 lib/rspec/matchers/aliased_matcher.rb
rspec-expectations-3.0.2 lib/rspec/matchers/aliased_matcher.rb
whos_dated_who-0.1.0 vendor/bundle/gems/rspec-expectations-3.0.1/lib/rspec/matchers/aliased_matcher.rb
whos_dated_who-0.0.1 vendor/bundle/gems/rspec-expectations-3.0.1/lib/rspec/matchers/aliased_matcher.rb
rspec-expectations-3.0.1 lib/rspec/matchers/aliased_matcher.rb
rspec-expectations-3.0.0 lib/rspec/matchers/aliased_matcher.rb
rspec-expectations-3.0.0.rc1 lib/rspec/matchers/aliased_matcher.rb