Sha256: b814d3c74a26881395a4890e78c46fe8e17ed38a9af3a572381b141fc948f261

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 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[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

10 entries across 10 versions & 5 rubygems

Version Path
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/matchers/built_in/include.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/matchers/built_in/include.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/matchers/built_in/include.rb
gem_repackager-0.1.0 support/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/include.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/include.rb
rspec-expectations-2.11.3 lib/rspec/matchers/built_in/include.rb
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/include.rb
rspec-expectations-2.11.2 lib/rspec/matchers/built_in/include.rb
rspec-expectations-2.11.1 lib/rspec/matchers/built_in/include.rb
rspec-expectations-2.11.0 lib/rspec/matchers/built_in/include.rb