forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer_test.go
134 lines (119 loc) · 3.38 KB
/
lexer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
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
package common_test
import (
"testing"
"github.com/graph-gophers/graphql-go/common"
)
type consumeTestCase struct {
description string
definition string
expected string // expected description
failureExpected bool
useStringDescriptions bool
}
// Note that these tests stop as soon as they parse the comments, so even though the rest of the file will fail to parse sometimes, the tests still pass
var consumeTests = []consumeTestCase{{
description: "no string descriptions allowed in old mode",
definition: `
# Comment line 1
#Comment line 2
,,,,,, # Commas are insignificant
"New style comments"
type Hello {
world: String!
}`,
expected: "Comment line 1\nComment line 2\nCommas are insignificant",
useStringDescriptions: false,
}, {
description: "simple string descriptions allowed in new mode",
definition: `
# Comment line 1
#Comment line 2
,,,,,, # Commas are insignificant
"New style comments"
type Hello {
world: String!
}`,
expected: "New style comments",
useStringDescriptions: true,
}, {
description: "comment after description works",
definition: `
# Comment line 1
#Comment line 2
,,,,,, # Commas are insignificant
type Hello {
world: String!
}`,
expected: "",
useStringDescriptions: true,
}, {
description: "triple quote descriptions allowed in new mode",
definition: `
# Comment line 1
#Comment line 2
,,,,,, # Commas are insignificant
"""
New style comments
Another line
"""
type Hello {
world: String!
}`,
expected: "New style comments\nAnother line",
useStringDescriptions: true,
}}
func TestConsume(t *testing.T) {
for _, test := range consumeTests {
t.Run(test.description, func(t *testing.T) {
lex := common.NewLexer(test.definition, test.useStringDescriptions)
err := lex.CatchSyntaxError(func() { lex.ConsumeWhitespace() })
if test.failureExpected {
if err == nil {
t.Fatalf("schema should have been invalid; comment: %s", lex.DescComment())
}
} else {
if err != nil {
t.Fatal(err)
}
}
if test.expected != lex.DescComment() {
t.Errorf("wrong description value:\nwant: %q\ngot : %q", test.expected, lex.DescComment())
}
})
}
}
var multilineStringTests = []consumeTestCase{
{
description: "Oneline strings are okay",
definition: `"Hello World"`,
expected: "",
failureExpected: false,
useStringDescriptions: true,
},
{
description: "Multiline strings are not allowed",
definition: `"Hello
World"`,
expected: `graphql: syntax error: literal not terminated (line 1, column 1)`,
failureExpected: true,
useStringDescriptions: true,
},
}
func TestMultilineString(t *testing.T) {
for _, test := range multilineStringTests {
t.Run(test.description, func(t *testing.T) {
lex := common.NewLexer(test.definition, test.useStringDescriptions)
err := lex.CatchSyntaxError(func() { lex.ConsumeWhitespace() })
if test.failureExpected && err == nil {
t.Fatalf("Test '%s' should fail", test.description)
} else if test.failureExpected && err != nil {
if test.expected != err.Error() {
t.Fatalf("Test '%s' failed with wrong error: '%s'. Error should be: '%s'", test.description, err.Error(), test.expected)
}
}
if !test.failureExpected && err != nil {
t.Fatalf("Test '%s' failed with error: '%s'", test.description, err.Error())
}
})
}
}