]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/t/09-Utils-JSON.t
85e51a31a95c476d3614adbbb0a6ee7c857d55a4
[OpenSRF.git] / src / perl / t / 09-Utils-JSON.t
1 #!perl -T
2
3 use Test::More tests => 49;
4
5 use OpenSRF::Utils::JSON;
6
7
8 #
9 # initial state from use
10 #
11
12 # do we have a JSON::XS object?
13 is (ref $OpenSRF::Utils::JSON::parser,   'JSON::XS');
14
15 # make sure the class and payload keys are as expected
16 is ($OpenSRF::Utils::JSON::JSON_CLASS_KEY,   '__c');
17 is ($OpenSRF::Utils::JSON::JSON_PAYLOAD_KEY, '__p');
18
19 # start with the simplest bits possible
20 is (OpenSRF::Utils::JSON::true, 1);
21 is (OpenSRF::Utils::JSON->true, 1);
22 is (OpenSRF::Utils::JSON->false, 0);
23
24
25 #
26 # register_class_hint
27 my $testmap =  { hints   => { osrfException =>
28                               { hint => 'osrfException',
29                                 name => 'OpenSRF::DomainObject::oilsException' }
30                             },
31                  classes => { OpenSRF::DomainObject::oilsException =>
32                               { hint => 'osrfException',
33                                 name => 'OpenSRF::DomainObject::oilsException' }
34                             }
35                };
36 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfException',
37                                            name => 'OpenSRF::DomainObject::oilsException');
38 is_deeply (\%OpenSRF::Utils::JSON::_class_map, $testmap);
39
40
41 #
42 # lookup_class
43 is (OpenSRF::Utils::JSON->lookup_class('osrfException'), 'OpenSRF::DomainObject::oilsException');
44 is (OpenSRF::Utils::JSON->lookup_class(37), undef, "Argument doesn't exist");
45 is (OpenSRF::Utils::JSON->lookup_class(''), undef, "Null string lookup");
46 is (OpenSRF::Utils::JSON->lookup_class(), undef, "Null request");
47
48
49 #
50 # lookup_hint
51 is (OpenSRF::Utils::JSON->lookup_hint('OpenSRF::DomainObject::oilsException'), 'osrfException');
52 is (OpenSRF::Utils::JSON->lookup_hint(37), undef, "Argument doesn't exist");
53 is (OpenSRF::Utils::JSON->lookup_hint(''), undef, "Null string lookup");
54 is (OpenSRF::Utils::JSON->lookup_hint(), undef, "Null request");
55
56
57 #
58 # rawPerl2JSON
59 my $struct = [ { foo => 'bar' }, 'baz', 'quux', 'x'];
60 is (OpenSRF::Utils::JSON->rawPerl2JSON($struct),
61     '[{"foo":"bar"},"baz","quux","x"]');
62 is (OpenSRF::Utils::JSON->rawPerl2JSON(''), '""', "Null string as argument");
63
64
65 #
66 # rawJSON2perl
67 is_deeply (OpenSRF::Utils::JSON->rawJSON2perl(OpenSRF::Utils::JSON->rawPerl2JSON($struct)),
68            [ { foo => 'bar' }, 'baz', 'quux', 'x']);
69 is (OpenSRF::Utils::JSON->rawJSON2perl(), undef, "Null argument");
70 is (OpenSRF::Utils::JSON->rawJSON2perl(''), undef, "Null string as argument");
71
72
73 #
74 # perl2JSONObject
75 is (OpenSRF::Utils::JSON->perl2JSONObject(),      undef, "Returns argument unless it's a ref");
76 is (OpenSRF::Utils::JSON->perl2JSONObject(3),     3,     "Returns argument unless it's a ref");
77 is (OpenSRF::Utils::JSON->perl2JSONObject('foo'), 'foo', "Returns argument unless it's a ref");
78
79 is (ref OpenSRF::Utils::JSON->true, 'JSON::XS::Boolean');
80 is (OpenSRF::Utils::JSON->perl2JSONObject(OpenSRF::Utils::JSON->true), '1', "Returns argument if it's a JSON::XS::Boolean");
81
82 my $hashref = { foo => 'bar' };
83 is (UNIVERSAL::isa($hashref,'HASH'), 1);
84 is_deeply (OpenSRF::Utils::JSON->perl2JSONObject($hashref), { foo => 'bar' }, "Passing in unblessed hashref");
85
86 my $arryref = [ 11, 12 ];
87 is (UNIVERSAL::isa($arryref,'ARRAY'), 1);
88 is_deeply (OpenSRF::Utils::JSON->perl2JSONObject($arryref), [ 11, 12 ], "Passing in unblessed arrayref");
89
90 my $coderef = sub { return 0 };            # this is almost certainly undesired behavior, but the
91 is (UNIVERSAL::isa($coderef,'CODE'), 1);   # code doesn't stop me from doing it
92 is_deeply (OpenSRF::Utils::JSON->perl2JSONObject($coderef),
93            { __c => 'CODE', __p => undef }, "Passing in coderef");
94
95 my $fakeobj = bless { foo => 'bar' }, 'OpenSRF::DomainObject::oilsException';
96 is (UNIVERSAL::isa($fakeobj,'HASH'), 1);
97 my $jsonobj = OpenSRF::Utils::JSON->perl2JSONObject($fakeobj);
98 is_deeply ($jsonobj, { __c => 'osrfException', __p => { foo => 'bar' } },
99            "Wrap object into an OpenSRF-shaped packet");
100
101
102 #
103 # perl2JSON
104 my $jsonstr = OpenSRF::Utils::JSON->perl2JSON($fakeobj);
105 is ($jsonstr, '{"__c":"osrfException","__p":{"foo":"bar"}}');
106
107
108 #
109 # JSONObject2Perl
110 is (OpenSRF::Utils::JSON->JSONObject2Perl(),      undef, "Returns argument unless it's a ref");
111 is (OpenSRF::Utils::JSON->JSONObject2Perl(3),     3,     "Returns argument unless it's a ref");
112 is (OpenSRF::Utils::JSON->JSONObject2Perl('foo'), 'foo', "Returns argument unless it's a ref");
113 is (OpenSRF::Utils::JSON->JSONObject2Perl($coderef), $coderef, "Returns argument unless it's a ref");
114
115 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl([11, 12]), [11, 12], "Arrayrefs get reconstructed as themselves");
116 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl([11, OpenSRF::Utils::JSON->true, 12]), [11, OpenSRF::Utils::JSON->true, 12],
117            "Even when they contain JSON::XS::Booleans; those just don't get recursed upon");
118            # note: [11, 1, 12] doesn't work here, even though you can do math on J:X:Booleans
119
120 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl($hashref), { foo => 'bar' }, "Hashrefs without the class flag also get turned into themselves");
121 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl({ foo => OpenSRF::Utils::JSON->true, bar => 'baz' }), 
122            { foo => OpenSRF::Utils::JSON->true, bar => 'baz'},
123            "Even when they contain JSON::XS::Booleans; those just don't get recursed upon");
124
125 my $vivobj = OpenSRF::Utils::JSON->JSONObject2Perl($jsonobj);
126 is (ref $vivobj, 'OpenSRF::DomainObject::oilsException');
127 is_deeply ($vivobj, { foo => 'bar' }, "perl2JSONObject-packaged things get blessed to their original contents and class");
128
129 my $codeobj = OpenSRF::Utils::JSON->perl2JSONObject($coderef);
130 is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl($codeobj), undef, "Things with undefined payloads (see above)return undef");
131
132 $vivobj = OpenSRF::Utils::JSON->JSONObject2Perl({ __c => 'foo', __p => 'bar' });
133 is (ref $vivobj, 'foo');
134 is_deeply ($vivobj, \'bar', "Scalar payload and non-resolvable class hint vivifies to a scalar *ref* and a class of the class flag");
135
136
137 #
138 # json2Perl
139 my $perlobj = OpenSRF::Utils::JSON->JSON2perl($jsonstr);
140 is (ref $perlobj, 'OpenSRF::DomainObject::oilsException');
141 is_deeply ($perlobj,  { foo => 'bar' }, "Successful revivification from JSON in one step");