Debian Codegraph @

t/20_unknown.t

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