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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 class ExpandoTest { | 7 class ExpandoTest { |
8 static Expando<int> visits; | 8 static Expando<int> visits; |
9 | 9 |
10 static testMain() { | 10 static testMain() { |
11 visits = new Expando<int>('visits'); | 11 visits = new Expando<int>('visits'); |
12 var legal = [ new Object(), | 12 var legal = [ new Object(), |
13 new List(), [1,2,3], const [1,2,3], | 13 new List(), [1,2,3], const [1,2,3], |
14 new Map(), {'x':1,'y':2}, const {'x':1,'y':2}, | 14 new Map(), {'x':1,'y':2}, const {'x':1,'y':2}, |
15 new Expando(), new Expando('horse') ]; | 15 new Expando(), new Expando('horse') ]; |
16 for (var object in legal) { | 16 for (var object in legal) { |
17 testNamedExpando(object); | 17 testNamedExpando(object); |
18 testUnnamedExpando(object); | 18 testUnnamedExpando(object); |
19 } | 19 } |
20 for (var object in legal) { | 20 for (var object in legal) { |
21 Expect.equals(2, visits[object]); | 21 Expect.equals(2, visits[object]); |
22 } | 22 } |
23 testIllegal(); | 23 testIllegal(); |
| 24 testIdentity(); |
24 } | 25 } |
25 | 26 |
26 static visit(object) { | 27 static visit(object) { |
27 int count = visits[object]; | 28 int count = visits[object]; |
28 count = (count == null) ? 1 : count + 1; | 29 count = (count == null) ? 1 : count + 1; |
29 visits[object] = count; | 30 visits[object] = count; |
30 } | 31 } |
31 | 32 |
32 static testNamedExpando(object) { | 33 static testNamedExpando(object) { |
33 Expando<int> expando = new Expando<int>('myexpando'); | 34 Expando<int> expando = new Expando<int>('myexpando'); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 => exception is ArgumentError); | 72 => exception is ArgumentError); |
72 Expect.throws(() => expando[42], (exception) | 73 Expect.throws(() => expando[42], (exception) |
73 => exception is ArgumentError); | 74 => exception is ArgumentError); |
74 Expect.throws(() => expando[42.87], (exception) | 75 Expect.throws(() => expando[42.87], (exception) |
75 => exception is ArgumentError); | 76 => exception is ArgumentError); |
76 Expect.throws(() => expando[true], (exception) | 77 Expect.throws(() => expando[true], (exception) |
77 => exception is ArgumentError); | 78 => exception is ArgumentError); |
78 Expect.throws(() => expando[false], (exception) | 79 Expect.throws(() => expando[false], (exception) |
79 => exception is ArgumentError); | 80 => exception is ArgumentError); |
80 } | 81 } |
| 82 |
| 83 static testIdentity() { |
| 84 // Expando only depends on identity of object. |
| 85 Expando<int> expando = new Expando<int>(); |
| 86 var m1 = new Mutable(1); |
| 87 var m2 = new Mutable(7); |
| 88 var m3 = new Mutable(13); |
| 89 expando[m1] = 42; |
| 90 Expect.equals(42, expando[m1]); |
| 91 m1.id = 37; |
| 92 Expect.equals(42, expando[m1]); |
| 93 expando[m2] = 37; |
| 94 expando[m3] = 10; |
| 95 m3.id = 1; |
| 96 Expect.equals(42, expando[m1]); |
| 97 Expect.equals(37, expando[m2]); |
| 98 Expect.equals(10, expando[m3]); |
| 99 } |
81 } | 100 } |
82 | 101 |
83 main() => ExpandoTest.testMain(); | 102 main() => ExpandoTest.testMain(); |
| 103 |
| 104 class Mutable { |
| 105 int id; |
| 106 Mutable(this.id); |
| 107 int get hashCode => id; |
| 108 bool operator==(other) => other is Mutable && other.id == id; |
| 109 } |
OLD | NEW |