Sha256: d85c7d303534f0e35cd650cd53d35744b48313015d9281b5b1c4de1e524d2acf

Contents?: true

Size: 1.76 KB

Versions: 37

Compression:

Stored size: 1.76 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?
          # Matchers do not diff well, since diff uses their inspect
          # output, which includes their instance variables and such.
          @expected.none? { |e| RSpec::Matchers.is_a_matcher?(e) }
        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)
            elsif comparing_with_matcher?(actual, expected)
              actual.any? { |value| expected.matches?(value) }
            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

        def comparing_with_matcher?(actual, expected)
          actual.is_a?(Array) && RSpec::Matchers.is_a_matcher?(expected)
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 10 rubygems

Version Path
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
vagrant-unbundled-1.8.4.2 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
vagrant-unbundled-1.8.1.1 vendor/bundle/ruby/2.3.0/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
opal-rspec-cj-0.4.4 vendor_lib/rspec/matchers/built_in/include.rb
opal-rspec-0.4.3 vendor_lib/rspec/matchers/built_in/include.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/rspec-expectations-2.14.5/lib/rspec/matchers/built_in/include.rb
opal-rspec-0.4.2 vendor_lib/rspec/matchers/built_in/include.rb
opal-rspec-0.4.1 vendor_lib/rspec/matchers/built_in/include.rb
opal-rspec-0.4.0 vendor_lib/rspec/matchers/built_in/include.rb
opal-rspec-0.4.0.beta4 vendor_lib/rspec/matchers/built_in/include.rb
dxruby_rp5-0.0.4 spec/vendor/rspec-expectations-2.14.4/lib/rspec/matchers/built_in/include.rb
rspec-expectations-2.99.0.beta2 lib/rspec/matchers/built_in/include.rb
rspec-expectations-2.14.5 lib/rspec/matchers/built_in/include.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/rspec-expectations-2.14.4/lib/rspec/matchers/built_in/include.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/rspec-expectations-2.14.4/lib/rspec/matchers/built_in/include.rb