summaryrefslogtreecommitdiff
path: root/t/20_unknown.t
blob: 921acae9aa9c51227ace2cb198b3f67926b8a7cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use strict;

use Test::More;
BEGIN { plan tests => 10 };
BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }


use strict;
use JSON;

my $json = JSON->new;

eval q| $json->encode( [ sub {} ] ) |;
ok( $@ =~ /encountered CODE/, $@ );

eval q|  $json->encode( [ \-1 ] ) |;
ok( $@ =~ /cannot encode reference to scalar/, $@ );

eval q|  $json->encode( [ \undef ] ) |;
ok( $@ =~ /cannot encode reference to scalar/, $@ );

eval q|  $json->encode( [ \{} ] ) |;
ok( $@ =~ /cannot encode reference to scalar/, $@ );

$json->allow_unknown;

is( $json->encode( [ sub {} ] ), '[null]' );
is( $json->encode( [ \-1 ] ),    '[null]' );
is( $json->encode( [ \undef ] ), '[null]' );
is( $json->encode( [ \{} ] ),    '[null]' );


SKIP: {

    skip "this test is for Perl 5.8 or later", 2 if( $] < 5.008 );

$json->allow_unknown(0);

my $fh;
open( $fh, '>hoge.txt' ) or die $!;

eval q| $json->encode( [ $fh ] ) |;
ok( $@ =~ /encountered GLOB|cannot encode reference to scalar/, $@ );

$json->allow_unknown(1);

is( $json->encode( [ $fh ] ),    '[null]' );

close $fh;

unlink('hoge.txt');

}