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], "$object"); |
22 } | 22 } |
23 testIllegal(); | 23 testIllegal(); |
24 testIdentity(); | 24 testIdentity(); |
25 } | 25 } |
26 | 26 |
27 static visit(object) { | 27 static visit(object) { |
28 int count = visits[object]; | 28 int count = visits[object]; |
29 count = (count == null) ? 1 : count + 1; | 29 count = (count == null) ? 1 : count + 1; |
30 visits[object] = count; | 30 visits[object] = count; |
31 } | 31 } |
(...skipping 26 matching lines...) Expand all Loading... |
58 alternative[object] = 87; | 58 alternative[object] = 87; |
59 Expect.isNull(expando[object]); | 59 Expect.isNull(expando[object]); |
60 expando[object] = 99; | 60 expando[object] = 99; |
61 Expect.equals(99, expando[object]); | 61 Expect.equals(99, expando[object]); |
62 Expect.equals(87, alternative[object]); | 62 Expect.equals(87, alternative[object]); |
63 } | 63 } |
64 | 64 |
65 static testIllegal() { | 65 static testIllegal() { |
66 Expando<int> expando = new Expando<int>(); | 66 Expando<int> expando = new Expando<int>(); |
67 Expect.throws(() => expando[null], (exception) | 67 Expect.throws(() => expando[null], (exception) |
68 => exception is ArgumentError); | 68 => exception is ArgumentError, "null"); |
69 Expect.throws(() => expando['string'], (exception) | 69 Expect.throws(() => expando['string'], (exception) |
70 => exception is ArgumentError); | 70 => exception is ArgumentError, "'string'"); |
71 Expect.throws(() => expando['string'], (exception) | 71 Expect.throws(() => expando['string'], (exception) |
72 => exception is ArgumentError); | 72 => exception is ArgumentError, "'string'"); |
73 Expect.throws(() => expando[42], (exception) | 73 Expect.throws(() => expando[42], (exception) |
74 => exception is ArgumentError); | 74 => exception is ArgumentError, "42"); |
75 Expect.throws(() => expando[42.87], (exception) | 75 Expect.throws(() => expando[42.87], (exception) |
76 => exception is ArgumentError); | 76 => exception is ArgumentError, "42.87"); |
77 Expect.throws(() => expando[true], (exception) | 77 Expect.throws(() => expando[true], (exception) |
78 => exception is ArgumentError); | 78 => exception is ArgumentError, "true"); |
79 Expect.throws(() => expando[false], (exception) | 79 Expect.throws(() => expando[false], (exception) |
80 => exception is ArgumentError); | 80 => exception is ArgumentError, "false"); |
81 } | 81 } |
82 | 82 |
83 static testIdentity() { | 83 static testIdentity() { |
84 // Expando only depends on identity of object. | 84 // Expando only depends on identity of object. |
85 Expando<int> expando = new Expando<int>(); | 85 Expando<int> expando = new Expando<int>(); |
86 var m1 = new Mutable(1); | 86 var m1 = new Mutable(1); |
87 var m2 = new Mutable(7); | 87 var m2 = new Mutable(7); |
88 var m3 = new Mutable(13); | 88 var m3 = new Mutable(13); |
89 expando[m1] = 42; | 89 expando[m1] = 42; |
90 Expect.equals(42, expando[m1]); | 90 Expect.equals(42, expando[m1]); |
91 m1.id = 37; | 91 m1.id = 37; |
92 Expect.equals(42, expando[m1]); | 92 Expect.equals(42, expando[m1]); |
93 expando[m2] = 37; | 93 expando[m2] = 37; |
94 expando[m3] = 10; | 94 expando[m3] = 10; |
95 m3.id = 1; | 95 m3.id = 1; |
96 Expect.equals(42, expando[m1]); | 96 Expect.equals(42, expando[m1]); |
97 Expect.equals(37, expando[m2]); | 97 Expect.equals(37, expando[m2]); |
98 Expect.equals(10, expando[m3]); | 98 Expect.equals(10, expando[m3]); |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 main() => ExpandoTest.testMain(); | 102 main() => ExpandoTest.testMain(); |
103 | 103 |
104 class Mutable { | 104 class Mutable { |
105 int id; | 105 int id; |
106 Mutable(this.id); | 106 Mutable(this.id); |
107 int get hashCode => id; | 107 int get hashCode => id; |
108 bool operator==(other) => other is Mutable && other.id == id; | 108 bool operator==(other) => other is Mutable && other.id == id; |
109 } | 109 } |
OLD | NEW |