Sha256: 10926d6f6c0ff7b63f4a2205f703a9f0d876d829fed7caafb1d6f72ccdb3c1cf

Contents?: true

Size: 1.6 KB

Versions: 10

Compression:

Stored size: 1.6 KB

Contents

module RSpec
  module Matchers
    # :call-seq:
    #   should include(expected)
    #   should_not include(expected)
    #
    # Passes if actual includes expected. This works for
    # collections and Strings. You can also pass in multiple args
    # and it will only pass if all args are found in collection.
    #
    # == Examples
    #
    #   [1,2,3].should include(3)
    #   [1,2,3].should include(2,3) #would pass
    #   [1,2,3].should include(2,3,4) #would fail
    #   [1,2,3].should_not include(4)
    #   "spread".should include("read")
    #   "spread".should_not include("red")
    def include(*expected)
      Matcher.new :include, *expected do |*_expected|

        diffable

        match_for_should do |actual|
          perform_match(:all?, :all?, actual, _expected)
        end

        match_for_should_not do |actual|
          perform_match(:none?, :any?, actual, _expected)
        end

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

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

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

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
rspec-expectations-2.7.0.rc1 lib/rspec/matchers/include.rb
messagebus_ruby_api-0.4.7 spec/ruby/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/matchers/include.rb
messagebus_ruby_api-0.4.4 spec/ruby/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/matchers/include.rb
rspec-expectations-2.6.0 lib/rspec/matchers/include.rb
rspec-expectations-2.6.0.rc6 lib/rspec/matchers/include.rb
rspec-expectations-2.6.0.rc4 lib/rspec/matchers/include.rb
rspec-expectations-2.6.0.rc2 lib/rspec/matchers/include.rb
rspec-expectations-2.5.0 lib/rspec/matchers/include.rb
rspec-expectations-2.4.0 lib/rspec/matchers/include.rb
rspec-expectations-2.3.0 lib/rspec/matchers/include.rb