aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/x/json2/scanner_test.v
blob: 73f4d797d71c630d371862050f16ad34c9d708e6 (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
module json2

fn test_str() {
	mut sc := Scanner{
		text: '"test"'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .str_
	assert tok.lit.len == 4
	assert tok.lit.bytestr() == 'test'
}

fn test_str_valid_unicode_escape() {
	mut sc := Scanner{
		text: r'"\u0048"'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .str_
	assert tok.lit.len == 1
	assert tok.lit.bytestr() == 'H'
}

fn test_str_valid_unicode_escape_2() {
	mut sc := Scanner{
		text: r'"\u2714"'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .str_
	assert tok.lit.len == 3
	assert tok.lit.bytestr() == '✔'
}

fn test_str_invalid_escape() {
	mut sc := Scanner{
		text: r'"\z"'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid backslash escape'
}

fn test_str_invalid_must_be_escape() {
	for char in important_escapable_chars {
		mut sc := Scanner{
			text: [byte(`"`), `t`, char, `"`]
		}
		tok := sc.scan()
		assert tok.kind == .error
		assert tok.lit.bytestr() == 'character must be escaped with a backslash'
	}
}

fn test_str_invalid_unicode_escape() {
	mut sc := Scanner{
		text: r'"\u010G"'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == '`G` is not a hex digit'
}

fn test_str_invalid_unicode_escape_len() {
	mut sc := Scanner{
		text: r'"\u001"'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'unicode escape must have 4 hex digits'
}

fn test_str_invalid_uppercase_u() {
	mut sc := Scanner{
		text: r'"\U0000"'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'unicode endpoints must be in lowercase `u`'
}

fn test_str_missing_closing_bracket() {
	mut sc := Scanner{
		text: '"incomplete'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'missing double quotes in string closing'
}

fn test_int() {
	mut sc := Scanner{
		text: '10'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .int_
	assert tok.lit.len == 2
	assert tok.lit.bytestr() == '10'
}

fn test_int_negative() {
	mut sc := Scanner{
		text: '-10'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .int_
	assert tok.lit.len == 3
	assert tok.lit.bytestr() == '-10'
}

fn test_float() {
	mut sc := Scanner{
		text: '123.400'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .float
	assert tok.lit.len == 7
	assert tok.lit.bytestr() == '123.400'
}

fn test_float_negative() {
	mut sc := Scanner{
		text: '-123.400'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .float
	assert tok.lit.len == 8
	assert tok.lit.bytestr() == '-123.400'
}

fn test_int_exp() {
	mut sc := Scanner{
		text: '1E22'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .int_
	assert tok.lit.len == 4
	assert tok.lit.bytestr() == '1E22'
}

fn test_int_exp_negative() {
	mut sc := Scanner{
		text: '1E-2'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .int_
	assert tok.lit.len == 4
	assert tok.lit.bytestr() == '1E-2'
}

fn test_int_exp_positive() {
	mut sc := Scanner{
		text: '1E+2'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .int_
	assert tok.lit.len == 4
	assert tok.lit.bytestr() == '1E+2'
}

fn test_float_exp() {
	mut sc := Scanner{
		text: '123.456e78'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .float
	assert tok.lit.len == 10
	assert tok.lit.bytestr() == '123.456e78'
}

fn test_float_exp_negative() {
	mut sc := Scanner{
		text: '20.56e-5'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .float
	assert tok.lit.len == 8
	assert tok.lit.bytestr() == '20.56e-5'
}

fn test_float_exp_positive() {
	mut sc := Scanner{
		text: '20.56e+5'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .float
	assert tok.lit.len == 8
	assert tok.lit.bytestr() == '20.56e+5'
}

fn test_number_with_space() {
	mut sc := Scanner{
		text: ' 4'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .int_
	assert tok.lit.len == 1
	assert tok.lit.bytestr() == '4'
}

fn test_number_invalid_leading_zero() {
	mut sc := Scanner{
		text: '0010'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'leading zeroes in a number are not allowed'
}

fn test_number_invalid_leading_zero_negative() {
	mut sc := Scanner{
		text: '-0010'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'leading zeroes in a number are not allowed'
}

fn test_number_invalid_start_char() {
	mut sc := Scanner{
		text: '+1'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid token `+`'
}

fn test_number_invalid_char() {
	mut sc := Scanner{
		text: '122x'.bytes()
	}
	sc.scan()
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid token `x`'
}

fn test_number_invalid_char_float() {
	mut sc := Scanner{
		text: '122x.1'.bytes()
	}
	sc.scan()
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid token `x`'
}

fn test_number_invalid_multiple_dot() {
	mut sc := Scanner{
		text: '122.108.10'.bytes()
	}
	sc.scan()
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid token `.`'
}

fn test_number_invalid_exp() {
	mut sc := Scanner{
		text: '0.3e'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid exponent'
}

fn test_number_invalid_exp_with_sign() {
	mut sc := Scanner{
		text: '0.3e+'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid exponent'
}

fn test_number_invalid_zero_exp() {
	mut sc := Scanner{
		text: '0e'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid exponent'
}

fn test_number_invalid_dot_exp() {
	mut sc := Scanner{
		text: '0.e'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid float'
}

fn test_number_invalid_double_exp() {
	mut sc := Scanner{
		text: '2eE'.bytes()
	}
	sc.scan()
	tok := sc.scan()
	assert tok.kind == .error
	assert tok.lit.bytestr() == 'invalid token `E`'
}

fn test_null() {
	mut sc := Scanner{
		text: 'null'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .null
	assert tok.lit.len == 4
	assert tok.lit.bytestr() == 'null'
}

fn test_bool_true() {
	mut sc := Scanner{
		text: 'true'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .bool_
	assert tok.lit.len == 4
	assert tok.lit.bytestr() == 'true'
}

fn test_bool_false() {
	mut sc := Scanner{
		text: 'false'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .bool_
	assert tok.lit.len == 5
	assert tok.lit.bytestr() == 'false'
}

fn test_json_with_whitespace_start() {
	mut sc := Scanner{
		text: ' \n  \n\t {'.bytes()
	}
	tok := sc.scan()
	eprintln(tok)
	assert tok.kind == .lcbr
	assert tok.lit.len == 0
}

fn test_json_with_whitespace_end() {
	mut sc := Scanner{
		text: '}  \n\t'.bytes()
	}
	tok := sc.scan()
	assert tok.kind == .rcbr
	tok2 := sc.scan()
	eprintln(tok2)
	assert tok2.kind == .eof
}