From 10d28ac5a9e05d9939f905e4145a4b8f52858d20 Mon Sep 17 00:00:00 2001 From: sboyette Date: Tue, 15 Sep 2009 16:19:48 +0000 Subject: [PATCH 1/1] 100% test coverage git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1790 9efc2488-bf62-4759-914b-345cdb29e865 --- src/perl/lib/OpenSRF/Utils/JSON.pm | 2 +- src/perl/t/09-Utils-JSON.t | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/perl/lib/OpenSRF/Utils/JSON.pm b/src/perl/lib/OpenSRF/Utils/JSON.pm index 723b779..e88eb1f 100644 --- a/src/perl/lib/OpenSRF/Utils/JSON.pm +++ b/src/perl/lib/OpenSRF/Utils/JSON.pm @@ -143,7 +143,7 @@ sub JSONObject2Perl { my $class = $obj->{$JSON_CLASS_KEY}; $class =~ s/^\s+//; # FIXME pretty sure these lines could condense to 's/\s+//g' $class =~ s/\s+$//; - $class = $pkg->lookup_class($class) || $class; + $class = $pkg->lookup_class($class) if $pkg->lookup_class($class); return bless(\$vivobj, $class) unless ref $vivobj; return bless($vivobj, $class); } diff --git a/src/perl/t/09-Utils-JSON.t b/src/perl/t/09-Utils-JSON.t index c063ecd..85e51a3 100644 --- a/src/perl/t/09-Utils-JSON.t +++ b/src/perl/t/09-Utils-JSON.t @@ -1,6 +1,6 @@ #!perl -T -use Test::More tests => 38; +use Test::More tests => 49; use OpenSRF::Utils::JSON; @@ -115,6 +115,27 @@ is (OpenSRF::Utils::JSON->JSONObject2Perl($coderef), $coderef, "Returns argument is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl([11, 12]), [11, 12], "Arrayrefs get reconstructed as themselves"); is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl([11, OpenSRF::Utils::JSON->true, 12]), [11, OpenSRF::Utils::JSON->true, 12], "Even when they contain JSON::XS::Booleans; those just don't get recursed upon"); - # note: [11, 1, 12] doesn't work here, even though you can - # do math on J:X:Booleans + # note: [11, 1, 12] doesn't work here, even though you can do math on J:X:Booleans +is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl($hashref), { foo => 'bar' }, "Hashrefs without the class flag also get turned into themselves"); +is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl({ foo => OpenSRF::Utils::JSON->true, bar => 'baz' }), + { foo => OpenSRF::Utils::JSON->true, bar => 'baz'}, + "Even when they contain JSON::XS::Booleans; those just don't get recursed upon"); + +my $vivobj = OpenSRF::Utils::JSON->JSONObject2Perl($jsonobj); +is (ref $vivobj, 'OpenSRF::DomainObject::oilsException'); +is_deeply ($vivobj, { foo => 'bar' }, "perl2JSONObject-packaged things get blessed to their original contents and class"); + +my $codeobj = OpenSRF::Utils::JSON->perl2JSONObject($coderef); +is_deeply (OpenSRF::Utils::JSON->JSONObject2Perl($codeobj), undef, "Things with undefined payloads (see above)return undef"); + +$vivobj = OpenSRF::Utils::JSON->JSONObject2Perl({ __c => 'foo', __p => 'bar' }); +is (ref $vivobj, 'foo'); +is_deeply ($vivobj, \'bar', "Scalar payload and non-resolvable class hint vivifies to a scalar *ref* and a class of the class flag"); + + +# +# json2Perl +my $perlobj = OpenSRF::Utils::JSON->JSON2perl($jsonstr); +is (ref $perlobj, 'OpenSRF::DomainObject::oilsException'); +is_deeply ($perlobj, { foo => 'bar' }, "Successful revivification from JSON in one step"); -- 2.43.2