Sha256: 84bc9d9c9cac1b78969c7cc34a38ddd0094e656d0bf15ac912a5f6944c452a71

Contents?: true

Size: 1.59 KB

Versions: 61

Compression:

Stored size: 1.59 KB

Contents

require 'mocha/parameter_matchers/base'
require 'mocha/parameter_matchers/all_of'
require 'mocha/parameter_matchers/has_entry'

module Mocha

  module ParameterMatchers

    # Matches +Hash+ containing all +entries+.
    #
    # @param [Hash] entries expected +Hash+ entries.
    # @return [HasEntries] parameter matcher.
    #
    # @see Expectation#with
    #
    # @example Actual parameter contains all expected entries.
    #   object = mock()
    #   object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
    #   object.method_1('key_1' => 1, 'key_2' => 2, 'key_3' => 3)
    #   # no error raised
    #
    # @example Actual parameter does not contain all expected entries.
    #   object = mock()
    #   object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
    #   object.method_1('key_1' => 1, 'key_2' => 99)
    #   # error raised, because method_1 was not called with Hash containing entries: 'key_1' => 1, 'key_2' => 2
    def has_entries(entries)
      HasEntries.new(entries)
    end

    # Parameter matcher which matches when actual parameter contains all expected +Hash+ entries.
    class HasEntries < Base

      # @private
      def initialize(entries)
        @entries = entries
      end

      # @private
      def matches?(available_parameters)
        parameter = available_parameters.shift
        has_entry_matchers = @entries.map { |key, value| HasEntry.new(key, value) }
        AllOf.new(*has_entry_matchers).matches?([parameter])
      end

      # @private
      def mocha_inspect
        "has_entries(#{@entries.mocha_inspect})"
      end

    end

  end

end

Version data entries

61 entries across 50 versions & 5 rubygems

Version Path
mocha-1.6.0 lib/mocha/parameter_matchers/has_entries.rb
mocha-1.5.0 lib/mocha/parameter_matchers/has_entries.rb
mocha-1.4.0 lib/mocha/parameter_matchers/has_entries.rb
mocha-1.3.0 lib/mocha/parameter_matchers/has_entries.rb
mocha-1.2.1 lib/mocha/parameter_matchers/has_entries.rb
mocha-1.2.0 lib/mocha/parameter_matchers/has_entries.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/mocha-1.0.0/lib/mocha/parameter_matchers/has_entries.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/mocha-1.0.0/lib/mocha/parameter_matchers/has_entries.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/mocha-1.0.0/lib/mocha/parameter_matchers/has_entries.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/mocha-1.0.0/lib/mocha/parameter_matchers/has_entries.rb
mocha-1.1.0 lib/mocha/parameter_matchers/has_entries.rb
mocha-1.0.0 lib/mocha/parameter_matchers/has_entries.rb
mocha-1.0.0.alpha lib/mocha/parameter_matchers/has_entries.rb
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/mocha-0.14.0/lib/mocha/parameter_matchers/has_entries.rb
challah-1.0.0 vendor/bundle/gems/mocha-0.14.0/lib/mocha/parameter_matchers/has_entries.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/mocha-0.14.0/lib/mocha/parameter_matchers/has_entries.rb
mocha-0.14.0 lib/mocha/parameter_matchers/has_entries.rb
challah-1.0.0.beta3 vendor/bundle/gems/mocha-0.13.3/lib/mocha/parameter_matchers/has_entries.rb
mocha-0.14.0.alpha lib/mocha/parameter_matchers/has_entries.rb
challah-1.0.0.beta2 vendor/bundle/gems/mocha-0.13.3/lib/mocha/parameter_matchers/has_entries.rb