OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library jsonTest; | 5 library jsonTest; |
6 | 6 |
7 import 'dart:json'; | 7 import 'dart:json'; |
8 | 8 |
9 main() { | 9 main() { |
10 testEscaping(); | 10 testEscaping(); |
11 testParse(); | 11 testParse(); |
12 testParseInvalid(); | 12 testParseInvalid(); |
13 } | 13 } |
14 | 14 |
15 void testParse() { | 15 void testParse() { |
16 // Scalars. | 16 // Scalars. |
17 Expect.equals(5, JSON.parse(' 5 ')); | 17 Expect.equals(5, parse(' 5 ')); |
18 Expect.equals(-42, JSON.parse(' -42 ')); | 18 Expect.equals(-42, parse(' -42 ')); |
19 Expect.equals(3, JSON.parse(' 3e0 ')); | 19 Expect.equals(3, parse(' 3e0 ')); |
20 Expect.equals(3.14, JSON.parse(' 3.14 ')); | 20 Expect.equals(3.14, parse(' 3.14 ')); |
21 Expect.equals(1.0E-06, JSON.parse(' 1.0E-06 ')); | 21 Expect.equals(1.0E-06, parse(' 1.0E-06 ')); |
22 Expect.equals(0, JSON.parse("0")); | 22 Expect.equals(0, parse("0")); |
23 Expect.equals(1, JSON.parse("1")); | 23 Expect.equals(1, parse("1")); |
24 Expect.equals(0.1, JSON.parse("0.1")); | 24 Expect.equals(0.1, parse("0.1")); |
25 Expect.equals(1.1, JSON.parse("1.1")); | 25 Expect.equals(1.1, parse("1.1")); |
26 Expect.equals(1.1, JSON.parse("1.100000")); | 26 Expect.equals(1.1, parse("1.100000")); |
27 Expect.equals(1.111111, JSON.parse("1.111111")); | 27 Expect.equals(1.111111, parse("1.111111")); |
28 Expect.equals(-0, JSON.parse("-0")); | 28 Expect.equals(-0, parse("-0")); |
29 Expect.equals(-1, JSON.parse("-1")); | 29 Expect.equals(-1, parse("-1")); |
30 Expect.equals(-0.1, JSON.parse("-0.1")); | 30 Expect.equals(-0.1, parse("-0.1")); |
31 Expect.equals(-1.1, JSON.parse("-1.1")); | 31 Expect.equals(-1.1, parse("-1.1")); |
32 Expect.equals(-1.1, JSON.parse("-1.100000")); | 32 Expect.equals(-1.1, parse("-1.100000")); |
33 Expect.equals(-1.111111, JSON.parse("-1.111111")); | 33 Expect.equals(-1.111111, parse("-1.111111")); |
34 Expect.equals(11, JSON.parse("1.1e1")); | 34 Expect.equals(11, parse("1.1e1")); |
35 Expect.equals(11, JSON.parse("1.1e+1")); | 35 Expect.equals(11, parse("1.1e+1")); |
36 Expect.equals(0.11, JSON.parse("1.1e-1")); | 36 Expect.equals(0.11, parse("1.1e-1")); |
37 Expect.equals(11, JSON.parse("1.1E1")); | 37 Expect.equals(11, parse("1.1E1")); |
38 Expect.equals(11, JSON.parse("1.1E+1")); | 38 Expect.equals(11, parse("1.1E+1")); |
39 Expect.equals(0.11, JSON.parse("1.1E-1")); | 39 Expect.equals(0.11, parse("1.1E-1")); |
40 Expect.equals(1E0, JSON.parse(" 1E0")); | 40 Expect.equals(1E0, parse(" 1E0")); |
41 Expect.equals(1E+0, JSON.parse(" 1E+0")); | 41 Expect.equals(1E+0, parse(" 1E+0")); |
42 Expect.equals(1E-0, JSON.parse(" 1E-0")); | 42 Expect.equals(1E-0, parse(" 1E-0")); |
43 Expect.equals(1E00, JSON.parse(" 1E00")); | 43 Expect.equals(1E00, parse(" 1E00")); |
44 Expect.equals(1E+00, JSON.parse(" 1E+00")); | 44 Expect.equals(1E+00, parse(" 1E+00")); |
45 Expect.equals(1E-00, JSON.parse(" 1E-00")); | 45 Expect.equals(1E-00, parse(" 1E-00")); |
46 Expect.equals(1E+10, JSON.parse(" 1E+10")); | 46 Expect.equals(1E+10, parse(" 1E+10")); |
47 Expect.equals(1E+010, JSON.parse(" 1E+010")); | 47 Expect.equals(1E+010, parse(" 1E+010")); |
48 Expect.equals(1E+0010, JSON.parse(" 1E+0010")); | 48 Expect.equals(1E+0010, parse(" 1E+0010")); |
49 Expect.equals(1E10, JSON.parse(" 1E10")); | 49 Expect.equals(1E10, parse(" 1E10")); |
50 Expect.equals(1E010, JSON.parse(" 1E010")); | 50 Expect.equals(1E010, parse(" 1E010")); |
51 Expect.equals(1E0010, JSON.parse(" 1E0010")); | 51 Expect.equals(1E0010, parse(" 1E0010")); |
52 Expect.equals(1E-10, JSON.parse(" 1E-10")); | 52 Expect.equals(1E-10, parse(" 1E-10")); |
53 Expect.equals(1E-0010, JSON.parse(" 1E-0010")); | 53 Expect.equals(1E-0010, parse(" 1E-0010")); |
54 Expect.equals(1E0, JSON.parse(" 1e0")); | 54 Expect.equals(1E0, parse(" 1e0")); |
55 Expect.equals(1E+0, JSON.parse(" 1e+0")); | 55 Expect.equals(1E+0, parse(" 1e+0")); |
56 Expect.equals(1E-0, JSON.parse(" 1e-0")); | 56 Expect.equals(1E-0, parse(" 1e-0")); |
57 Expect.equals(1E00, JSON.parse(" 1e00")); | 57 Expect.equals(1E00, parse(" 1e00")); |
58 Expect.equals(1E+00, JSON.parse(" 1e+00")); | 58 Expect.equals(1E+00, parse(" 1e+00")); |
59 Expect.equals(1E-00, JSON.parse(" 1e-00")); | 59 Expect.equals(1E-00, parse(" 1e-00")); |
60 Expect.equals(1E+10, JSON.parse(" 1e+10")); | 60 Expect.equals(1E+10, parse(" 1e+10")); |
61 Expect.equals(1E+010, JSON.parse(" 1e+010")); | 61 Expect.equals(1E+010, parse(" 1e+010")); |
62 Expect.equals(1E+0010, JSON.parse(" 1e+0010")); | 62 Expect.equals(1E+0010, parse(" 1e+0010")); |
63 Expect.equals(1E10, JSON.parse(" 1e10")); | 63 Expect.equals(1E10, parse(" 1e10")); |
64 Expect.equals(1E010, JSON.parse(" 1e010")); | 64 Expect.equals(1E010, parse(" 1e010")); |
65 Expect.equals(1E0010, JSON.parse(" 1e0010")); | 65 Expect.equals(1E0010, parse(" 1e0010")); |
66 Expect.equals(1E-10, JSON.parse(" 1e-10")); | 66 Expect.equals(1E-10, parse(" 1e-10")); |
67 Expect.equals(1E-0010, JSON.parse(" 1e-0010")); | 67 Expect.equals(1E-0010, parse(" 1e-0010")); |
68 Expect.equals(true, JSON.parse(' true ')); | 68 Expect.equals(true, parse(' true ')); |
69 Expect.equals(false, JSON.parse(' false')); | 69 Expect.equals(false, parse(' false')); |
70 Expect.equals(null, JSON.parse(' null ')); | 70 Expect.equals(null, parse(' null ')); |
71 Expect.equals(null, JSON.parse('\n\rnull\t')); | 71 Expect.equals(null, parse('\n\rnull\t')); |
72 Expect.equals('hi there" bob', JSON.parse(' "hi there\\" bob" ')); | 72 Expect.equals('hi there" bob', parse(' "hi there\\" bob" ')); |
73 Expect.equals('', JSON.parse(' "" ')); | 73 Expect.equals('', parse(' "" ')); |
74 | 74 |
75 // Lists. | 75 // Lists. |
76 Expect.listEquals([], JSON.parse(' [] ')); | 76 Expect.listEquals([], parse(' [] ')); |
77 Expect.listEquals(["entry"], JSON.parse(' ["entry"] ')); | 77 Expect.listEquals(["entry"], parse(' ["entry"] ')); |
78 Expect.listEquals([true, false], JSON.parse(' [true, false] ')); | 78 Expect.listEquals([true, false], parse(' [true, false] ')); |
79 Expect.listEquals([1, 2, 3], JSON.parse(' [ 1 , 2 , 3 ] ')); | 79 Expect.listEquals([1, 2, 3], parse(' [ 1 , 2 , 3 ] ')); |
80 | 80 |
81 // Maps. | 81 // Maps. |
82 Expect.mapEquals({}, JSON.parse(' {} ')); | 82 Expect.mapEquals({}, parse(' {} ')); |
83 Expect.mapEquals({"key": "value"}, JSON.parse(' {"key": "value" } ')); | 83 Expect.mapEquals({"key": "value"}, parse(' {"key": "value" } ')); |
84 Expect.mapEquals({"key1": 1, "key2": 2}, | 84 Expect.mapEquals({"key1": 1, "key2": 2}, |
85 JSON.parse(' {"key1": 1, "key2": 2} ')); | 85 parse(' {"key1": 1, "key2": 2} ')); |
86 Expect.mapEquals({"key1": 1}, | 86 Expect.mapEquals({"key1": 1}, |
87 JSON.parse(' { "key1" : 1 } ')); | 87 parse(' { "key1" : 1 } ')); |
88 } | 88 } |
89 | 89 |
90 void testParseInvalid() { | 90 void testParseInvalid() { |
91 void testString(String s) { | 91 void testString(String s) { |
92 // TODO(ajohnsen): Require JSONParseException exception once all JSON libs | 92 Expect.throws(() => parse(s), (e) => e is FormatException); |
93 // have been updated. | |
94 Expect.throws(() => JSON.parse(s)); | |
95 } | 93 } |
96 // Scalars | 94 // Scalars |
97 testString(""); | 95 testString(""); |
98 testString("-"); | 96 testString("-"); |
99 testString("-."); | 97 testString("-."); |
100 testString("3.a"); | 98 testString("3.a"); |
101 testString("{ key: value }"); | 99 testString("{ key: value }"); |
102 testString("tru"); | 100 testString("tru"); |
103 testString("1E--6"); | 101 testString("1E--6"); |
104 testString("1E-+6"); | 102 testString("1E-+6"); |
(...skipping 25 matching lines...) Expand all Loading... |
130 testString('["", ]'); | 128 testString('["", ]'); |
131 testString('["", "]'); | 129 testString('["", "]'); |
132 testString('["" ""]'); | 130 testString('["" ""]'); |
133 | 131 |
134 // Maps | 132 // Maps |
135 testString('{"" ""}'); | 133 testString('{"" ""}'); |
136 testString('{"": "",}'); | 134 testString('{"": "",}'); |
137 } | 135 } |
138 | 136 |
139 void testEscaping() { | 137 void testEscaping() { |
140 Expect.stringEquals('""', JSON.stringify('')); | 138 Expect.stringEquals('""', stringify('')); |
141 Expect.stringEquals('"\\u0000"', JSON.stringify('\u0000')); | 139 Expect.stringEquals('"\\u0000"', stringify('\u0000')); |
142 Expect.stringEquals('"\\u0001"', JSON.stringify('\u0001')); | 140 Expect.stringEquals('"\\u0001"', stringify('\u0001')); |
143 Expect.stringEquals('"\\u0002"', JSON.stringify('\u0002')); | 141 Expect.stringEquals('"\\u0002"', stringify('\u0002')); |
144 Expect.stringEquals('"\\u0003"', JSON.stringify('\u0003')); | 142 Expect.stringEquals('"\\u0003"', stringify('\u0003')); |
145 Expect.stringEquals('"\\u0004"', JSON.stringify('\u0004')); | 143 Expect.stringEquals('"\\u0004"', stringify('\u0004')); |
146 Expect.stringEquals('"\\u0005"', JSON.stringify('\u0005')); | 144 Expect.stringEquals('"\\u0005"', stringify('\u0005')); |
147 Expect.stringEquals('"\\u0006"', JSON.stringify('\u0006')); | 145 Expect.stringEquals('"\\u0006"', stringify('\u0006')); |
148 Expect.stringEquals('"\\u0007"', JSON.stringify('\u0007')); | 146 Expect.stringEquals('"\\u0007"', stringify('\u0007')); |
149 Expect.stringEquals('"\\b"', JSON.stringify('\u0008')); | 147 Expect.stringEquals('"\\b"', stringify('\u0008')); |
150 Expect.stringEquals('"\\t"', JSON.stringify('\u0009')); | 148 Expect.stringEquals('"\\t"', stringify('\u0009')); |
151 Expect.stringEquals('"\\n"', JSON.stringify('\u000a')); | 149 Expect.stringEquals('"\\n"', stringify('\u000a')); |
152 Expect.stringEquals('"\\u000b"', JSON.stringify('\u000b')); | 150 Expect.stringEquals('"\\u000b"', stringify('\u000b')); |
153 Expect.stringEquals('"\\f"', JSON.stringify('\u000c')); | 151 Expect.stringEquals('"\\f"', stringify('\u000c')); |
154 Expect.stringEquals('"\\r"', JSON.stringify('\u000d')); | 152 Expect.stringEquals('"\\r"', stringify('\u000d')); |
155 Expect.stringEquals('"\\u000e"', JSON.stringify('\u000e')); | 153 Expect.stringEquals('"\\u000e"', stringify('\u000e')); |
156 Expect.stringEquals('"\\u000f"', JSON.stringify('\u000f')); | 154 Expect.stringEquals('"\\u000f"', stringify('\u000f')); |
157 Expect.stringEquals('"\\u0010"', JSON.stringify('\u0010')); | 155 Expect.stringEquals('"\\u0010"', stringify('\u0010')); |
158 Expect.stringEquals('"\\u0011"', JSON.stringify('\u0011')); | 156 Expect.stringEquals('"\\u0011"', stringify('\u0011')); |
159 Expect.stringEquals('"\\u0012"', JSON.stringify('\u0012')); | 157 Expect.stringEquals('"\\u0012"', stringify('\u0012')); |
160 Expect.stringEquals('"\\u0013"', JSON.stringify('\u0013')); | 158 Expect.stringEquals('"\\u0013"', stringify('\u0013')); |
161 Expect.stringEquals('"\\u0014"', JSON.stringify('\u0014')); | 159 Expect.stringEquals('"\\u0014"', stringify('\u0014')); |
162 Expect.stringEquals('"\\u0015"', JSON.stringify('\u0015')); | 160 Expect.stringEquals('"\\u0015"', stringify('\u0015')); |
163 Expect.stringEquals('"\\u0016"', JSON.stringify('\u0016')); | 161 Expect.stringEquals('"\\u0016"', stringify('\u0016')); |
164 Expect.stringEquals('"\\u0017"', JSON.stringify('\u0017')); | 162 Expect.stringEquals('"\\u0017"', stringify('\u0017')); |
165 Expect.stringEquals('"\\u0018"', JSON.stringify('\u0018')); | 163 Expect.stringEquals('"\\u0018"', stringify('\u0018')); |
166 Expect.stringEquals('"\\u0019"', JSON.stringify('\u0019')); | 164 Expect.stringEquals('"\\u0019"', stringify('\u0019')); |
167 Expect.stringEquals('"\\u001a"', JSON.stringify('\u001a')); | 165 Expect.stringEquals('"\\u001a"', stringify('\u001a')); |
168 Expect.stringEquals('"\\u001b"', JSON.stringify('\u001b')); | 166 Expect.stringEquals('"\\u001b"', stringify('\u001b')); |
169 Expect.stringEquals('"\\u001c"', JSON.stringify('\u001c')); | 167 Expect.stringEquals('"\\u001c"', stringify('\u001c')); |
170 Expect.stringEquals('"\\u001d"', JSON.stringify('\u001d')); | 168 Expect.stringEquals('"\\u001d"', stringify('\u001d')); |
171 Expect.stringEquals('"\\u001e"', JSON.stringify('\u001e')); | 169 Expect.stringEquals('"\\u001e"', stringify('\u001e')); |
172 Expect.stringEquals('"\\u001f"', JSON.stringify('\u001f')); | 170 Expect.stringEquals('"\\u001f"', stringify('\u001f')); |
173 Expect.stringEquals('"\\\""', JSON.stringify('"')); | 171 Expect.stringEquals('"\\\""', stringify('"')); |
174 Expect.stringEquals('"\\\\"', JSON.stringify('\\')); | 172 Expect.stringEquals('"\\\\"', stringify('\\')); |
175 Expect.stringEquals('"Got \\b, \\f, \\n, \\r, \\t, \\u0000, \\\\, and \\"."', | 173 Expect.stringEquals('"Got \\b, \\f, \\n, \\r, \\t, \\u0000, \\\\, and \\"."', |
176 JSON.stringify('Got \b, \f, \n, \r, \t, \u0000, \\, and ".')); | 174 stringify('Got \b, \f, \n, \r, \t, \u0000, \\, and ".')); |
177 Expect.stringEquals('"Got \\b\\f\\n\\r\\t\\u0000\\\\\\"."', | 175 Expect.stringEquals('"Got \\b\\f\\n\\r\\t\\u0000\\\\\\"."', |
178 JSON.stringify('Got \b\f\n\r\t\u0000\\".')); | 176 stringify('Got \b\f\n\r\t\u0000\\".')); |
179 } | 177 } |
OLD | NEW |