2 # Copyright (C) 2001-2007, The Perl Foundation.
7 use lib qw( t . lib ../lib ../../lib );
10 use Parrot::Test tests => 60;
14 t/compilers/json/to_parrot.t - test JSON to parrot conversions
18 % prove t/compilers/json/to_parrot.t
22 Tests JSON->Parrot conversions.
26 json_dump_is( <<'JSON', <<'OUT', 'empty string' );
32 json_dump_is( <<'JSON', <<'OUT', 'string' );
38 json_isnt( 'a', 'strings need quotes' );
40 json_dump_is( <<'JSON', <<'OUT', 'string with backslash "' );
46 json_dump_is( <<'JSON', <<'OUT', 'string with backslash \\' );
52 json_dump_is( <<'JSON', <<'OUT', 'string with /' );
58 json_dump_is( <<'JSON', <<'OUT', 'string with backslash b' );
64 json_dump_is( <<'JSON', <<'OUT', 'string with backslash f' );
70 json_dump_is( <<'JSON', <<'OUT', 'string with backslash n' );
76 json_dump_is( <<'JSON', <<'OUT', 'string with backslash r' );
82 json_dump_is( <<'JSON', <<'OUT', 'string with backslash t' );
88 json_dump_is( <<'JSON', <<'OUT', 'number int' );
94 json_dump_is( <<'JSON', <<'OUT', 'number int minus' );
100 json_dump_is( <<'JSON', <<'OUT', 'number int frac' );
106 json_dump_is( <<'JSON', <<'OUT', 'number int frac minus' );
112 json_dump_is( <<'JSON', <<'OUT', 'number int exp' );
118 json_dump_is( <<'JSON', <<'OUT', 'number int exp' );
124 json_dump_is( <<'JSON', <<'OUT', 'number int exp minus' );
130 json_dump_is( <<'JSON', <<'OUT', 'number int exp minus' );
136 json_dump_is( <<'JSON', <<'OUT', 'number int frac exp' );
142 json_dump_is( <<'JSON', <<'OUT', 'number int frac exp minus' );
148 json_dump_is( <<'JSON', <<'OUT', 'null' );
154 json_dump_is( <<'JSON', <<'OUT', 'true' );
160 json_dump_is( <<'JSON', <<'OUT', 'false' );
166 json_dump_is( <<'JSON', <<'OUT', 'empty array' );
169 "JSON" => ResizablePMCArray (size:0) [
173 json_dump_is( <<'JSON', <<'OUT', 'simple array' );
176 "JSON" => ResizablePMCArray (size:3) [
183 json_dump_is( <<'JSON', <<'OUT', 'simple array (check white spaces)' );
186 "JSON" => ResizablePMCArray (size:3) [
193 json_dump_is( <<'JSON', <<'OUT', 'array of empty arrays' );
196 "JSON" => ResizablePMCArray (size:3) [
197 ResizablePMCArray (size:0) [
199 ResizablePMCArray (size:0) [
201 ResizablePMCArray (size:0) [
206 json_dump_is( <<'JSON', <<'OUT', 'array of empty arrays (check white spaces)' );
209 "JSON" => ResizablePMCArray (size:3) [
210 ResizablePMCArray (size:0) [
212 ResizablePMCArray (size:0) [
214 ResizablePMCArray (size:0) [
219 json_dump_is( <<'JSON', <<'OUT', 'array of arrays of integers' );
220 [[1,2,3],[1,2,3],[1,2,3]]
222 "JSON" => ResizablePMCArray (size:3) [
223 ResizablePMCArray (size:3) [
228 ResizablePMCArray (size:3) [
233 ResizablePMCArray (size:3) [
241 json_dump_is( <<'JSON', <<'OUT', 'array of empty strings' );
244 "JSON" => ResizablePMCArray (size:3) [
251 json_dump_is( <<'JSON', <<'OUT', 'array of strings' );
252 ["string a","string b","string c"]
254 "JSON" => ResizablePMCArray (size:3) [
261 json_dump_is( <<'JSON', <<'OUT', 'array of empty objects' );
264 "JSON" => ResizablePMCArray (size:3) [
274 json_dump_is( <<'JSON', <<'OUT', 'array of empty objects (check white spaces)' );
277 "JSON" => ResizablePMCArray (size:3) [
287 json_dump_is( <<'JSON', <<'OUT', 'array of objects with one element' );
288 [{"one":1},{"two":2},{"three":3}]
290 "JSON" => ResizablePMCArray (size:3) [
303 json_dump_is( <<'JSON', <<'OUT', 'array of objects with one element (white space check)' );
304 [ { "one" : 1 } , { "two" : 2 } , {"three" : 3} ]
306 "JSON" => ResizablePMCArray (size:3) [
319 json_dump_is( <<'JSON', <<'OUT', 'array of objects with multiple elements' );
320 [{"one":1,"two":2,"three":3},{"one":1,"two":2,"three":3},{"one":1,"two":2,"three":3}]
322 "JSON" => ResizablePMCArray (size:3) [
342 <<'JSON', <<'OUT', 'array of boolean objects' );
345 "JSON" => ResizablePMCArray (size:3) [
352 json_dump_is( <<'JSON', <<'OUT', 'empty object' );
359 json_dump_is( <<'JSON', <<'OUT', 'object with one element' );
367 json_dump_is( <<'JSON', <<'OUT', 'object with numbers' );
368 {"one":1,"two":2,"three":3}
377 json_dump_is( <<'JSON', <<'OUT', 'object with strings' );
378 {"one":"string a","two":"string b","three":"string c"}
382 "three" => "string c",
387 json_dump_is( <<'JSON', <<'OUT', 'object with strings (white space check)' );
388 { "one" : "string a" , "two" : "string b" , "three" : "string c" }
392 "three" => "string c",
397 json_dump_is( <<'JSON', <<'OUT', 'object with one empty object' );
406 json_dump_is( <<'JSON', <<'OUT', 'object with one object with one element' );
416 json_dump_is( <<'JSON', <<'OUT', 'object with one object of various element with integers' );
417 {"one":{"one":1,"two":2,"three":3}}
428 json_dump_is( <<'JSON', <<'OUT', 'object with one object of various element with strings' );
429 {"one":{"one":"string a","two":"string b","three":"string c"}}
434 "three" => "string c",
441 <<'JSON', <<'OUT', 'object with one object of various element with strings (check white spaces)' );
442 { "one" : { "one" : "string a" , "two" : "string b" , "three" : "string c" } }
447 "three" => "string c",
453 json_dump_is( <<'JSON', <<'OUT', 'object with more than one empty object' );
454 {"one":{},"two":{},"three":{}}
466 json_dump_is( <<'JSON', <<'OUT', 'object with more than one object with one integer element' );
467 {"one":{"one":1},"two":{"two":2},"three":{"three":3}}
482 json_dump_is( <<'JSON', <<'OUT', 'object with more than one object with various integer elements' );
483 {"one":{"one":1,"two":2,"three":3},"two":{"one":1,"two":2,"three":3},"three":{"one":1,"two":2,"three":3}}
504 json_dump_is( <<'JSON', <<'OUT', 'object with more than one object with one string element' );
505 {"one":{"one":"string a"},"two":{"two":"string b"},"three":{"three":"string c"}}
512 "three" => "string c"
520 json_dump_is( <<'JSON', <<'OUT', 'object with more than one object with various integer elements' );
521 {"one":{"one":"string a","two":"string b","three":"string c"},"two":{"one":"string a","two":"string b","three":"string c"},"three":{"one":"string a","two":"string b","three":"string c"}}
526 "three" => "string c",
531 "three" => "string c",
536 "three" => "string c",
542 json_dump_is( <<'JSON', <<'OUT', 'object with empty array' );
546 "one" => ResizablePMCArray (size:0) [
551 json_dump_is( <<'JSON', <<'OUT', 'object with array' );
555 "one" => ResizablePMCArray (size:3) [
563 json_dump_is( <<'JSON', <<'OUT', 'object with various arrays' );
564 {"one":[1,2,3],"two":[1,2,3],"three":[1,2,3]}
567 "one" => ResizablePMCArray (size:3) [
572 "three" => ResizablePMCArray (size:3) [
577 "two" => ResizablePMCArray (size:3) [
585 json_dump_is( <<'JSON', <<'OUT', 'object with boolean values' );
586 {"one":true,"two":false,"three":null}
595 json_dump_is( <<'JSON', <<'OUT', 'example taken from the RFC' );
596 {"Image":{"Width":800,"Height":600,"Title":"View from 15th Floor","Thumbnail":{"Url":"http://www.example.com/image/481989943","Height":125,"Width":"100"},"IDs":[116, 943, 234, 38793]}}
601 "IDs" => ResizablePMCArray (size:4) [
607 "Thumbnail" => Hash {
609 "Url" => "http://www.example.com/image/481989943",
612 "Title" => "View from 15th Floor",
619 <<'JSON', <<'OUT', 'another example taken from the RFC', todo => 'check number precision' );
620 [{"precision":"zip","Latitude":37.7668,"Longitude":-122.3959,"Address":"","City":"SAN FRANCISCO","State":"CA","Zip":"94107","Country":"US"},{"precision":"zip","Latitude":37.371991,"Longitude":-122.026020,"Address":"","City":"SUNNYVALE", "State":"CA", "Zip":"94085", "Country":"US"}]
622 "JSON" => ResizablePMCArray (size:2) [
625 "City" => "SAN FRANCISCO",
627 "Latitude" => 37.7668,
628 "Longitude" => -122.3959,
635 "City" => "SUNNYVALE",
637 "Latitude" => 37.371991,
638 "Longitude" => -122.026020,
646 json_dump_is( <<'JSON', <<'OUT', 'random object/array example' );
647 [[],[{},{}],{"str":true},{"a":"1","b":{},"c":"2"},[true],false,{"e":[],"d":3}]
649 "JSON" => ResizablePMCArray (size:7) [
650 ResizablePMCArray (size:0) [
652 ResizablePMCArray (size:2) [
667 ResizablePMCArray (size:1) [
673 "e" => ResizablePMCArray (size:0) [
679 # RT#44443 Need many more tests, exercising all aspects of http://www.json.org/
682 my ( $code, $dumped, $reason, %args ) = @_;
685 $code =~ s{("|\\)}{\\$1}g;
686 $code =~ s{\n}{\\n}g;
688 return pir_output_is( <<"END_PIR", $dumped, $reason, %args );
691 load_bytecode 'compilers/json/JSON.pbc'
692 load_bytecode 'library/dumper.pbc'
694 .local pmc JSON, result
695 JSON = compreg "JSON"
696 result = JSON("$code")
697 _dumper(result, "JSON")
704 my ( $code, $reason, %args ) = @_;
706 $code =~ s{("|\\)}{\\$1}g;
707 $code =~ s{\n}{\\n}g;
709 return pir_error_output_like( <<"END_PIR", qr/not a valid JSON value/, $reason, %args );
712 load_bytecode 'compilers/json/JSON.pbc'
714 .local pmc JSON, result
715 JSON = compreg "JSON"
716 result = JSON("$code")
724 # cperl-indent-level: 4
727 # vim: expandtab shiftwidth=4: