rspec / rspec-mocks

RSpec's 'test double' framework, with support for stubbing and mocking
https://rspec.info
MIT License
1.16k stars 357 forks source link

array_excluding matcher? #1527

Closed sirwolfgang closed 1 year ago

sirwolfgang commented 1 year ago

Is there any technical reason there is not an array_excluding argument matcher? There's hash excluding right below it.

https://github.com/rspec/rspec-mocks/blob/main/lib/rspec/mocks/argument_matchers.rb#L74-L93

sirwolfgang commented 1 year ago

This seems to work fine; Just weird to me that this has never come up before?

module RSpec
  module Mocks
    module ArgumentMatchers
      def array_excluding(*args)
        actually_an_array = Array === args.first && args.count == 1 ? args.first : args
        ArrayExcludingMatcher.new(actually_an_array)
      end

      # @private
      class ArrayExcludingMatcher
        def initialize(expected)
          @expected = expected
        end

        def ===(actual)
          actual = actual.uniq
          @expected.uniq.none? do |expected_element|
            actual.any? do |actual_element|
              RSpec::Support::FuzzyMatcher.values_match?(expected_element, actual_element)
            end
          end
        rescue NoMethodError
          false
        end

        def description
          "array_excluding(#{formatted_expected_values})"
        end

      private

        def formatted_expected_values
          @expected.map do |x|
            RSpec::Support.rspec_description_for_object(x)
          end.join(", ")
        end
      end
    end
  end
end
JonRowe commented 1 year ago

Closing to continue discussion in #1528