1 2use strict; 3 4use Test::More; 5BEGIN { plan tests => 10 }; 6BEGIN { $ENV{PERL_JSON_BACKEND} = "JSON::backportPP"; } 7 8 9use strict; 10use JSON; 11 12my $json = JSON->new; 13 14eval q| $json->encode( [ sub {} ] ) |; 15ok( $@ =~ /encountered CODE/, $@ ); 16 17eval q| $json->encode( [ \-1 ] ) |; 18ok( $@ =~ /cannot encode reference to scalar/, $@ ); 19 20eval q| $json->encode( [ \undef ] ) |; 21ok( $@ =~ /cannot encode reference to scalar/, $@ ); 22 23eval q| $json->encode( [ \{} ] ) |; 24ok( $@ =~ /cannot encode reference to scalar/, $@ ); 25 26$json->allow_unknown; 27 28is( $json->encode( [ sub {} ] ), '[null]' ); 29is( $json->encode( [ \-1 ] ), '[null]' ); 30is( $json->encode( [ \undef ] ), '[null]' ); 31is( $json->encode( [ \{} ] ), '[null]' ); 32 33 34SKIP: { 35 36 skip "this test is for Perl 5.8 or later", 2 if( $] < 5.008 ); 37 38$json->allow_unknown(0); 39 40my $fh; 41open( $fh, '>hoge.txt' ) or die $!; 42 43eval q| $json->encode( [ $fh ] ) |; 44ok( $@ =~ /encountered GLOB/, $@ ); 45 46$json->allow_unknown(1); 47 48is( $json->encode( [ $fh ] ), '[null]' ); 49 50close $fh; 51 52unlink('hoge.txt'); 53 54} 55