Skip to content

Commit

Permalink
Added string escape tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
marc.mims committed Jan 15, 2009
1 parent 2fd0666 commit 1c89db3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for JSON-Any
1.19 2009-01-??
* Make JSON::Syck optional and deprecated. (perigrin)
* Added tests for string escapes. (semifor)

1.18 2008-11-12
* Removed JSON::PC support as it's been removed from CPAN (perigrin)
Expand Down
76 changes: 76 additions & 0 deletions t/11-string-escape.t
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;
}

}

0 comments on commit 1c89db3

Please sign in to comment.