OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'package:expect/expect.dart'; | 4 import 'package:expect/expect.dart'; |
5 | 5 |
6 class Foo { | 6 class C { |
7 dynamic method(int x) {} | 7 dynamic f(int x) => x + 1; |
8 dynamic method2(int x) {} | 8 dynamic g(int x) => x + 2; |
| 9 } |
| 10 |
| 11 class D extends C { |
| 12 f(int x) => x + 41; |
| 13 get superF => super.f; |
| 14 get superG => super.g; |
| 15 } |
| 16 |
| 17 tearoffEquals(f1, f2) { |
| 18 Expect.equals(f1, f2); |
| 19 Expect.equals(f1.hashCode, f2.hashCode); |
| 20 } |
| 21 |
| 22 tearoffNotEquals(f1, f2) { |
| 23 Expect.notEquals(f1, f2); |
| 24 Expect.notEquals(f1.hashCode, f2.hashCode); |
| 25 } |
| 26 |
| 27 testDynamic() { |
| 28 C c = new C(); |
| 29 |
| 30 dynamic f1 = c.f; |
| 31 Expect.throws(() => f1(2.5)); |
| 32 Expect.equals(f1(41), 42); |
| 33 |
| 34 dynamic f2 = (c as dynamic).f; |
| 35 Expect.throws(() => f2(2.5)); |
| 36 Expect.equals(f2(41), 42); |
| 37 |
| 38 tearoffEquals(f1, f1); |
| 39 tearoffEquals(f1, f2); |
| 40 tearoffEquals(f1, c.f); |
| 41 tearoffEquals(c.g, (c as dynamic).g); |
| 42 |
| 43 tearoffNotEquals(f1, new C().f); |
| 44 tearoffNotEquals(f1, (new C() as dynamic).f); |
| 45 tearoffNotEquals(f1, c.g); |
| 46 tearoffNotEquals(f1, (c as dynamic).g); |
| 47 } |
| 48 |
| 49 testSuper() { |
| 50 D d = new D(); |
| 51 dynamic superF1 = d.superF; |
| 52 dynamic superF2 = (d as dynamic).superF; |
| 53 |
| 54 Expect.throws(() => superF1(2.5)); |
| 55 Expect.throws(() => superF2(2.5)); |
| 56 Expect.equals(superF1(41), 42); |
| 57 Expect.equals(superF2(41), 42); |
| 58 |
| 59 tearoffEquals(superF1, superF1); |
| 60 tearoffEquals(superF1, superF2); |
| 61 tearoffEquals(superF1, d.superF); |
| 62 tearoffEquals(d.f, (d as dynamic).f); |
| 63 |
| 64 tearoffNotEquals(superF1, d.f); |
| 65 tearoffNotEquals(superF1, (d as dynamic).f); |
| 66 tearoffNotEquals(superF1, new D().superF); |
| 67 tearoffNotEquals(superF1, (new D() as dynamic).superF); |
| 68 tearoffNotEquals(superF1, d.superG); |
| 69 tearoffNotEquals(superF1, (d as dynamic).superG); |
| 70 |
| 71 tearoffEquals(d.superG, (d as dynamic).superG); |
| 72 tearoffEquals(d.g, d.superG); |
| 73 } |
| 74 |
| 75 class S { |
| 76 final int id; |
| 77 S(this.id); |
| 78 toString() => 'S#$id'; |
| 79 } |
| 80 |
| 81 testToString() { |
| 82 testType<T>(T c) { |
| 83 dynamic d = c; |
| 84 Object o = c; |
| 85 tearoffEquals(c.toString, d.toString); |
| 86 tearoffEquals(c.toString, o.toString); |
| 87 |
| 88 var expected = c.toString(); |
| 89 dynamic f = d.toString; |
| 90 tearoffEquals(f(), expected); |
| 91 f = o.toString; |
| 92 tearoffEquals(f(), expected); |
| 93 var g = c.toString; |
| 94 tearoffEquals(g(), expected); |
| 95 } |
| 96 |
| 97 testType(new C()); |
| 98 testType(new D()); |
| 99 testType(new S(1)); |
| 100 testType(new S(2)); |
| 101 testType(new Object()); |
| 102 testType(null); |
| 103 testType(Object); // Type |
| 104 testType(C); // Type |
| 105 testType(42); |
| 106 testType('hi'); |
| 107 testType(true); |
| 108 testType([1, 2, 3]); |
| 109 testType({'a': 'b'}); |
| 110 testType((x) => x + 1); |
| 111 testType(testType); |
| 112 } |
| 113 |
| 114 class N { |
| 115 noSuchMethod(i) => i; |
| 116 } |
| 117 |
| 118 testNoSuchMethod() { |
| 119 // Create an invocation. |
| 120 Invocation i = (new N() as dynamic).foo(1, bar: 2); |
| 121 tearoffEquals(i.memberName, #foo); |
| 122 |
| 123 testType<T>(T c) { |
| 124 dynamic d = c; |
| 125 Object o = c; |
| 126 tearoffEquals(c.noSuchMethod, d.noSuchMethod); |
| 127 tearoffEquals(c.noSuchMethod, o.noSuchMethod); |
| 128 |
| 129 var expected; |
| 130 try { |
| 131 c.noSuchMethod(i); |
| 132 } on NoSuchMethodError catch (error) { |
| 133 var nsm = '$error'; |
| 134 Expect.isTrue(nsm.startsWith("NoSuchMethodError: ")); |
| 135 Expect.isTrue(nsm.contains("'foo'")); |
| 136 expected = (e) => e is NoSuchMethodError && '$e' == nsm; |
| 137 } |
| 138 dynamic f = d.noSuchMethod; |
| 139 Expect.throws(() => f(i), expected); |
| 140 f = o.noSuchMethod; |
| 141 Expect.throws(() => f(i), expected); |
| 142 var g = c.noSuchMethod; |
| 143 Expect.throws(() => g(i), expected); |
| 144 } |
| 145 |
| 146 testType(new C()); |
| 147 testType(new D()); |
| 148 testType(new S(1)); |
| 149 testType(new S(2)); |
| 150 testType(new Object()); |
| 151 testType(null); |
| 152 testType(Object); // Type |
| 153 testType(C); // Type |
| 154 testType(42); |
| 155 testType('hi'); |
| 156 testType(true); |
| 157 testType([1, 2, 3]); |
| 158 testType({'a': 'b'}); |
| 159 testType((x) => x + 1); |
| 160 testType(testType); |
9 } | 161 } |
10 | 162 |
11 main() { | 163 main() { |
12 Foo foo = new Foo(); | 164 testDynamic(); |
13 | 165 testSuper(); |
14 dynamic dynamicMethod1 = foo.method; | 166 testToString(); |
15 Expect.throws(() => dynamicMethod1(2.5)); | 167 testNoSuchMethod(); |
16 | |
17 dynamic dynamicMethod2 = (foo as dynamic).method; | |
18 Expect.throws(() => dynamicMethod2(2.5)); | |
19 | |
20 Expect.equals(dynamicMethod1, dynamicMethod1); | |
21 Expect.equals(dynamicMethod1, dynamicMethod2); | |
22 Expect.equals(dynamicMethod1, foo.method); | |
23 Expect.equals(foo.method2, (foo as dynamic).method2); | |
24 | |
25 Expect.notEquals(dynamicMethod1, new Foo().method); | |
26 Expect.notEquals(dynamicMethod1, (new Foo() as dynamic).method); | |
27 Expect.notEquals(dynamicMethod1, foo.method2); | |
28 Expect.notEquals(dynamicMethod1, (foo as dynamic).method2); | |
29 } | 168 } |
OLD | NEW |