OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 type_substitution_test; | 5 library type_substitution_test; |
6 | 6 |
7 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
9 import 'package:compiler/src/dart_types.dart'; | 9 import 'package:compiler/src/dart_types.dart'; |
10 import 'compiler_helper.dart'; | 10 import 'compiler_helper.dart'; |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 void testAsInstanceOf() { | 42 void testAsInstanceOf() { |
43 asyncTest(() => TypeEnvironment.create(''' | 43 asyncTest(() => TypeEnvironment.create(''' |
44 class A<T> {} | 44 class A<T> {} |
45 class B<T> {} | 45 class B<T> {} |
46 class C<T> extends A<T> {} | 46 class C<T> extends A<T> {} |
47 class D<T> extends A<int> {} | 47 class D<T> extends A<int> {} |
48 class E<T> extends A<A<T>> {} | 48 class E<T> extends A<A<T>> {} |
49 class F<T, U> extends B<F<T, String>> implements A<F<B<U>, int>> {} | 49 class F<T, U> extends B<F<T, String>> implements A<F<B<U>, int>> {} |
50 ''').then((env) { | 50 ''').then((env) { |
51 var compiler = env.compiler; | 51 var compiler = env.compiler; |
52 | 52 |
53 ClassElement A = env.getElement("A"); | 53 ClassElement A = env.getElement("A"); |
54 ClassElement B = env.getElement("B"); | 54 ClassElement B = env.getElement("B"); |
55 ClassElement C = env.getElement("C"); | 55 ClassElement C = env.getElement("C"); |
56 ClassElement D = env.getElement("D"); | 56 ClassElement D = env.getElement("D"); |
57 ClassElement E = env.getElement("E"); | 57 ClassElement E = env.getElement("E"); |
58 ClassElement F = env.getElement("F"); | 58 ClassElement F = env.getElement("F"); |
59 | 59 |
60 DartType numType = env['num']; | 60 DartType numType = env['num']; |
61 DartType intType = env['int']; | 61 DartType intType = env['int']; |
62 DartType stringType = env['String']; | 62 DartType stringType = env['String']; |
63 | 63 |
64 InterfaceType C_int = instantiate(C, [intType]); | 64 InterfaceType C_int = instantiate(C, [intType]); |
65 Expect.equals(instantiate(C, [intType]), C_int); | 65 Expect.equals(instantiate(C, [intType]), C_int); |
66 Expect.equals(instantiate(A, [intType]), C_int.asInstanceOf(A)); | 66 Expect.equals(instantiate(A, [intType]), C_int.asInstanceOf(A)); |
67 | 67 |
68 InterfaceType D_int = instantiate(D, [stringType]); | 68 InterfaceType D_int = instantiate(D, [stringType]); |
69 Expect.equals(instantiate(A, [intType]), D_int.asInstanceOf(A)); | 69 Expect.equals(instantiate(A, [intType]), D_int.asInstanceOf(A)); |
70 | 70 |
71 InterfaceType E_int = instantiate(E, [intType]); | 71 InterfaceType E_int = instantiate(E, [intType]); |
72 Expect.equals(instantiate(A, [instantiate(A, [intType])]), | 72 Expect.equals( |
73 E_int.asInstanceOf(A)); | 73 instantiate(A, [ |
| 74 instantiate(A, [intType]) |
| 75 ]), |
| 76 E_int.asInstanceOf(A)); |
74 | 77 |
75 InterfaceType F_int_string = instantiate(F, [intType, stringType]); | 78 InterfaceType F_int_string = instantiate(F, [intType, stringType]); |
76 Expect.equals(instantiate(B, [instantiate(F, [intType, stringType])]), | 79 Expect.equals( |
77 F_int_string.asInstanceOf(B)); | 80 instantiate(B, [ |
78 Expect.equals(instantiate(A, [instantiate(F, [instantiate(B, [stringType]), | 81 instantiate(F, [intType, stringType]) |
79 intType])]), | 82 ]), |
80 F_int_string.asInstanceOf(A)); | 83 F_int_string.asInstanceOf(B)); |
81 | 84 Expect.equals( |
82 })); | 85 instantiate(A, [ |
| 86 instantiate(F, [ |
| 87 instantiate(B, [stringType]), |
| 88 intType |
| 89 ]) |
| 90 ]), |
| 91 F_int_string.asInstanceOf(A)); |
| 92 })); |
83 } | 93 } |
84 | 94 |
85 /** | 95 /** |
86 * Test that substitution of [parameters] by [arguments] in the type found | 96 * Test that substitution of [parameters] by [arguments] in the type found |
87 * through [name1] is the same as the type found through [name2]. | 97 * through [name1] is the same as the type found through [name2]. |
88 */ | 98 */ |
89 void testSubstitution(compiler, arguments, parameters, | 99 void testSubstitution( |
90 String name1, String name2) { | 100 compiler, arguments, parameters, String name1, String name2) { |
91 DartType type1 = getType(compiler, name1); | 101 DartType type1 = getType(compiler, name1); |
92 DartType type2 = getType(compiler, name2); | 102 DartType type2 = getType(compiler, name2); |
93 DartType subst = type1.subst(arguments, parameters); | 103 DartType subst = type1.subst(arguments, parameters); |
94 Expect.equals(type2, subst, | 104 Expect.equals( |
95 "$type1.subst($arguments,$parameters)=$subst != $type2"); | 105 type2, subst, "$type1.subst($arguments,$parameters)=$subst != $type2"); |
96 } | 106 } |
97 | 107 |
98 void testTypeSubstitution() { | 108 void testTypeSubstitution() { |
99 asyncTest(() => TypeEnvironment.create(r""" | 109 asyncTest(() => TypeEnvironment.create(r""" |
100 typedef void Typedef1<X,Y>(X x1, Y y2); | 110 typedef void Typedef1<X,Y>(X x1, Y y2); |
101 typedef void Typedef2<Z>(Z z1); | 111 typedef void Typedef2<Z>(Z z1); |
102 | 112 |
103 class Class<T,S> { | 113 class Class<T,S> { |
104 void void1() {} | 114 void void1() {} |
105 void void2() {} | 115 void void2() {} |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 void Typedef1b(Typedef1<dynamic,dynamic> a) {} | 152 void Typedef1b(Typedef1<dynamic,dynamic> a) {} |
143 void Typedef2b(Typedef1<dynamic,dynamic> b) {} | 153 void Typedef2b(Typedef1<dynamic,dynamic> b) {} |
144 void Typedef1c(Typedef1 a) {} | 154 void Typedef1c(Typedef1 a) {} |
145 void Typedef2c(Typedef1 b) {} | 155 void Typedef2c(Typedef1 b) {} |
146 void Typedef1d(Typedef2<T> a) {} | 156 void Typedef1d(Typedef2<T> a) {} |
147 void Typedef2d(Typedef2<int> b) {} | 157 void Typedef2d(Typedef2<int> b) {} |
148 void Typedef1e(Typedef2<S> a) {} | 158 void Typedef1e(Typedef2<S> a) {} |
149 void Typedef2e(Typedef2<String> b) {} | 159 void Typedef2e(Typedef2<String> b) {} |
150 } | 160 } |
151 """).then((env) { | 161 """).then((env) { |
152 var compiler = env.compiler; | 162 var compiler = env.compiler; |
153 | 163 |
154 InterfaceType Class_T_S = env["Class"]; | 164 InterfaceType Class_T_S = env["Class"]; |
155 Expect.isNotNull(Class_T_S); | 165 Expect.isNotNull(Class_T_S); |
156 Expect.identical(Class_T_S.kind, TypeKind.INTERFACE); | 166 Expect.identical(Class_T_S.kind, TypeKind.INTERFACE); |
157 Expect.equals(2, Class_T_S.typeArguments.length); | 167 Expect.equals(2, Class_T_S.typeArguments.length); |
158 | 168 |
159 DartType T = Class_T_S.typeArguments[0]; | 169 DartType T = Class_T_S.typeArguments[0]; |
160 Expect.isNotNull(T); | 170 Expect.isNotNull(T); |
161 Expect.identical(T.kind, TypeKind.TYPE_VARIABLE); | 171 Expect.identical(T.kind, TypeKind.TYPE_VARIABLE); |
162 | 172 |
163 DartType S = Class_T_S.typeArguments[1]; | 173 DartType S = Class_T_S.typeArguments[1]; |
164 Expect.isNotNull(S); | 174 Expect.isNotNull(S); |
165 Expect.identical(S.kind, TypeKind.TYPE_VARIABLE); | 175 Expect.identical(S.kind, TypeKind.TYPE_VARIABLE); |
166 | 176 |
167 DartType intType = env['int'];//getType(compiler, "int1"); | 177 DartType intType = env['int']; //getType(compiler, "int1"); |
168 Expect.isNotNull(intType); | 178 Expect.isNotNull(intType); |
169 Expect.identical(intType.kind, TypeKind.INTERFACE); | 179 Expect.identical(intType.kind, TypeKind.INTERFACE); |
170 | 180 |
171 DartType StringType = env['String'];//getType(compiler, "String1"); | 181 DartType StringType = env['String']; //getType(compiler, "String1"); |
172 Expect.isNotNull(StringType); | 182 Expect.isNotNull(StringType); |
173 Expect.identical(StringType.kind, TypeKind.INTERFACE); | 183 Expect.identical(StringType.kind, TypeKind.INTERFACE); |
174 | 184 |
175 List<DartType> parameters = <DartType>[T, S]; | 185 List<DartType> parameters = <DartType>[T, S]; |
176 List<DartType> arguments = <DartType>[intType, StringType]; | 186 List<DartType> arguments = <DartType>[intType, StringType]; |
177 | 187 |
178 // TODO(johnniwinther): Create types directly from strings to improve test | 188 // TODO(johnniwinther): Create types directly from strings to improve te
st |
179 // readability. | 189 // readability. |
180 | 190 |
181 testSubstitution(compiler, arguments, parameters, "void1", "void2"); | 191 testSubstitution(compiler, arguments, parameters, "void1", "void2"); |
182 testSubstitution(compiler, arguments, parameters, "dynamic1", "dynamic2"); | 192 testSubstitution( |
183 testSubstitution(compiler, arguments, parameters, "int1", "int2"); | 193 compiler, arguments, parameters, "dynamic1", "dynamic2"); |
184 testSubstitution(compiler, arguments, parameters, "String1", "String2"); | 194 testSubstitution(compiler, arguments, parameters, "int1", "int2"); |
185 testSubstitution(compiler, arguments, parameters, "ListInt1", "ListInt2"); | 195 testSubstitution(compiler, arguments, parameters, "String1", "String2"); |
186 testSubstitution(compiler, arguments, parameters, "ListT1", "ListT2"); | 196 testSubstitution( |
187 testSubstitution(compiler, arguments, parameters, "ListS1", "ListS2"); | 197 compiler, arguments, parameters, "ListInt1", "ListInt2"); |
188 testSubstitution(compiler, arguments, parameters, "ListListT1", "ListListT2"
); | 198 testSubstitution(compiler, arguments, parameters, "ListT1", "ListT2"); |
189 testSubstitution(compiler, arguments, parameters, "ListRaw1", "ListRaw2"); | 199 testSubstitution(compiler, arguments, parameters, "ListS1", "ListS2"); |
190 testSubstitution(compiler, arguments, parameters, | 200 testSubstitution( |
191 "ListDynamic1", "ListDynamic2"); | 201 compiler, arguments, parameters, "ListListT1", "ListListT2"); |
192 testSubstitution(compiler, arguments, parameters, | 202 testSubstitution( |
193 "MapIntString1", "MapIntString2"); | 203 compiler, arguments, parameters, "ListRaw1", "ListRaw2"); |
194 testSubstitution(compiler, arguments, parameters, | 204 testSubstitution( |
195 "MapTString1", "MapTString2"); | 205 compiler, arguments, parameters, "ListDynamic1", "ListDynamic2"); |
196 testSubstitution(compiler, arguments, parameters, | 206 testSubstitution( |
197 "MapDynamicString1", "MapDynamicString2"); | 207 compiler, arguments, parameters, "MapIntString1", "MapIntString2"); |
198 testSubstitution(compiler, arguments, parameters, "TypeVarT1", "TypeVarT2"); | 208 testSubstitution( |
199 testSubstitution(compiler, arguments, parameters, "TypeVarS1", "TypeVarS2"); | 209 compiler, arguments, parameters, "MapTString1", "MapTString2"); |
200 testSubstitution(compiler, arguments, parameters, "Function1a", "Function2a"
); | 210 testSubstitution(compiler, arguments, parameters, "MapDynamicString1", |
201 testSubstitution(compiler, arguments, parameters, "Function1b", "Function2b"
); | 211 "MapDynamicString2"); |
202 testSubstitution(compiler, arguments, parameters, "Function1c", "Function2c"
); | 212 testSubstitution( |
203 testSubstitution(compiler, arguments, parameters, "Typedef1a", "Typedef2a"); | 213 compiler, arguments, parameters, "TypeVarT1", "TypeVarT2"); |
204 testSubstitution(compiler, arguments, parameters, "Typedef1b", "Typedef2b"); | 214 testSubstitution( |
205 testSubstitution(compiler, arguments, parameters, "Typedef1c", "Typedef2c"); | 215 compiler, arguments, parameters, "TypeVarS1", "TypeVarS2"); |
206 testSubstitution(compiler, arguments, parameters, "Typedef1d", "Typedef2d"); | 216 testSubstitution( |
207 testSubstitution(compiler, arguments, parameters, "Typedef1e", "Typedef2e"); | 217 compiler, arguments, parameters, "Function1a", "Function2a"); |
| 218 testSubstitution( |
| 219 compiler, arguments, parameters, "Function1b", "Function2b"); |
| 220 testSubstitution( |
| 221 compiler, arguments, parameters, "Function1c", "Function2c"); |
| 222 testSubstitution( |
| 223 compiler, arguments, parameters, "Typedef1a", "Typedef2a"); |
| 224 testSubstitution( |
| 225 compiler, arguments, parameters, "Typedef1b", "Typedef2b"); |
| 226 testSubstitution( |
| 227 compiler, arguments, parameters, "Typedef1c", "Typedef2c"); |
| 228 testSubstitution( |
| 229 compiler, arguments, parameters, "Typedef1d", "Typedef2d"); |
| 230 testSubstitution( |
| 231 compiler, arguments, parameters, "Typedef1e", "Typedef2e"); |
208 | 232 |
209 // Substitution in unalias. | 233 // Substitution in unalias. |
210 DartType Typedef2_int_String = getType(compiler, "Typedef2a"); | 234 DartType Typedef2_int_String = getType(compiler, "Typedef2a"); |
211 Expect.isNotNull(Typedef2_int_String); | 235 Expect.isNotNull(Typedef2_int_String); |
212 DartType Function_int_String = getType(compiler, "Function2b"); | 236 DartType Function_int_String = getType(compiler, "Function2b"); |
213 Expect.isNotNull(Function_int_String); | 237 Expect.isNotNull(Function_int_String); |
214 DartType unalias1 = Typedef2_int_String.unaliased; | 238 DartType unalias1 = Typedef2_int_String.unaliased; |
215 Expect.equals(Function_int_String, unalias1, | 239 Expect.equals(Function_int_String, unalias1, |
216 '$Typedef2_int_String.unalias=$unalias1 != $Function_int_String'); | 240 '$Typedef2_int_String.unalias=$unalias1 != $Function_int_String'); |
217 | 241 |
218 DartType Typedef1 = getType(compiler, "Typedef1c"); | 242 DartType Typedef1 = getType(compiler, "Typedef1c"); |
219 Expect.isNotNull(Typedef1); | 243 Expect.isNotNull(Typedef1); |
220 DartType Function_dynamic_dynamic = getType(compiler, "Function1c"); | 244 DartType Function_dynamic_dynamic = getType(compiler, "Function1c"); |
221 Expect.isNotNull(Function_dynamic_dynamic); | 245 Expect.isNotNull(Function_dynamic_dynamic); |
222 DartType unalias2 = Typedef1.unaliased; | 246 DartType unalias2 = Typedef1.unaliased; |
223 Expect.equals(Function_dynamic_dynamic, unalias2, | 247 Expect.equals(Function_dynamic_dynamic, unalias2, |
224 '$Typedef1.unalias=$unalias2 != $Function_dynamic_dynamic'); | 248 '$Typedef1.unalias=$unalias2 != $Function_dynamic_dynamic'); |
225 })); | 249 })); |
226 } | 250 } |
OLD | NEW |