Sha256: 1e4fa956093eec52a2ca79e04a41eb259e3d913fd58b1387e089810161a02248

Contents?: true

Size: 1.58 KB

Versions: 22

Compression:

Stored size: 1.58 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|
        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

22 entries across 22 versions & 3 rubygems

Version Path
vim-jar-0.1.2.0001 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/include.rb
vim-jar-0.1.2 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/include.rb
vim-jar-0.1.1 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/include.rb
vim-jar-0.1.0 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/include.rb
vim-jar-0.0.3 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/include.rb
vim-jar-0.0.2 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/include.rb
vim-jar-0.0.1 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/include.rb
rspec-expectations-2.2.0 lib/rspec/matchers/include.rb
rspec-expectations-2.1.0 lib/rspec/matchers/include.rb
rspec-expectations-2.0.1 lib/rspec/matchers/include.rb
gemrage-1.0.0 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.4.1 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.4.0 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.3.2 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.3.1 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.3.0 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.2.0 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.1.2 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.1.1 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb
gemrage-0.1.0 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/matchers/include.rb