tagged release 0.7.1
[parrot.git] / t / compilers / json / to_parrot.t
blob75d3f81570a4d32579d6e603d7289b93edd643ac
1 #!perl
2 # Copyright (C) 2001-2007, The Perl Foundation.
3 # $Id$
5 use strict;
6 use warnings;
7 use lib qw( t . lib ../lib ../../lib );
9 use Test::More;
10 use Parrot::Test tests => 60;
12 =head1 NAME
14 t/compilers/json/to_parrot.t - test JSON to parrot conversions
16 =head1 SYNOPSIS
18     % prove t/compilers/json/to_parrot.t
20 =head1 DESCRIPTION
22 Tests JSON->Parrot conversions.
24 =cut
26 json_dump_is( <<'JSON', <<'OUT', 'empty string' );
28 JSON
29 "JSON" => ""
30 OUT
32 json_dump_is( <<'JSON', <<'OUT', 'string' );
33 "json"
34 JSON
35 "JSON" => "json"
36 OUT
38 json_isnt( 'a', 'strings need quotes' );
40 json_dump_is( <<'JSON', <<'OUT', 'string with backslash "' );
41 "\"json\""
42 JSON
43 "JSON" => "\"json\""
44 OUT
46 json_dump_is( <<'JSON', <<'OUT', 'string with backslash \\' );
47 "\\json\\"
48 JSON
49 "JSON" => "\\json\\"
50 OUT
52 json_dump_is( <<'JSON', <<'OUT', 'string with /' );
53 "\/json\/"
54 JSON
55 "JSON" => "/json/"
56 OUT
58 json_dump_is( <<'JSON', <<'OUT', 'string with backslash b' );
59 "\bjson\b"
60 JSON
61 "JSON" => "\bjson\b"
62 OUT
64 json_dump_is( <<'JSON', <<'OUT', 'string with backslash f' );
65 "\fjson\f"
66 JSON
67 "JSON" => "\fjson\f"
68 OUT
70 json_dump_is( <<'JSON', <<'OUT', 'string with backslash n' );
71 "\njson\n"
72 JSON
73 "JSON" => "\njson\n"
74 OUT
76 json_dump_is( <<'JSON', <<'OUT', 'string with backslash r' );
77 "\rjson\r"
78 JSON
79 "JSON" => "\rjson\r"
80 OUT
82 json_dump_is( <<'JSON', <<'OUT', 'string with backslash t' );
83 "\tjson\t"
84 JSON
85 "JSON" => "\tjson\t"
86 OUT
88 json_dump_is( <<'JSON', <<'OUT', 'number int' );
90 JSON
91 "JSON" => 1
92 OUT
94 json_dump_is( <<'JSON', <<'OUT', 'number int minus' );
96 JSON
97 "JSON" => -1
98 OUT
100 json_dump_is( <<'JSON', <<'OUT', 'number int frac' );
101 3.14
102 JSON
103 "JSON" => 3.14
106 json_dump_is( <<'JSON', <<'OUT', 'number int frac minus' );
107 -3.14
108 JSON
109 "JSON" => -3.14
112 json_dump_is( <<'JSON', <<'OUT', 'number int exp' );
113 1e+11
114 JSON
115 "JSON" => 1e+11
118 json_dump_is( <<'JSON', <<'OUT', 'number int exp' );
119 2e-12
120 JSON
121 "JSON" => 2e-12
124 json_dump_is( <<'JSON', <<'OUT', 'number int exp minus' );
125 -1e+11
126 JSON
127 "JSON" => -1e+11
130 json_dump_is( <<'JSON', <<'OUT', 'number int exp minus' );
131 -2e-12
132 JSON
133 "JSON" => -2e-12
136 json_dump_is( <<'JSON', <<'OUT', 'number int frac exp' );
137 3.14e+10
138 JSON
139 "JSON" => 3.14e+10
142 json_dump_is( <<'JSON', <<'OUT', 'number int frac exp minus' );
143 -3.14e+10
144 JSON
145 "JSON" => -3.14e+10
148 json_dump_is( <<'JSON', <<'OUT', 'null' );
149 null
150 JSON
151 "JSON" => null
154 json_dump_is( <<'JSON', <<'OUT', 'true' );
155 true
156 JSON
157 "JSON" => 1
160 json_dump_is( <<'JSON', <<'OUT', 'false' );
161 false
162 JSON
163 "JSON" => 0
166 json_dump_is( <<'JSON', <<'OUT', 'empty array' );
168 JSON
169 "JSON" => ResizablePMCArray (size:0) [
173 json_dump_is( <<'JSON', <<'OUT', 'simple array' );
174 [1,2,3]
175 JSON
176 "JSON" => ResizablePMCArray (size:3) [
177     1,
178     2,
179     3
183 json_dump_is( <<'JSON', <<'OUT', 'simple array (check white spaces)' );
184 [    1 , 2    ,  3   ]
185 JSON
186 "JSON" => ResizablePMCArray (size:3) [
187     1,
188     2,
189     3
193 json_dump_is( <<'JSON', <<'OUT', 'array of empty arrays' );
194 [[],[],[]]
195 JSON
196 "JSON" => ResizablePMCArray (size:3) [
197     ResizablePMCArray (size:0) [
198     ],
199     ResizablePMCArray (size:0) [
200     ],
201     ResizablePMCArray (size:0) [
202     ]
206 json_dump_is( <<'JSON', <<'OUT', 'array of empty arrays (check white spaces)' );
207 [    []  ,  [] , []     ]
208 JSON
209 "JSON" => ResizablePMCArray (size:3) [
210     ResizablePMCArray (size:0) [
211     ],
212     ResizablePMCArray (size:0) [
213     ],
214     ResizablePMCArray (size:0) [
215     ]
219 json_dump_is( <<'JSON', <<'OUT', 'array of arrays of integers' );
220 [[1,2,3],[1,2,3],[1,2,3]]
221 JSON
222 "JSON" => ResizablePMCArray (size:3) [
223     ResizablePMCArray (size:3) [
224         1,
225         2,
226         3
227     ],
228     ResizablePMCArray (size:3) [
229         1,
230         2,
231         3
232     ],
233     ResizablePMCArray (size:3) [
234         1,
235         2,
236         3
237     ]
241 json_dump_is( <<'JSON', <<'OUT', 'array of empty strings' );
242 ["","",""]
243 JSON
244 "JSON" => ResizablePMCArray (size:3) [
245     "",
246     "",
247     ""
251 json_dump_is( <<'JSON', <<'OUT', 'array of strings' );
252 ["string a","string b","string c"]
253 JSON
254 "JSON" => ResizablePMCArray (size:3) [
255     "string a",
256     "string b",
257     "string c"
261 json_dump_is( <<'JSON', <<'OUT', 'array of empty objects' );
262 [{},{},{}]
263 JSON
264 "JSON" => ResizablePMCArray (size:3) [
265     Hash {
266     },
267     Hash {
268     },
269     Hash {
270     }
274 json_dump_is( <<'JSON', <<'OUT', 'array of empty objects (check white spaces)' );
275 [    {} , {}  , {}    ]
276 JSON
277 "JSON" => ResizablePMCArray (size:3) [
278     Hash {
279     },
280     Hash {
281     },
282     Hash {
283     }
287 json_dump_is( <<'JSON', <<'OUT', 'array of objects with one element' );
288 [{"one":1},{"two":2},{"three":3}]
289 JSON
290 "JSON" => ResizablePMCArray (size:3) [
291     Hash {
292         "one" => 1
293     },
294     Hash {
295         "two" => 2
296     },
297     Hash {
298         "three" => 3
299     }
303 json_dump_is( <<'JSON', <<'OUT', 'array of objects with one element (white space check)' );
304 [  { "one"  : 1 }  , {    "two"    :  2 } , {"three"  : 3} ]
305 JSON
306 "JSON" => ResizablePMCArray (size:3) [
307     Hash {
308         "one" => 1
309     },
310     Hash {
311         "two" => 2
312     },
313     Hash {
314         "three" => 3
315     }
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}]
321 JSON
322 "JSON" => ResizablePMCArray (size:3) [
323     Hash {
324         "one" => 1,
325         "three" => 3,
326         "two" => 2
327     },
328     Hash {
329         "one" => 1,
330         "three" => 3,
331         "two" => 2
332     },
333     Hash {
334         "one" => 1,
335         "three" => 3,
336         "two" => 2
337     }
341 json_dump_is(
342     <<'JSON', <<'OUT', 'array of boolean objects' );
343 [false,true,null]
344 JSON
345 "JSON" => ResizablePMCArray (size:3) [
346     0,
347     1,
348     null
352 json_dump_is( <<'JSON', <<'OUT', 'empty object' );
354 JSON
355 "JSON" => Hash {
359 json_dump_is( <<'JSON', <<'OUT', 'object with one element' );
360 {"one":1}
361 JSON
362 "JSON" => Hash {
363     "one" => 1
367 json_dump_is( <<'JSON', <<'OUT', 'object with numbers' );
368 {"one":1,"two":2,"three":3}
369 JSON
370 "JSON" => Hash {
371     "one" => 1,
372     "three" => 3,
373     "two" => 2
377 json_dump_is( <<'JSON', <<'OUT', 'object with strings' );
378 {"one":"string a","two":"string b","three":"string c"}
379 JSON
380 "JSON" => Hash {
381     "one" => "string a",
382     "three" => "string c",
383     "two" => "string b"
387 json_dump_is( <<'JSON', <<'OUT', 'object with strings (white space check)' );
388 {  "one" : "string a"    ,   "two"  :  "string b" , "three"   : "string c"    }
389 JSON
390 "JSON" => Hash {
391     "one" => "string a",
392     "three" => "string c",
393     "two" => "string b"
397 json_dump_is( <<'JSON', <<'OUT', 'object with one empty object' );
398 {"one":{}}
399 JSON
400 "JSON" => Hash {
401     "one" => Hash {
402     }
406 json_dump_is( <<'JSON', <<'OUT', 'object with one object with one element' );
407 {"one":{"one":1}}
408 JSON
409 "JSON" => Hash {
410     "one" => Hash {
411         "one" => 1
412     }
416 json_dump_is( <<'JSON', <<'OUT', 'object with one object of various element with integers' );
417 {"one":{"one":1,"two":2,"three":3}}
418 JSON
419 "JSON" => Hash {
420     "one" => Hash {
421         "one" => 1,
422         "three" => 3,
423         "two" => 2
424     }
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"}}
430 JSON
431 "JSON" => Hash {
432     "one" => Hash {
433         "one" => "string a",
434         "three" => "string c",
435         "two" => "string b"
436     }
440 json_dump_is(
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"   }    }
443 JSON
444 "JSON" => Hash {
445     "one" => Hash {
446         "one" => "string a",
447         "three" => "string c",
448         "two" => "string b"
449     }
453 json_dump_is( <<'JSON', <<'OUT', 'object with more than one empty object' );
454 {"one":{},"two":{},"three":{}}
455 JSON
456 "JSON" => Hash {
457     "one" => Hash {
458     },
459     "three" => Hash {
460     },
461     "two" => Hash {
462     }
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}}
468 JSON
469 "JSON" => Hash {
470     "one" => Hash {
471         "one" => 1
472     },
473     "three" => Hash {
474         "three" => 3
475     },
476     "two" => Hash {
477         "two" => 2
478     }
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}}
484 JSON
485 "JSON" => Hash {
486     "one" => Hash {
487         "one" => 1,
488         "three" => 3,
489         "two" => 2
490     },
491     "three" => Hash {
492         "one" => 1,
493         "three" => 3,
494         "two" => 2
495     },
496     "two" => Hash {
497         "one" => 1,
498         "three" => 3,
499         "two" => 2
500     }
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"}}
506 JSON
507 "JSON" => Hash {
508     "one" => Hash {
509         "one" => "string a"
510     },
511     "three" => Hash {
512         "three" => "string c"
513     },
514     "two" => Hash {
515         "two" => "string b"
516     }
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"}}
522 JSON
523 "JSON" => Hash {
524     "one" => Hash {
525         "one" => "string a",
526         "three" => "string c",
527         "two" => "string b"
528     },
529     "three" => Hash {
530         "one" => "string a",
531         "three" => "string c",
532         "two" => "string b"
533     },
534     "two" => Hash {
535         "one" => "string a",
536         "three" => "string c",
537         "two" => "string b"
538     }
542 json_dump_is( <<'JSON', <<'OUT', 'object with empty array' );
543 {"one":[]}
544 JSON
545 "JSON" => Hash {
546     "one" => ResizablePMCArray (size:0) [
547     ]
551 json_dump_is( <<'JSON', <<'OUT', 'object with array' );
552 {"one":[1,2,3]}
553 JSON
554 "JSON" => Hash {
555     "one" => ResizablePMCArray (size:3) [
556         1,
557         2,
558         3
559     ]
563 json_dump_is( <<'JSON', <<'OUT', 'object with various arrays' );
564 {"one":[1,2,3],"two":[1,2,3],"three":[1,2,3]}
565 JSON
566 "JSON" => Hash {
567     "one" => ResizablePMCArray (size:3) [
568         1,
569         2,
570         3
571     ],
572     "three" => ResizablePMCArray (size:3) [
573         1,
574         2,
575         3
576     ],
577     "two" => ResizablePMCArray (size:3) [
578         1,
579         2,
580         3
581     ]
585 json_dump_is( <<'JSON', <<'OUT', 'object with boolean values' );
586 {"one":true,"two":false,"three":null}
587 JSON
588 "JSON" => Hash {
589     "one" => 1,
590     "three" => null,
591     "two" => 0
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]}}
597 JSON
598 "JSON" => Hash {
599     "Image" => Hash {
600         "Height" => 600,
601         "IDs" => ResizablePMCArray (size:4) [
602             116,
603             943,
604             234,
605             38793
606         ],
607         "Thumbnail" => Hash {
608             "Height" => 125,
609             "Url" => "http://www.example.com/image/481989943",
610             "Width" => "100"
611         },
612         "Title" => "View from 15th Floor",
613         "Width" => 800
614     }
618 json_dump_is(
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"}]
621 JSON
622 "JSON" => ResizablePMCArray (size:2) [
623     Hash {
624         "Address" => "",
625         "City" => "SAN FRANCISCO",
626         "Country" => "US",
627         "Latitude" => 37.7668,
628         "Longitude" => -122.3959,
629         "State" => "CA",
630         "Zip" => "94107",
631         "precision" => "zip"
632     },
633     Hash {
634         "Address" => "",
635         "City" => "SUNNYVALE",
636         "Country" => "US",
637         "Latitude" => 37.371991,
638         "Longitude" => -122.026020,
639         "State" => "CA",
640         "Zip" => "94085",
641         "precision" => "zip"
642     }
646 json_dump_is( <<'JSON', <<'OUT', 'random object/array example' );
647 [[],[{},{}],{"str":true},{"a":"1","b":{},"c":"2"},[true],false,{"e":[],"d":3}]
648 JSON
649 "JSON" => ResizablePMCArray (size:7) [
650     ResizablePMCArray (size:0) [
651     ],
652     ResizablePMCArray (size:2) [
653         Hash {
654         },
655         Hash {
656         }
657     ],
658     Hash {
659         "str" => 1
660     },
661     Hash {
662         "a" => "1",
663         "b" => Hash {
664         },
665         "c" => "2"
666     },
667     ResizablePMCArray (size:1) [
668         1
669     ],
670     0,
671     Hash {
672         "d" => 3,
673         "e" => ResizablePMCArray (size:0) [
674         ]
675     }
679 # RT#44443 Need many more tests, exercising all aspects of http://www.json.org/
681 sub json_dump_is {
682     my ( $code, $dumped, $reason, %args ) = @_;
684     chomp $code;
685     $code =~ s{("|\\)}{\\$1}g;
686     $code =~ s{\n}{\\n}g;
688     return pir_output_is( <<"END_PIR", $dumped, $reason, %args );
690 .sub test :main
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")
698 .end
699 END_PIR
703 sub json_isnt {
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 );
711 .sub test :main
712     load_bytecode 'compilers/json/JSON.pbc'
714     .local pmc JSON, result
715     JSON = compreg "JSON"
716     result = JSON("$code")
717 .end
718 END_PIR
722 # Local Variables:
723 #   mode: cperl
724 #   cperl-indent-level: 4
725 #   fill-column: 100
726 # End:
727 # vim: expandtab shiftwidth=4: