# frozen_string_literal: true require "spec_helper" describe GraphQL::Language::Nodes::AbstractNode do describe ".visit_method" do # `.visit_method` is really helpful for generating methods in # custom visitor classes -- make sure this API keeps working. it "names a method on the visitor class" do node_classes = GraphQL::Language::Nodes.constants .map { |c| GraphQL::Language::Nodes.const_get(c) } .select { |obj| obj.is_a?(Class) && obj < GraphQL::Language::Nodes::AbstractNode } node_classes -= [GraphQL::Language::Nodes::WrapperType, GraphQL::Language::Nodes::NameOnlyNode] expected_classes = 35 assert_equal 35, node_classes.size tested_classes = 0 node_classes.each do |node_class| expected_method_name = "on_#{GraphQL::Schema::Member::BuildType.underscore(node_class.name.split("::").last)}" assert_equal node_class.visit_method.to_s, expected_method_name, "#{node_class} has #{expected_method_name} for visit_method" assert GraphQL::Language::Visitor.method_defined?(expected_method_name), "Visitor has ##{expected_method_name}" assert GraphQL::Language::StaticVisitor.method_defined?(expected_method_name), "Visitor has ##{expected_method_name}" tested_classes += 1 end assert_equal expected_classes, tested_classes, "All classes were tested" end end describe "Marshal" do it "marshals and unmarshals parsed ASTs" do str = "query($var: [Int!] = [100001]) { f1(arg: {input: $var, nullInput: null}) @stuff { ...F2 } } fragment F2 on SomeType { ... { someField(arg1: true, arg2: THING, arg3: 5.01234) { a @someDirective @anotherDirective @yetAnother b c } } }" doc = GraphQL.parse(str) data = Marshal.dump(doc) new_doc = Marshal.load(data) assert_equal doc, new_doc assert_equal str, doc.to_query_string assert_equal str, new_doc.to_query_string # also test schema definition nodes: str2 = Dummy::Schema.to_definition.strip doc2 = GraphQL.parse(str2) data2 = Marshal.dump(doc2) new_doc2 = Marshal.load(data2) assert_equal doc, new_doc assert_equal str2, doc2.to_query_string assert_equal str2, new_doc2.to_query_string end end describe "#filename" do it "is set after .parse_file" do filename = "spec/support/parser/filename_example.graphql" doc = GraphQL.parse_file(filename) op = doc.definitions.first field = op.selections.first arg = field.arguments.first assert_equal filename, doc.filename assert_equal filename, op.filename assert_equal filename, field.filename assert_equal filename, arg.filename end it "is null when parse from string" do doc = GraphQL.parse("{ thing }") assert_nil doc.filename end end describe "#to_query_tring" do let(:document) { GraphQL.parse('type Query { a: String! }') } let(:custom_printer_class) { Class.new(GraphQL::Language::Printer) { def print_field_definition(print_field_definition) print_string("