File: gh_28_json_test_suite.t

package info (click to toggle)
perl 5.32.1-4%2Bdeb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 113,408 kB
  • sloc: ansic: 641,443; perl: 491,650; sh: 70,967; pascal: 8,354; cpp: 4,103; xml: 2,428; makefile: 2,237; yacc: 1,173; lisp: 1
file content (59 lines) | stat: -rw-r--r-- 1,359 bytes parent folder | download | duplicates (3)
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
54
55
56
57
58
59
# the following test cases are taken from JSONTestSuite
# by Nicolas Seriot (https://github.com/nst/JSONTestSuite)

use strict;
use Test::More;

BEGIN { plan skip_all => 'this test is for Perl 5.8 or later' if $] < 5.008; }

BEGIN { plan tests => 20 };

BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }

use JSON::PP;

my $DECODER = JSON::PP->new->utf8->allow_nonref;

# n_multidigit_number_then_00
decode_should_fail(qq!123\x00!);

# number_-01
decode_should_fail(qq![-01]!);

# number_neg_int_starting_with_zero
decode_should_fail(qq![-012]!);

# n_object_trailing_comment
decode_should_fail(qq!{"a":"b"}/**/!);

# n_object_trailing_comment_slash_open
decode_should_fail(qq!{"a":"b"}//!);

# n_structure_null-byte-outside-sting
decode_should_fail(qq![\x00]!);

# n_structure_object_with_comment
decode_should_fail(qq!{"a":/*comment*/"b"}!);

# n_structure_whitespace_formfeed
decode_should_fail(qq![\0x0c]!);

# y_string_utf16BE_no_BOM
decode_should_pass(qq!\x00[\x00"\x00\xE9\x00"\x00]!);

# y_string_utf16LE_no_BOM
decode_should_pass(qq![\x00"\x00\xE9\x00"\x00]\x00!);

sub decode_should_pass {
    my $json = shift;
    my $result = eval { $DECODER->decode($json); };
    ok !$@, $@ || '';
    ok defined $result;
}

sub decode_should_fail {
    my $json = shift;
    my $result = eval { $DECODER->decode($json); };
    ok $@, $@ || '';
    ok !defined $result;
}