| 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 import "package:expect/expect.dart"; | |
| 6 | |
| 7 class ExpandoTest { | |
| 8 static Expando<int> visits; | |
| 9 | |
| 10 static testMain() { | |
| 11 visits = new Expando<int>('visits'); | |
| 12 var legal = [ | |
| 13 new Object(), | |
| 14 new List(), | |
| 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 ]; | |
| 23 for (var object in legal) { | |
| 24 testNamedExpando(object); | |
| 25 testUnnamedExpando(object); | |
| 26 } | |
| 27 for (var object in legal) { | |
| 28 Expect.equals(2, visits[object], "$object"); | |
| 29 } | |
| 30 testIllegal(); | |
| 31 testIdentity(); | |
| 32 } | |
| 33 | |
| 34 static visit(object) { | |
| 35 int count = visits[object]; | |
| 36 count = (count == null) ? 1 : count + 1; | |
| 37 visits[object] = count; | |
| 38 } | |
| 39 | |
| 40 static testNamedExpando(object) { | |
| 41 Expando<int> expando = new Expando<int>('myexpando'); | |
| 42 Expect.equals('myexpando', expando.name); | |
| 43 Expect.isTrue(expando.toString().startsWith('Expando:myexpando')); | |
| 44 testExpando(expando, object); | |
| 45 } | |
| 46 | |
| 47 static testUnnamedExpando(object) { | |
| 48 Expando<int> expando = new Expando<int>(); | |
| 49 Expect.isNull(expando.name); | |
| 50 Expect.isTrue(expando.toString().startsWith('Expando:')); | |
| 51 testExpando(expando, object); | |
| 52 } | |
| 53 | |
| 54 static testExpando(Expando<int> expando, object) { | |
| 55 visit(object); | |
| 56 | |
| 57 Expect.isNull(expando[object]); | |
| 58 expando[object] = 42; | |
| 59 Expect.equals(42, expando[object]); | |
| 60 expando[object] = null; | |
| 61 Expect.isNull(expando[object]); | |
| 62 | |
| 63 Expando<int> alternative = new Expando('myexpando'); | |
| 64 Expect.isNull(alternative[object]); | |
| 65 alternative[object] = 87; | |
| 66 Expect.isNull(expando[object]); | |
| 67 expando[object] = 99; | |
| 68 Expect.equals(99, expando[object]); | |
| 69 Expect.equals(87, alternative[object]); | |
| 70 } | |
| 71 | |
| 72 static testIllegal() { | |
| 73 Expando<int> expando = new Expando<int>(); | |
| 74 Expect.throws( | |
| 75 () => expando[null], (exception) => exception is ArgumentError, "null"); | |
| 76 Expect.throws(() => expando['string'], | |
| 77 (exception) => exception is ArgumentError, "'string'"); | |
| 78 Expect.throws(() => expando['string'], | |
| 79 (exception) => exception is ArgumentError, "'string'"); | |
| 80 Expect.throws( | |
| 81 () => expando[42], (exception) => exception is ArgumentError, "42"); | |
| 82 Expect.throws(() => expando[42.87], | |
| 83 (exception) => exception is ArgumentError, "42.87"); | |
| 84 Expect.throws( | |
| 85 () => expando[true], (exception) => exception is ArgumentError, "true"); | |
| 86 Expect.throws(() => expando[false], | |
| 87 (exception) => exception is ArgumentError, "false"); | |
| 88 } | |
| 89 | |
| 90 static testIdentity() { | |
| 91 // Expando only depends on identity of object. | |
| 92 Expando<int> expando = new Expando<int>(); | |
| 93 var m1 = new Mutable(1); | |
| 94 var m2 = new Mutable(7); | |
| 95 var m3 = new Mutable(13); | |
| 96 expando[m1] = 42; | |
| 97 Expect.equals(42, expando[m1]); | |
| 98 m1.id = 37; | |
| 99 Expect.equals(42, expando[m1]); | |
| 100 expando[m2] = 37; | |
| 101 expando[m3] = 10; | |
| 102 m3.id = 1; | |
| 103 Expect.equals(42, expando[m1]); | |
| 104 Expect.equals(37, expando[m2]); | |
| 105 Expect.equals(10, expando[m3]); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 main() => ExpandoTest.testMain(); | |
| 110 | |
| 111 class Mutable { | |
| 112 int id; | |
| 113 Mutable(this.id); | |
| 114 int get hashCode => id; | |
| 115 bool operator ==(other) => other is Mutable && other.id == id; | |
| 116 } | |
| OLD | NEW |