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 = [ |
13 new List(), [1,2,3], const [1,2,3], | 13 new Object(), |
14 new Map(), {'x':1,'y':2}, const {'x':1,'y':2}, | 14 new List(), |
15 new Expando(), new Expando('horse') ]; | 15 [1, 2, 3], |
| 16 const [1, 2, 3], |
| 17 new Map(), |
| 18 {'x': 1, 'y': 2}, |
| 19 const {'x': 1, 'y': 2}, |
| 20 new Expando(), |
| 21 new Expando('horse') |
| 22 ]; |
16 for (var object in legal) { | 23 for (var object in legal) { |
17 testNamedExpando(object); | 24 testNamedExpando(object); |
18 testUnnamedExpando(object); | 25 testUnnamedExpando(object); |
19 } | 26 } |
20 for (var object in legal) { | 27 for (var object in legal) { |
21 Expect.equals(2, visits[object], "$object"); | 28 Expect.equals(2, visits[object], "$object"); |
22 } | 29 } |
23 testIllegal(); | 30 testIllegal(); |
24 testIdentity(); | 31 testIdentity(); |
25 } | 32 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 Expect.isNull(alternative[object]); | 64 Expect.isNull(alternative[object]); |
58 alternative[object] = 87; | 65 alternative[object] = 87; |
59 Expect.isNull(expando[object]); | 66 Expect.isNull(expando[object]); |
60 expando[object] = 99; | 67 expando[object] = 99; |
61 Expect.equals(99, expando[object]); | 68 Expect.equals(99, expando[object]); |
62 Expect.equals(87, alternative[object]); | 69 Expect.equals(87, alternative[object]); |
63 } | 70 } |
64 | 71 |
65 static testIllegal() { | 72 static testIllegal() { |
66 Expando<int> expando = new Expando<int>(); | 73 Expando<int> expando = new Expando<int>(); |
67 Expect.throws(() => expando[null], (exception) | 74 Expect.throws( |
68 => exception is ArgumentError, "null"); | 75 () => expando[null], (exception) => exception is ArgumentError, "null"); |
69 Expect.throws(() => expando['string'], (exception) | 76 Expect.throws(() => expando['string'], |
70 => exception is ArgumentError, "'string'"); | 77 (exception) => exception is ArgumentError, "'string'"); |
71 Expect.throws(() => expando['string'], (exception) | 78 Expect.throws(() => expando['string'], |
72 => exception is ArgumentError, "'string'"); | 79 (exception) => exception is ArgumentError, "'string'"); |
73 Expect.throws(() => expando[42], (exception) | 80 Expect.throws( |
74 => exception is ArgumentError, "42"); | 81 () => expando[42], (exception) => exception is ArgumentError, "42"); |
75 Expect.throws(() => expando[42.87], (exception) | 82 Expect.throws(() => expando[42.87], |
76 => exception is ArgumentError, "42.87"); | 83 (exception) => exception is ArgumentError, "42.87"); |
77 Expect.throws(() => expando[true], (exception) | 84 Expect.throws( |
78 => exception is ArgumentError, "true"); | 85 () => expando[true], (exception) => exception is ArgumentError, "true"); |
79 Expect.throws(() => expando[false], (exception) | 86 Expect.throws(() => expando[false], |
80 => exception is ArgumentError, "false"); | 87 (exception) => exception is ArgumentError, "false"); |
81 } | 88 } |
82 | 89 |
83 static testIdentity() { | 90 static testIdentity() { |
84 // Expando only depends on identity of object. | 91 // Expando only depends on identity of object. |
85 Expando<int> expando = new Expando<int>(); | 92 Expando<int> expando = new Expando<int>(); |
86 var m1 = new Mutable(1); | 93 var m1 = new Mutable(1); |
87 var m2 = new Mutable(7); | 94 var m2 = new Mutable(7); |
88 var m3 = new Mutable(13); | 95 var m3 = new Mutable(13); |
89 expando[m1] = 42; | 96 expando[m1] = 42; |
90 Expect.equals(42, expando[m1]); | 97 Expect.equals(42, expando[m1]); |
91 m1.id = 37; | 98 m1.id = 37; |
92 Expect.equals(42, expando[m1]); | 99 Expect.equals(42, expando[m1]); |
93 expando[m2] = 37; | 100 expando[m2] = 37; |
94 expando[m3] = 10; | 101 expando[m3] = 10; |
95 m3.id = 1; | 102 m3.id = 1; |
96 Expect.equals(42, expando[m1]); | 103 Expect.equals(42, expando[m1]); |
97 Expect.equals(37, expando[m2]); | 104 Expect.equals(37, expando[m2]); |
98 Expect.equals(10, expando[m3]); | 105 Expect.equals(10, expando[m3]); |
99 } | 106 } |
100 } | 107 } |
101 | 108 |
102 main() => ExpandoTest.testMain(); | 109 main() => ExpandoTest.testMain(); |
103 | 110 |
104 class Mutable { | 111 class Mutable { |
105 int id; | 112 int id; |
106 Mutable(this.id); | 113 Mutable(this.id); |
107 int get hashCode => id; | 114 int get hashCode => id; |
108 bool operator==(other) => other is Mutable && other.id == id; | 115 bool operator ==(other) => other is Mutable && other.id == id; |
109 } | 116 } |
OLD | NEW |