Sha256: 03eee4010ae38b1f5f2725862b7a5c3116dc88c119d641a35d0c30ccd129a63d

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

require_relative 'base_matcher'

module RSpec
  module GraphqlMatchers
    class Implement < BaseMatcher
      def initialize(interfaces)
        @expected = interfaces.map {|interface| interface_name(interface) }
      end

      def matches?(graph_object)
        @graph_object = graph_object
        @actual = actual
        @expected.all? { |name| @actual.include?(name) }
      end

      def failure_message
        message  = "expected interfaces: #{@expected.join(', ')}\n"
        message += "actual interfaces:   #{@actual.join(', ')}"
        message
      end

      def failure_message_when_negated
        message  = "unexpected interfaces: #{@expected.join(', ')}\n"
        message += "actual interfaces:     #{@actual.join(', ')}"
        message
      end

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

      private

      def actual
        if @graph_object.respond_to?(:interfaces)
          return @graph_object.interfaces.map do |interface|
            interface_name(interface)
          end
        end

        raise "Invalid object #{@graph_object} provided to #{matcher_name} " \
          'matcher. It does not seem to be a valid GraphQL object type.'
      end

      def interface_name(interface)
        return interface.graphql_name if interface.respond_to?(:graphql_name)

        interface.to_s
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rspec-graphql_matchers-1.3.0 lib/rspec/graphql_matchers/implement.rb
rspec-graphql_matchers-1.2.1 lib/rspec/graphql_matchers/implement.rb
rspec-graphql_matchers-1.2 lib/rspec/graphql_matchers/implement.rb
rspec-graphql_matchers-1.1 lib/rspec/graphql_matchers/implement.rb
rspec-graphql_matchers-1.0.1 lib/rspec/graphql_matchers/implement.rb
rspec-graphql_matchers-1.0 lib/rspec/graphql_matchers/implement.rb