forked from crystal-lang/crystal
/
pull_parser_spec.cr
332 lines (284 loc) · 7.71 KB
/
pull_parser_spec.cr
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
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
require "spec"
require "json"
class JSON::PullParser
def assert(event_kind : Symbol)
kind.should eq(event_kind)
read_next
end
def assert(value : Nil)
kind.should eq(:null)
read_next
end
def assert(value : Int)
kind.should eq(:int)
int_value.should eq(value)
read_next
end
def assert(value : Float)
kind.should eq(:float)
float_value.should eq(value)
read_next
end
def assert(value : Bool)
kind.should eq(:bool)
bool_value.should eq(value)
read_next
end
def assert(value : String)
kind.should eq(:string)
string_value.should eq(value)
read_next
end
def assert(value : String)
kind.should eq(:string)
string_value.should eq(value)
read_next
yield
end
def assert(array : Array)
assert_array do
array.each do |x|
assert x.raw
end
end
end
def assert(hash : Hash)
assert_object do
hash.each do |key, value|
assert(key) do
assert value.raw
end
end
end
end
def assert_array
kind.should eq(:begin_array)
read_next
yield
kind.should eq(:end_array)
read_next
end
def assert_array
assert_array { }
end
def assert_object
kind.should eq(:begin_object)
read_next
yield
kind.should eq(:end_object)
read_next
end
def assert_object
assert_object { }
end
def assert_error
expect_raises JSON::ParseException do
read_next
end
end
end
private def assert_pull_parse(string)
it "parses #{string}" do
parser = JSON::PullParser.new string
parser.assert JSON.parse(string).raw
parser.kind.should eq(:EOF)
end
end
private def assert_pull_parse_error(string)
it "errors on #{string}" do
expect_raises JSON::ParseException do
parser = JSON::PullParser.new string
while parser.kind != :EOF
parser.read_next
end
end
end
end
private def assert_raw(string, file = __FILE__, line = __LINE__)
it "parses raw #{string.inspect}", file, line do
pull = JSON::PullParser.new(string)
pull.read_raw.should eq(string)
end
end
describe JSON::PullParser do
assert_pull_parse "null"
assert_pull_parse "false"
assert_pull_parse "true"
assert_pull_parse "1"
assert_pull_parse "1.5"
assert_pull_parse %("hello")
assert_pull_parse "[]"
assert_pull_parse "[[]]"
assert_pull_parse "[1]"
assert_pull_parse "[1.5]"
assert_pull_parse "[null]"
assert_pull_parse "[true]"
assert_pull_parse "[false]"
assert_pull_parse %(["hello"])
assert_pull_parse "[1, 2]"
assert_pull_parse "{}"
assert_pull_parse %({"foo": 1})
assert_pull_parse %({"foo": "bar"})
assert_pull_parse %({"foo": [1, 2]})
assert_pull_parse %({"foo": 1, "bar": 2})
assert_pull_parse %({"foo": "foo1", "bar": "bar1"})
assert_pull_parse_error "[null 2]"
assert_pull_parse_error "[false 2]"
assert_pull_parse_error "[true 2]"
assert_pull_parse_error "[1 2]"
assert_pull_parse_error "[1.5 2]"
assert_pull_parse_error %(["hello" 2])
assert_pull_parse_error "[,1]"
assert_pull_parse_error "[}]"
assert_pull_parse_error "["
assert_pull_parse_error %({,"foo": 1})
assert_pull_parse_error "[]]"
assert_pull_parse_error "{}}"
assert_pull_parse_error %({"foo",1})
assert_pull_parse_error %({"foo"::1})
assert_pull_parse_error %(["foo":1])
assert_pull_parse_error %({"foo": []:1})
assert_pull_parse_error "[[]"
assert_pull_parse_error %({"foo": {})
assert_pull_parse_error %({"name": "John", "age", 1})
assert_pull_parse_error %({"name": "John", "age": "foo", "bar"})
it "prevents stack overflow for arrays" do
parser = JSON::PullParser.new(("[" * 513) + ("]" * 513))
expect_raises JSON::ParseException, "Nesting of 513 is too deep" do
while true
break if parser.kind == :EOF
parser.read_next
end
end
end
it "prevents stack overflow for hashes" do
parser = JSON::PullParser.new((%({"x": ) * 513) + ("}" * 513))
expect_raises JSON::ParseException, "Nesting of 513 is too deep" do
while true
break if parser.kind == :EOF
parser.read_next
end
end
end
# Prevent too deep nesting (prevents stack overflow)
assert_pull_parse_error(("[" * 513) + ("]" * 513))
assert_pull_parse_error(("{" * 513) + ("}" * 513))
describe "skip" do
[
{"null", "null"},
{"bool", "false"},
{"int", "3"},
{"float", "3.5"},
{"string", %("hello")},
{"array", %([10, 20, [30], [40]])},
{"object", %({"foo": [1, 2], "bar": {"baz": [3]}})},
].each do |(desc, obj)|
it "skips #{desc}" do
pull = JSON::PullParser.new("[1, #{obj}, 2]")
pull.read_array do
pull.read_int.should eq(1)
pull.skip
pull.read_int.should eq(2)
end
end
end
end
it "reads bool or null" do
JSON::PullParser.new("null").read_bool_or_null.should be_nil
JSON::PullParser.new("false").read_bool_or_null.should be_false
end
it "reads int or null" do
JSON::PullParser.new("null").read_int_or_null.should be_nil
JSON::PullParser.new("1").read_int_or_null.should eq(1)
end
it "reads float or null" do
JSON::PullParser.new("null").read_float_or_null.should be_nil
JSON::PullParser.new("1.5").read_float_or_null.should eq(1.5)
end
it "reads string or null" do
JSON::PullParser.new("null").read_string_or_null.should be_nil
JSON::PullParser.new(%("hello")).read_string_or_null.should eq("hello")
end
it "reads array or null" do
JSON::PullParser.new("null").read_array_or_null { fail "expected block not to be called" }
pull = JSON::PullParser.new(%([1]))
pull.read_array_or_null do
pull.read_int.should eq(1)
end
end
it "reads object or null" do
JSON::PullParser.new("null").read_object_or_null { fail "expected block not to be called" }
pull = JSON::PullParser.new(%({"foo": 1}))
pull.read_object_or_null do |key|
key.should eq("foo")
pull.read_int.should eq(1)
end
end
describe "on key" do
it "finds key" do
pull = JSON::PullParser.new(%({"foo": 1, "bar": 2}))
bar = nil
pull.on_key("bar") do
bar = pull.read_int
end
bar.should eq(2)
end
it "finds key" do
pull = JSON::PullParser.new(%({"foo": 1, "bar": 2}))
bar = nil
pull.on_key("bar") do
bar = pull.read_int
end
bar.should eq(2)
end
it "doesn't find key" do
pull = JSON::PullParser.new(%({"foo": 1, "baz": 2}))
bar = nil
pull.on_key("bar") do
bar = pull.read_int
end
bar.should be_nil
end
it "finds key with bang" do
pull = JSON::PullParser.new(%({"foo": 1, "bar": 2}))
bar = nil
pull.on_key!("bar") do
bar = pull.read_int
end
bar.should eq(2)
end
it "doesn't find key with bang" do
pull = JSON::PullParser.new(%({"foo": 1, "baz": 2}))
expect_raises Exception, "JSON key not found: bar" do
pull.on_key!("bar") do
end
end
end
it "reads float when it is an int" do
pull = JSON::PullParser.new(%(1))
f = pull.read_float
f.should be_a(Float64)
f.should eq(1.0)
end
["1", "[1]", %({"x": [1]})].each do |value|
it "yields all keys when skipping #{value}" do
pull = JSON::PullParser.new(%({"foo": #{value}, "bar": 2}))
pull.read_object do |key|
key.should_not eq("")
pull.skip
end
end
end
end
describe "raw" do
assert_raw "null"
assert_raw "true"
assert_raw "false"
assert_raw "1234"
assert_raw "1234.5678"
assert_raw %("hello")
assert_raw %([1,"hello",true,false,null,[1,2,3]])
assert_raw %({"foo":[1,2,{"bar":[1,"hello",true,false,1.5]}]})
assert_raw %({"foo":"bar"})
end
end