1#!perl
2$!++;
3use strict;
4use Data::Dumper;
5use Test::More;
6
7use Storable;
8
9eval "use JSON::Any";
10
11if ($@) {
12    plan skip_all => "$@";
13    exit;
14}
15
16$Data::Dumper::Indent = 0;
17$Data::Dumper::Terse  = 1;
18
19my @round_trip = (
20    '"\""',
21    '"\b"',
22    '"\f"',
23    '"\n"',
24    '"\r"',
25    '"\t"',
26    '"\u0001"',
27);
28
29# Seems most handlers decode the escaped slash (solidus), but don't
30# encode it escaped.  TODO: What does the spec *really* say?
31# For now, just test decoding.  Improper decoding breaks things.
32my %one_way = (
33    '"\/"' => '/',  # escaped solidus
34);
35
36test ($_) for qw(XS JSON DWIW);
37
38TODO: { 
39    local $TODO = q[JSON::Syck doesn't escape things properly];
40    test ($_) for qw(Syck);
41}
42
43sub test {
44    my ($backend) = @_;
45    my $j = eval {
46        JSON::Any->import($backend);
47        JSON::Any->new;
48    };
49
50    diag "$backend: " . $@ and next if $@;
51
52    $j and $j->handler or next;
53
54    diag "handler is " . ( ref( $j->handler ) || $j->handlerType );
55
56    plan 'no_plan' unless $ENV{JSON_ANY_RAN_TESTS};
57    $ENV{JSON_ANY_RAN_TESTS} = 1;
58    
59    for my $test_orig ( @round_trip ) {
60        my $test = "[$test_orig]"; # make it an array
61        my $data = eval { JSON::Any->jsonToObj($test) };
62        my $json = JSON::Any->objToJson($data);
63
64        # don't bother white spaces
65        for ($test, $json) {
66            s/([,:]) /$1/eg;
67        }
68
69        my $desc = "roundtrip $test -> " . Dumper($data) . " -> $json";
70        utf8::encode($desc);
71        is $json, $test, $desc;
72
73    }
74
75    while ( my ($encoded, $expected) = each %one_way ) {
76        my $test = "[$encoded]";
77        my $data = eval { JSON::Any->jsonToObj($test) };
78
79        my $desc = "oneway $test -> " . Dumper($data);
80        utf8::encode($desc);
81        is $data->[0], $expected, $desc;
82    }
83}