authorgravatar for Lewis@scphillips.comLewis Gaul <Lewis@scphillips.com> 2021-04-05 00:27:47+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-04-04 19:27:47-04:00
log7302b096bd6e4274080eab051f6f3e1ab49213b9
tree0f9f8178bf508a86967f436efc3fa95de08c4330
parentc9ffb6f734567ecb2a368d0d8786e9d1365f42b8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Tidy-up in json test module (#8431)

* Switch json testing 'roundTrip()' to use FixedBufferStream, improve error handling, remove comptime from param * Add 'try' to calls to roundTrip() that can now return an error * Remove comptime from params in json testing, replace expect(false) with letting error propagate * Add 'try' to calls to ok() that can now return an error Co-authored-by: Lewis Gaul <legaul@cisco.com>

1 files changed, 78 insertions(+), 78 deletions(-)

lib/std/json/test.zig+78-78
...@@ -12,7 +12,7 @@ const std = @import("../std.zig");...@@ -12,7 +12,7 @@ const std = @import("../std.zig");
12const json = std.json;12const json = std.json;
13const testing = std.testing;13const testing = std.testing;
1414
15fn testNonStreaming(comptime s: []const u8) !void {15fn testNonStreaming(s: []const u8) !void {
16 var p = json.Parser.init(testing.allocator, false);16 var p = json.Parser.init(testing.allocator, false);
17 defer p.deinit();17 defer p.deinit();
1818
...@@ -20,32 +20,32 @@ fn testNonStreaming(comptime s: []const u8) !void {...@@ -20,32 +20,32 @@ fn testNonStreaming(comptime s: []const u8) !void {
20 defer tree.deinit();20 defer tree.deinit();
21}21}
2222
23fn ok(comptime s: []const u8) void {23fn ok(s: []const u8) !void {
24 testing.expect(json.validate(s));24 testing.expect(json.validate(s));
2525
26 testNonStreaming(s) catch testing.expect(false);26 try testNonStreaming(s);
27}27}
2828
29fn err(comptime s: []const u8) void {29fn err(s: []const u8) void {
30 testing.expect(!json.validate(s));30 testing.expect(!json.validate(s));
3131
32 testNonStreaming(s) catch return;32 testNonStreaming(s) catch return;
33 testing.expect(false);33 testing.expect(false);
34}34}
3535
36fn utf8Error(comptime s: []const u8) void {36fn utf8Error(s: []const u8) void {
37 testing.expect(!json.validate(s));37 testing.expect(!json.validate(s));
3838
39 testing.expectError(error.InvalidUtf8Byte, testNonStreaming(s));39 testing.expectError(error.InvalidUtf8Byte, testNonStreaming(s));
40}40}
4141
42fn any(comptime s: []const u8) void {42fn any(s: []const u8) void {
43 _ = json.validate(s);43 _ = json.validate(s);
4444
45 testNonStreaming(s) catch {};45 testNonStreaming(s) catch {};
46}46}
4747
48fn anyStreamingErrNonStreaming(comptime s: []const u8) void {48fn anyStreamingErrNonStreaming(s: []const u8) void {
49 _ = json.validate(s);49 _ = json.validate(s);
5050
51 testNonStreaming(s) catch return;51 testNonStreaming(s) catch return;
...@@ -81,7 +81,7 @@ test "y_trailing_comma_after_empty" {...@@ -81,7 +81,7 @@ test "y_trailing_comma_after_empty" {
81////////////////////////////////////////////////////////////////////////////////////////////////////81////////////////////////////////////////////////////////////////////////////////////////////////////
8282
83test "y_array_arraysWithSpaces" {83test "y_array_arraysWithSpaces" {
84 ok(84 try ok(
85 \\[[] ]85 \\[[] ]
86 );86 );
87}87}
...@@ -111,7 +111,7 @@ test "y_array_false" {...@@ -111,7 +111,7 @@ test "y_array_false" {
111}111}
112112
113test "y_array_heterogeneous" {113test "y_array_heterogeneous" {
114 ok(114 try ok(
115 \\[null, 1, "1", {}]115 \\[null, 1, "1", {}]
116 );116 );
117}117}
...@@ -123,14 +123,14 @@ test "y_array_null" {...@@ -123,14 +123,14 @@ test "y_array_null" {
123}123}
124124
125test "y_array_with_1_and_newline" {125test "y_array_with_1_and_newline" {
126 ok(126 try ok(
127 \\[1127 \\[1
128 \\]128 \\]
129 );129 );
130}130}
131131
132test "y_array_with_leading_space" {132test "y_array_with_leading_space" {
133 ok(133 try ok(
134 \\ [1]134 \\ [1]
135 );135 );
136}136}
...@@ -142,47 +142,47 @@ test "y_array_with_several_null" {...@@ -142,47 +142,47 @@ test "y_array_with_several_null" {
142}142}
143143
144test "y_array_with_trailing_space" {144test "y_array_with_trailing_space" {
145 ok("[2] ");145 try ok("[2] ");
146}146}
147147
148test "y_number_0e+1" {148test "y_number_0e+1" {
149 ok(149 try ok(
150 \\[0e+1]150 \\[0e+1]
151 );151 );
152}152}
153153
154test "y_number_0e1" {154test "y_number_0e1" {
155 ok(155 try ok(
156 \\[0e1]156 \\[0e1]
157 );157 );
158}158}
159159
160test "y_number_after_space" {160test "y_number_after_space" {
161 ok(161 try ok(
162 \\[ 4]162 \\[ 4]
163 );163 );
164}164}
165165
166test "y_number_double_close_to_zero" {166test "y_number_double_close_to_zero" {
167 ok(167 try ok(
168 \\[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001]168 \\[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001]
169 );169 );
170}170}
171171
172test "y_number_int_with_exp" {172test "y_number_int_with_exp" {
173 ok(173 try ok(
174 \\[20e1]174 \\[20e1]
175 );175 );
176}176}
177177
178test "y_number" {178test "y_number" {
179 ok(179 try ok(
180 \\[123e65]180 \\[123e65]
181 );181 );
182}182}
183183
184test "y_number_minus_zero" {184test "y_number_minus_zero" {
185 ok(185 try ok(
186 \\[-0]186 \\[-0]
187 );187 );
188}188}
...@@ -200,49 +200,49 @@ test "y_number_negative_one" {...@@ -200,49 +200,49 @@ test "y_number_negative_one" {
200}200}
201201
202test "y_number_negative_zero" {202test "y_number_negative_zero" {
203 ok(203 try ok(
204 \\[-0]204 \\[-0]
205 );205 );
206}206}
207207
208test "y_number_real_capital_e" {208test "y_number_real_capital_e" {
209 ok(209 try ok(
210 \\[1E22]210 \\[1E22]
211 );211 );
212}212}
213213
214test "y_number_real_capital_e_neg_exp" {214test "y_number_real_capital_e_neg_exp" {
215 ok(215 try ok(
216 \\[1E-2]216 \\[1E-2]
217 );217 );
218}218}
219219
220test "y_number_real_capital_e_pos_exp" {220test "y_number_real_capital_e_pos_exp" {
221 ok(221 try ok(
222 \\[1E+2]222 \\[1E+2]
223 );223 );
224}224}
225225
226test "y_number_real_exponent" {226test "y_number_real_exponent" {
227 ok(227 try ok(
228 \\[123e45]228 \\[123e45]
229 );229 );
230}230}
231231
232test "y_number_real_fraction_exponent" {232test "y_number_real_fraction_exponent" {
233 ok(233 try ok(
234 \\[123.456e78]234 \\[123.456e78]
235 );235 );
236}236}
237237
238test "y_number_real_neg_exp" {238test "y_number_real_neg_exp" {
239 ok(239 try ok(
240 \\[1e-2]240 \\[1e-2]
241 );241 );
242}242}
243243
244test "y_number_real_pos_exponent" {244test "y_number_real_pos_exponent" {
245 ok(245 try ok(
246 \\[1e+2]246 \\[1e+2]
247 );247 );
248}248}
...@@ -254,7 +254,7 @@ test "y_number_simple_int" {...@@ -254,7 +254,7 @@ test "y_number_simple_int" {
254}254}
255255
256test "y_number_simple_real" {256test "y_number_simple_real" {
257 ok(257 try ok(
258 \\[123.456789]258 \\[123.456789]
259 );259 );
260}260}
...@@ -266,13 +266,13 @@ test "y_object_basic" {...@@ -266,13 +266,13 @@ test "y_object_basic" {
266}266}
267267
268test "y_object_duplicated_key_and_value" {268test "y_object_duplicated_key_and_value" {
269 ok(269 try ok(
270 \\{"a":"b","a":"b"}270 \\{"a":"b","a":"b"}
271 );271 );
272}272}
273273
274test "y_object_duplicated_key" {274test "y_object_duplicated_key" {
275 ok(275 try ok(
276 \\{"a":"b","a":"c"}276 \\{"a":"b","a":"c"}
277 );277 );
278}278}
...@@ -290,25 +290,25 @@ test "y_object_empty_key" {...@@ -290,25 +290,25 @@ test "y_object_empty_key" {
290}290}
291291
292test "y_object_escaped_null_in_key" {292test "y_object_escaped_null_in_key" {
293 ok(293 try ok(
294 \\{"foo\u0000bar": 42}294 \\{"foo\u0000bar": 42}
295 );295 );
296}296}
297297
298test "y_object_extreme_numbers" {298test "y_object_extreme_numbers" {
299 ok(299 try ok(
300 \\{ "min": -1.0e+28, "max": 1.0e+28 }300 \\{ "min": -1.0e+28, "max": 1.0e+28 }
301 );301 );
302}302}
303303
304test "y_object" {304test "y_object" {
305 ok(305 try ok(
306 \\{"asd":"sdf", "dfg":"fgh"}306 \\{"asd":"sdf", "dfg":"fgh"}
307 );307 );
308}308}
309309
310test "y_object_long_strings" {310test "y_object_long_strings" {
311 ok(311 try ok(
312 \\{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}312 \\{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
313 );313 );
314}314}
...@@ -320,13 +320,13 @@ test "y_object_simple" {...@@ -320,13 +320,13 @@ test "y_object_simple" {
320}320}
321321
322test "y_object_string_unicode" {322test "y_object_string_unicode" {
323 ok(323 try ok(
324 \\{"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" }324 \\{"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" }
325 );325 );
326}326}
327327
328test "y_object_with_newlines" {328test "y_object_with_newlines" {
329 ok(329 try ok(
330 \\{330 \\{
331 \\"a": "b"331 \\"a": "b"
332 \\}332 \\}
...@@ -334,31 +334,31 @@ test "y_object_with_newlines" {...@@ -334,31 +334,31 @@ test "y_object_with_newlines" {
334}334}
335335
336test "y_string_1_2_3_bytes_UTF-8_sequences" {336test "y_string_1_2_3_bytes_UTF-8_sequences" {
337 ok(337 try ok(
338 \\["\u0060\u012a\u12AB"]338 \\["\u0060\u012a\u12AB"]
339 );339 );
340}340}
341341
342test "y_string_accepted_surrogate_pair" {342test "y_string_accepted_surrogate_pair" {
343 ok(343 try ok(
344 \\["\uD801\udc37"]344 \\["\uD801\udc37"]
345 );345 );
346}346}
347347
348test "y_string_accepted_surrogate_pairs" {348test "y_string_accepted_surrogate_pairs" {
349 ok(349 try ok(
350 \\["\ud83d\ude39\ud83d\udc8d"]350 \\["\ud83d\ude39\ud83d\udc8d"]
351 );351 );
352}352}
353353
354test "y_string_allowed_escapes" {354test "y_string_allowed_escapes" {
355 ok(355 try ok(
356 \\["\"\\\/\b\f\n\r\t"]356 \\["\"\\\/\b\f\n\r\t"]
357 );357 );
358}358}
359359
360test "y_string_backslash_and_u_escaped_zero" {360test "y_string_backslash_and_u_escaped_zero" {
361 ok(361 try ok(
362 \\["\\u0000"]362 \\["\\u0000"]
363 );363 );
364}364}
...@@ -370,13 +370,13 @@ test "y_string_backslash_doublequotes" {...@@ -370,13 +370,13 @@ test "y_string_backslash_doublequotes" {
370}370}
371371
372test "y_string_comments" {372test "y_string_comments" {
373 ok(373 try ok(
374 \\["a/*b*/c/*d//e"]374 \\["a/*b*/c/*d//e"]
375 );375 );
376}376}
377377
378test "y_string_double_escape_a" {378test "y_string_double_escape_a" {
379 ok(379 try ok(
380 \\["\\a"]380 \\["\\a"]
381 );381 );
382}382}
...@@ -388,79 +388,79 @@ test "y_string_double_escape_n" {...@@ -388,79 +388,79 @@ test "y_string_double_escape_n" {
388}388}
389389
390test "y_string_escaped_control_character" {390test "y_string_escaped_control_character" {
391 ok(391 try ok(
392 \\["\u0012"]392 \\["\u0012"]
393 );393 );
394}394}
395395
396test "y_string_escaped_noncharacter" {396test "y_string_escaped_noncharacter" {
397 ok(397 try ok(
398 \\["\uFFFF"]398 \\["\uFFFF"]
399 );399 );
400}400}
401401
402test "y_string_in_array" {402test "y_string_in_array" {
403 ok(403 try ok(
404 \\["asd"]404 \\["asd"]
405 );405 );
406}406}
407407
408test "y_string_in_array_with_leading_space" {408test "y_string_in_array_with_leading_space" {
409 ok(409 try ok(
410 \\[ "asd"]410 \\[ "asd"]
411 );411 );
412}412}
413413
414test "y_string_last_surrogates_1_and_2" {414test "y_string_last_surrogates_1_and_2" {
415 ok(415 try ok(
416 \\["\uDBFF\uDFFF"]416 \\["\uDBFF\uDFFF"]
417 );417 );
418}418}
419419
420test "y_string_nbsp_uescaped" {420test "y_string_nbsp_uescaped" {
421 ok(421 try ok(
422 \\["new\u00A0line"]422 \\["new\u00A0line"]
423 );423 );
424}424}
425425
426test "y_string_nonCharacterInUTF-8_U+10FFFF" {426test "y_string_nonCharacterInUTF-8_U+10FFFF" {
427 ok(427 try ok(
428 \\["􏿿"]428 \\["􏿿"]
429 );429 );
430}430}
431431
432test "y_string_nonCharacterInUTF-8_U+FFFF" {432test "y_string_nonCharacterInUTF-8_U+FFFF" {
433 ok(433 try ok(
434 \\["￿"]434 \\["￿"]
435 );435 );
436}436}
437437
438test "y_string_null_escape" {438test "y_string_null_escape" {
439 ok(439 try ok(
440 \\["\u0000"]440 \\["\u0000"]
441 );441 );
442}442}
443443
444test "y_string_one-byte-utf-8" {444test "y_string_one-byte-utf-8" {
445 ok(445 try ok(
446 \\["\u002c"]446 \\["\u002c"]
447 );447 );
448}448}
449449
450test "y_string_pi" {450test "y_string_pi" {
451 ok(451 try ok(
452 \\["π"]452 \\["π"]
453 );453 );
454}454}
455455
456test "y_string_reservedCharacterInUTF-8_U+1BFFF" {456test "y_string_reservedCharacterInUTF-8_U+1BFFF" {
457 ok(457 try ok(
458 \\["𛿿"]458 \\["𛿿"]
459 );459 );
460}460}
461461
462test "y_string_simple_ascii" {462test "y_string_simple_ascii" {
463 ok(463 try ok(
464 \\["asd "]464 \\["asd "]
465 );465 );
466}466}
...@@ -472,115 +472,115 @@ test "y_string_space" {...@@ -472,115 +472,115 @@ test "y_string_space" {
472}472}
473473
474test "y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" {474test "y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" {
475 ok(475 try ok(
476 \\["\uD834\uDd1e"]476 \\["\uD834\uDd1e"]
477 );477 );
478}478}
479479
480test "y_string_three-byte-utf-8" {480test "y_string_three-byte-utf-8" {
481 ok(481 try ok(
482 \\["\u0821"]482 \\["\u0821"]
483 );483 );
484}484}
485485
486test "y_string_two-byte-utf-8" {486test "y_string_two-byte-utf-8" {
487 ok(487 try ok(
488 \\["\u0123"]488 \\["\u0123"]
489 );489 );
490}490}
491491
492test "y_string_u+2028_line_sep" {492test "y_string_u+2028_line_sep" {
493 ok("[\"\xe2\x80\xa8\"]");493 try ok("[\"\xe2\x80\xa8\"]");
494}494}
495495
496test "y_string_u+2029_par_sep" {496test "y_string_u+2029_par_sep" {
497 ok("[\"\xe2\x80\xa9\"]");497 try ok("[\"\xe2\x80\xa9\"]");
498}498}
499499
500test "y_string_uescaped_newline" {500test "y_string_uescaped_newline" {
501 ok(501 try ok(
502 \\["new\u000Aline"]502 \\["new\u000Aline"]
503 );503 );
504}504}
505505
506test "y_string_uEscape" {506test "y_string_uEscape" {
507 ok(507 try ok(
508 \\["\u0061\u30af\u30EA\u30b9"]508 \\["\u0061\u30af\u30EA\u30b9"]
509 );509 );
510}510}
511511
512test "y_string_unescaped_char_delete" {512test "y_string_unescaped_char_delete" {
513 ok("[\"\x7f\"]");513 try ok("[\"\x7f\"]");
514}514}
515515
516test "y_string_unicode_2" {516test "y_string_unicode_2" {
517 ok(517 try ok(
518 \\["⍂㈴⍂"]518 \\["⍂㈴⍂"]
519 );519 );
520}520}
521521
522test "y_string_unicodeEscapedBackslash" {522test "y_string_unicodeEscapedBackslash" {
523 ok(523 try ok(
524 \\["\u005C"]524 \\["\u005C"]
525 );525 );
526}526}
527527
528test "y_string_unicode_escaped_double_quote" {528test "y_string_unicode_escaped_double_quote" {
529 ok(529 try ok(
530 \\["\u0022"]530 \\["\u0022"]
531 );531 );
532}532}
533533
534test "y_string_unicode" {534test "y_string_unicode" {
535 ok(535 try ok(
536 \\["\uA66D"]536 \\["\uA66D"]
537 );537 );
538}538}
539539
540test "y_string_unicode_U+10FFFE_nonchar" {540test "y_string_unicode_U+10FFFE_nonchar" {
541 ok(541 try ok(
542 \\["\uDBFF\uDFFE"]542 \\["\uDBFF\uDFFE"]
543 );543 );
544}544}
545545
546test "y_string_unicode_U+1FFFE_nonchar" {546test "y_string_unicode_U+1FFFE_nonchar" {
547 ok(547 try ok(
548 \\["\uD83F\uDFFE"]548 \\["\uD83F\uDFFE"]
549 );549 );
550}550}
551551
552test "y_string_unicode_U+200B_ZERO_WIDTH_SPACE" {552test "y_string_unicode_U+200B_ZERO_WIDTH_SPACE" {
553 ok(553 try ok(
554 \\["\u200B"]554 \\["\u200B"]
555 );555 );
556}556}
557557
558test "y_string_unicode_U+2064_invisible_plus" {558test "y_string_unicode_U+2064_invisible_plus" {
559 ok(559 try ok(
560 \\["\u2064"]560 \\["\u2064"]
561 );561 );
562}562}
563563
564test "y_string_unicode_U+FDD0_nonchar" {564test "y_string_unicode_U+FDD0_nonchar" {
565 ok(565 try ok(
566 \\["\uFDD0"]566 \\["\uFDD0"]
567 );567 );
568}568}
569569
570test "y_string_unicode_U+FFFE_nonchar" {570test "y_string_unicode_U+FFFE_nonchar" {
571 ok(571 try ok(
572 \\["\uFFFE"]572 \\["\uFFFE"]
573 );573 );
574}574}
575575
576test "y_string_utf8" {576test "y_string_utf8" {
577 ok(577 try ok(
578 \\["€𝄞"]578 \\["€𝄞"]
579 );579 );
580}580}
581581
582test "y_string_with_del_character" {582test "y_string_with_del_character" {
583 ok("[\"a\x7fa\"]");583 try ok("[\"a\x7fa\"]");
584}584}
585585
586test "y_structure_lonely_false" {586test "y_structure_lonely_false" {
...@@ -596,7 +596,7 @@ test "y_structure_lonely_int" {...@@ -596,7 +596,7 @@ test "y_structure_lonely_int" {
596}596}
597597
598test "y_structure_lonely_negative_real" {598test "y_structure_lonely_negative_real" {
599 ok(599 try ok(
600 \\-0.1600 \\-0.1
601 );601 );
602}602}
...@@ -638,7 +638,7 @@ test "y_structure_true_in_array" {...@@ -638,7 +638,7 @@ test "y_structure_true_in_array" {
638}638}
639639
640test "y_structure_whitespace_array" {640test "y_structure_whitespace_array" {
641 ok(" [] ");641 try ok(" [] ");
642}642}
643643
644////////////////////////////////////////////////////////////////////////////////////////////////////644////////////////////////////////////////////////////////////////////////////////////////////////////