| Revision: | 1.3 |
| Committed: | Thu Mar 29 02:45:49 2007 UTC (19 years, 6 months ago) by root |
| Content type: | application/x-troff |
| Branch: | MAIN |
| CVS Tags: | rel-2_01, rel-2_3, rel-1_11, rel-1_51, rel-1_53, rel-4_0, rel-2_222, rel-3_0, rel-4_01, rel-4_03, rel-4_02, rel-2_2, rel-4_04, rel-2_0, rel-2_1, rel-4_0_00, rel-2_34, rel-2_32, rel-2_33, rel-2_31, rel-1_1, rel-1_0, rel-1_3, rel-1_2, rel-1_5, rel-1_4, rel-2_2311, rel-1_01, rel-1_24, rel-3_01, rel-3_02, rel-3_03, rel-3_04, rel-1_21, rel-1_22, rel-1_23, rel-1_43, rel-1_41, rel-2_232, rel-2_231, rel-2_29, rel-2_28, rel-2_21, rel-2_2222, rel-2_23, rel-2_22, rel-2_25, rel-2_24, rel-2_27, rel-2_26, HEAD |
| Changes since 1.2: | +3 -7 lines |
| Log Message: | *** empty log message *** |
| # | User | Rev | Content |
|---|---|---|---|
| 1 | root | 1.1 | use Test::More; |
| 2 | |||
| 3 | # copied over from JSON::PC and modified to use JSON::XS | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | root | 1.3 | BEGIN { plan tests => 20 }; |
| 7 | root | 1.1 | use JSON::XS; |
| 8 | |||
| 9 | my ($js,$obj); | ||
| 10 | |||
| 11 | my $pc = new JSON::XS; | ||
| 12 | |||
| 13 | $js = q|{}|; | ||
| 14 | |||
| 15 | $obj = $pc->decode($js); | ||
| 16 | $js = $pc->encode($obj); | ||
| 17 | is($js,'{}', '{}'); | ||
| 18 | |||
| 19 | $js = q|[]|; | ||
| 20 | $obj = $pc->decode($js); | ||
| 21 | $js = $pc->encode($obj); | ||
| 22 | is($js,'[]', '[]'); | ||
| 23 | |||
| 24 | |||
| 25 | $js = q|{"foo":"bar"}|; | ||
| 26 | $obj = $pc->decode($js); | ||
| 27 | is($obj->{foo},'bar'); | ||
| 28 | $js = $pc->encode($obj); | ||
| 29 | is($js,'{"foo":"bar"}', '{"foo":"bar"}'); | ||
| 30 | |||
| 31 | $js = q|{"foo":""}|; | ||
| 32 | $obj = $pc->decode($js); | ||
| 33 | $js = $pc->encode($obj); | ||
| 34 | is($js,'{"foo":""}', '{"foo":""}'); | ||
| 35 | |||
| 36 | $js = q|{"foo":" "}|; | ||
| 37 | $obj = $pc->decode($js); | ||
| 38 | $js = $pc->encode($obj); | ||
| 39 | is($js,'{"foo":" "}' ,'{"foo":" "}'); | ||
| 40 | |||
| 41 | $js = q|{"foo":"0"}|; | ||
| 42 | $obj = $pc->decode($js); | ||
| 43 | $js = $pc->encode($obj); | ||
| 44 | is($js,'{"foo":"0"}',q|{"foo":"0"} - autoencode (default)|); | ||
| 45 | |||
| 46 | |||
| 47 | $js = q|{"foo":"0 0"}|; | ||
| 48 | $obj = $pc->decode($js); | ||
| 49 | $js = $pc->encode($obj); | ||
| 50 | is($js,'{"foo":"0 0"}','{"foo":"0 0"}'); | ||
| 51 | |||
| 52 | $js = q|[1,2,3]|; | ||
| 53 | $obj = $pc->decode($js); | ||
| 54 | is($obj->[1],2); | ||
| 55 | $js = $pc->encode($obj); | ||
| 56 | is($js,'[1,2,3]'); | ||
| 57 | |||
| 58 | $js = q|{"foo":{"bar":"hoge"}}|; | ||
| 59 | $obj = $pc->decode($js); | ||
| 60 | is($obj->{foo}->{bar},'hoge'); | ||
| 61 | $js = $pc->encode($obj); | ||
| 62 | is($js,q|{"foo":{"bar":"hoge"}}|); | ||
| 63 | |||
| 64 | $js = q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|; | ||
| 65 | $obj = $pc->decode($js); | ||
| 66 | $js = $pc->encode($obj); | ||
| 67 | is($js,q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|); | ||
| 68 | |||
| 69 | |||
| 70 | $obj = ["\x01"]; | ||
| 71 | is($js = $pc->encode($obj),'["\\u0001"]'); | ||
| 72 | $obj = $pc->decode($js); | ||
| 73 | is($obj->[0],"\x01"); | ||
| 74 | |||
| 75 | $obj = ["\e"]; | ||
| 76 | is($js = $pc->encode($obj),'["\\u001b"]'); | ||
| 77 | $obj = $pc->decode($js); | ||
| 78 | is($obj->[0],"\e"); | ||
| 79 | |||
| 80 | $js = '{"id":"}'; | ||
| 81 | eval q{ $pc->decode($js) }; | ||
| 82 | like($@, qr/unexpected end/i); | ||
| 83 | |||
| 84 | root | 1.3 | $obj = { foo => sub { "bar" } }; |
| 85 | root | 1.1 | eval q{ $js = $pc->encode($obj) }; |
| 86 | like($@, qr/JSON can only/i, 'invalid value (coderef)'); | ||
| 87 | |||
| 88 | #$obj = { foo => bless {}, "Hoge" }; | ||
| 89 | #eval q{ $js = $pc->encode($obj) }; | ||
| 90 | #like($@, qr/JSON can only/i, 'invalid value (blessd object)'); | ||
| 91 | |||
| 92 | $obj = { foo => \$js }; | ||
| 93 | eval q{ $js = $pc->encode($obj) }; | ||
| 94 | root | 1.3 | like($@, qr/cannot encode reference/i, 'invalid value (ref)'); |
| 95 | root | 1.1 |