Module: GraphQL::Language::ParserTests

Defined in:
lib/graphql/language/parser_tests.rb

Overview

If you create your own GraphQL parser, can verify it using these tests.

Examples:

Include these tests in a Minitest suite

require 'graphql/language/parser_tests'

describe MyParser do
  include GraphQL::Language::ParserTests
  subject { MyParser }
end

Class Method Summary collapse

Class Method Details

.included(test) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/graphql/language/parser_tests.rb', line 13

def self.included(test)
  test.send(:describe, "Parser Tests") do
    let(:document) { subject.parse(query_string) }
    let(:query_string) {%|
      query getStuff($someVar: Int = 1, $anotherVar: [String!] ) @skip(if: false) {
        myField: someField(someArg: $someVar, ok: 1.4) @skip(if: $anotherVar) @thing(or: "Whatever")

        anotherField(someArg: [1,2,3]) {
          nestedField
          ... moreNestedFields @skip(if: true)
        }

        ... on OtherType @include(unless: false){
          field(arg: [{key: "value", anotherKey: 0.9, anotherAnotherKey: WHATEVER}])
          anotherField
        }

        ... {
          id
        }
      }

      fragment moreNestedFields on NestedType @or(something: "ok") {
        anotherNestedField @enum(directive: true)
      }
    |}

    describe ".parse" do
      it "parses queries" do
        assert document
      end

      describe "visited nodes" do
        let(:query) { document.definitions.first }
        let(:fragment_def) { document.definitions.last }

        it "creates a valid document" do
          assert document.is_a?(GraphQL::Language::Nodes::Document)
          assert_equal 2, document.definitions.length
        end

        it "creates a valid operation" do
          assert query.is_a?(GraphQL::Language::Nodes::OperationDefinition)
          assert_equal "getStuff", query.name
          assert_equal "query", query.operation_type
          assert_equal 2, query.variables.length
          assert_equal 4, query.selections.length
          assert_equal 1, query.directives.length
          assert_equal [2, 13], [query.line, query.col]
        end

        it "creates a valid fragment definition" do
          assert fragment_def.is_a?(GraphQL::Language::Nodes::FragmentDefinition)
          assert_equal "moreNestedFields", fragment_def.name
          assert_equal 1, fragment_def.selections.length
          assert_equal "NestedType", fragment_def.type
          assert_equal 1, fragment_def.directives.length
          assert_equal [20, 13], fragment_def.position
        end

        describe "variable definitions" do
          let(:optional_var) { query.variables.first }
          it "gets name and type" do
            assert_equal "someVar", optional_var.name
            assert_equal "Int", optional_var.type.name
          end

          it "gets default value" do
            assert_equal 1, optional_var.default_value
          end

          it "gets position info" do
            assert_equal [2, 28], optional_var.position
          end
        end

        describe "fields" do
          let(:leaf_field) { query.selections.first }
          let(:parent_field) { query.selections[1] }

          it "gets name, alias, arguments and directives" do
            assert_equal "someField", leaf_field.name
            assert_equal "myField", leaf_field.alias
            assert_equal 2, leaf_field.directives.length
            assert_equal 2, leaf_field.arguments.length
          end

          it "gets nested fields" do
            assert_equal 2, parent_field.selections.length
          end

          it "gets location info" do
            assert_equal [3 ,15], leaf_field.position
          end

          describe "when the arguments list is empty" do
            let(:query_string) { "{ field() }"}
            let(:field) { query.selections.first }
            it "has zero arguments" do
              assert_equal 0, field.arguments.length
            end
          end

          describe "when selections are empty" do
            let(:query_string) { "{ field { } }"}
            let(:field) { query.selections.first }
            it "has zero selections" do
              assert_equal 0, field.selections.length
            end
          end
        end

        describe "arguments" do
          let(:literal_argument) { query.selections.first.arguments.last }
          let(:variable_argument) { query.selections.first.arguments.first }

          it "gets name and literal value" do
            assert_equal "ok", literal_argument.name
            assert_equal 1.4, literal_argument.value
          end

          it "gets name and variable value" do
            assert_equal "someArg", variable_argument.name
            assert_equal "someVar", variable_argument.value.name
          end


          it "gets position info" do
            assert_equal [3, 34], variable_argument.position
          end
        end

        describe "fragment spreads" do
          let(:fragment_spread) { query.selections[1].selections.last }
          it "gets the name and directives" do
            assert_equal "moreNestedFields", fragment_spread.name
            assert_equal 1, fragment_spread.directives.length
          end

          it "gets position info" do
            assert_equal [7, 17], fragment_spread.position
          end
        end

        describe "directives" do
          let(:variable_directive) { query.selections.first.directives.first }

          it "gets the name and arguments" do
            assert_equal "skip", variable_directive.name
            assert_equal "if", variable_directive.arguments.first.name
            assert_equal 1, variable_directive.arguments.length
          end

          it "gets position info" do
            assert_equal [3, 62], variable_directive.position
          end
        end

        describe "inline fragments" do
          let(:inline_fragment) { query.selections[2] }
          let(:typeless_inline_fragment) { query.selections[3] }

          it "gets the type and directives" do
            assert_equal "OtherType", inline_fragment.type
            assert_equal 2, inline_fragment.selections.length
            assert_equal 1, inline_fragment.directives.length
          end

          it "gets inline fragments without type conditions" do
            assert_equal nil, typeless_inline_fragment.type
            assert_equal 1, typeless_inline_fragment.selections.length
            assert_equal 0, typeless_inline_fragment.directives.length
          end

          it "gets position info" do
            assert_equal [10, 15], inline_fragment.position
          end
        end

        describe "inputs" do
          let(:query_string) {%|
            {
              field(
                int: 3,
                float: 4.7e-24,
                bool: false,
                string: "β˜€οΈŽπŸ†\\n escaped \\" unicode \\u00b6 /",
                enum: ENUM_NAME,
                array: [7, 8, 9]
                object: {a: [1,2,3], b: {c: "4"}}
                unicode_bom: "\xef\xbb\xbfquery"
                keywordEnum: on
              )
            }
          |}

          let(:inputs) { document.definitions.first.selections.first.arguments }

          it "parses ints" do
            assert_equal 3, inputs[0].value
          end

          it "parses floats" do
            assert_equal 0.47e-23, inputs[1].value
          end

          it "parses booleans" do
            assert_equal false, inputs[2].value
          end

          it "parses UTF-8 strings" do
            assert_equal %|β˜€οΈŽπŸ†\n escaped " unicode ΒΆ /|, inputs[3].value
          end

          it "parses enums" do
            assert_instance_of GraphQL::Language::Nodes::Enum, inputs[4].value
            assert_equal "ENUM_NAME", inputs[4].value.name
          end

          it "parses arrays" do
            assert_equal [7,8,9], inputs[5].value
          end

          it "parses objects" do
            obj = inputs[6].value
            assert_equal "a", obj.arguments[0].name
            assert_equal [1,2,3], obj.arguments[0].value
            assert_equal "b", obj.arguments[1].name
            assert_equal "c", obj.arguments[1].value.arguments[0].name
            assert_equal "4", obj.arguments[1].value.arguments[0].value
          end

          it "parses unicode bom" do
            obj = inputs[7].value
            assert_equal %|\xef\xbb\xbfquery|, inputs[7].value
          end

          it "parses enum 'on''" do
            assert_equal "on", inputs[8].value.name
          end
        end
      end

      describe "unnamed queries" do
        let(:query_string) {%|
          { name, age, height }
        |}
        let(:operation) { document.definitions.first }

        it "parses unnamed queries" do
          assert_equal 1, document.definitions.length
          assert_equal "query", operation.operation_type
          assert_equal nil, operation.name
          assert_equal 3, operation.selections.length
        end
      end

      describe "introspection query" do
        let(:query_string) { GraphQL::Introspection::INTROSPECTION_QUERY }

        it "parses a big ol' query" do
          assert(document)
        end
      end

      describe "schema with comments" do
        let(:query_string) {%|
          # Schema at beginning of file

          schema {
            query: Hello
          }

          # Comment between two definitions are omitted

          # This is a directive
          directive @foo(
            # It has an argument
            arg: Int
          ) on FIELD

          # Multiline comment
          #
          # With an enum
          enum Color {
            RED

            # Not a creative color
            GREEN
            BLUE
          }

          #Comment without preceding space
          type Hello {
            # And a field to boot
            str: String
          }

          # Comment for input object types
          input Car {
            # Color of the car
            color: String!
          }

          # Comment for interface definitions
          interface Vehicle {
            # Amount of wheels
            wheels: Int!
          }

          # Comment at the end of schema
        |}

        it "parses successfully" do
          document = subject.parse(query_string)

          assert_equal 6, document.definitions.size

          schema_definition = document.definitions.shift
          assert_equal GraphQL::Language::Nodes::SchemaDefinition, schema_definition.class

          directive_definition = document.definitions.shift
          assert_equal GraphQL::Language::Nodes::DirectiveDefinition, directive_definition.class
          assert_equal 'This is a directive', directive_definition.description

          enum_type_definition = document.definitions.shift
          assert_equal GraphQL::Language::Nodes::EnumTypeDefinition, enum_type_definition.class
          assert_equal "Multiline comment\n\nWith an enum", enum_type_definition.description

          assert_nil enum_type_definition.values[0].description
          assert_equal 'Not a creative color', enum_type_definition.values[1].description

          object_type_definition = document.definitions.shift
          assert_equal GraphQL::Language::Nodes::ObjectTypeDefinition, object_type_definition.class
          assert_equal 'Comment without preceding space', object_type_definition.description
          assert_equal 'And a field to boot', object_type_definition.fields[0].description

          input_object_type_definition = document.definitions.shift
          assert_equal GraphQL::Language::Nodes::InputObjectTypeDefinition, input_object_type_definition.class
          assert_equal 'Comment for input object types', input_object_type_definition.description
          assert_equal 'Color of the car', input_object_type_definition.fields[0].description

          interface_type_definition = document.definitions.shift
          assert_equal GraphQL::Language::Nodes::InterfaceTypeDefinition, interface_type_definition.class
          assert_equal 'Comment for interface definitions', interface_type_definition.description
          assert_equal 'Amount of wheels', interface_type_definition.fields[0].description
        end
      end

      describe "schema" do
        it "parses the test schema" do
          schema = DummySchema
          schema_string = GraphQL::Schema::Printer.print_schema(schema)

          document = subject.parse(schema_string)

          assert_equal schema_string, document.to_query_string
        end

        it "parses mimal schema definition" do
          document = subject.parse('schema { query: QueryRoot }')

          schema = document.definitions.first
          assert_equal 'QueryRoot', schema.query
          assert_equal nil, schema.mutation
          assert_equal nil, schema.subscription
        end

        it "parses full schema definitions" do
          document = subject.parse('
            schema {
              query: QueryRoot
              mutation: MutationRoot
              subscription: SubscriptionRoot
            }
          ')

          schema = document.definitions.first
          assert_equal 'QueryRoot', schema.query
          assert_equal 'MutationRoot', schema.mutation
          assert_equal 'SubscriptionRoot', schema.subscription
        end

        it "parses object types" do
          document = subject.parse('
            type Comment implements Node {
              id: ID!
            }
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::ObjectTypeDefinition, type.class
          assert_equal 'Comment', type.name
          assert_equal ['Node'], type.interfaces.map(&:name)
          assert_equal ['id'], type.fields.map(&:name)
          assert_equal [], type.fields[0].arguments
          assert_equal 'ID', type.fields[0].type.of_type.name
        end

        it "parses object types with directives" do
          document = subject.parse('
            type Comment implements Node @deprecated(reason: "No longer supported") {
              id: ID!
            }
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::ObjectTypeDefinition, type.class
          assert_equal 'Comment', type.name
          assert_equal ['Node'], type.interfaces.map(&:name)
          assert_equal ['id'], type.fields.map(&:name)
          assert_equal [], type.fields[0].arguments
          assert_equal 'ID', type.fields[0].type.of_type.name
          assert_equal 1, type.directives.length

          deprecated_directive = type.directives[0]
          assert_equal 'deprecated', deprecated_directive.name
          assert_equal 'reason', deprecated_directive.arguments[0].name
          assert_equal 'No longer supported', deprecated_directive.arguments[0].value
        end

        it "parses field arguments" do
          document = subject.parse('
            type Mutation {
              post(id: ID!, data: PostData = { message: "First!1!", type: BLOG, tags: ["Test", "Annoying"] }): Post
            }
          ')

          field = document.definitions.first.fields.first
          assert_equal ['id', 'data'], field.arguments.map(&:name)
          data_arg = field.arguments[1]
          assert_equal 'PostData', data_arg.type.name
          assert_equal ['message', 'type', 'tags'], data_arg.default_value.arguments.map(&:name)
          tags_arg = data_arg.default_value.arguments[2]
          assert_equal ['Test', 'Annoying'], tags_arg.value
        end

        it "parses field arguments with directives" do
          document = subject.parse('
            type Mutation {
              post(id: ID! @deprecated(reason: "No longer supported"), data: String): Post
            }
          ')

          field = document.definitions.first.fields.first
          assert_equal ['id', 'data'], field.arguments.map(&:name)
          id_arg = field.arguments[0]

          deprecated_directive = id_arg.directives[0]
          assert_equal 'deprecated', deprecated_directive.name
          assert_equal 'reason', deprecated_directive.arguments[0].name
          assert_equal 'No longer supported', deprecated_directive.arguments[0].value
        end

        it "parses directive definition" do
          document = subject.parse('
            directive @include(if: Boolean!)
              on FIELD
              | FRAGMENT_SPREAD
              | INLINE_FRAGMENT
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::DirectiveDefinition, type.class
          assert_equal 'include', type.name

          assert_equal 1, type.arguments.length
          assert_equal 'if', type.arguments[0].name
          assert_equal 'Boolean', type.arguments[0].type.of_type.name

          assert_equal ['FIELD', 'FRAGMENT_SPREAD', 'INLINE_FRAGMENT'], type.locations
        end

        it "parses scalar types" do
          document = subject.parse('scalar DateTime')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::ScalarTypeDefinition, type.class
          assert_equal 'DateTime', type.name
        end

        it "parses scalar types with directives" do
          document = subject.parse('scalar DateTime @deprecated(reason: "No longer supported")')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::ScalarTypeDefinition, type.class
          assert_equal 'DateTime', type.name
          assert_equal 1, type.directives.length

          deprecated_directive = type.directives[0]
          assert_equal 'deprecated', deprecated_directive.name
          assert_equal 'reason', deprecated_directive.arguments[0].name
          assert_equal 'No longer supported', deprecated_directive.arguments[0].value
        end

        it "parses interface types" do
          document = subject.parse('
            interface Node {
              id: ID!
            }
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::InterfaceTypeDefinition, type.class
          assert_equal 'Node', type.name
          assert_equal ['id'], type.fields.map(&:name)
          assert_equal [], type.fields[0].arguments
          assert_equal 'ID', type.fields[0].type.of_type.name
        end

        it "parses interface types with directives" do
          document = subject.parse('
            interface Node @deprecated(reason: "No longer supported") {
              id: ID!
            }
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::InterfaceTypeDefinition, type.class
          assert_equal 'Node', type.name
          assert_equal 1, type.directives.length

          deprecated_directive = type.directives[0]
          assert_equal 'deprecated', deprecated_directive.name
          assert_equal 'reason', deprecated_directive.arguments[0].name
          assert_equal 'No longer supported', deprecated_directive.arguments[0].value
        end

        it "parses enum types" do
          document = subject.parse('
            enum DogCommand {
              SIT
              DOWN @deprecated(reason: "No longer supported")
              HEEL
            }
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::EnumTypeDefinition, type.class
          assert_equal 'DogCommand', type.name
          assert_equal 3, type.values.length

          assert_equal 'SIT', type.values[0].name
          assert_equal [], type.values[0].directives

          assert_equal 'DOWN', type.values[1].name
          assert_equal 1, type.values[1].directives.length
          deprecated_directive = type.values[1].directives[0]
          assert_equal 'deprecated', deprecated_directive.name
          assert_equal 'reason', deprecated_directive.arguments[0].name
          assert_equal 'No longer supported', deprecated_directive.arguments[0].value

          assert_equal 'HEEL', type.values[2].name
          assert_equal [], type.values[2].directives
        end

        it "parses enum types with directives" do
          document = subject.parse('
            enum DogCommand @deprecated(reason: "No longer supported") {
              SIT
            }
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::EnumTypeDefinition, type.class
          assert_equal 'DogCommand', type.name
          assert_equal 1, type.directives.length

          deprecated_directive = type.directives[0]
          assert_equal 'deprecated', deprecated_directive.name
          assert_equal 'reason', deprecated_directive.arguments[0].name
          assert_equal 'No longer supported', deprecated_directive.arguments[0].value
        end

        it "parses input object types" do
          document = subject.parse('
            input EmptyMutationInput {
              clientMutationId: String
            }
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::InputObjectTypeDefinition, type.class
          assert_equal 'EmptyMutationInput', type.name
          assert_equal ['clientMutationId'], type.fields.map(&:name)
          assert_equal 'String', type.fields[0].type.name
          assert_equal nil, type.fields[0].default_value
        end

        it "parses input object types with directives" do
          document = subject.parse('
            input EmptyMutationInput @deprecated(reason: "No longer supported") {
              clientMutationId: String
            }
          ')

          type = document.definitions.first
          assert_equal GraphQL::Language::Nodes::InputObjectTypeDefinition, type.class
          assert_equal 'EmptyMutationInput', type.name
          assert_equal ['clientMutationId'], type.fields.map(&:name)
          assert_equal 'String', type.fields[0].type.name
          assert_equal nil, type.fields[0].default_value
          assert_equal 1, type.directives.length

          deprecated_directive = type.directives[0]
          assert_equal 'deprecated', deprecated_directive.name
          assert_equal 'reason', deprecated_directive.arguments[0].name
          assert_equal 'No longer supported', deprecated_directive.arguments[0].value

        end
      end
    end

    describe "errors" do
      let(:query_string) {%| query doSomething { bogus { } |}
      it "raises a parse error" do
        err = assert_raises(GraphQL::ParseError) { document }
      end

      it "correctly identifies parse error location and content" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("
            query getCoupons {
              allCoupons: {data{id}}
            }
          ")
        end
        assert_includes(e.message, '"{"')
        assert_includes(e.message, "LCURLY")
        assert_equal(3, e.line)
        assert_equal(33, e.col)
      end

      it "handles unexpected ends" do
        err = assert_raises(GraphQL::ParseError) { GraphQL.parse("{ ") }
        assert_equal "Unexpected end of document", err.message
      end

      it "rejects unsupported characters" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ field; }")
        end

        assert_includes(e.message, "Parse error on \";\"")
      end

      it "rejects control characters" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ \afield }")
        end

        assert_includes(e.message, "Parse error on \"\\a\"")
      end

      it "rejects partial BOM" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ \xeffield }")
        end

        assert_includes(e.message, "Parse error on \"\\xEF\"")
      end

      it "rejects vertical tabs" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ \vfield }")
        end

        assert_includes(e.message, "Parse error on \"\\v\"")
      end

      it "rejects form feed" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ \ffield }")
        end

        assert_includes(e.message, "Parse error on \"\\f\"")
      end

      it "rejects no break space" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ \xa0field }")
        end

        assert_includes(e.message, "Parse error on \"\\xA0\"")
      end

      it "rejects unterminated strings" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("\"")
        end

        assert_includes(e.message, "Parse error on \"\\\"\"")

        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("\"\n\"")
        end

        assert_includes(e.message, "Parse error on \"\\n\"")
      end

      it "rejects bad escape sequence in strings" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ field(arg:\"\\x\") }")
        end

        assert_includes(e.message, "Parse error on bad Unicode escape sequence")
      end

      it "rejects incomplete escape sequence in strings" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ field(arg:\"\\u1\") }")
        end

        assert_includes(e.message, "bad Unicode escape sequence")
      end

      it "rejects unicode escape with bad chars" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ field(arg:\"\\u0XX1\") }")
        end

        assert_includes(e.message, "bad Unicode escape sequence")

        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ field(arg:\"\\uXXXX\") }")
        end

        assert_includes(e.message, "bad Unicode escape sequence")


        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ field(arg:\"\\uFXXX\") }")
        end

        assert_includes(e.message, "bad Unicode escape sequence")


        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ field(arg:\"\\uXXXF\") }")
        end

        assert_includes(e.message, "bad Unicode escape sequence")
      end

      it "rejects fragments named 'on'" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("fragment on on on { on }")
        end

        assert_includes(e.message, "Parse error on \"on\"")
      end

      it "rejects fragment spread of 'on'" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ ...on }")
        end

        assert_includes(e.message, "Parse error on \"}\"")
      end

      it "rejects null value" do
        e = assert_raises(GraphQL::ParseError) do
          GraphQL.parse("{ fieldWithNullableStringInput(input: null) }")
        end

        assert_includes(e.message, "Parse error on \"null\"")
      end
    end


    describe "whitespace" do
      describe "whitespace-only queries" do
        let(:query_string) { " " }
        it "doesn't blow up" do
          assert_equal [], document.definitions
        end
      end

      describe "empty string queries" do
        let(:query_string) { "" }
        it "doesn't blow up" do
          assert_equal [], document.definitions
        end
      end

      describe "using tabs as whitespace" do
        let(:query_string) { "\t{\t\tid, \tname}"}
        it "parses the query" do
          assert_equal 1, document.definitions.length
        end
      end
    end
  end
end