root/JSON-Syck/trunk/t/01_json.t

Revision 1725 (checked in by miyagawa, 14 years ago)

merge Audrey's changes; Test fixed and document updated

  • Property svn:mime-type set to text/plain
Line 
1 use strict;
2 use Data::Dumper;
3 use Test::More;
4 use JSON::Syck;
5 use Storable;
6
7 our $HAS_JSON = 0;
8 eval { require JSON; $HAS_JSON = 1 };
9
10 $Data::Dumper::Indent = 0;
11 $Data::Dumper::Terse  = 1;
12
13 my @tests = (
14     '"foo"',
15     '[1, 2, 3]',
16     '[1, 2, 3]',
17     '2',
18     '2.1',
19     '"foo\'bar"',
20     '[1,2,3]',
21     '[1.1, 2.2, 3.3]',
22     '[1.1,2.2,3.3]',
23     '{"foo": "bar"}',
24     '{"foo":"bar"}',
25     '[{"foo": 2}, {"foo": "bar"}]',
26 );
27
28 plan tests => scalar @tests * (1 + $HAS_JSON);
29
30 my $conv = $HAS_JSON ? JSON::Converter->new : undef;
31
32 for my $test (@tests) {
33     my $data = eval { JSON::Syck::Load($test) };
34     my $json = JSON::Syck::Dump($data);
35
36     # don't bother white spaces
37     for ($test, $json) {
38         s/([,:]) /$1/eg;
39     }
40     is $json, $test, "roundtrip $test -> " . Dumper($data) . " -> $json";
41
42     # try parsing the data with JSON.pm
43     if ($HAS_JSON) {
44         my $data_pp = eval { JSON::jsonToObj($json) };
45         is_deeply $data_pp, $data, "compatibility with JSON.pm $test";
46     }
47 }
Note: See TracBrowser for help on using the browser.