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
signature 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");
1212const json = std.json;
1313const testing = std.testing;
1414
15fn testNonStreaming(comptime s: []const u8) !void {
15fn testNonStreaming(s: []const u8) !void {
1616 var p = json.Parser.init(testing.allocator, false);
1717 defer p.deinit();
1818
......@@ -20,32 +20,32 @@ fn testNonStreaming(comptime s: []const u8) !void {
2020 defer tree.deinit();
2121}
2222
23fn ok(comptime s: []const u8) void {
23fn ok(s: []const u8) !void {
2424 testing.expect(json.validate(s));
2525
26 testNonStreaming(s) catch testing.expect(false);
26 try testNonStreaming(s);
2727}
2828
29fn err(comptime s: []const u8) void {
29fn err(s: []const u8) void {
3030 testing.expect(!json.validate(s));
3131
3232 testNonStreaming(s) catch return;
3333 testing.expect(false);
3434}
3535
36fn utf8Error(comptime s: []const u8) void {
36fn utf8Error(s: []const u8) void {
3737 testing.expect(!json.validate(s));
3838
3939 testing.expectError(error.InvalidUtf8Byte, testNonStreaming(s));
4040}
4141
42fn any(comptime s: []const u8) void {
42fn any(s: []const u8) void {
4343 _ = json.validate(s);
4444
4545 testNonStreaming(s) catch {};
4646}
4747
48fn anyStreamingErrNonStreaming(comptime s: []const u8) void {
48fn anyStreamingErrNonStreaming(s: []const u8) void {
4949 _ = json.validate(s);
5050
5151 testNonStreaming(s) catch return;
......@@ -81,7 +81,7 @@ test "y_trailing_comma_after_empty" {
8181////////////////////////////////////////////////////////////////////////////////////////////////////
8282
8383test "y_array_arraysWithSpaces" {
84 ok(
84 try ok(
8585 \\[[] ]
8686 );
8787}
......@@ -111,7 +111,7 @@ test "y_array_false" {
111111}
112112
113113test "y_array_heterogeneous" {
114 ok(
114 try ok(
115115 \\[null, 1, "1", {}]
116116 );
117117}
......@@ -123,14 +123,14 @@ test "y_array_null" {
123123}
124124
125125test "y_array_with_1_and_newline" {
126 ok(
126 try ok(
127127 \\[1
128128 \\]
129129 );
130130}
131131
132132test "y_array_with_leading_space" {
133 ok(
133 try ok(
134134 \\ [1]
135135 );
136136}
......@@ -142,47 +142,47 @@ test "y_array_with_several_null" {
142142}
143143
144144test "y_array_with_trailing_space" {
145 ok("[2] ");
145 try ok("[2] ");
146146}
147147
148148test "y_number_0e+1" {
149 ok(
149 try ok(
150150 \\[0e+1]
151151 );
152152}
153153
154154test "y_number_0e1" {
155 ok(
155 try ok(
156156 \\[0e1]
157157 );
158158}
159159
160160test "y_number_after_space" {
161 ok(
161 try ok(
162162 \\[ 4]
163163 );
164164}
165165
166166test "y_number_double_close_to_zero" {
167 ok(
167 try ok(
168168 \\[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001]
169169 );
170170}
171171
172172test "y_number_int_with_exp" {
173 ok(
173 try ok(
174174 \\[20e1]
175175 );
176176}
177177
178178test "y_number" {
179 ok(
179 try ok(
180180 \\[123e65]
181181 );
182182}
183183
184184test "y_number_minus_zero" {
185 ok(
185 try ok(
186186 \\[-0]
187187 );
188188}
......@@ -200,49 +200,49 @@ test "y_number_negative_one" {
200200}
201201
202202test "y_number_negative_zero" {
203 ok(
203 try ok(
204204 \\[-0]
205205 );
206206}
207207
208208test "y_number_real_capital_e" {
209 ok(
209 try ok(
210210 \\[1E22]
211211 );
212212}
213213
214214test "y_number_real_capital_e_neg_exp" {
215 ok(
215 try ok(
216216 \\[1E-2]
217217 );
218218}
219219
220220test "y_number_real_capital_e_pos_exp" {
221 ok(
221 try ok(
222222 \\[1E+2]
223223 );
224224}
225225
226226test "y_number_real_exponent" {
227 ok(
227 try ok(
228228 \\[123e45]
229229 );
230230}
231231
232232test "y_number_real_fraction_exponent" {
233 ok(
233 try ok(
234234 \\[123.456e78]
235235 );
236236}
237237
238238test "y_number_real_neg_exp" {
239 ok(
239 try ok(
240240 \\[1e-2]
241241 );
242242}
243243
244244test "y_number_real_pos_exponent" {
245 ok(
245 try ok(
246246 \\[1e+2]
247247 );
248248}
......@@ -254,7 +254,7 @@ test "y_number_simple_int" {
254254}
255255
256256test "y_number_simple_real" {
257 ok(
257 try ok(
258258 \\[123.456789]
259259 );
260260}
......@@ -266,13 +266,13 @@ test "y_object_basic" {
266266}
267267
268268test "y_object_duplicated_key_and_value" {
269 ok(
269 try ok(
270270 \\{"a":"b","a":"b"}
271271 );
272272}
273273
274274test "y_object_duplicated_key" {
275 ok(
275 try ok(
276276 \\{"a":"b","a":"c"}
277277 );
278278}
......@@ -290,25 +290,25 @@ test "y_object_empty_key" {
290290}
291291
292292test "y_object_escaped_null_in_key" {
293 ok(
293 try ok(
294294 \\{"foo\u0000bar": 42}
295295 );
296296}
297297
298298test "y_object_extreme_numbers" {
299 ok(
299 try ok(
300300 \\{ "min": -1.0e+28, "max": 1.0e+28 }
301301 );
302302}
303303
304304test "y_object" {
305 ok(
305 try ok(
306306 \\{"asd":"sdf", "dfg":"fgh"}
307307 );
308308}
309309
310310test "y_object_long_strings" {
311 ok(
311 try ok(
312312 \\{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
313313 );
314314}
......@@ -320,13 +320,13 @@ test "y_object_simple" {
320320}
321321
322322test "y_object_string_unicode" {
323 ok(
323 try ok(
324324 \\{"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" }
325325 );
326326}
327327
328328test "y_object_with_newlines" {
329 ok(
329 try ok(
330330 \\{
331331 \\"a": "b"
332332 \\}
......@@ -334,31 +334,31 @@ test "y_object_with_newlines" {
334334}
335335
336336test "y_string_1_2_3_bytes_UTF-8_sequences" {
337 ok(
337 try ok(
338338 \\["\u0060\u012a\u12AB"]
339339 );
340340}
341341
342342test "y_string_accepted_surrogate_pair" {
343 ok(
343 try ok(
344344 \\["\uD801\udc37"]
345345 );
346346}
347347
348348test "y_string_accepted_surrogate_pairs" {
349 ok(
349 try ok(
350350 \\["\ud83d\ude39\ud83d\udc8d"]
351351 );
352352}
353353
354354test "y_string_allowed_escapes" {
355 ok(
355 try ok(
356356 \\["\"\\\/\b\f\n\r\t"]
357357 );
358358}
359359
360360test "y_string_backslash_and_u_escaped_zero" {
361 ok(
361 try ok(
362362 \\["\\u0000"]
363363 );
364364}
......@@ -370,13 +370,13 @@ test "y_string_backslash_doublequotes" {
370370}
371371
372372test "y_string_comments" {
373 ok(
373 try ok(
374374 \\["a/*b*/c/*d//e"]
375375 );
376376}
377377
378378test "y_string_double_escape_a" {
379 ok(
379 try ok(
380380 \\["\\a"]
381381 );
382382}
......@@ -388,79 +388,79 @@ test "y_string_double_escape_n" {
388388}
389389
390390test "y_string_escaped_control_character" {
391 ok(
391 try ok(
392392 \\["\u0012"]
393393 );
394394}
395395
396396test "y_string_escaped_noncharacter" {
397 ok(
397 try ok(
398398 \\["\uFFFF"]
399399 );
400400}
401401
402402test "y_string_in_array" {
403 ok(
403 try ok(
404404 \\["asd"]
405405 );
406406}
407407
408408test "y_string_in_array_with_leading_space" {
409 ok(
409 try ok(
410410 \\[ "asd"]
411411 );
412412}
413413
414414test "y_string_last_surrogates_1_and_2" {
415 ok(
415 try ok(
416416 \\["\uDBFF\uDFFF"]
417417 );
418418}
419419
420420test "y_string_nbsp_uescaped" {
421 ok(
421 try ok(
422422 \\["new\u00A0line"]
423423 );
424424}
425425
426426test "y_string_nonCharacterInUTF-8_U+10FFFF" {
427 ok(
427 try ok(
428428 \\["􏿿"]
429429 );
430430}
431431
432432test "y_string_nonCharacterInUTF-8_U+FFFF" {
433 ok(
433 try ok(
434434 \\["￿"]
435435 );
436436}
437437
438438test "y_string_null_escape" {
439 ok(
439 try ok(
440440 \\["\u0000"]
441441 );
442442}
443443
444444test "y_string_one-byte-utf-8" {
445 ok(
445 try ok(
446446 \\["\u002c"]
447447 );
448448}
449449
450450test "y_string_pi" {
451 ok(
451 try ok(
452452 \\["π"]
453453 );
454454}
455455
456456test "y_string_reservedCharacterInUTF-8_U+1BFFF" {
457 ok(
457 try ok(
458458 \\["𛿿"]
459459 );
460460}
461461
462462test "y_string_simple_ascii" {
463 ok(
463 try ok(
464464 \\["asd "]
465465 );
466466}
......@@ -472,115 +472,115 @@ test "y_string_space" {
472472}
473473
474474test "y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" {
475 ok(
475 try ok(
476476 \\["\uD834\uDd1e"]
477477 );
478478}
479479
480480test "y_string_three-byte-utf-8" {
481 ok(
481 try ok(
482482 \\["\u0821"]
483483 );
484484}
485485
486486test "y_string_two-byte-utf-8" {
487 ok(
487 try ok(
488488 \\["\u0123"]
489489 );
490490}
491491
492492test "y_string_u+2028_line_sep" {
493 ok("[\"\xe2\x80\xa8\"]");
493 try ok("[\"\xe2\x80\xa8\"]");
494494}
495495
496496test "y_string_u+2029_par_sep" {
497 ok("[\"\xe2\x80\xa9\"]");
497 try ok("[\"\xe2\x80\xa9\"]");
498498}
499499
500500test "y_string_uescaped_newline" {
501 ok(
501 try ok(
502502 \\["new\u000Aline"]
503503 );
504504}
505505
506506test "y_string_uEscape" {
507 ok(
507 try ok(
508508 \\["\u0061\u30af\u30EA\u30b9"]
509509 );
510510}
511511
512512test "y_string_unescaped_char_delete" {
513 ok("[\"\x7f\"]");
513 try ok("[\"\x7f\"]");
514514}
515515
516516test "y_string_unicode_2" {
517 ok(
517 try ok(
518518 \\["⍂㈴⍂"]
519519 );
520520}
521521
522522test "y_string_unicodeEscapedBackslash" {
523 ok(
523 try ok(
524524 \\["\u005C"]
525525 );
526526}
527527
528528test "y_string_unicode_escaped_double_quote" {
529 ok(
529 try ok(
530530 \\["\u0022"]
531531 );
532532}
533533
534534test "y_string_unicode" {
535 ok(
535 try ok(
536536 \\["\uA66D"]
537537 );
538538}
539539
540540test "y_string_unicode_U+10FFFE_nonchar" {
541 ok(
541 try ok(
542542 \\["\uDBFF\uDFFE"]
543543 );
544544}
545545
546546test "y_string_unicode_U+1FFFE_nonchar" {
547 ok(
547 try ok(
548548 \\["\uD83F\uDFFE"]
549549 );
550550}
551551
552552test "y_string_unicode_U+200B_ZERO_WIDTH_SPACE" {
553 ok(
553 try ok(
554554 \\["\u200B"]
555555 );
556556}
557557
558558test "y_string_unicode_U+2064_invisible_plus" {
559 ok(
559 try ok(
560560 \\["\u2064"]
561561 );
562562}
563563
564564test "y_string_unicode_U+FDD0_nonchar" {
565 ok(
565 try ok(
566566 \\["\uFDD0"]
567567 );
568568}
569569
570570test "y_string_unicode_U+FFFE_nonchar" {
571 ok(
571 try ok(
572572 \\["\uFFFE"]
573573 );
574574}
575575
576576test "y_string_utf8" {
577 ok(
577 try ok(
578578 \\["€𝄞"]
579579 );
580580}
581581
582582test "y_string_with_del_character" {
583 ok("[\"a\x7fa\"]");
583 try ok("[\"a\x7fa\"]");
584584}
585585
586586test "y_structure_lonely_false" {
......@@ -596,7 +596,7 @@ test "y_structure_lonely_int" {
596596}
597597
598598test "y_structure_lonely_negative_real" {
599 ok(
599 try ok(
600600 \\-0.1
601601 );
602602}
......@@ -638,7 +638,7 @@ test "y_structure_true_in_array" {
638638}
639639
640640test "y_structure_whitespace_array" {
641 ok(" [] ");
641 try ok(" [] ");
642642}
643643
644644////////////////////////////////////////////////////////////////////////////////////////////////////