X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=t%2F9_jsondump_00_base.t;fp=t%2F9_jsondump_00_base.t;h=a124a4f60d1e3d5ec0a3a14f56f9cf037e7f52ca;hb=d710d6cd21be21c0ab2df3566c2bd61d9015cac6;hp=0000000000000000000000000000000000000000;hpb=d2b7c937e86e6e8c4b4193e9f4a8da075919b4fd;p=chaz%2Fp5-CGI-Ex diff --git a/t/9_jsondump_00_base.t b/t/9_jsondump_00_base.t new file mode 100644 index 0000000..a124a4f --- /dev/null +++ b/t/9_jsondump_00_base.t @@ -0,0 +1,109 @@ +# -*- Mode: Perl; -*- + +=head1 NAME + +9_jsondump_00_base.t - Testing of the CGI::Ex::JSONDump module. + +=cut + +use strict; +use Test::More tests => 49; + +use_ok('CGI::Ex::JSONDump'); + +ok(eval { CGI::Ex::JSONDump->import('JSONDump'); 1 }, "Import JSONDump"); + +ok(&JSONDump, "Got the sub"); + +my $obj = CGI::Ex::JSONDump->new; + +ok(JSONDump({a => 1}) eq $obj->dump({a => 1}), "Function and OO Match"); + +ok($obj->dump("foo") eq $obj->js_escape("foo"), "js_escape works"); + +sub test_dump { + my $data = shift; + my $str = shift; + my $args = shift || {}; + my ($sub, $file, $line) = caller; + + my $out = JSONDump($data, $args); + + if ($out eq $str) { + ok(1, "Dump matched at line $line"); + } else { + ok(0, "Didn't match at line $line - shouldv'e been" + ."\n---------------------\n" + . $str + ."\n---------------------\n" + ."Was" + ."\n---------------------\n" + . $out + ."\n---------------------\n" + ); + } +} + +###----------------------------------------------------------------### + +test_dump({a => 1}, "{\n \"a\" : 1\n}", {pretty => 1}); +test_dump({a => 1}, "{\"a\":1}", {pretty => 0}); + +test_dump([1, 2, 3], "[\n 1,\n 2,\n 3\n]", {pretty => 1}); +test_dump([1, 2, 3], "[1,2,3]", {pretty => 0}); + +test_dump({a => [1,2]}, "{\"a\":[1,2]}", {pretty => 0}); +test_dump({a => [1,2]}, "{\n \"a\" : [\n 1,\n 2\n ]\n}", {pretty => 1}); + +test_dump({a => sub {}}, "{}", {pretty => 0}); +test_dump({a => sub {}}, "{\"a\":\"CODE\"}", {handle_unknown_types => sub {my $self=shift;return $self->js_escape(ref shift)}, pretty => 0}); + +test_dump({a => 1}, "{}", {skip_keys => ['a']}); +test_dump({a => 1}, "{}", {skip_keys => {a=>1}}); + +test_dump({2 => 1, _a => 1}, "{2:1,\"_a\":1}", {pretty=>0}); +test_dump({2 => 1, _a => 1}, "{2:1}", {pretty=>0, skip_keys_qr => qr/^_/}); + +test_dump({a => 1}, "{\n \"a\" : 1\n}", {pretty => 1}); +test_dump({a => 1}, "{\n \"a\" : 1\n}", {pretty => 1, hash_nl => "\n", hash_sep => " : ", indent => " "}); +test_dump({a => 1}, "{\n\"a\" : 1\n}", {pretty => 1, hash_nl => "\n", hash_sep => " : ", indent => ""}); +test_dump({a => 1}, "{\"a\" : 1}", {pretty => 1, hash_nl => "", hash_sep => " : ", indent => ""}); +test_dump({a => 1}, "{\"a\":1}", {pretty => 1, hash_nl => "", hash_sep => ":", indent => ""}); +test_dump({a => 1}, "{\"a\":1}", {pretty => 0, hash_nl => "\n", hash_sep => " : "}); + +test_dump(['a' => 1], "[\n \"a\",\n 1\n]", {pretty => 1}); +test_dump(['a' => 1], "[\n \"a\",\n 1\n]", {pretty => 1, array_nl => "\n", indent => " "}); +test_dump(['a' => 1], "[\n\"a\",\n1\n]", {pretty => 1, array_nl => "\n", indent => ""}); +test_dump(['a' => 1], "[\"a\",1]", {pretty => 1, array_nl => "", indent => ""}); +test_dump(['a' => 1], "[\"a\",1]", {pretty => 0, array_nl => "\n"}); + + + +test_dump(1, "1"); +test_dump('1.0', '"1.0"'); +test_dump('123456789012345', '"123456789012345"'); +test_dump('a', '"a"'); +test_dump("\n", '"\\n"'); +test_dump("\\", '"\\\\"'); +test_dump('