]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/9_jsondump_00_base.t
a124a4f60d1e3d5ec0a3a14f56f9cf037e7f52ca
[chaz/p5-CGI-Ex] / t / 9_jsondump_00_base.t
1 # -*- Mode: Perl; -*-
2
3 =head1 NAME
4
5 9_jsondump_00_base.t - Testing of the CGI::Ex::JSONDump module.
6
7 =cut
8
9 use strict;
10 use Test::More tests => 49;
11
12 use_ok('CGI::Ex::JSONDump');
13
14 ok(eval { CGI::Ex::JSONDump->import('JSONDump'); 1 }, "Import JSONDump");
15
16 ok(&JSONDump, "Got the sub");
17
18 my $obj = CGI::Ex::JSONDump->new;
19
20 ok(JSONDump({a => 1}) eq $obj->dump({a => 1}), "Function and OO Match");
21
22 ok($obj->dump("foo") eq $obj->js_escape("foo"), "js_escape works");
23
24 sub test_dump {
25 my $data = shift;
26 my $str = shift;
27 my $args = shift || {};
28 my ($sub, $file, $line) = caller;
29
30 my $out = JSONDump($data, $args);
31
32 if ($out eq $str) {
33 ok(1, "Dump matched at line $line");
34 } else {
35 ok(0, "Didn't match at line $line - shouldv'e been"
36 ."\n---------------------\n"
37 . $str
38 ."\n---------------------\n"
39 ."Was"
40 ."\n---------------------\n"
41 . $out
42 ."\n---------------------\n"
43 );
44 }
45 }
46
47 ###----------------------------------------------------------------###
48
49 test_dump({a => 1}, "{\n \"a\" : 1\n}", {pretty => 1});
50 test_dump({a => 1}, "{\"a\":1}", {pretty => 0});
51
52 test_dump([1, 2, 3], "[\n 1,\n 2,\n 3\n]", {pretty => 1});
53 test_dump([1, 2, 3], "[1,2,3]", {pretty => 0});
54
55 test_dump({a => [1,2]}, "{\"a\":[1,2]}", {pretty => 0});
56 test_dump({a => [1,2]}, "{\n \"a\" : [\n 1,\n 2\n ]\n}", {pretty => 1});
57
58 test_dump({a => sub {}}, "{}", {pretty => 0});
59 test_dump({a => sub {}}, "{\"a\":\"CODE\"}", {handle_unknown_types => sub {my $self=shift;return $self->js_escape(ref shift)}, pretty => 0});
60
61 test_dump({a => 1}, "{}", {skip_keys => ['a']});
62 test_dump({a => 1}, "{}", {skip_keys => {a=>1}});
63
64 test_dump({2 => 1, _a => 1}, "{2:1,\"_a\":1}", {pretty=>0});
65 test_dump({2 => 1, _a => 1}, "{2:1}", {pretty=>0, skip_keys_qr => qr/^_/});
66
67 test_dump({a => 1}, "{\n \"a\" : 1\n}", {pretty => 1});
68 test_dump({a => 1}, "{\n \"a\" : 1\n}", {pretty => 1, hash_nl => "\n", hash_sep => " : ", indent => " "});
69 test_dump({a => 1}, "{\n\"a\" : 1\n}", {pretty => 1, hash_nl => "\n", hash_sep => " : ", indent => ""});
70 test_dump({a => 1}, "{\"a\" : 1}", {pretty => 1, hash_nl => "", hash_sep => " : ", indent => ""});
71 test_dump({a => 1}, "{\"a\":1}", {pretty => 1, hash_nl => "", hash_sep => ":", indent => ""});
72 test_dump({a => 1}, "{\"a\":1}", {pretty => 0, hash_nl => "\n", hash_sep => " : "});
73
74 test_dump(['a' => 1], "[\n \"a\",\n 1\n]", {pretty => 1});
75 test_dump(['a' => 1], "[\n \"a\",\n 1\n]", {pretty => 1, array_nl => "\n", indent => " "});
76 test_dump(['a' => 1], "[\n\"a\",\n1\n]", {pretty => 1, array_nl => "\n", indent => ""});
77 test_dump(['a' => 1], "[\"a\",1]", {pretty => 1, array_nl => "", indent => ""});
78 test_dump(['a' => 1], "[\"a\",1]", {pretty => 0, array_nl => "\n"});
79
80
81
82 test_dump(1, "1");
83 test_dump('1.0', '"1.0"');
84 test_dump('123456789012345', '"123456789012345"');
85 test_dump('a', '"a"');
86 test_dump("\n", '"\\n"');
87 test_dump("\\", '"\\\\"');
88 test_dump('<script>', '"<scrip"+"t>"');
89 test_dump('<script>', "'<scrip'+'t>'", {single_quote => 1});
90 test_dump('<html>', '"<htm"+"l>"');
91 test_dump('<!--', '"<!-"+"-"');
92 test_dump('"', '"\\""');
93 test_dump('a', "'a'", {single_quote => 1});
94 test_dump('"', "'\"'", {single_quote => 1});
95
96 my $code = sub {};
97 my $str = "\"$code\"";
98 test_dump($code, $str);
99 test_dump($code, "\"CODE\"", {handle_unknown_types => sub { my($self, $data)=@_; return '"'.ref($data).'"'}});
100
101
102 test_dump(sub { "ab" }, '"ab"', {play_coderefs => 1});
103 test_dump({a => sub { "ab" }}, '{"a":"ab"}', {pretty=>0,play_coderefs => 1});
104
105 test_dump("Foo\n".("Bar"x30), "\"Foo\\n\"\n +\"".("Bar"x30)."\"", {pretty => 1});
106 test_dump("Foo\n".("Bar"x30), "\"Foo\\n\"\n\n +\"".("Bar"x30)."\"", {pretty => 1, str_nl => "\n\n"});
107
108 test_dump("Foo\n".("Bar"x30), "'Foo\\n'\n +'".("Bar"x30)."'", {pretty => 1, single_quote => 1});
109 test_dump("Foo\n".("Bar"x30), "'Foo\\n'\n\n +'".("Bar"x30)."'", {pretty => 1, str_nl => "\n\n", single_quote => 1});
This page took 0.032757 seconds and 3 git commands to generate.