forked from perigrin/json-any
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
marc.mims
committed
Jan 15, 2009
1 parent
2fd0666
commit 1c89db3
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!perl | ||
$!++; | ||
use strict; | ||
use Data::Dumper; | ||
use Test::More; | ||
|
||
use Storable; | ||
|
||
eval "use JSON::Any"; | ||
|
||
if ($@) { | ||
plan skip_all => "$@"; | ||
exit; | ||
} | ||
|
||
$Data::Dumper::Indent = 0; | ||
$Data::Dumper::Terse = 1; | ||
|
||
my @round_trip = ( | ||
'"\""', | ||
'"\b"', | ||
'"\f"', | ||
'"\n"', | ||
'"\r"', | ||
'"\t"', | ||
'"\u0001"', | ||
); | ||
|
||
# Seems most handlers decode the escaped slash (solidus), but don't | ||
# encode it escaped. TODO: What does the spec *really* say? | ||
# For now, just test decoding. Improper decoding breaks things. | ||
my %one_way = ( | ||
'"\/"' => '/', # escaped solidus | ||
); | ||
|
||
foreach my $backend qw(XS JSON DWIW Syck PC) { | ||
my $j = eval { | ||
JSON::Any->import($backend); | ||
JSON::Any->new; | ||
}; | ||
|
||
diag "$backend: " . $@ and next if $@; | ||
|
||
$j and $j->handler or next; | ||
|
||
diag "handler is " . ( ref( $j->handler ) || $j->handlerType ); | ||
|
||
plan 'no_plan' unless $ENV{JSON_ANY_RAN_TESTS}; | ||
$ENV{JSON_ANY_RAN_TESTS} = 1; | ||
|
||
for my $test_orig ( @round_trip ) { | ||
my $test = "[$test_orig]"; # make it an array | ||
my $data = eval { JSON::Any->jsonToObj($test) }; | ||
my $json = JSON::Any->objToJson($data); | ||
|
||
# don't bother white spaces | ||
for ($test, $json) { | ||
s/([,:]) /$1/eg; | ||
} | ||
|
||
my $desc = "roundtrip $test -> " . Dumper($data) . " -> $json"; | ||
utf8::encode($desc); | ||
is $json, $test, $desc; | ||
|
||
} | ||
|
||
while ( my ($encoded, $expected) = each %one_way ) { | ||
my $test = "[$encoded]"; | ||
my $data = eval { JSON::Any->jsonToObj($test) }; | ||
|
||
my $desc = "oneway $test -> " . Dumper($data); | ||
utf8::encode($desc); | ||
is $data->[0], $expected, $desc; | ||
} | ||
|
||
} |