1#!/usr/bin/perl -w
2use strict;
3use Test::More;
4BEGIN {
5  # allow_unknown method added to JSON::PP in 2.09
6  eval 'use JSON::PP 2.09 (); 1'
7    or plan skip_all => 'JSON::PP 2.09 required for cross testing';
8  $ENV{PERL_JSON_BACKEND} = 'JSON::PP';
9}
10plan tests => 32;
11use JSON::PP ();
12use Cpanel::JSON::XS ();
13
14my $pp = JSON::PP->new;
15my $json = Cpanel::JSON::XS->new;
16
17eval q| $json->encode( [ sub {} ] ) |;
18ok( $@ =~ /encountered CODE/, $@ );
19
20eval q|  $json->encode( [ \-1 ] ) |;
21ok( $@ =~ /cannot encode reference to scalar/, $@ );
22
23eval q|  $json->encode( [ \undef ] ) |;
24ok( $@ =~ /cannot encode reference to scalar/, $@ );
25
26eval q|  $json->encode( [ \{} ] ) |;
27ok( $@ =~ /cannot encode reference to scalar/, $@ );
28
29# 46
30eval q| $json->encode( {false => \""} ) |;
31ok( $@ =~ /cannot encode reference to scalar/, $@ );
32eval q| $json->encode( {false => \!!""} ) |;
33ok( $@ =~ /cannot encode reference to scalar/, $@ );
34eval q| $pp->encode( {false => \""} ) |;
35ok( $@ =~ /cannot encode reference to scalar/, $@ );
36eval q| $pp->encode( {false => \!!""} ) |;
37ok( $@ =~ /cannot encode reference to scalar/, $@ );
38
39$json->allow_unknown;
40$pp->allow_unknown;
41
42is( $json->encode( [ sub {} ] ), '[null]' );
43is( $json->encode( [ \-1 ] ),    '[null]' );
44is( $json->encode( [ \undef ] ), '[null]' );
45is( $json->encode( [ \{} ] ),    '[null]' );
46
47# 46
48is( $pp->encode( {null => \"some"} ),   '{"null":null}',   'pp unknown' );
49is( $pp->encode( {null => \""} ),       '{"null":null}',   'pp unknown' );
50# valid special yes/no values even without nonref
51my $e = $pp->encode( {true => !!1} ); # pp is a bit inconsistent
52ok( ($e eq '{"true":"1"}') || ($e eq '{"true":1}'),    'pp sv_yes' );
53is( $pp->encode( {false => !!0} ),      '{"false":""}',    'pp sv_no' );
54is( $pp->encode( {false => !!""} ),     '{"false":""}',    'pp sv_no' );
55is( $pp->encode( {true => \!!1} ),      '{"true":true}',   'pp \sv_yes');
56is( $pp->encode( {false => \!!0} ),     '{"false":null}',  'pp \sv_no' );
57is( $pp->encode( {false => \!!""} ),    '{"false":null}',  'pp \sv_no' );
58
59is( $json->encode( {null => \"some"} ), '{"null":null}',   'js unknown' );
60is( $json->encode( {null => \""} ),     '{"null":null}',   'js unknown' );
61is( $json->encode( {true => !!1} ),     '{"true":1}',      'js sv_yes' );
62is( $json->encode( {false => !!0} ),    '{"false":""}',    'js sv_no' );
63is( $json->encode( {false => !!""} ),   '{"false":""}',    'js sv_no' );
64is( $json->encode( {true => \!!1} ),    '{"true":true}',   'js \sv_yes' );
65is( $json->encode( {false => \!!0} ),   '{"false":null}',  'js \sv_no' );
66is( $json->encode( {false => \!!""} ),  '{"false":null}',  'js \sv_no' );
67
68SKIP: {
69
70  skip "this test is for Perl 5.8 or later", 4 if $] < 5.008;
71
72$pp->allow_unknown(0);
73$json->allow_unknown(0);
74
75my $fh;
76open( $fh, '>hoge.txt' ) or die $!;
77END { unlink('hoge.txt'); }
78
79eval q| $pp->encode( [ $fh ] ) |; # upstream changed due to this JSON::XS bug
80ok( $@ =~ /(encountered GLOB|cannot encode reference to scalar)/, "pp ".$@ );
81eval q| $json->encode( [ $fh ] ) |;
82ok( $@ =~ /encountered GLOB/, "js ".$@ );
83
84$pp->allow_unknown(1);
85$json->allow_unknown(1);
86
87is( $pp->encode  ( [ $fh ] ),    '[null]' );
88is( $json->encode( [ $fh ] ),    '[null]' );
89
90close $fh;
91
92} # skip 5.6
93