| ... | @@ -52,12 +52,28 @@ fn anyStreamingErrNonStreaming(comptime s: []const u8) void { | ... | @@ -52,12 +52,28 @@ fn anyStreamingErrNonStreaming(comptime s: []const u8) void { |
| 52 | testing.expect(false); | 52 | testing.expect(false); |
| 53 | } | 53 | } |
| 54 | | 54 | |
| | 55 | fn roundTrip(s: []const u8) !void { |
| | 56 | testing.expect(json.validate(s)); |
| | 57 | |
| | 58 | var p = json.Parser.init(testing.allocator, false); |
| | 59 | defer p.deinit(); |
| | 60 | |
| | 61 | var tree = try p.parse(s); |
| | 62 | defer tree.deinit(); |
| | 63 | |
| | 64 | var buf: [256]u8 = undefined; |
| | 65 | var fbs = std.io.fixedBufferStream(&buf); |
| | 66 | try tree.root.jsonStringify(.{}, fbs.writer()); |
| | 67 | |
| | 68 | testing.expectEqualStrings(s, fbs.getWritten()); |
| | 69 | } |
| | 70 | |
| 55 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 71 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 56 | // | 72 | // |
| 57 | // Additional tests not part of test JSONTestSuite. | 73 | // Additional tests not part of test JSONTestSuite. |
| 58 | | 74 | |
| 59 | test "y_trailing_comma_after_empty" { | 75 | test "y_trailing_comma_after_empty" { |
| 60 | ok( | 76 | try roundTrip( |
| 61 | \\{"1":[],"2":{},"3":"4"} | 77 | \\{"1":[],"2":{},"3":"4"} |
| 62 | ); | 78 | ); |
| 63 | } | 79 | } |
| ... | @@ -71,25 +87,25 @@ test "y_array_arraysWithSpaces" { | ... | @@ -71,25 +87,25 @@ test "y_array_arraysWithSpaces" { |
| 71 | } | 87 | } |
| 72 | | 88 | |
| 73 | test "y_array_empty" { | 89 | test "y_array_empty" { |
| 74 | ok( | 90 | try roundTrip( |
| 75 | \\[] | 91 | \\[] |
| 76 | ); | 92 | ); |
| 77 | } | 93 | } |
| 78 | | 94 | |
| 79 | test "y_array_empty-string" { | 95 | test "y_array_empty-string" { |
| 80 | ok( | 96 | try roundTrip( |
| 81 | \\[""] | 97 | \\[""] |
| 82 | ); | 98 | ); |
| 83 | } | 99 | } |
| 84 | | 100 | |
| 85 | test "y_array_ending_with_newline" { | 101 | test "y_array_ending_with_newline" { |
| 86 | ok( | 102 | try roundTrip( |
| 87 | \\["a"] | 103 | \\["a"] |
| 88 | ); | 104 | ); |
| 89 | } | 105 | } |
| 90 | | 106 | |
| 91 | test "y_array_false" { | 107 | test "y_array_false" { |
| 92 | ok( | 108 | try roundTrip( |
| 93 | \\[false] | 109 | \\[false] |
| 94 | ); | 110 | ); |
| 95 | } | 111 | } |
| ... | @@ -101,7 +117,7 @@ test "y_array_heterogeneous" { | ... | @@ -101,7 +117,7 @@ test "y_array_heterogeneous" { |
| 101 | } | 117 | } |
| 102 | | 118 | |
| 103 | test "y_array_null" { | 119 | test "y_array_null" { |
| 104 | ok( | 120 | try roundTrip( |
| 105 | \\[null] | 121 | \\[null] |
| 106 | ); | 122 | ); |
| 107 | } | 123 | } |
| ... | @@ -120,7 +136,7 @@ test "y_array_with_leading_space" { | ... | @@ -120,7 +136,7 @@ test "y_array_with_leading_space" { |
| 120 | } | 136 | } |
| 121 | | 137 | |
| 122 | test "y_array_with_several_null" { | 138 | test "y_array_with_several_null" { |
| 123 | ok( | 139 | try roundTrip( |
| 124 | \\[1,null,null,null,2] | 140 | \\[1,null,null,null,2] |
| 125 | ); | 141 | ); |
| 126 | } | 142 | } |
| ... | @@ -172,13 +188,13 @@ test "y_number_minus_zero" { | ... | @@ -172,13 +188,13 @@ test "y_number_minus_zero" { |
| 172 | } | 188 | } |
| 173 | | 189 | |
| 174 | test "y_number_negative_int" { | 190 | test "y_number_negative_int" { |
| 175 | ok( | 191 | try roundTrip( |
| 176 | \\[-123] | 192 | \\[-123] |
| 177 | ); | 193 | ); |
| 178 | } | 194 | } |
| 179 | | 195 | |
| 180 | test "y_number_negative_one" { | 196 | test "y_number_negative_one" { |
| 181 | ok( | 197 | try roundTrip( |
| 182 | \\[-1] | 198 | \\[-1] |
| 183 | ); | 199 | ); |
| 184 | } | 200 | } |
| ... | @@ -232,7 +248,7 @@ test "y_number_real_pos_exponent" { | ... | @@ -232,7 +248,7 @@ test "y_number_real_pos_exponent" { |
| 232 | } | 248 | } |
| 233 | | 249 | |
| 234 | test "y_number_simple_int" { | 250 | test "y_number_simple_int" { |
| 235 | ok( | 251 | try roundTrip( |
| 236 | \\[123] | 252 | \\[123] |
| 237 | ); | 253 | ); |
| 238 | } | 254 | } |
| ... | @@ -244,7 +260,7 @@ test "y_number_simple_real" { | ... | @@ -244,7 +260,7 @@ test "y_number_simple_real" { |
| 244 | } | 260 | } |
| 245 | | 261 | |
| 246 | test "y_object_basic" { | 262 | test "y_object_basic" { |
| 247 | ok( | 263 | try roundTrip( |
| 248 | \\{"asd":"sdf"} | 264 | \\{"asd":"sdf"} |
| 249 | ); | 265 | ); |
| 250 | } | 266 | } |
| ... | @@ -262,13 +278,13 @@ test "y_object_duplicated_key" { | ... | @@ -262,13 +278,13 @@ test "y_object_duplicated_key" { |
| 262 | } | 278 | } |
| 263 | | 279 | |
| 264 | test "y_object_empty" { | 280 | test "y_object_empty" { |
| 265 | ok( | 281 | try roundTrip( |
| 266 | \\{} | 282 | \\{} |
| 267 | ); | 283 | ); |
| 268 | } | 284 | } |
| 269 | | 285 | |
| 270 | test "y_object_empty_key" { | 286 | test "y_object_empty_key" { |
| 271 | ok( | 287 | try roundTrip( |
| 272 | \\{"":0} | 288 | \\{"":0} |
| 273 | ); | 289 | ); |
| 274 | } | 290 | } |
| ... | @@ -298,7 +314,7 @@ test "y_object_long_strings" { | ... | @@ -298,7 +314,7 @@ test "y_object_long_strings" { |
| 298 | } | 314 | } |
| 299 | | 315 | |
| 300 | test "y_object_simple" { | 316 | test "y_object_simple" { |
| 301 | ok( | 317 | try roundTrip( |
| 302 | \\{"a":[]} | 318 | \\{"a":[]} |
| 303 | ); | 319 | ); |
| 304 | } | 320 | } |
| ... | @@ -348,7 +364,7 @@ test "y_string_backslash_and_u_escaped_zero" { | ... | @@ -348,7 +364,7 @@ test "y_string_backslash_and_u_escaped_zero" { |
| 348 | } | 364 | } |
| 349 | | 365 | |
| 350 | test "y_string_backslash_doublequotes" { | 366 | test "y_string_backslash_doublequotes" { |
| 351 | ok( | 367 | try roundTrip( |
| 352 | \\["\""] | 368 | \\["\""] |
| 353 | ); | 369 | ); |
| 354 | } | 370 | } |
| ... | @@ -366,7 +382,7 @@ test "y_string_double_escape_a" { | ... | @@ -366,7 +382,7 @@ test "y_string_double_escape_a" { |
| 366 | } | 382 | } |
| 367 | | 383 | |
| 368 | test "y_string_double_escape_n" { | 384 | test "y_string_double_escape_n" { |
| 369 | ok( | 385 | try roundTrip( |
| 370 | \\["\\n"] | 386 | \\["\\n"] |
| 371 | ); | 387 | ); |
| 372 | } | 388 | } |
| ... | @@ -450,7 +466,7 @@ test "y_string_simple_ascii" { | ... | @@ -450,7 +466,7 @@ test "y_string_simple_ascii" { |
| 450 | } | 466 | } |
| 451 | | 467 | |
| 452 | test "y_string_space" { | 468 | test "y_string_space" { |
| 453 | ok( | 469 | try roundTrip( |
| 454 | \\" " | 470 | \\" " |
| 455 | ); | 471 | ); |
| 456 | } | 472 | } |
| ... | @@ -568,13 +584,13 @@ test "y_string_with_del_character" { | ... | @@ -568,13 +584,13 @@ test "y_string_with_del_character" { |
| 568 | } | 584 | } |
| 569 | | 585 | |
| 570 | test "y_structure_lonely_false" { | 586 | test "y_structure_lonely_false" { |
| 571 | ok( | 587 | try roundTrip( |
| 572 | \\false | 588 | \\false |
| 573 | ); | 589 | ); |
| 574 | } | 590 | } |
| 575 | | 591 | |
| 576 | test "y_structure_lonely_int" { | 592 | test "y_structure_lonely_int" { |
| 577 | ok( | 593 | try roundTrip( |
| 578 | \\42 | 594 | \\42 |
| 579 | ); | 595 | ); |
| 580 | } | 596 | } |
| ... | @@ -586,37 +602,37 @@ test "y_structure_lonely_negative_real" { | ... | @@ -586,37 +602,37 @@ test "y_structure_lonely_negative_real" { |
| 586 | } | 602 | } |
| 587 | | 603 | |
| 588 | test "y_structure_lonely_null" { | 604 | test "y_structure_lonely_null" { |
| 589 | ok( | 605 | try roundTrip( |
| 590 | \\null | 606 | \\null |
| 591 | ); | 607 | ); |
| 592 | } | 608 | } |
| 593 | | 609 | |
| 594 | test "y_structure_lonely_string" { | 610 | test "y_structure_lonely_string" { |
| 595 | ok( | 611 | try roundTrip( |
| 596 | \\"asd" | 612 | \\"asd" |
| 597 | ); | 613 | ); |
| 598 | } | 614 | } |
| 599 | | 615 | |
| 600 | test "y_structure_lonely_true" { | 616 | test "y_structure_lonely_true" { |
| 601 | ok( | 617 | try roundTrip( |
| 602 | \\true | 618 | \\true |
| 603 | ); | 619 | ); |
| 604 | } | 620 | } |
| 605 | | 621 | |
| 606 | test "y_structure_string_empty" { | 622 | test "y_structure_string_empty" { |
| 607 | ok( | 623 | try roundTrip( |
| 608 | \\"" | 624 | \\"" |
| 609 | ); | 625 | ); |
| 610 | } | 626 | } |
| 611 | | 627 | |
| 612 | test "y_structure_trailing_newline" { | 628 | test "y_structure_trailing_newline" { |
| 613 | ok( | 629 | try roundTrip( |
| 614 | \\["a"] | 630 | \\["a"] |
| 615 | ); | 631 | ); |
| 616 | } | 632 | } |
| 617 | | 633 | |
| 618 | test "y_structure_true_in_array" { | 634 | test "y_structure_true_in_array" { |
| 619 | ok( | 635 | try roundTrip( |
| 620 | \\[true] | 636 | \\[true] |
| 621 | ); | 637 | ); |
| 622 | } | 638 | } |