Debian Codegraph @

t/18_json_checker.t

raw
1 # copied over from JSON::XS and modified to use JSON
2
3 # use the testsuite from http://www.json.org/JSON_checker/
4 # except for fail18.json, as we do not support a depth of 20 (but 16 and 32).
5
6 use strict;
7 no warnings;
8 use Test::More;
9 BEGIN { plan tests => 38 };
10
11 BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }
12
13 use JSON;
14
15 # emulate JSON_checker default config
16 my $json = JSON->new->utf8->max_depth(32)->canonical;
17
18 my $vax_float = (pack("d",1) =~ /^[\x80\x10]\x40/);
19
20 binmode DATA;
21
22 for (;;) {
23 $/ = "\n# ";
24 chomp (my $test = <DATA>)
25 or last;
26 $/ = "\n";
27 my $name = <DATA>;
28 if ($vax_float && $name =~ /pass1.json/) {
29 $test =~ s/\b23456789012E66\b/23456789012E20/;
30 }
31
32 if (my $perl = eval { $json->decode ($test) }) {
33 ok ($name =~ /^pass/, $name);
34 is ($json->encode ($json->decode ($json->encode ($perl))), $json->encode ($perl));
35 } else {
36 ok ($name =~ /^fail/, "$name ($@)");
37 }
38 }
39
40 __DATA__
41 {"Extra value after close": true} "misplaced quoted value"
42 # fail10.json
43 {"Illegal expression": 1 + 2}
44 # fail11.json
45 {"Illegal invocation": alert()}
46 # fail12.json
47 {"Numbers cannot have leading zeroes": 013}
48 # fail13.json
49 {"Numbers cannot be hex": 0x14}
50 # fail14.json
51 ["Illegal backslash escape: \x15"]
52 # fail15.json
53 [\naked]
54 # fail16.json
55 ["Illegal backslash escape: \017"]
56 # fail17.json
57 [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
58 # fail18.json
59 {"Missing colon" null}
60 # fail19.json
61 ["Unclosed array"
62 # fail2.json
63 {"Double colon":: null}
64 # fail20.json
65 {"Comma instead of colon", null}
66 # fail21.json
67 ["Colon instead of comma": false]
68 # fail22.json
69 ["Bad value", truth]
70 # fail23.json
71 ['single quote']
72 # fail24.json
73 [" tab character in string "]
74 # fail25.json
75 ["tab\ character\ in\ string\ "]
76 # fail26.json
77 ["line
78 break"]
79 # fail27.json
80 ["line\
81 break"]
82 # fail28.json
83 [0e]
84 # fail29.json
85 {unquoted_key: "keys must be quoted"}
86 # fail3.json
87 [0e+]
88 # fail30.json
89 [0e+-1]
90 # fail31.json
91 {"Comma instead if closing brace": true,
92 # fail32.json
93 ["mismatch"}
94 # fail33.json
95 ["extra comma",]
96 # fail4.json
97 ["double extra comma",,]
98 # fail5.json
99 [ , "<-- missing value"]
100 # fail6.json
101 ["Comma after the close"],
102 # fail7.json
103 ["Extra close"]]
104 # fail8.json
105 {"Extra comma": true,}
106 # fail9.json
107 [
108 "JSON Test Pattern pass1",
109 {"object with 1 member":["array with 1 element"]},
110 {},
111 [],
112 -42,
113 true,
114 false,
115 null,
116 {
117 "integer": 1234567890,
118 "real": -9876.543210,
119 "e": 0.123456789e-12,
120 "E": 1.234567890E+34,
121 "": 23456789012E66,
122 "zero": 0,
123 "one": 1,
124 "space": " ",
125 "quote": "\"",
126 "backslash": "\\",
127 "controls": "\b\f\n\r\t",
128 "slash": "/ & \/",
129 "alpha": "abcdefghijklmnopqrstuvwyz",
130 "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
131 "digit": "0123456789",
132 "0123456789": "digit",
133 "special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
134 "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
135 "true": true,
136 "false": false,
137 "null": null,
138 "array":[ ],
139 "object":{ },
140 "address": "50 St. James Street",
141 "url": "http://www.JSON.org/",
142 "comment": "// /* <!-- --",
143 "# -- --> */": " ",
144 " s p a c e d " :[1,2 , 3
145
146 ,
147
148 4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
149 "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
150 "quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
151 "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
152 : "A key can be any string"
153 },
154 0.5 ,98.6
155 ,
156 99.44
157 ,
158
159 1066,
160 1e1,
161 0.1e1,
162 1e-1,
163 1e00,2e+00,2e-00
164 ,"rosebud"]
165 # pass1.json
166 [[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
167 # pass2.json
168 {
169 "JSON Test Pattern pass3": {
170 "The outermost value": "must be an object or array.",
171 "In this test": "It is an object."
172 }
173 }
174
175 # pass3.json