diff --git t/compilers/json/from_parrot.t t/compilers/json/from_parrot.t index 170176c..f328141 100644 --- t/compilers/json/from_parrot.t +++ t/compilers/json/from_parrot.t @@ -1,14 +1,7 @@ -#!perl +#!parrot # Copyright (C) 2001-2006, Parrot Foundation. # $Id$ -use strict; -use warnings; -use lib qw( t . lib ../lib ../../lib ); - -use Test::More; -use Parrot::Test tests => 18; - =head1 NAME t/compilers/json/from_parrot.t - test parrot to JSON conversion. @@ -23,137 +16,122 @@ Tests JSON->Parrot conversions. =cut -# no. 1 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of an empty string' ); +.sub main :main + .include 'test_more.pir' + plan(39) + + test_create_json_of_an_empty_string() + test_create_json_of_a_non_empty_string() + test_create_json_of_a_string_with_simple_escapes() + test_create_json_of_some_integers() + test_create_json_of_some_numbers() + test_create_json_of_various_scalars_with_pretty_option() + test_create_json_of_an_array() + test_create_pretty_json_of_an_array() + test_create_json_of_array_keep_element_ordering() + test_create_json_of_a_mixed_array() + test_create_json_of_hash() + test_create_non_pretty_json_of_hash() + test_create_json_of_nested_structure_including_resizablepmcarray_and_empties() + test_create_non_pretty_json_of_nested_structure() + test_create_json_of_string_pmcs() + test_create_json_of_integer_pmcs() + test_create_json_of_boolean_pmcs() + test_create_json_of_null_and_undef() +.end -.sub test :main +# no. 1 +.sub test_create_json_of_an_empty_string .local string s s = '' load_bytecode 'JSON.pbc' $S0 = _json( s, 0 ) - say $S0 + is($S0, '""', 'Create JSON of an empty string') .end -CODE -"" -OUT # no. 2 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of a non-empty string' ); +.sub test_create_json_of_a_non_empty_string -.sub test :main .local string s s = 'abcdeABCDE01234$%^&*' load_bytecode 'JSON.pbc' $S0 = _json( s, 0 ) - say $S0 + is($S0, '"abcdeABCDE01234$%^&*"', 'Create JSON of a non-empty string') .end -CODE -"abcdeABCDE01234$%^&*" -OUT # no. 3 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of a string with simple escapes' ); - -.sub test :main +.sub test_create_json_of_a_string_with_simple_escapes .local string s s = "abcde\\ABCDE\"01234\n$%^&*" # XXX more escapes need to be tested; see http://www.json.org/ load_bytecode 'JSON.pbc' $S0 = _json( s, 0 ) - say $S0 + is($S0, '"abcde\\ABCDE\"01234\n$%^&*"', 'Create JSON of a string with simple escapes') .end -CODE -"abcde\\ABCDE\"01234\n$%^&*" -OUT # no. 4 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of some integers' ); +.sub test_create_json_of_some_integers -.sub test :main .local int i i = 0 load_bytecode 'JSON.pbc' $S0 = _json( i, 0 ) - say $S0 + is($S0, 0, 'Create JSON of some integers') i = 35 $S0 = _json( i, 0 ) - say $S0 + is($S0, 35, 'Create JSON of some integers') i = -42 $S0 = _json( i, 0 ) - say $S0 + is($S0, -42, 'Create JSON of some integers') i = 2147483647 $S0 = _json( i, 0 ) - say $S0 + is($S0, 2147483647, 'Create JSON of some integers') i = -2147483648 $S0 = _json( i, 0 ) - say $S0 + is($S0, -2147483648, 'Create JSON of some integers') .end -CODE -0 -35 --42 -2147483647 --2147483648 -OUT # no. 5 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of some numbers' ); - -.sub test :main +.sub test_create_json_of_some_numbers .local num n n = 0.0 load_bytecode 'JSON.pbc' $S0 = _json( n ) - say $S0 + is($S0, 0, 'Create JSON of some numbers') n = 2.50 $S0 = _json( n ) - say $S0 + is($S0, 2.5, 'Create JSON of some numbers') n = -42.0 $S0 = _json( n ) - say $S0 + is($S0, -42, 'Create JSON of some numbers') n = 4.5e1 $S0 = _json( n ) - say $S0 + is($S0, 45, 'Create JSON of some numbers') .end -CODE -0 -2.5 --42 -45 -OUT # no. 6 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of various scalars with pretty option' ); - -.sub test :main +.sub test_create_json_of_various_scalars_with_pretty_option .local string s s = "abcde\\ABCDE\"01234\n$%^&*" load_bytecode 'JSON.pbc' $S0 = _json( s, 1 ) - print $S0 + is($S0, "\"abcde\\\\ABCDE\\\"01234\\n\$\%\^\&\*\"\n", 'Create JSON of various scalars with pretty option') .local int i i = -42 $S0 = _json( i, 1 ) - print $S0 + is($S0, "-42\n", 'Create JSON of various scalars with pretty option') .local num n n = 2.50 $S0 = _json( n, 1 ) - print $S0 + is($S0, "2.5\n", 'Create JSON of various scalars with pretty option') .end -CODE -"abcde\\ABCDE\"01234\n$%^&*" --42 -2.5 -OUT # no. 7 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of an array' ); - -.sub test :main +.sub test_create_json_of_an_array .local pmc array new array, 'Array' @@ -171,16 +149,11 @@ pir_output_is( <<'CODE', <<'OUT', 'Create JSON of an array' ); load_bytecode 'JSON.pbc' $S0 = _json( array, 0 ) - say $S0 + is($S0, '[0,1,2,3,4,5,6,7,8,9]', 'Create JSON of an array') .end -CODE -[0,1,2,3,4,5,6,7,8,9] -OUT # no. 8 -pir_output_is( <<'CODE', <<'OUT', 'Create pretty JSON of an array' ); - -.sub test :main +.sub test_create_pretty_json_of_an_array .local pmc array new array, 'Array' @@ -198,9 +171,7 @@ pir_output_is( <<'CODE', <<'OUT', 'Create pretty JSON of an array' ); load_bytecode 'JSON.pbc' $S0 = _json( array, 1 ) - print $S0 -.end -CODE + is($S0, <<'OUTPUTPUT', 'Create pretty JSON of an array') [ 0, 1, @@ -213,12 +184,11 @@ CODE 8, 9 ] -OUT +OUTPUTPUT +.end # no. 9 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of array, keep element ordering' ); - -.sub test :main +.sub test_create_json_of_array_keep_element_ordering .local pmc array new array, 'Array' @@ -232,9 +202,7 @@ pir_output_is( <<'CODE', <<'OUT', 'Create JSON of array, keep element ordering' load_bytecode 'JSON.pbc' $S0 = _json( array, 1 ) - print $S0 -.end -CODE + is($S0, <<'OUTPUTPUT', 'Create JSON of an array, keep element ordering') [ 35, 1, @@ -243,12 +211,11 @@ CODE -2147483648, 2147483647 ] -OUT +OUTPUTPUT +.end # no. 10 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of a mixed array' ); - -.sub test :main +.sub test_create_json_of_a_mixed_array .local pmc array new array, 'Array' @@ -262,9 +229,7 @@ pir_output_is( <<'CODE', <<'OUT', 'Create JSON of a mixed array' ); load_bytecode 'JSON.pbc' $S0 = _json( array, 1 ) - print $S0 -.end -CODE + is($S0, <<'OUTPUT', 'Create JSON of a mixed array') [ 0, 15, @@ -273,12 +238,11 @@ CODE "json", 0 ] -OUT +OUTPUT +.end # no. 11 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of hash' ); - -.sub test :main +.sub test_create_json_of_hash .local pmc hash new hash, 'Hash' @@ -289,21 +253,18 @@ pir_output_is( <<'CODE', <<'OUT', 'Create JSON of hash' ); load_bytecode 'JSON.pbc' $S0 = _json( hash, 1 ) - print $S0 -.end -CODE + is($S0, <<'OUTPUT', 'Create JSON of hash') { "alpha" : 29, "beta" : "B", "delta" : "DELTA", "gamma" : 3.1 } -OUT +OUTPUT +.end # no. 12 -pir_output_is( <<'CODE', <<'OUT', 'Create non-pretty JSON of hash' ); - -.sub test :main +.sub test_create_non_pretty_json_of_hash .local pmc hash new hash, 'Hash' @@ -314,17 +275,11 @@ pir_output_is( <<'CODE', <<'OUT', 'Create non-pretty JSON of hash' ); load_bytecode 'JSON.pbc' $S0 = _json( hash, 0 ) - say $S0 + is($S0, '{"alpha":29,"beta":"B","delta":"DELTA","gamma":3.1}', 'Create non-pretty JSON of hash') .end -CODE -{"alpha":29,"beta":"B","delta":"DELTA","gamma":3.1} -OUT # no. 13 -pir_output_is( - <<'CODE', <<'OUT', 'Create JSON of nested structure including ResizablePMCArray and empties' ); - -.sub test :main +.sub test_create_json_of_nested_structure_including_resizablepmcarray_and_empties .local pmc street1, street2, city1, city2, country, world street1 = new 'Hash' @@ -351,9 +306,7 @@ pir_output_is( load_bytecode 'JSON.pbc' $S0 = _json( world, 1 ) - print $S0 -.end -CODE + is($S0, <<'OUTPUT', 'Create JSON of nested structure including ResizablePMCArray and empties') { "population" : 1234567890, "some_country" : [ @@ -370,12 +323,11 @@ CODE ] ] } -OUT +OUTPUT +.end # no. 14 -pir_output_is( <<'CODE', <<'OUT', 'Create non-pretty JSON of nested structure' ); - -.sub test :main +.sub test_create_non_pretty_json_of_nested_structure .local pmc street1, street2, city1, city2, country, world street1 = new 'Hash' @@ -402,125 +354,90 @@ pir_output_is( <<'CODE', <<'OUT', 'Create non-pretty JSON of nested structure' ) load_bytecode 'JSON.pbc' $S0 = _json( world, 0 ) - say $S0 + is($S0, '{"population":1234567890,"some_country":[[{"Perl":"Highway","Python":"Grove","Ruby":"Lane"},{}],[]]}', 'Create non-pretty JSON of nested structure') .end -CODE -{"population":1234567890,"some_country":[[{"Perl":"Highway","Python":"Grove","Ruby":"Lane"},{}],[]]} -OUT # no. 15 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of String PMCs' ); - -.sub test :main +.sub test_create_json_of_string_pmcs .local pmc s s = new 'String' s = '' load_bytecode 'JSON.pbc' $S0 = _json( s, 0 ) - say $S0 + is($S0, '""', 'Create JSON of String PMCs') $S0 = _json( s, 1 ) - print $S0 + is($S0, "\"\"\n", 'Create JSON of String PMCs') s = new 'String' s = "12345\"67890" $S0 = _json( s, 0 ) - say $S0 + is($S0, '"12345\"67890"', 'Create JSON of String PMCs') $S0 = _json( s, 1 ) - print $S0 + is($S0, "\"12345\\\"67890\"\n", 'Create JSON of String PMCs') .end -CODE -"" -"" -"12345\"67890" -"12345\"67890" -OUT # no. 16 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of Integer PMCs' ); - -.sub test :main +.sub test_create_json_of_integer_pmcs .local pmc i i = new 'Integer' i = 0 load_bytecode 'JSON.pbc' $S0 = _json( i, 0 ) - say $S0 + is($S0, 0, 'Create JSON of String PMCs') $S0 = _json( i, 1 ) - print $S0 + is($S0, "0\n", 'Create JSON of String PMCs') i = new 'Integer' i = -42 $S0 = _json( i, 0 ) - say $S0 + is($S0, -42, 'Create JSON of String PMCs') $S0 = _json( i, 1 ) - print $S0 + is($S0, "-42\n", 'Create JSON of String PMCs') .end -CODE -0 -0 --42 --42 -OUT # no. 17 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of Boolean PMCs' ); - -.sub test :main +.sub test_create_json_of_boolean_pmcs .local pmc b b = new 'Boolean' b = 0 load_bytecode 'JSON.pbc' $S0 = _json( b, 0 ) - say $S0 + is($S0, 'false', 'Create JSON of Boolean PMCs') $S0 = _json( b, 1 ) - print $S0 + is($S0, "false\n", 'Create JSON of Boolean PMCs') b = new 'Boolean' b = 1 $S0 = _json( b, 0 ) - say $S0 + is($S0, 'true', 'Create JSON of Boolean PMCs') $S0 = _json( b, 1 ) - print $S0 + is($S0, "true\n", 'Create JSON of Boolean PMCs') .end -CODE -false -false -true -true -OUT # no. 18 -pir_output_is( <<'CODE', <<'OUT', 'Create JSON of null and .Undef' ); - -.sub test :main +.sub test_create_json_of_null_and_undef .local pmc n null n load_bytecode 'JSON.pbc' $S0 = _json( n, 0 ) - say $S0 + is($S0, 'null', 'Create JSON of null and .Undef') $S0 = _json( n, 1 ) - print $S0 + is($S0, "null\n", 'Create JSON of null and .Undef') n = new 'Undef' $S0 = _json( n, 0 ) - say $S0 + is($S0, 'null', 'Create JSON of null and .Undef') $S0 = _json( n, 1 ) - print $S0 + is($S0, "null\n", 'Create JSON of null and .Undef') .end -CODE -null -null -null -null -OUT # Local Variables: # mode: cperl # cperl-indent-level: 4 # fill-column: 100 # End: -# vim: expandtab shiftwidth=4: +# vim: expandtab shiftwidth=4 filetype=pir: