-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
184 lines (142 loc) · 4.2 KB
/
parser_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
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
package main
import (
"testing"
)
func assertParsingFails(t *testing.T, json string) {
tokens, lexOk := Lex(json)
if !lexOk {
t.Fatal("Expected lexing to succeed, but it failed.")
}
_, parseOk := Parse(tokens)
if parseOk {
t.Fatal("Expected parsing to fail, but it succeeded.")
}
}
func areAstsEqual(t *testing.T, expected *Node, actual *Node) bool {
if (*expected).GetNodeType() != (*actual).GetNodeType() {
return false
}
switch nodeType := (*expected).GetNodeType(); nodeType {
case NodeTypeNumber:
return (*expected).(NumberNode).Value == (*actual).(NumberNode).Value
case NodeTypeBoolean:
return (*expected).(BooleanNode).Value == (*actual).(BooleanNode).Value
case NodeTypeString:
return (*expected).(StringNode).Value == (*actual).(StringNode).Value
case NodeTypeArray:
expectedElements := (*expected).(ArrayNode).Elements
actualElements := (*actual).(ArrayNode).Elements
if len(expectedElements) != len(actualElements) {
return false
}
for i := 0; i < len(expectedElements); i++ {
if !areAstsEqual(t, expectedElements[i], actualElements[i]) {
return false
}
}
return true
case NodeTypeProperty:
expectedProperty := (*expected).(PropertyNode)
actualProperty := (*actual).(PropertyNode)
return expectedProperty.Key == actualProperty.Key &&
areAstsEqual(t, expectedProperty.Value, actualProperty.Value)
case NodeTypeObject:
expectedProperties := (*expected).(ObjectNode).Properties
actualProperties := (*actual).(ObjectNode).Properties
if len(expectedProperties) != len(actualProperties) {
return false
}
for i := 0; i < len(expectedProperties); i++ {
var expectedProperty Node = *expectedProperties[i]
var actualProperty Node = *actualProperties[i]
if !areAstsEqual(t, &expectedProperty, &actualProperty) {
return false
}
}
return true
}
t.Fatal("areAstsEqual: none of the switch cases matched.")
return false
}
func assertAstsEqual(t *testing.T, json string, expected *Node) {
tokens, lexOk := Lex(json)
if !lexOk {
t.Fatal("Lexing failed.")
}
actual, parseOk := Parse(tokens)
if !parseOk {
t.Fatal("Parsing failed.")
}
if !areAstsEqual(t, expected, actual) {
// If this happens, use the debugger to see how `expected` and `actual`
// differ
t.Fatal("ASTs are not equal.")
}
}
func TestParseNumber(t *testing.T) {
var expected Node = NumberNode{12.3}
assertAstsEqual(t, "12.3", &expected)
}
func TestParseInvalidConsecutiveValues(t *testing.T) {
assertParsingFails(t, "12.3.2")
assertParsingFails(t, "12.3false")
assertParsingFails(t, "12.3 false")
assertParsingFails(t, `12."foo"`)
}
func TestParseBoolean(t *testing.T) {
var expected Node = BooleanNode{true}
assertAstsEqual(t, "true", &expected)
expected = BooleanNode{false}
assertAstsEqual(t, "false", &expected)
}
func TestParseString(t *testing.T) {
var expected Node = StringNode{"foo bar"}
assertAstsEqual(t, `"foo bar"`, &expected)
}
func TestParseArray(t *testing.T) {
var expected Node = ArrayNode{[]*Node{}}
assertAstsEqual(t, `[]`, &expected)
var el0 Node = NumberNode{7}
expected = ArrayNode{[]*Node{
&el0,
}}
assertAstsEqual(t, `[7]`, &expected)
el0 = BooleanNode{false}
var el1 Node = NumberNode{2}
var el2 Node = StringNode{"foo"}
expected = ArrayNode{[]*Node{
&el0, &el1, &el2,
}}
assertAstsEqual(t, `[false, 2, "foo"]`, &expected)
}
func TestParseObject(t *testing.T) {
var expected Node = ObjectNode{[]*PropertyNode{}}
assertAstsEqual(t, `{}`, &expected)
var value0 Node = NumberNode{0}
expected = ObjectNode{[]*PropertyNode{
{"a", &value0},
}}
assertAstsEqual(t, `{ "a": 0 }`, &expected)
var element0 Node = StringNode{"foo"}
var value1 Node = ArrayNode{[]*Node{&element0}}
expected = ObjectNode{[]*PropertyNode{
{"a", &value0},
{"b", &value1},
}}
assertAstsEqual(t, `{ "a": 0, "b": ["foo"] }`, &expected)
}
func TestParseNested(t *testing.T) {
json := `{ "array0": [{ "array1": [13]}] }`
var number13 Node = NumberNode{13}
var array1 Node = ArrayNode{[]*Node{
&number13,
}}
var object1 Node = ObjectNode{[]*PropertyNode{
{"array1", &array1},
}}
var array0 Node = ArrayNode{[]*Node{&object1}}
var expected Node = ObjectNode{[]*PropertyNode{
{"array0", &array0},
}}
assertAstsEqual(t, json, &expected)
}