Sha256: 3c723ef4fbdb95eb4ca784d5c81f3e225b14fe5bdaaa812a40edfe038a6bb855

Contents?: true

Size: 1.7 KB

Versions: 25

Compression:

Stored size: 1.7 KB

Contents

# From http://gist.github.com/62943
# Author http://github.com/trotter
module RSpec
  module Mocks
    module ArgumentMatchers

      class ArrayIncludingMatcher
        # We'll allow an array of arguments to be passed in, so that you can do
        # things like obj.should_receive(:blah).with(array_including('a', 'b'))
        def initialize(*expected)
          @expected = expected
        end

        # actual is the array (hopefully) passed to the method by the user.
        # We'll check that it includes all the expected values, and return false
        # if it doesn't or if we blow up because #include? is not defined.
        def ==(actual)
          @expected.each do |expected|
            return false unless actual.include?(expected)
          end
          true
        rescue NoMethodError => ex
          return false
        end

        def description
          "array_including(#{@expected.join(', ')})"
        end
      end

      class ArrayNotIncludingMatcher
        def initialize(*expected)
          @expected = expected
        end

        def ==(actual)
          @expected.each do |expected|
            return false if actual.include?(expected)
          end
          true
        rescue NoMethodError => ex
          return false
        end

        def description
          "array_not_including(#{@expected.join(', ')})"
        end
      end

      # array_including is a helpful wrapper that allows us to actually type
      # #with(array_including(...)) instead of ArrayIncludingMatcher.new(...)
      def array_including(*args)
        ArrayIncludingMatcher.new(*args)
      end

      def array_not_including(*args)
        ArrayNotIncludingMatcher.new(*args)
      end

    end
  end
end

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
mongo_doc-0.6.26 spec/array_including_argument_matcher.rb
mongo_doc-0.6.25 spec/array_including_argument_matcher.rb
mongo_doc-0.6.23 spec/array_including_argument_matcher.rb
mongo_doc-0.6.22 spec/array_including_argument_matcher.rb
mongo_doc-0.6.21 spec/array_including_argument_matcher.rb
mongo_doc-0.6.20 spec/array_including_argument_matcher.rb
mongo_doc-0.6.19 spec/array_including_argument_matcher.rb
mongo_doc-0.6.18 spec/array_including_argument_matcher.rb
mongo_doc-0.6.17 spec/array_including_argument_matcher.rb
mongo_doc-0.6.16 spec/array_including_argument_matcher.rb
mongo_doc-0.6.15 spec/array_including_argument_matcher.rb
mongo_doc-0.6.14 spec/array_including_argument_matcher.rb
mongo_doc-0.6.13 spec/array_including_argument_matcher.rb
mongo_doc-0.6.12 spec/array_including_argument_matcher.rb
mongo_doc-0.6.11 spec/array_including_argument_matcher.rb
mongo_doc-0.6.10 spec/array_including_argument_matcher.rb
mongo_doc-0.6.9 spec/array_including_argument_matcher.rb
mongo_doc-0.6.8 spec/array_including_argument_matcher.rb
mongo_doc-0.6.7 spec/array_including_argument_matcher.rb
mongo_doc-0.6.6 spec/array_including_argument_matcher.rb