OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 json_tests; | 5 library json_tests; |
| 6 |
6 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
7 import 'dart:convert'; | 8 import 'dart:convert'; |
8 | 9 |
9 main() { | 10 main() { |
10 test('Parse', () { | 11 test('Parse', () { |
11 // Scalars. | 12 // Scalars. |
12 expect(JSON.decode(' 5 '), equals(5)); | 13 expect(JSON.decode(' 5 '), equals(5)); |
13 expect(JSON.decode(' -42 '), equals(-42)); | 14 expect(JSON.decode(' -42 '), equals(-42)); |
14 expect(JSON.decode(' 3e0 '), equals(3)); | 15 expect(JSON.decode(' 3e0 '), equals(3)); |
15 expect(JSON.decode(' 3.14 '), equals(3.14)); | 16 expect(JSON.decode(' 3.14 '), equals(3.14)); |
16 expect(JSON.decode('true '), isTrue); | 17 expect(JSON.decode('true '), isTrue); |
17 expect(JSON.decode(' false'), isFalse); | 18 expect(JSON.decode(' false'), isFalse); |
18 expect(JSON.decode(' null '), isNull); | 19 expect(JSON.decode(' null '), isNull); |
19 expect(JSON.decode('\n\rnull\t'), isNull); | 20 expect(JSON.decode('\n\rnull\t'), isNull); |
20 expect(JSON.decode(' "hi there\\" bob" '), equals('hi there" bob')); | 21 expect(JSON.decode(' "hi there\\" bob" '), equals('hi there" bob')); |
21 expect(JSON.decode(' "" '), isEmpty); | 22 expect(JSON.decode(' "" '), isEmpty); |
22 | 23 |
23 // Lists. | 24 // Lists. |
24 expect(JSON.decode(' [] '), isEmpty); | 25 expect(JSON.decode(' [] '), isEmpty); |
25 expect(JSON.decode('[ ]'), isEmpty); | 26 expect(JSON.decode('[ ]'), isEmpty); |
26 expect(JSON.decode(' [3, -4.5, true, "hi", false] '), | 27 expect(JSON.decode(' [3, -4.5, true, "hi", false] '), |
27 equals([3, -4.5, true, 'hi', false])); | 28 equals([3, -4.5, true, 'hi', false])); |
28 // Nulls are tricky. | 29 // Nulls are tricky. |
29 expect(JSON.decode('[null]'), orderedEquals([null])); | 30 expect(JSON.decode('[null]'), orderedEquals([null])); |
30 expect(JSON.decode(' [3, -4.5, null, true, "hi", false] '), | 31 expect(JSON.decode(' [3, -4.5, null, true, "hi", false] '), |
31 equals([3, -4.5, null, true, 'hi', false])); | 32 equals([3, -4.5, null, true, 'hi', false])); |
32 expect(JSON.decode('[[null]]'), equals([[null]])); | 33 expect( |
33 expect(JSON.decode(' [ [3], [], [null], ["hi", true]] '), | 34 JSON.decode('[[null]]'), |
34 equals([[3], [], [null], ['hi', true]])); | 35 equals([ |
| 36 [null] |
| 37 ])); |
| 38 expect( |
| 39 JSON.decode(' [ [3], [], [null], ["hi", true]] '), |
| 40 equals([ |
| 41 [3], |
| 42 [], |
| 43 [null], |
| 44 ['hi', true] |
| 45 ])); |
35 | 46 |
36 // Maps. | 47 // Maps. |
37 expect(JSON.decode(' {} '), isEmpty); | 48 expect(JSON.decode(' {} '), isEmpty); |
38 expect(JSON.decode('{ }'), isEmpty); | 49 expect(JSON.decode('{ }'), isEmpty); |
39 | 50 |
40 expect(JSON.decode( | 51 expect( |
41 ' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '), | 52 JSON.decode( |
42 equals({"x":3, "y": -4.5, "z" : "hi", "u" : true, "v": false })); | 53 ' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '), |
| 54 equals({"x": 3, "y": -4.5, "z": "hi", "u": true, "v": false})); |
43 | 55 |
44 expect(JSON.decode(' {"x":3, "y": -4.5, "z" : "hi" } '), | 56 expect(JSON.decode(' {"x":3, "y": -4.5, "z" : "hi" } '), |
45 equals({"x":3, "y": -4.5, "z" : "hi" })); | 57 equals({"x": 3, "y": -4.5, "z": "hi"})); |
46 | 58 |
47 expect(JSON.decode(' {"y": -4.5, "z" : "hi" ,"x":3 } '), | 59 expect(JSON.decode(' {"y": -4.5, "z" : "hi" ,"x":3 } '), |
48 equals({"y": -4.5, "z" : "hi" ,"x":3 })); | 60 equals({"y": -4.5, "z": "hi", "x": 3})); |
49 | 61 |
50 expect(JSON.decode('{ " hi bob " :3, "": 4.5}'), | 62 expect(JSON.decode('{ " hi bob " :3, "": 4.5}'), |
51 equals({ " hi bob " :3, "": 4.5})); | 63 equals({" hi bob ": 3, "": 4.5})); |
52 | 64 |
53 expect(JSON.decode(' { "x" : { } } '), equals({ 'x' : {}})); | 65 expect(JSON.decode(' { "x" : { } } '), equals({'x': {}})); |
54 expect(JSON.decode('{"x":{}}'), equals({ 'x' : {}})); | 66 expect(JSON.decode('{"x":{}}'), equals({'x': {}})); |
55 | 67 |
56 // Nulls are tricky. | 68 // Nulls are tricky. |
57 expect(JSON.decode('{"w":null}'), equals({ 'w' : null})); | 69 expect(JSON.decode('{"w":null}'), equals({'w': null})); |
58 | 70 |
59 expect(JSON.decode('{"x":{"w":null}}'), equals({"x":{"w":null}})); | 71 expect( |
| 72 JSON.decode('{"x":{"w":null}}'), |
| 73 equals({ |
| 74 "x": {"w": null} |
| 75 })); |
60 | 76 |
61 expect(JSON.decode(' {"x":3, "y": -4.5, "z" : "hi",' | 77 expect( |
62 '"w":null, "u" : true, "v": false } '), | 78 JSON.decode(' {"x":3, "y": -4.5, "z" : "hi",' |
63 equals({"x":3, "y": -4.5, "z" : "hi", | 79 '"w":null, "u" : true, "v": false } '), |
64 "w":null, "u" : true, "v": false })); | 80 equals( |
| 81 {"x": 3, "y": -4.5, "z": "hi", "w": null, "u": true, "v": false})); |
65 | 82 |
66 expect(JSON.decode('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 83 expect( |
67 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 84 JSON.decode('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |
68 equals({"x": {"a":3, "b": -4.5}, "y":[{}], | 85 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), |
69 "z":"hi","w":{"c":null,"d":true}, "v":null})); | 86 equals({ |
| 87 "x": {"a": 3, "b": -4.5}, |
| 88 "y": [{}], |
| 89 "z": "hi", |
| 90 "w": {"c": null, "d": true}, |
| 91 "v": null |
| 92 })); |
70 }); | 93 }); |
71 | 94 |
72 test('stringify', () { | 95 test('stringify', () { |
73 // Scalars. | 96 // Scalars. |
74 expect(JSON.encode(5), equals('5')); | 97 expect(JSON.encode(5), equals('5')); |
75 expect(JSON.encode(-42), equals('-42')); | 98 expect(JSON.encode(-42), equals('-42')); |
76 // Dart does not guarantee a formatting for doubles, | 99 // Dart does not guarantee a formatting for doubles, |
77 // so reparse and compare to the original. | 100 // so reparse and compare to the original. |
78 validateRoundTrip(3.14); | 101 validateRoundTrip(3.14); |
79 expect(JSON.encode(true), equals('true')); | 102 expect(JSON.encode(true), equals('true')); |
80 expect(JSON.encode(false), equals('false')); | 103 expect(JSON.encode(false), equals('false')); |
81 expect(JSON.encode(null), equals('null')); | 104 expect(JSON.encode(null), equals('null')); |
82 expect(JSON.encode(' hi there" bob '), equals('" hi there\\" bob "')); | 105 expect(JSON.encode(' hi there" bob '), equals('" hi there\\" bob "')); |
83 expect(JSON.encode('hi\\there'), equals('"hi\\\\there"')); | 106 expect(JSON.encode('hi\\there'), equals('"hi\\\\there"')); |
84 expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"')); | 107 expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"')); |
85 expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"')); | 108 expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"')); |
86 expect(JSON.encode(''), equals('""')); | 109 expect(JSON.encode(''), equals('""')); |
87 | 110 |
88 // Lists. | 111 // Lists. |
89 expect(JSON.encode([]), equals('[]')); | 112 expect(JSON.encode([]), equals('[]')); |
90 expect(JSON.encode(new List(0)), equals('[]')); | 113 expect(JSON.encode(new List(0)), equals('[]')); |
91 expect(JSON.encode(new List(3)), equals('[null,null,null]')); | 114 expect(JSON.encode(new List(3)), equals('[null,null,null]')); |
92 validateRoundTrip([3, -4.5, null, true, 'hi', false]); | 115 validateRoundTrip([3, -4.5, null, true, 'hi', false]); |
93 expect(JSON.encode([[3], [], [null], ['hi', true]]), | 116 expect( |
94 equals('[[3],[],[null],["hi",true]]')); | 117 JSON.encode([ |
| 118 [3], |
| 119 [], |
| 120 [null], |
| 121 ['hi', true] |
| 122 ]), |
| 123 equals('[[3],[],[null],["hi",true]]')); |
95 | 124 |
96 // Maps. | 125 // Maps. |
97 expect(JSON.encode({}), equals('{}')); | 126 expect(JSON.encode({}), equals('{}')); |
98 expect(JSON.encode(new Map()), equals('{}')); | 127 expect(JSON.encode(new Map()), equals('{}')); |
99 expect(JSON.encode({'x':{}}), equals('{"x":{}}')); | 128 expect(JSON.encode({'x': {}}), equals('{"x":{}}')); |
100 expect(JSON.encode({'x':{'a':3}}), equals('{"x":{"a":3}}')); | 129 expect( |
| 130 JSON.encode({ |
| 131 'x': {'a': 3} |
| 132 }), |
| 133 equals('{"x":{"a":3}}')); |
101 | 134 |
102 // Dart does not guarantee an order on the keys | 135 // Dart does not guarantee an order on the keys |
103 // of a map literal, so reparse and compare to the original Map. | 136 // of a map literal, so reparse and compare to the original Map. |
104 validateRoundTrip( | 137 validateRoundTrip( |
105 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false}); | 138 {'x': 3, 'y': -4.5, 'z': 'hi', 'w': null, 'u': true, 'v': false}); |
106 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'}); | 139 validateRoundTrip({"x": 3, "y": -4.5, "z": 'hi'}); |
107 validateRoundTrip({' hi bob ':3, '':4.5}); | 140 validateRoundTrip({' hi bob ': 3, '': 4.5}); |
108 validateRoundTrip( | 141 validateRoundTrip({ |
109 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, | 142 'x': {'a': 3, 'b': -4.5}, |
110 'v':null}); | 143 'y': [{}], |
| 144 'z': 'hi', |
| 145 'w': {'c': null, 'd': true}, |
| 146 'v': null |
| 147 }); |
111 | 148 |
112 expect(JSON.encode(new ToJson(4)), "4"); | 149 expect(JSON.encode(new ToJson(4)), "4"); |
113 expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]'); | 150 expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]'); |
114 expect(JSON.encode(new ToJson([4, new ToJson({"x":42})])), | 151 expect( |
115 '[4,{"x":42}]'); | 152 JSON.encode(new ToJson([ |
| 153 4, |
| 154 new ToJson({"x": 42}) |
| 155 ])), |
| 156 '[4,{"x":42}]'); |
116 | 157 |
117 expect(() { | 158 expect(() { |
118 JSON.encode([new ToJson(new ToJson(4))]); | 159 JSON.encode([new ToJson(new ToJson(4))]); |
119 }, throwsJsonError); | 160 }, throwsJsonError); |
120 | 161 |
121 expect(() { | 162 expect(() { |
122 JSON.encode([new Object()]); | 163 JSON.encode([new Object()]); |
123 }, throwsJsonError); | 164 }, throwsJsonError); |
124 | |
125 }); | 165 }); |
126 | 166 |
127 test('stringify throws if argument cannot be converted', () { | 167 test('stringify throws if argument cannot be converted', () { |
128 /** | 168 /** |
129 * Checks that we get an exception (rather than silently returning null) if | 169 * Checks that we get an exception (rather than silently returning null) if |
130 * we try to stringify something that cannot be converted to json. | 170 * we try to stringify something that cannot be converted to json. |
131 */ | 171 */ |
132 expect(() => JSON.encode(new TestClass()), throwsJsonError); | 172 expect(() => JSON.encode(new TestClass()), throwsJsonError); |
133 }); | 173 }); |
134 | 174 |
135 test('stringify throws if toJson throws', () { | 175 test('stringify throws if toJson throws', () { |
136 expect(() => JSON.encode(new ToJsoner("bad", throws: true)), | 176 expect( |
137 throwsJsonError); | 177 () => JSON.encode(new ToJsoner("bad", throws: true)), throwsJsonError); |
138 }); | 178 }); |
139 | 179 |
140 test('stringify throws if toJson returns non-serializable value', () { | 180 test('stringify throws if toJson returns non-serializable value', () { |
141 expect(() => JSON.encode(new ToJsoner(new TestClass())), | 181 expect(() => JSON.encode(new ToJsoner(new TestClass())), throwsJsonError); |
142 throwsJsonError); | |
143 }); | 182 }); |
144 | 183 |
145 test('stringify throws on cyclic values', () { | 184 test('stringify throws on cyclic values', () { |
146 var a = []; | 185 var a = []; |
147 var b = a; | 186 var b = a; |
148 for (int i = 0; i < 50; i++) { | 187 for (int i = 0; i < 50; i++) { |
149 b = [b]; | 188 b = [b]; |
150 } | 189 } |
151 a.add(b); | 190 a.add(b); |
152 expect(() => JSON.encode(a), throwsJsonError); | 191 expect(() => JSON.encode(a), throwsJsonError); |
153 }); | 192 }); |
154 } | 193 } |
155 | 194 |
156 class TestClass { | 195 class TestClass { |
157 int x; | 196 int x; |
158 String y; | 197 String y; |
159 | 198 |
160 TestClass() : x = 3, y = 'joe' { } | 199 TestClass() |
| 200 : x = 3, |
| 201 y = 'joe' {} |
161 } | 202 } |
162 | 203 |
163 class ToJsoner { | 204 class ToJsoner { |
164 final Object returnValue; | 205 final Object returnValue; |
165 final bool throws; | 206 final bool throws; |
166 ToJsoner(this.returnValue, {this.throws}); | 207 ToJsoner(this.returnValue, {this.throws}); |
167 Object toJson() { | 208 Object toJson() { |
168 if (throws) throw returnValue; | 209 if (throws) throw returnValue; |
169 return returnValue; | 210 return returnValue; |
170 } | 211 } |
171 } | 212 } |
172 | 213 |
173 class ToJson { | 214 class ToJson { |
174 final object; | 215 final object; |
175 const ToJson(this.object); | 216 const ToJson(this.object); |
176 toJson() => object; | 217 toJson() => object; |
177 } | 218 } |
178 | 219 |
179 var throwsJsonError = | 220 var throwsJsonError = throwsA(new isInstanceOf<JsonUnsupportedObjectError>()); |
180 throwsA(new isInstanceOf<JsonUnsupportedObjectError>()); | |
181 | 221 |
182 /** | 222 /** |
183 * Checks that the argument can be converted to a JSON string and | 223 * Checks that the argument can be converted to a JSON string and |
184 * back, and produce something equivalent to the argument. | 224 * back, and produce something equivalent to the argument. |
185 */ | 225 */ |
186 validateRoundTrip(expected) { | 226 validateRoundTrip(expected) { |
187 expect(JSON.decode(JSON.encode(expected)), equals(expected)); | 227 expect(JSON.decode(JSON.encode(expected)), equals(expected)); |
188 } | 228 } |
OLD | NEW |