OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library subtype_test; | 5 library subtype_test; |
6 | 6 |
7 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
8 import 'type_test_helper.dart'; | 8 import 'type_test_helper.dart'; |
9 import '../../../sdk/lib/_internal/compiler/implementation/dart_types.dart'; | 9 import '../../../sdk/lib/_internal/compiler/implementation/dart_types.dart'; |
10 import "../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar
t" | 10 import "../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar
t" |
11 show Element, ClassElement; | 11 show Element, ClassElement; |
12 | 12 |
13 void main() { | 13 void main() { |
14 test(); | 14 test(); |
15 } | 15 } |
16 | 16 |
17 void test() { | 17 void test() { |
18 var env = new TypeEnvironment(r""" | 18 TypeEnvironment.create(r""" |
19 class A<T> { | 19 class A<T> { |
20 T foo; | 20 T foo; |
21 } | 21 } |
22 class B<S> extends A<A<S>> { | 22 class B<S> extends A<A<S>> { |
23 S bar; | 23 S bar; |
24 } | 24 } |
25 class C<U> extends B<String> with D<B<U>> { | 25 class C<U> extends B<String> with D<B<U>> { |
26 U baz; | 26 U baz; |
27 } | 27 } |
28 class D<V> { | 28 class D<V> { |
29 V boz; | 29 V boz; |
30 } | 30 } |
31 """); | 31 """).then((env) { |
| 32 void expect(DartType receiverType, String memberName, |
| 33 DartType expectedType) { |
| 34 Member member = receiverType.lookupMember(env.sourceString(memberName)); |
| 35 Expect.isNotNull(member); |
| 36 DartType memberType = member.computeType(env.compiler); |
| 37 Expect.equals(expectedType, memberType, |
| 38 'Wrong member type for $receiverType.$memberName.'); |
| 39 } |
32 | 40 |
33 void expect(DartType receiverType, String memberName, DartType expectedType) { | 41 DartType int_ = env['int']; |
34 Member member = receiverType.lookupMember(env.sourceString(memberName)); | 42 DartType String_ = env['String']; |
35 Expect.isNotNull(member); | |
36 DartType memberType = member.computeType(env.compiler); | |
37 Expect.equals(expectedType, memberType, | |
38 'Wrong member type for $receiverType.$memberName.'); | |
39 } | |
40 | 43 |
41 DartType int_ = env['int']; | 44 ClassElement A = env.getElement('A'); |
42 DartType String_ = env['String']; | 45 DartType T = A.typeVariables.head; |
| 46 DartType A_T = A.thisType; |
| 47 expect(A_T, 'foo', T); |
43 | 48 |
44 ClassElement A = env.getElement('A'); | 49 DartType A_int = instantiate(A, [int_]); |
45 DartType T = A.typeVariables.head; | 50 expect(A_int, 'foo', int_); |
46 DartType A_T = A.thisType; | |
47 expect(A_T, 'foo', T); | |
48 | 51 |
49 DartType A_int = instantiate(A, [int_]); | 52 ClassElement B = env.getElement('B'); |
50 expect(A_int, 'foo', int_); | 53 DartType S = B.typeVariables.head; |
| 54 DartType B_S = B.thisType; |
| 55 expect(B_S, 'foo', instantiate(A, [S])); |
| 56 expect(B_S, 'bar', S); |
51 | 57 |
52 ClassElement B = env.getElement('B'); | 58 DartType B_int = instantiate(B, [int_]); |
53 DartType S = B.typeVariables.head; | 59 expect(B_int, 'foo', A_int); |
54 DartType B_S = B.thisType; | 60 expect(B_int, 'bar', int_); |
55 expect(B_S, 'foo', instantiate(A, [S])); | |
56 expect(B_S, 'bar', S); | |
57 | 61 |
58 DartType B_int = instantiate(B, [int_]); | 62 ClassElement C = env.getElement('C'); |
59 expect(B_int, 'foo', A_int); | 63 DartType U = C.typeVariables.head; |
60 expect(B_int, 'bar', int_); | 64 DartType C_U = C.thisType; |
| 65 expect(C_U, 'foo', instantiate(A, [String_])); |
| 66 expect(C_U, 'bar', String_); |
| 67 expect(C_U, 'baz', U); |
| 68 expect(C_U, 'boz', instantiate(B, [U])); |
61 | 69 |
62 ClassElement C = env.getElement('C'); | 70 DartType C_int = instantiate(C, [int_]); |
63 DartType U = C.typeVariables.head; | 71 expect(C_int, 'foo', instantiate(A, [String_])); |
64 DartType C_U = C.thisType; | 72 expect(C_int, 'bar', String_); |
65 expect(C_U, 'foo', instantiate(A, [String_])); | 73 expect(C_int, 'baz', int_); |
66 expect(C_U, 'bar', String_); | 74 expect(C_int, 'boz', instantiate(B, [int_])); |
67 expect(C_U, 'baz', U); | 75 }); |
68 expect(C_U, 'boz', instantiate(B, [U])); | |
69 | |
70 DartType C_int = instantiate(C, [int_]); | |
71 expect(C_int, 'foo', instantiate(A, [String_])); | |
72 expect(C_int, 'bar', String_); | |
73 expect(C_int, 'baz', int_); | |
74 expect(C_int, 'boz', instantiate(B, [int_])); | |
75 } | 76 } |
76 | 77 |
OLD | NEW |