OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 import 'package:expect/expect.dart'; |
| 6 import 'package:async_helper/async_helper.dart'; |
| 7 import 'dart:async'; |
| 8 |
| 9 main() { |
| 10 // Unique object. |
| 11 var baz = new Object(); |
| 12 // Not so unique object that thinks it's the same as baz. |
| 13 var mimic = new Mimic(baz); |
| 14 |
| 15 // runGuarded calls run, captures the synchronous error (if any) and |
| 16 // gives that one to handleUncaughtError. |
| 17 |
| 18 Expect.identical(Zone.ROOT, Zone.current); |
| 19 |
| 20 // Create a map with various key types. |
| 21 Map zoneValues = new Map(); |
| 22 zoneValues[#foo] = 499; |
| 23 zoneValues["bar"] = []; |
| 24 zoneValues[baz] = "baz"; |
| 25 zoneValues[0] = "zero!"; |
| 26 zoneValues[null] = baz; |
| 27 |
| 28 Zone forked = Zone.current.fork(zoneValues: zoneValues); |
| 29 |
| 30 // Values are not present when not inside the zone. |
| 31 Expect.identical(Zone.ROOT, Zone.current); |
| 32 Expect.isNull(Zone.current[#foo]); |
| 33 Expect.isNull(Zone.current["bar"]); |
| 34 Expect.isNull(Zone.current[baz]); |
| 35 Expect.isNull(Zone.current[mimic]); |
| 36 Expect.isNull(Zone.current[0]); |
| 37 Expect.isNull(Zone.current[null]); |
| 38 Expect.isNull(Zone.current["qux"]); |
| 39 |
| 40 // Changing the original map has no effect after the zone is created. |
| 41 zoneValues[#foo] = -1; |
| 42 |
| 43 // Values are available directly on the zone. |
| 44 Expect.equals(499, forked[#foo]); |
| 45 Expect.listEquals([], forked["bar"]); |
| 46 Expect.equals("baz", forked[baz]); |
| 47 Expect.isNull(Zone.current[mimic]); |
| 48 Expect.equals("zero!", forked[0]); |
| 49 Expect.equals("zero!", forked[0.0]); // Lookup uses equality. |
| 50 Expect.equals("zero!", forked[-0.0]); |
| 51 Expect.equals(baz, forked[null]); |
| 52 Expect.isNull(forked["qux"]); |
| 53 |
| 54 forked.run(() { |
| 55 Expect.identical(forked, Zone.current); // Sanity check. |
| 56 // Values are present on current when inside zone. |
| 57 Expect.equals(499, Zone.current[#foo]); |
| 58 Expect.listEquals([], Zone.current["bar"]); |
| 59 Expect.equals("baz", Zone.current[baz]); |
| 60 Expect.isNull(Zone.current[mimic]); |
| 61 Expect.equals("zero!", Zone.current[0]); |
| 62 Expect.equals("zero!", Zone.current[0.0]); // Lookup uses equality. |
| 63 Expect.equals("zero!", Zone.current[-0.0]); |
| 64 Expect.equals(baz, Zone.current[null]); |
| 65 Expect.isNull(Zone.current["qux"]); |
| 66 }); |
| 67 |
| 68 // Values are still not present when not inside the zone. |
| 69 Expect.identical(Zone.ROOT, Zone.current); |
| 70 Expect.isNull(Zone.current[#foo]); |
| 71 Expect.isNull(Zone.current["bar"]); |
| 72 Expect.isNull(Zone.current[baz]); |
| 73 Expect.isNull(Zone.current[mimic]); |
| 74 Expect.isNull(Zone.current[0]); |
| 75 Expect.isNull(Zone.current[null]); |
| 76 Expect.isNull(Zone.current["qux"]); |
| 77 |
| 78 // Modifying the stored values work as expected. |
| 79 zoneValues["bar"].add(42); |
| 80 |
| 81 forked.run(() { |
| 82 Expect.identical(forked, Zone.current); // Sanity check. |
| 83 // Values are still there when inside the zone. The list was modified. |
| 84 Expect.equals(499, Zone.current[#foo]); |
| 85 Expect.listEquals([42], Zone.current["bar"]); |
| 86 Expect.equals("baz", Zone.current[baz]); |
| 87 Expect.isNull(Zone.current[mimic]); |
| 88 Expect.equals("zero!", Zone.current[0]); |
| 89 Expect.equals(baz, Zone.current[null]); |
| 90 Expect.isNull(Zone.current["qux"]); |
| 91 }); |
| 92 |
| 93 // Creating a further nested zone with new values allows keeping, overriding, |
| 94 // and shadowing existing values from the outer zone. |
| 95 zoneValues = new Map(); |
| 96 zoneValues[#foo] = -499; // Values can be overridden. |
| 97 zoneValues["bar"] = null; // Values can be changed to null. |
| 98 zoneValues["qux"] = 99; // Values can be added |
| 99 // Overriding with equal, but not identical, key is possible. |
| 100 zoneValues[mimic] = "floo"; |
| 101 zoneValues[0.0] = "zero!ZERO!"; |
| 102 |
| 103 Zone forkedChild = forked.fork(zoneValues: zoneValues); |
| 104 |
| 105 // New values available on zone. |
| 106 Expect.equals(-499, forkedChild[#foo]); // Overridden. |
| 107 Expect.isNull(forkedChild["bar"]); // Overridden to null. |
| 108 Expect.equals("floo", forkedChild[baz]); // Overridden by mimic. |
| 109 Expect.equals("floo", forkedChild[mimic]); // Now recognizes mimic. |
| 110 Expect.equals("zero!ZERO!", forkedChild[0]); // Overridden by 0.0. |
| 111 Expect.equals("zero!ZERO!", forkedChild[0.0]); // Overriding 0. |
| 112 Expect.equals(baz, forkedChild[null]); // Inherited. |
| 113 Expect.equals(99, forkedChild["qux"]); // Added. |
| 114 |
| 115 forkedChild.run(() { |
| 116 Expect.identical(forkedChild, Zone.current); // Sanity check. |
| 117 // New values available on current zone when the zone is current. |
| 118 Expect.equals(-499, Zone.current[#foo]); // Overridden. |
| 119 Expect.isNull(Zone.current["bar"]); // Overridden to null. |
| 120 Expect.equals("floo", Zone.current[baz]); // Overridden by mimic. |
| 121 Expect.equals("floo", Zone.current[mimic]); // Now recognizes mimic. |
| 122 Expect.equals("zero!ZERO!", Zone.current[0]); // Overridden by 0.0. |
| 123 Expect.equals("zero!ZERO!", Zone.current[0.0]); // Overriding 0. |
| 124 Expect.equals(baz, Zone.current[null]); // Inherited. |
| 125 Expect.equals(99, Zone.current["qux"]); // Added. |
| 126 }); |
| 127 |
| 128 // Parent zone values are unchanged. |
| 129 Expect.equals(499, forked[#foo]); |
| 130 Expect.listEquals([42], forked["bar"]); |
| 131 Expect.equals("baz", forked[baz]); |
| 132 Expect.isNull(Zone.current[mimic]); |
| 133 Expect.equals("zero!", forked[0]); |
| 134 Expect.equals("zero!", forked[0.0]); // Lookup uses equality. |
| 135 Expect.equals("zero!", forked[-0.0]); |
| 136 Expect.equals(baz, forked[null]); |
| 137 Expect.isNull(forked["qux"]); |
| 138 } |
| 139 |
| 140 // Class of objects that consider themselves equal to their originals. |
| 141 // Sees through mimickry. |
| 142 class Mimic { |
| 143 final Object original; |
| 144 Mimic(this.original); |
| 145 int get hashCode => original.hashCode; |
| 146 bool operator==(Object other) => |
| 147 (other is Mimic) ? this == other.original : original == other; |
| 148 } |
OLD | NEW |