OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 final Expando<int> visits = const Expando<int>('visits'); | |
6 | |
7 main() { | |
8 var legal = [ new Object(), | |
9 new List(), [1,2,3], const [1,2,3], | |
10 new Map(), {'x':1,'y':2}, const {'x':1,'y':2}, | |
11 new Expando(), new Expando('horse') ]; | |
12 for (var object in legal) { | |
13 testNamedExpando(object); | |
14 testUnnamedExpando(object); | |
15 testConstExpando(object); | |
16 } | |
17 for (var object in legal) { | |
18 Expect.equals(3, visits[object]); | |
19 } | |
20 testIllegal(); | |
21 } | |
22 | |
23 visit(object) { | |
24 int count = visits[object]; | |
25 count = (count === null) ? 1 : count + 1; | |
26 visits[object] = count; | |
27 } | |
28 | |
29 testNamedExpando(object) { | |
30 Expando<int> expando = new Expando<int>('myexpando'); | |
31 Expect.equals('myexpando', expando.name); | |
32 Expect.isTrue(expando.toString().startsWith('Expando:myexpando')); | |
33 testExpando(expando, object); | |
34 } | |
35 | |
36 testUnnamedExpando(object) { | |
37 Expando<int> expando = new Expando<int>(); | |
38 Expect.isNull(expando.name); | |
39 Expect.isTrue(expando.toString().startsWith('Expando:')); | |
40 testExpando(expando, object); | |
41 } | |
42 | |
43 testExpando(Expando<int> expando, object) { | |
44 visit(object); | |
45 | |
46 Expect.isNull(expando[object]); | |
47 expando[object] = 42; | |
48 Expect.equals(42, expando[object]); | |
49 expando[object] = null; | |
50 Expect.isNull(expando[object]); | |
51 | |
52 Expando<int> alternative = new Expando('myexpando'); | |
53 Expect.isNull(alternative[object]); | |
54 alternative[object] = 87; | |
55 Expect.isNull(expando[object]); | |
56 expando[object] = 99; | |
57 Expect.equals(99, expando[object]); | |
58 Expect.equals(87, alternative[object]); | |
59 } | |
60 | |
61 testConstExpando(object) { | |
62 visit(object); | |
63 | |
64 var e0 = const Expando<int>('horse'); | |
sra1
2012/06/12 07:10:26
Add 'difficult' names:
'prototype'
'toString'
'__p
| |
65 var e1 = const Expando<int>('horse'); | |
66 var e2 = new Expando<int>('horse'); | |
67 var e3 = new Expando<int>('horse'); | |
68 | |
69 e0[object] = 0; | |
70 Expect.equals(0, e0[object]); | |
71 | |
72 e1[object] = 1; | |
73 Expect.equals(1, e0[object]); | |
74 Expect.equals(1, e1[object]); | |
75 | |
76 e2[object] = 2; | |
77 Expect.equals(1, e0[object]); | |
78 Expect.equals(1, e1[object]); | |
79 Expect.equals(2, e2[object]); | |
80 | |
81 e3[object] = 3; | |
82 Expect.equals(1, e0[object]); | |
83 Expect.equals(1, e1[object]); | |
84 Expect.equals(2, e2[object]); | |
85 Expect.equals(3, e3[object]); | |
86 | |
87 e0[object] = null; | |
88 Expect.isNull(e0[object]); | |
89 Expect.isNull(e1[object]); | |
90 Expect.equals(2, e2[object]); | |
91 Expect.equals(3, e3[object]); | |
92 } | |
93 | |
94 testIllegal() { | |
sra1
2012/06/12 07:10:26
Illegal cases make the Expando API difficult to us
| |
95 Expando<int> expando = new Expando<int>(); | |
96 Expect.throws(() => expando[null], (exception) | |
97 => exception is NullPointerException); | |
98 Expect.throws(() => expando['string'], (exception) | |
99 => exception is IllegalArgumentException); | |
100 Expect.throws(() => expando['string'], (exception) | |
101 => exception is IllegalArgumentException); | |
102 Expect.throws(() => expando[42], (exception) | |
103 => exception is IllegalArgumentException); | |
104 Expect.throws(() => expando[42.87], (exception) | |
105 => exception is IllegalArgumentException); | |
106 Expect.throws(() => expando[true], (exception) | |
107 => exception is IllegalArgumentException); | |
108 Expect.throws(() => expando[false], (exception) | |
109 => exception is IllegalArgumentException); | |
110 } | |
OLD | NEW |