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, DartType expectedType)
{ |
| 33 Member member = receiverType.lookupMember(env.sourceString(memberName)); |
| 34 Expect.isNotNull(member); |
| 35 DartType memberType = member.computeType(env.compiler); |
| 36 Expect.equals(expectedType, memberType, |
| 37 'Wrong member type for $receiverType.$memberName.'); |
| 38 } |
32 | 39 |
33 void expect(DartType receiverType, String memberName, DartType expectedType) { | 40 DartType int_ = env['int']; |
34 Member member = receiverType.lookupMember(env.sourceString(memberName)); | 41 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 | 42 |
41 DartType int_ = env['int']; | 43 ClassElement A = env.getElement('A'); |
42 DartType String_ = env['String']; | 44 DartType T = A.typeVariables.head; |
| 45 DartType A_T = A.thisType; |
| 46 expect(A_T, 'foo', T); |
43 | 47 |
44 ClassElement A = env.getElement('A'); | 48 DartType A_int = instantiate(A, [int_]); |
45 DartType T = A.typeVariables.head; | 49 expect(A_int, 'foo', int_); |
46 DartType A_T = A.thisType; | |
47 expect(A_T, 'foo', T); | |
48 | 50 |
49 DartType A_int = instantiate(A, [int_]); | 51 ClassElement B = env.getElement('B'); |
50 expect(A_int, 'foo', int_); | 52 DartType S = B.typeVariables.head; |
| 53 DartType B_S = B.thisType; |
| 54 expect(B_S, 'foo', instantiate(A, [S])); |
| 55 expect(B_S, 'bar', S); |
51 | 56 |
52 ClassElement B = env.getElement('B'); | 57 DartType B_int = instantiate(B, [int_]); |
53 DartType S = B.typeVariables.head; | 58 expect(B_int, 'foo', A_int); |
54 DartType B_S = B.thisType; | 59 expect(B_int, 'bar', int_); |
55 expect(B_S, 'foo', instantiate(A, [S])); | |
56 expect(B_S, 'bar', S); | |
57 | 60 |
58 DartType B_int = instantiate(B, [int_]); | 61 ClassElement C = env.getElement('C'); |
59 expect(B_int, 'foo', A_int); | 62 DartType U = C.typeVariables.head; |
60 expect(B_int, 'bar', int_); | 63 DartType C_U = C.thisType; |
| 64 expect(C_U, 'foo', instantiate(A, [String_])); |
| 65 expect(C_U, 'bar', String_); |
| 66 expect(C_U, 'baz', U); |
| 67 expect(C_U, 'boz', instantiate(B, [U])); |
61 | 68 |
62 ClassElement C = env.getElement('C'); | 69 DartType C_int = instantiate(C, [int_]); |
63 DartType U = C.typeVariables.head; | 70 expect(C_int, 'foo', instantiate(A, [String_])); |
64 DartType C_U = C.thisType; | 71 expect(C_int, 'bar', String_); |
65 expect(C_U, 'foo', instantiate(A, [String_])); | 72 expect(C_int, 'baz', int_); |
66 expect(C_U, 'bar', String_); | 73 expect(C_int, 'boz', instantiate(B, [int_])); |
67 expect(C_U, 'baz', U); | 74 }); |
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 } | 75 } |
76 | 76 |
OLD | NEW |