Sha256: 215b115746075974c643d5b9f722867305d4303354f41f5a3b7f86940b1cb8d9

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      class Include < BaseMatcher
        def initialize(*expected)
          @expected = expected
        end

        def matches?(actual)
          @actual = actual
          perform_match(:all?, :all?, @actual, @expected)
        end

        def does_not_match?(actual)
          @actual = actual
          perform_match(:none?, :any?, @actual, @expected)
        end

        def description
          "include#{expected_to_sentence}"
        end

        def diffable?
          true
        end

        private

        def perform_match(predicate, hash_predicate, actuals, expecteds)
          expecteds.send(predicate) do |expected|
            if comparing_hash_values?(actuals, expected)
              expected.send(hash_predicate) {|k,v|
                actuals.has_key?(k) && actuals[k] == v
              }
            elsif comparing_hash_keys?(actuals, expected)
              actuals.has_key?(expected)
            else
              actuals.include?(expected)
            end
          end
        end

        def comparing_hash_keys?(actual, expected)
          actual.is_a?(Hash) && !expected.is_a?(Hash)
        end

        def comparing_hash_values?(actual, expected)
          actual.is_a?(Hash) && expected.is_a?(Hash)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/matchers/built_in/include.rb
remq-0.0.4 vendor/bundle/gems/rspec-expectations-2.12.1/lib/rspec/matchers/built_in/include.rb
remq-0.0.3 vendor/bundle/gems/rspec-expectations-2.12.1/lib/rspec/matchers/built_in/include.rb
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/matchers/built_in/include.rb
rspec-expectations-2.12.1 lib/rspec/matchers/built_in/include.rb
rspec-expectations-2.12.0 lib/rspec/matchers/built_in/include.rb