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