|
| 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 |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +library json_map_test; |
| 6 | + |
| 7 | +import "package:expect/expect.dart"; |
| 8 | +import 'dart:convert' show json; |
| 9 | +import 'dart:collection' show LinkedHashMap, HashMap; |
| 10 | + |
| 11 | +bool useReviver = false; |
| 12 | +Map<String, dynamic> 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 | +} |
| 18 | + |
| 19 | +List listEach(Map map) { |
| 20 | + var result = []; |
| 21 | + map.forEach((key, value) { |
| 22 | + result.add(key); |
| 23 | + result.add(value); |
| 24 | + }); |
| 25 | + return result; |
| 26 | +} |
| 27 | + |
| 28 | +void main() { |
| 29 | + test(false); |
| 30 | + test(true); |
| 31 | +} |
| 32 | + |
| 33 | +void test(bool revive) { |
| 34 | + useReviver = revive; |
| 35 | + testEmpty(jsonify({})); |
| 36 | + testAtoB(jsonify({'a': 'b'})); |
| 37 | + |
| 38 | + // You can write 'Map<String, dynamic>' here (or 'var' which infers the |
| 39 | + // same), but if you write just 'Map' as the type, then the type of the |
| 40 | + // constant argument in the addAll below is not inferred correctly. |
| 41 | + var map = jsonify({}); |
| 42 | + map['a'] = 'b'; |
| 43 | + testAtoB(map); |
| 44 | + |
| 45 | + map = jsonify({}); |
| 46 | + Expect.equals('b', map.putIfAbsent('a', () => 'b')); |
| 47 | + testAtoB(map); |
| 48 | + |
| 49 | + map = jsonify({}); |
| 50 | + map.addAll({'a': 'b'}); |
| 51 | + testAtoB(map); |
| 52 | + |
| 53 | + testOrder(['a', 'b', 'c', 'd', 'e', 'f']); |
| 54 | + |
| 55 | + testProto(); |
| 56 | + testToString(); |
| 57 | + testConcurrentModifications(); |
| 58 | + testType(); |
| 59 | + testClear(); |
| 60 | + |
| 61 | + testListEntry(); |
| 62 | + testMutation(); |
| 63 | +} |
| 64 | + |
| 65 | +void testEmpty(Map map) { |
| 66 | + for (int i = 0; i < 2; i++) { |
| 67 | + Expect.equals(0, map.length); |
| 68 | + Expect.isTrue(map.isEmpty); |
| 69 | + Expect.isFalse(map.isNotEmpty); |
| 70 | + Expect.listEquals([], map.keys.toList()); |
| 71 | + Expect.listEquals([], map.values.toList()); |
| 72 | + Expect.isNull(map['a']); |
| 73 | + Expect.listEquals([], listEach(map)); |
| 74 | + Expect.isFalse(map.containsKey('a')); |
| 75 | + Expect.isFalse(map.containsValue('a')); |
| 76 | + Expect.isNull(map.remove('a')); |
| 77 | + testLookupNonExistingKeys(map); |
| 78 | + testLookupNonExistingValues(map); |
| 79 | + map.clear(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void testAtoB(Map map) { |
| 84 | + Expect.equals(1, map.length); |
| 85 | + Expect.isFalse(map.isEmpty); |
| 86 | + Expect.isTrue(map.isNotEmpty); |
| 87 | + Expect.listEquals(['a'], map.keys.toList()); |
| 88 | + Expect.listEquals(['b'], map.values.toList()); |
| 89 | + Expect.equals('b', map['a']); |
| 90 | + Expect.listEquals(['a', 'b'], listEach(map)); |
| 91 | + Expect.isTrue(map.containsKey('a')); |
| 92 | + Expect.isFalse(map.containsKey('b')); |
| 93 | + Expect.isTrue(map.containsValue('b')); |
| 94 | + Expect.isFalse(map.containsValue('a')); |
| 95 | + |
| 96 | + testLookupNonExistingKeys(map); |
| 97 | + testLookupNonExistingValues(map); |
| 98 | + Expect.equals('b', map.remove('a')); |
| 99 | + Expect.isNull(map.remove('b')); |
| 100 | + testLookupNonExistingKeys(map); |
| 101 | + testLookupNonExistingValues(map); |
| 102 | + |
| 103 | + map.clear(); |
| 104 | + testEmpty(map); |
| 105 | +} |
| 106 | + |
| 107 | +void testLookupNonExistingKeys(Map map) { |
| 108 | + for (var key in ['__proto__', 'null', null]) { |
| 109 | + Expect.isNull(map[key]); |
| 110 | + Expect.isFalse(map.containsKey(key)); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void testLookupNonExistingValues(Map map) { |
| 115 | + for (var value in ['__proto__', 'null', null]) { |
| 116 | + Expect.isFalse(map.containsValue(value)); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +void testOrder(List list) { |
| 121 | + if (list.isEmpty) |
| 122 | + return; |
| 123 | + else |
| 124 | + testOrder(list.skip(1).toList()); |
| 125 | + |
| 126 | + Map original = {}; |
| 127 | + for (int i = 0; i < list.length; i++) { |
| 128 | + original[list[i]] = i; |
| 129 | + } |
| 130 | + |
| 131 | + Map map = jsonify(original); |
| 132 | + Expect.equals(list.length, map.length); |
| 133 | + Expect.listEquals(list, map.keys.toList()); |
| 134 | + |
| 135 | + for (int i = 0; i < 10; i++) { |
| 136 | + map["$i"] = i; |
| 137 | + Expect.equals(list.length + i + 1, map.length); |
| 138 | + Expect.listEquals(list, map.keys.take(list.length).toList()); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +void testProto() { |
| 143 | + Map map = jsonify({'__proto__': 0}); |
| 144 | + Expect.equals(1, map.length); |
| 145 | + Expect.isTrue(map.containsKey('__proto__')); |
| 146 | + Expect.listEquals(['__proto__'], map.keys.toList()); |
| 147 | + Expect.equals(0, map['__proto__']); |
| 148 | + Expect.equals(0, map.remove('__proto__')); |
| 149 | + testEmpty(map); |
| 150 | + |
| 151 | + map = jsonify({'__proto__': null}); |
| 152 | + Expect.equals(1, map.length); |
| 153 | + Expect.isTrue(map.containsKey('__proto__')); |
| 154 | + Expect.listEquals(['__proto__'], map.keys.toList()); |
| 155 | + Expect.isNull(map['__proto__']); |
| 156 | + Expect.isNull(map.remove('__proto__')); |
| 157 | + testEmpty(map); |
| 158 | +} |
| 159 | + |
| 160 | +void testToString() { |
| 161 | + Expect.equals("{}", jsonify({}).toString()); |
| 162 | + Expect.equals("{a: 0}", jsonify({'a': 0}).toString()); |
| 163 | +} |
| 164 | + |
| 165 | +void testConcurrentModifications() { |
| 166 | + void testIterate(Map map, Iterable iterable, Function f) { |
| 167 | + Iterator iterator = iterable.iterator; |
| 168 | + f(map); |
| 169 | + iterator.moveNext(); |
| 170 | + } |
| 171 | + |
| 172 | + void testKeys(Map map, Function f) => testIterate(map, map.keys, f); |
| 173 | + void testValues(Map map, Function f) => testIterate(map, map.values, f); |
| 174 | + |
| 175 | + void testForEach(Map map, Function f) { |
| 176 | + map.forEach((key, value) { |
| 177 | + f(map); |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + bool throwsCME(Function f) { |
| 182 | + try { |
| 183 | + f(); |
| 184 | + } on ConcurrentModificationError catch (e) { |
| 185 | + return true; |
| 186 | + } catch (e) { |
| 187 | + return false; |
| 188 | + } |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + Map map = {}; |
| 193 | + Expect.isTrue(throwsCME(() => testKeys(jsonify(map), (map) => map['a'] = 0))); |
| 194 | + Expect |
| 195 | + .isTrue(throwsCME(() => testValues(jsonify(map), (map) => map['a'] = 0))); |
| 196 | + Expect.isFalse( |
| 197 | + throwsCME(() => testForEach(jsonify(map), (map) => map['a'] = 0))); |
| 198 | + |
| 199 | + Expect.isFalse(throwsCME(() => testKeys(jsonify(map), (map) => map.clear()))); |
| 200 | + Expect |
| 201 | + .isFalse(throwsCME(() => testValues(jsonify(map), (map) => map.clear()))); |
| 202 | + Expect.isFalse( |
| 203 | + throwsCME(() => testForEach(jsonify(map), (map) => map.clear()))); |
| 204 | + |
| 205 | + Expect.isFalse( |
| 206 | + throwsCME(() => testKeys(jsonify(map), (map) => map.remove('a')))); |
| 207 | + Expect.isFalse( |
| 208 | + throwsCME(() => testValues(jsonify(map), (map) => map.remove('a')))); |
| 209 | + Expect.isFalse( |
| 210 | + throwsCME(() => testForEach(jsonify(map), (map) => map.remove('a')))); |
| 211 | + |
| 212 | + Expect.isTrue(throwsCME( |
| 213 | + () => testKeys(jsonify(map), (map) => map.putIfAbsent('a', () => 0)))); |
| 214 | + Expect.isTrue(throwsCME( |
| 215 | + () => testValues(jsonify(map), (map) => map.putIfAbsent('a', () => 0)))); |
| 216 | + Expect.isFalse(throwsCME( |
| 217 | + () => testForEach(jsonify(map), (map) => map.putIfAbsent('a', () => 0)))); |
| 218 | + |
| 219 | + Expect.isFalse( |
| 220 | + throwsCME(() => testKeys(jsonify(map), (map) => map.addAll({})))); |
| 221 | + Expect.isFalse( |
| 222 | + throwsCME(() => testValues(jsonify(map), (map) => map.addAll({})))); |
| 223 | + Expect.isFalse( |
| 224 | + throwsCME(() => testForEach(jsonify(map), (map) => map.addAll({})))); |
| 225 | + |
| 226 | + Expect.isTrue( |
| 227 | + throwsCME(() => testKeys(jsonify(map), (map) => map.addAll({'a': 0})))); |
| 228 | + Expect.isTrue( |
| 229 | + throwsCME(() => testValues(jsonify(map), (map) => map.addAll({'a': 0})))); |
| 230 | + Expect.isFalse(throwsCME( |
| 231 | + () => testForEach(jsonify(map), (map) => map.addAll({'a': 0})))); |
| 232 | + |
| 233 | + map = {'a': 1}; |
| 234 | + Expect |
| 235 | + .isFalse(throwsCME(() => testKeys(jsonify(map), (map) => map['a'] = 0))); |
| 236 | + Expect.isFalse( |
| 237 | + throwsCME(() => testValues(jsonify(map), (map) => map['a'] = 0))); |
| 238 | + Expect.isFalse( |
| 239 | + throwsCME(() => testForEach(jsonify(map), (map) => map['a'] = 0))); |
| 240 | + |
| 241 | + Expect.isTrue(throwsCME(() => testKeys(jsonify(map), (map) => map['b'] = 0))); |
| 242 | + Expect |
| 243 | + .isTrue(throwsCME(() => testValues(jsonify(map), (map) => map['b'] = 0))); |
| 244 | + Expect.isTrue( |
| 245 | + throwsCME(() => testForEach(jsonify(map), (map) => map['b'] = 0))); |
| 246 | + |
| 247 | + Expect.isTrue(throwsCME(() => testKeys(jsonify(map), (map) => map.clear()))); |
| 248 | + Expect |
| 249 | + .isTrue(throwsCME(() => testValues(jsonify(map), (map) => map.clear()))); |
| 250 | + Expect |
| 251 | + .isTrue(throwsCME(() => testForEach(jsonify(map), (map) => map.clear()))); |
| 252 | + |
| 253 | + Expect.isTrue( |
| 254 | + throwsCME(() => testKeys(jsonify(map), (map) => map.remove('a')))); |
| 255 | + Expect.isTrue( |
| 256 | + throwsCME(() => testValues(jsonify(map), (map) => map.remove('a')))); |
| 257 | + Expect.isTrue( |
| 258 | + throwsCME(() => testForEach(jsonify(map), (map) => map.remove('a')))); |
| 259 | + |
| 260 | + Expect.isFalse( |
| 261 | + throwsCME(() => testKeys(jsonify(map), (map) => map.remove('b')))); |
| 262 | + Expect.isFalse( |
| 263 | + throwsCME(() => testValues(jsonify(map), (map) => map.remove('b')))); |
| 264 | + Expect.isFalse( |
| 265 | + throwsCME(() => testForEach(jsonify(map), (map) => map.remove('b')))); |
| 266 | + |
| 267 | + Expect.isFalse(throwsCME( |
| 268 | + () => testKeys(jsonify(map), (map) => map.putIfAbsent('a', () => 0)))); |
| 269 | + Expect.isFalse(throwsCME( |
| 270 | + () => testValues(jsonify(map), (map) => map.putIfAbsent('a', () => 0)))); |
| 271 | + Expect.isFalse(throwsCME( |
| 272 | + () => testForEach(jsonify(map), (map) => map.putIfAbsent('a', () => 0)))); |
| 273 | + |
| 274 | + Expect.isTrue(throwsCME( |
| 275 | + () => testKeys(jsonify(map), (map) => map.putIfAbsent('b', () => 0)))); |
| 276 | + Expect.isTrue(throwsCME( |
| 277 | + () => testValues(jsonify(map), (map) => map.putIfAbsent('b', () => 0)))); |
| 278 | + Expect.isTrue(throwsCME( |
| 279 | + () => testForEach(jsonify(map), (map) => map.putIfAbsent('b', () => 0)))); |
| 280 | + |
| 281 | + Expect.isFalse( |
| 282 | + throwsCME(() => testKeys(jsonify(map), (map) => map.addAll({})))); |
| 283 | + Expect.isFalse( |
| 284 | + throwsCME(() => testValues(jsonify(map), (map) => map.addAll({})))); |
| 285 | + Expect.isFalse( |
| 286 | + throwsCME(() => testForEach(jsonify(map), (map) => map.addAll({})))); |
| 287 | + |
| 288 | + Expect.isFalse( |
| 289 | + throwsCME(() => testKeys(jsonify(map), (map) => map.addAll({'a': 0})))); |
| 290 | + Expect.isFalse( |
| 291 | + throwsCME(() => testValues(jsonify(map), (map) => map.addAll({'a': 0})))); |
| 292 | + Expect.isFalse(throwsCME( |
| 293 | + () => testForEach(jsonify(map), (map) => map.addAll({'a': 0})))); |
| 294 | + |
| 295 | + Expect.isTrue( |
| 296 | + throwsCME(() => testKeys(jsonify(map), (map) => map.addAll({'b': 0})))); |
| 297 | + Expect.isTrue( |
| 298 | + throwsCME(() => testValues(jsonify(map), (map) => map.addAll({'b': 0})))); |
| 299 | + Expect.isTrue(throwsCME( |
| 300 | + () => testForEach(jsonify(map), (map) => map.addAll({'b': 0})))); |
| 301 | +} |
| 302 | + |
| 303 | +void testType() { |
| 304 | + var map = jsonify({}); |
| 305 | + var type = "${map.runtimeType}"; |
| 306 | + |
| 307 | + // The documentation of json.decode doesn't actually specify that it returns |
| 308 | + // a map (it's marked dynamic), but it's a reasonable expectation if you |
| 309 | + // don't provide a reviver function. |
| 310 | + Expect.isTrue(map is Map, type); |
| 311 | + Expect.isTrue(map is Map<String, dynamic>, type); |
| 312 | + Expect.isFalse(map is Map<int, dynamic>, type); |
| 313 | +} |
| 314 | + |
| 315 | +void testClear() { |
| 316 | + Map map = jsonify({'a': 0}); |
| 317 | + map.clear(); |
| 318 | + Expect.equals(0, map.length); |
| 319 | +} |
| 320 | + |
| 321 | +void testListEntry() { |
| 322 | + Map map = jsonify({ |
| 323 | + 'a': [ |
| 324 | + 7, |
| 325 | + 8, |
| 326 | + {'b': 9} |
| 327 | + ] |
| 328 | + }); |
| 329 | + List list = map['a']; |
| 330 | + Expect.equals(3, list.length); |
| 331 | + Expect.equals(7, list[0]); |
| 332 | + Expect.equals(8, list[1]); |
| 333 | + Expect.equals(9, list[2]['b']); |
| 334 | +} |
| 335 | + |
| 336 | +void testMutation() { |
| 337 | + Map map = jsonify({'a': 0}); |
| 338 | + Expect.listEquals(['a', 0], listEach(map)); |
| 339 | + map['a'] = 1; |
| 340 | + Expect.listEquals(['a', 1], listEach(map)); |
| 341 | + map['a']++; |
| 342 | + Expect.listEquals(['a', 2], listEach(map)); |
| 343 | +} |
0 commit comments