| 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 json_test; | 5 library json_test; |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import "dart:convert"; | 8 import "dart:convert"; |
| 9 | 9 |
| 10 bool badFormat(e) => e is FormatException; | 10 bool badFormat(e) => e is FormatException; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 testThrows('[$i"s"]'); | 259 testThrows('[$i"s"]'); |
| 260 testThrows('["s"$i]'); | 260 testThrows('["s"$i]'); |
| 261 testThrows('$i{"k":"v"}'); | 261 testThrows('$i{"k":"v"}'); |
| 262 testThrows('{$i"k":"v"}'); | 262 testThrows('{$i"k":"v"}'); |
| 263 testThrows('{"k"$i:"v"}'); | 263 testThrows('{"k"$i:"v"}'); |
| 264 testThrows('{"k":$i"v"}'); | 264 testThrows('{"k":$i"v"}'); |
| 265 testThrows('{"k":"v"$i}'); | 265 testThrows('{"k":"v"$i}'); |
| 266 } | 266 } |
| 267 } | 267 } |
| 268 | 268 |
| 269 void testOutput() { |
| 270 var nonJsonValue = new Object(); |
| 271 // Test that results are integers or doubles as appropriate. |
| 272 var res = JSON.decode("0"); |
| 273 Expect.equals(0, res); |
| 274 Expect.isTrue(res is int); |
| 275 res = JSON.decode("999999999"); |
| 276 Expect.equals(999999999, res); |
| 277 Expect.isTrue(res is int); |
| 278 res = JSON.decode("0.5"); |
| 279 Expect.equals(0.5, res); |
| 280 Expect.isTrue(res is double); |
| 281 res = JSON.decode("5e5"); |
| 282 Expect.equals(500000, res); |
| 283 Expect.isTrue(res is double); |
| 284 |
| 285 // Test that lists are extensible and untyped. |
| 286 res = JSON.decode("[42]"); |
| 287 Expect.isTrue(res is List); |
| 288 Expect.listEquals([42], res); |
| 289 res.add(37); |
| 290 Expect.listEquals([42, 37], res); |
| 291 res.add(nonJsonValue); |
| 292 Expect.listEquals([42, 37, nonJsonValue], res); |
| 293 // List handles concurrent modification correctly. |
| 294 res = JSON.decode("[1, 2, 3]"); |
| 295 res.forEach((v) { res[0] = 42; }); // Not a modification, does not throw. |
| 296 Expect.throws(() { res.forEach((v) { res.add(37); }); }, |
| 297 (e) => e is ConcurrentModificationError); |
| 298 |
| 299 // Test that maps are untyped, modifiable and ordered. |
| 300 res = JSON.decode('{"x": 42}'); |
| 301 Expect.isTrue(res is Map); |
| 302 Expect.equals(1, res.length); |
| 303 Expect.equals(42, res["x"]); |
| 304 res["y"] = nonJsonValue; |
| 305 Expect.equals(2, res.length); |
| 306 Expect.equals(nonJsonValue, res["y"]); |
| 307 // TODO(lrn): Can we avoid this? It would be great for optimizations |
| 308 // if maps must be string keyed. |
| 309 res[nonJsonValue] = 37; |
| 310 Expect.equals(3, res.length); |
| 311 Expect.equals(37, res[nonJsonValue]); |
| 312 // Concurrent modifications. |
| 313 res = JSON.decode('{"x": 42, "y": 37}'); |
| 314 res.forEach((k, v) { res["x"] = 99; }); // Not a modification, doesn't throw. |
| 315 |
| 316 res = JSON.decode('{"x": 42, "y": 37}'); |
| 317 Expect.throws(() { res.forEach((k, v) { res["z"] = 12; }); }, |
| 318 (e) => e is ConcurrentModificationError); |
| 319 res = JSON.decode('{"x": 42, "y": 37}'); |
| 320 Expect.throws(() { res.keys.forEach((k) { res["z"] = 12; }); }, |
| 321 (e) => e is ConcurrentModificationError); |
| 322 |
| 323 res = JSON.decode('{"x": 42, "y": 37}'); |
| 324 res.forEach((k, v) { res.remove("z"); }); |
| 325 |
| 326 res = JSON.decode('{"x": 42, "y": 37}'); |
| 327 Expect.throws(() { res.forEach((k, v) { res.remove("x"); }); }, |
| 328 (e) => e is ConcurrentModificationError); |
| 329 res = JSON.decode('{"x": 42, "y": 37}'); |
| 330 Expect.throws(() { res.keys.forEach((k) { res.remove("x"); }); }, |
| 331 (e) => e is ConcurrentModificationError); |
| 332 |
| 333 res = JSON.decode('{"x": 42, "y": 37}'); |
| 334 Expect.throws(() { res.forEach((k, v) { res.clear(); }); }, |
| 335 (e) => e is ConcurrentModificationError); |
| 336 |
| 337 res = JSON.decode('{"x": 42, "y": 37}'); |
| 338 Expect.throws(() { res.keys.forEach((k) { res.clear(); }); }, |
| 339 (e) => e is ConcurrentModificationError); |
| 340 |
| 341 res = JSON.decode('{"x": 42, "y": 37}'); |
| 342 Expect.throws(() { res.forEach((k, v) { res.putIfAbsent("z", () => 99); }); }, |
| 343 (e) => e is ConcurrentModificationError); |
| 344 |
| 345 res = JSON.decode('{"x": 42, "y": 37}'); |
| 346 Expect.throws(() { |
| 347 res.keys.forEach((k) { res.putIfAbsent("z", () => 99); }); |
| 348 }, |
| 349 (e) => e is ConcurrentModificationError); |
| 350 } |
| 351 |
| 352 |
| 269 main() { | 353 main() { |
| 270 testNumbers(); | 354 testNumbers(); |
| 271 testStrings(); | 355 testStrings(); |
| 272 testWords(); | 356 testWords(); |
| 273 testObjects(); | 357 testObjects(); |
| 274 testArrays(); | 358 testArrays(); |
| 275 testWhitespace(); | 359 testWhitespace(); |
| 360 testOutput(); |
| 276 } | 361 } |
| OLD | NEW |