OLD | NEW |
1 // Copyright (c) 2017, 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 | 4 |
5 // Test that if the type of a parameter of a generic method is a type parameter, | 5 // Test that if the type of a parameter of a generic method is a type parameter, |
6 // the type of the passed argument is checked (01) at compile time | 6 // the type of the passed argument is checked (01) at compile time |
7 // if the receiver is given via an interface-type variable, and (02) at runtime | 7 // if the receiver is given via an interface-type variable, and (02) at runtime |
8 // if the receiver is dynamic. | 8 // if the receiver is dynamic. |
9 | 9 |
10 library generic_methods_dynamic_test; | 10 library generic_methods_dynamic_test; |
11 | 11 |
12 import "package:expect/expect.dart"; | 12 import "package:expect/expect.dart"; |
13 | 13 |
14 class A {} | 14 class A {} |
15 | 15 |
16 class B {} | 16 class B {} |
17 | 17 |
18 class C { | 18 class C { |
19 T foo<T>(T t) => t; | 19 T foo<T>(T t) => t; |
20 List<T> bar<T>(Iterable<T> t) => <T>[t.first]; | 20 List<T> bar<T>(Iterable<T> t) => <T>[t.first]; |
21 } | 21 } |
22 | 22 |
23 main() { | 23 main() { |
24 B b = new B(); | 24 B b = new B(); |
25 C c = new C(); | 25 C c = new C(); |
26 dynamic obj = c; | 26 dynamic obj = c; |
27 | 27 |
28 c.foo<A>(b); /// 01: compile-time error | 28 c.foo<A>(b); //# 01: compile-time error |
29 obj.foo<A>(b); /// 02: runtime error | 29 obj.foo<A>(b); //# 02: runtime error |
30 | 30 |
31 c.bar<A>(<B>[new B()]); /// 03: compile-time error | 31 c.bar<A>(<B>[new B()]); //# 03: compile-time error |
32 obj.bar<A>(<B>[new B()]); /// 04: runtime error | 32 obj.bar<A>(<B>[new B()]); //# 04: runtime error |
33 | 33 |
34 Expect.equals(c.foo<B>(b), b); /// 05: ok | 34 Expect.equals(c.foo<B>(b), b); //# 05: ok |
35 Expect.equals(obj.foo<B>(b), b); /// 05: continued | 35 Expect.equals(obj.foo<B>(b), b); //# 05: continued |
36 | 36 |
37 dynamic x = c.bar<B>(<B>[new B()]); /// 05: continued | 37 dynamic x = c.bar<B>(<B>[new B()]); //# 05: continued |
38 Expect.isTrue(x is List<B>); /// 05: continued | 38 Expect.isTrue(x is List<B>); //# 05: continued |
39 Expect.equals(x.length, 1); /// 05: continued | 39 Expect.equals(x.length, 1); //# 05: continued |
40 | 40 |
41 dynamic y = obj.bar<B>(<B>[new B()]); /// 05: continued | 41 dynamic y = obj.bar<B>(<B>[new B()]); //# 05: continued |
42 Expect.isTrue(y is List<B>); /// 05: continued | 42 Expect.isTrue(y is List<B>); //# 05: continued |
43 Expect.equals(y.length, 1); /// 05: continued | 43 Expect.equals(y.length, 1); //# 05: continued |
44 } | 44 } |
OLD | NEW |