Sha256: 0b04b6132d078b901c76523e717f1498c94ac9d1f0d5f8b232d2fc91466e719b

Contents?: true

Size: 1.28 KB

Versions: 17

Compression:

Stored size: 1.28 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 do |actual|
          _expected.all? do |expected|
            if comparing_hash_values?(actual, expected)
              expected.all? {|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)
          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

17 entries across 17 versions & 1 rubygems

Version Path
rspec-expectations-2.0.0.beta.8 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.beta.7 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.beta.6 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.beta.5 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.beta.4 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.beta.3 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.beta.2 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.beta.1 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a10 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a9 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a8 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a7 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a6 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a5 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a4 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a3 lib/rspec/matchers/include.rb
rspec-expectations-2.0.0.a2 lib/rspec/matchers/include.rb