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_map_test; | 5 library json_map_test; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:convert' show JSON; | 8 import 'dart:convert' show JSON; |
9 import 'dart:collection' show LinkedHashMap, HashMap; | 9 import 'dart:collection' show LinkedHashMap, HashMap; |
10 | 10 |
11 Map jsonify(Map map) => JSON.decode(JSON.encode(map)); | 11 bool useReviver = false; |
| 12 Map jsonify(Map map) { |
| 13 String encoded = JSON.encode(map); |
| 14 return useReviver |
| 15 ? JSON.decode(encoded, reviver: (key, value) => value) |
| 16 : JSON.decode(encoded); |
| 17 } |
12 | 18 |
13 List listEach(Map map) { | 19 List listEach(Map map) { |
14 var result = []; | 20 var result = []; |
15 map.forEach((String key, value) { | 21 map.forEach((String key, value) { |
16 result.add(key); | 22 result.add(key); |
17 result.add(value); | 23 result.add(value); |
18 }); | 24 }); |
19 return result; | 25 return result; |
20 } | 26 } |
21 | 27 |
22 void main() { | 28 void main() { |
| 29 test(false); |
| 30 test(true); |
| 31 } |
| 32 |
| 33 void test(bool revive) { |
| 34 useReviver = revive; |
23 testEmpty(jsonify({})); | 35 testEmpty(jsonify({})); |
24 testAtoB(jsonify({'a': 'b'})); | 36 testAtoB(jsonify({'a': 'b'})); |
25 | 37 |
26 Map map = jsonify({}); | 38 Map map = jsonify({}); |
27 map['a'] = 'b'; | 39 map['a'] = 'b'; |
28 testAtoB(map); | 40 testAtoB(map); |
29 | 41 |
30 map = jsonify({}); | 42 map = jsonify({}); |
31 Expect.equals('b', map.putIfAbsent('a', () => 'b')); | 43 Expect.equals('b', map.putIfAbsent('a', () => 'b')); |
32 testAtoB(map); | 44 testAtoB(map); |
33 | 45 |
34 map = jsonify({}); | 46 map = jsonify({}); |
35 map.addAll({'a': 'b'}); | 47 map.addAll({'a': 'b'}); |
36 testAtoB(map); | 48 testAtoB(map); |
37 | 49 |
38 testOrder(['a', 'b', 'c', 'd', 'e', 'f']); | 50 testOrder(['a', 'b', 'c', 'd', 'e', 'f']); |
39 | 51 |
40 testProto(); | 52 testProto(); |
41 testToString(); | 53 testToString(); |
42 testConcurrentModifications(); | 54 testConcurrentModifications(); |
43 testType(); | 55 testType(); |
44 testNonStringKeys(); | 56 testNonStringKeys(); |
45 testClear(); | 57 testClear(); |
46 | 58 |
47 testListEntry(); | 59 testListEntry(); |
| 60 testMutation(); |
48 } | 61 } |
49 | 62 |
50 void testEmpty(Map map) { | 63 void testEmpty(Map map) { |
51 for (int i = 0; i < 2; i++) { | 64 for (int i = 0; i < 2; i++) { |
52 Expect.equals(0, map.length); | 65 Expect.equals(0, map.length); |
53 Expect.isTrue(map.isEmpty); | 66 Expect.isTrue(map.isEmpty); |
54 Expect.isFalse(map.isNotEmpty); | 67 Expect.isFalse(map.isNotEmpty); |
55 Expect.listEquals([], map.keys.toList()); | 68 Expect.listEquals([], map.keys.toList()); |
56 Expect.listEquals([], map.values.toList()); | 69 Expect.listEquals([], map.values.toList()); |
57 Expect.isNull(map['a']); | 70 Expect.isNull(map['a']); |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 } | 328 } |
316 | 329 |
317 void testListEntry() { | 330 void testListEntry() { |
318 Map map = jsonify({'a': [7, 8, {'b': 9}]}); | 331 Map map = jsonify({'a': [7, 8, {'b': 9}]}); |
319 List list = map['a']; | 332 List list = map['a']; |
320 Expect.equals(3, list.length); | 333 Expect.equals(3, list.length); |
321 Expect.equals(7, list[0]); | 334 Expect.equals(7, list[0]); |
322 Expect.equals(8, list[1]); | 335 Expect.equals(8, list[1]); |
323 Expect.equals(9, list[2]['b']); | 336 Expect.equals(9, list[2]['b']); |
324 } | 337 } |
| 338 |
| 339 void testMutation() { |
| 340 Map map = jsonify({'a': 0}); |
| 341 Expect.listEquals(['a', 0], listEach(map)); |
| 342 map['a'] = 1; |
| 343 Expect.listEquals(['a', 1], listEach(map)); |
| 344 map['a']++; |
| 345 Expect.listEquals(['a', 2], listEach(map)); |
| 346 } |
OLD | NEW |