Sha256: 827076169e85c56e1c461ffe20157784a0ebf5a9ea237c5519ad5ffd30c9502d

Contents?: true

Size: 1.12 KB

Versions: 13

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module GrpcMock
  module Matchers
    # Base class for Hash matchers
    # https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_matchers.rb
    class HashArgumentMatcher
      def self.stringify_keys!(arg, options = {})
        case arg
        when Array
          arg.map do |elem|
            options[:deep] ? stringify_keys!(elem, options) : elem
          end
        when Hash
          Hash[
            *arg.map do |key, value|
              k = key.is_a?(Symbol) ? key.to_s : key
              v = (options[:deep] ? stringify_keys!(value, options) : value)
              [k, v]
            end.inject([]) { |r, x| r + x }]
        else
          arg
        end
      end

      def initialize(expected)
        @expected = Hash[
          GrpcMock::Matchers::HashArgumentMatcher.stringify_keys!(expected, deep: true).sort,
        ]
      end

      def ==(_actual, &block)
        @expected.all?(&block)
      rescue NoMethodError
        false
      end

      def self.from_rspec_matcher(matcher)
        new(matcher.instance_variable_get(:@expected))
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/grpc_mock-0.4.6/lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.4.6 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.4.5 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.4.4 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.4.3 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.4.2 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.4.1 lib/grpc_mock/matchers/hash_argument_matcher.rb
gruffish-0.5.0.pre1 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.4.0 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.3.1 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.3.0 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.2.2 lib/grpc_mock/matchers/hash_argument_matcher.rb
grpc_mock-0.2.1 lib/grpc_mock/matchers/hash_argument_matcher.rb