OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // Dart test program testing type checks. | |
6 | |
7 class TypeTest { | |
8 static test() { | |
9 int result = 0; | |
10 try { | |
11 // Throws a TypeError if type checks are enabled. | |
12 int i = "hello"; /// static type warning | |
13 } catch (TypeError error) { | |
14 result = 1; | |
15 Expect.equals("int", error.dstType); | |
16 Expect.equals("String", error.srcType); | |
17 Expect.equals("i", error.dstName); | |
18 int pos = error.url.lastIndexOf("/", error.url.length); | |
19 if (pos == -1) { | |
20 pos = error.url.lastIndexOf("\\", error.url.length); | |
21 } | |
22 String subs = error.url.substring(pos + 1, error.url.length); | |
23 Expect.equals("TypeTest.dart", subs); | |
24 Expect.equals(12, error.line); | |
25 Expect.equals(15, error.column); | |
26 } | |
27 return result; | |
28 } | |
29 | |
30 static testSideEffect() { | |
31 int result = 0; | |
32 int index() { | |
33 result++; | |
34 return 0; | |
35 } | |
36 try { | |
37 List<int> a = new List<int>(1); | |
38 a[0] = 0; | |
39 a[index()]++; // Type check succeeds, but does not create side effects. | |
40 assert(a[0] == 1); | |
41 } catch (TypeError error) { | |
42 result = 100; | |
43 } | |
44 return result; | |
45 } | |
46 | |
47 static testArgument() { | |
48 int result = 0; | |
49 int f(int i) { | |
50 return i; | |
51 } | |
52 try { | |
53 // Throws a TypeError if type checks are enabled. | |
54 int i = f("hello"); /// static type warning | |
55 } catch (TypeError error) { | |
56 result = 1; | |
57 Expect.equals("int", error.dstType); | |
58 Expect.equals("String", error.srcType); | |
59 Expect.equals("i", error.dstName); | |
60 int pos = error.url.lastIndexOf("/", error.url.length); | |
61 if (pos == -1) { | |
62 pos = error.url.lastIndexOf("\\", error.url.length); | |
63 } | |
64 String subs = error.url.substring(pos + 1, error.url.length); | |
65 Expect.equals("TypeTest.dart", subs); | |
66 Expect.equals(49, error.line); | |
67 Expect.equals(15, error.column); | |
68 } | |
69 return result; | |
70 } | |
71 | |
72 static testReturn() { | |
73 int result = 0; | |
74 int f(String s) { | |
75 // String not assignable to int on return | |
76 return s; /// static type warning | |
77 } | |
78 try { | |
79 // Throws a TypeError if type checks are enabled. | |
80 int i = f("hello"); | |
81 } catch (TypeError error) { | |
82 result = 1; | |
83 Expect.equals("int", error.dstType); | |
84 Expect.equals("String", error.srcType); | |
85 Expect.equals("function result", error.dstName); | |
86 int pos = error.url.lastIndexOf("/", error.url.length); | |
87 if (pos == -1) { | |
88 pos = error.url.lastIndexOf("\\", error.url.length); | |
89 } | |
90 String subs = error.url.substring(pos + 1, error.url.length); | |
91 Expect.equals("TypeTest.dart", subs); | |
92 Expect.equals(74, error.line); | |
93 Expect.equals(14, error.column); | |
94 } | |
95 return result; | |
96 } | |
97 | |
98 static int field; | |
99 static testField() { | |
100 int result = 0; | |
101 try { | |
102 // Throws a TypeError if type checks are enabled. | |
103 field = "hello"; /// static type warning | |
104 } catch (TypeError error) { | |
105 result = 1; | |
106 Expect.equals("int", error.dstType); | |
107 Expect.equals("String", error.srcType); | |
108 Expect.equals("field", error.dstName); | |
109 int pos = error.url.lastIndexOf("/", error.url.length); | |
110 if (pos == -1) { | |
111 pos = error.url.lastIndexOf("\\", error.url.length); | |
112 } | |
113 String subs = error.url.substring(pos + 1, error.url.length); | |
114 Expect.equals("TypeTest.dart", subs); | |
115 Expect.equals(99, error.line); | |
116 Expect.equals(15, error.column); | |
117 } | |
118 return result; | |
119 } | |
120 | |
121 static testAnyFunction() { | |
122 int result = 0; | |
123 Function anyFunction; | |
124 f() { }; | |
125 anyFunction = f; // No error. | |
126 try { | |
127 // Throws a TypeError if type checks are enabled. | |
128 int i = f; /// static type warning | |
129 } catch (TypeError error) { | |
130 result = 1; | |
131 Expect.equals("int", error.dstType); | |
132 Expect.equals("() => var", error.srcType); // TODO(regis): => Dynamic. | |
133 Expect.equals("i", error.dstName); | |
134 int pos = error.url.lastIndexOf("/", error.url.length); | |
135 if (pos == -1) { | |
136 pos = error.url.lastIndexOf("\\", error.url.length); | |
137 } | |
138 String subs = error.url.substring(pos + 1, error.url.length); | |
139 Expect.equals("TypeTest.dart", subs); | |
140 Expect.equals(123, error.line); | |
141 Expect.equals(15, error.column); | |
142 } | |
143 return result; | |
144 } | |
145 | |
146 static testVoidFunction() { | |
147 int result = 0; | |
148 Function anyFunction; | |
149 void acceptVoidFunObj(void voidFunObj(Object obj)) { }; | |
150 void acceptObjFunObj(Object objFunObj(Object obj)) { }; | |
151 void voidFunObj(Object obj) { }; | |
152 Object objFunObj(Object obj) { return obj; }; | |
153 anyFunction = voidFunObj; // No error. | |
154 anyFunction = objFunObj; // No error. | |
155 acceptVoidFunObj(voidFunObj); | |
156 acceptVoidFunObj(objFunObj); | |
157 acceptObjFunObj(objFunObj); | |
158 try { | |
159 // Throws a TypeError. Issue 2348 | |
160 acceptObjFunObj(voidFunObj); /// static type warning | |
161 } catch (TypeError error) { | |
162 result = 1; | |
163 Expect.equals("(Object) => Object", error.dstType); | |
164 Expect.equals("(Object) => void", error.srcType); | |
165 Expect.equals("objFunObj", error.dstName); | |
166 int pos = error.url.lastIndexOf("/", error.url.length); | |
167 if (pos == -1) { | |
168 pos = error.url.lastIndexOf("\\", error.url.length); | |
169 } | |
170 String subs = error.url.substring(pos + 1, error.url.length); | |
171 Expect.equals("TypeTest.dart", subs); | |
172 Expect.equals(145, error.line); | |
173 Expect.equals(33, error.column); | |
174 } | |
175 return result; | |
176 } | |
177 | |
178 static testFunctionNum() { | |
179 int result = 0; | |
180 Function anyFunction; | |
181 void acceptFunNum(void funNum(num num)) { }; | |
182 void funObj(Object obj) { }; | |
183 void funNum(num num) { }; | |
184 void funInt(int i) { }; | |
185 void funString(String s) { }; | |
186 anyFunction = funObj; // No error. | |
187 anyFunction = funNum; // No error. | |
188 anyFunction = funInt; // No error. | |
189 anyFunction = funString; // No error. | |
190 acceptFunNum(funObj); // No error. | |
191 acceptFunNum(funNum); // No error. | |
192 acceptFunNum(funInt); // No error. | |
193 try { | |
194 // Throws an error. | |
195 acceptFunNum(funString); /// static type warning | |
196 } catch (TypeError error) { | |
197 result = 1; | |
198 Expect.equals("(num) => void", error.dstType); | |
199 Expect.equals("(String) => void", error.srcType); | |
200 Expect.equals("funNum", error.dstName); | |
201 int pos = error.url.lastIndexOf("/", error.url.length); | |
202 if (pos == -1) { | |
203 pos = error.url.lastIndexOf("\\", error.url.length); | |
204 } | |
205 String subs = error.url.substring(pos + 1, error.url.length); | |
206 Expect.equals("TypeTest.dart", subs); | |
207 Expect.equals(175, error.line); | |
208 Expect.equals(28, error.column); | |
209 } | |
210 return result; | |
211 } | |
212 | |
213 static testBoolCheck() { | |
214 int result = 0; | |
215 try { | |
216 // Throws a TypeError if type checks are enabled. | |
217 bool i = !"hello"; /// static type warning | |
218 } catch (TypeError error) { | |
219 result++; | |
220 Expect.equals("bool", error.dstType); | |
221 Expect.equals("String", error.srcType); | |
222 Expect.equals("boolean expression", error.dstName); | |
223 int pos = error.url.lastIndexOf("/", error.url.length); | |
224 if (pos == -1) { | |
225 pos = error.url.lastIndexOf("\\", error.url.length); | |
226 } | |
227 String subs = error.url.substring(pos + 1, error.url.length); | |
228 Expect.equals("TypeTest.dart", subs); | |
229 Expect.equals(209, error.line); | |
230 Expect.equals(17, error.column); | |
231 } | |
232 try { | |
233 // Throws a TypeError if type checks are enabled. | |
234 while ("hello") {}; /// static type warning | |
235 } catch (TypeError error) { | |
236 result++; | |
237 Expect.equals("bool", error.dstType); | |
238 Expect.equals("String", error.srcType); | |
239 Expect.equals("boolean expression", error.dstName); | |
240 int pos = error.url.lastIndexOf("/", error.url.length); | |
241 if (pos == -1) { | |
242 pos = error.url.lastIndexOf("\\", error.url.length); | |
243 } | |
244 String subs = error.url.substring(pos + 1, error.url.length); | |
245 Expect.equals("TypeTest.dart", subs); | |
246 Expect.equals(225, error.line); | |
247 Expect.equals(14, error.column); | |
248 } | |
249 try { | |
250 // Throws a TypeError if type checks are enabled. | |
251 do {} while ("hello"); /// static type warning | |
252 } catch (TypeError error) { | |
253 result++; | |
254 Expect.equals("bool", error.dstType); | |
255 Expect.equals("String", error.srcType); | |
256 Expect.equals("boolean expression", error.dstName); | |
257 int pos = error.url.lastIndexOf("/", error.url.length); | |
258 if (pos == -1) { | |
259 pos = error.url.lastIndexOf("\\", error.url.length); | |
260 } | |
261 String subs = error.url.substring(pos + 1, error.url.length); | |
262 Expect.equals("TypeTest.dart", subs); | |
263 Expect.equals(241, error.line); | |
264 Expect.equals(20, error.column); | |
265 } | |
266 try { | |
267 // Throws a TypeError if type checks are enabled. | |
268 for (;"hello";) {}; /// static type warning | |
269 } catch (TypeError error) { | |
270 result++; | |
271 Expect.equals("bool", error.dstType); | |
272 Expect.equals("String", error.srcType); | |
273 Expect.equals("boolean expression", error.dstName); | |
274 int pos = error.url.lastIndexOf("/", error.url.length); | |
275 if (pos == -1) { | |
276 pos = error.url.lastIndexOf("\\", error.url.length); | |
277 } | |
278 String subs = error.url.substring(pos + 1, error.url.length); | |
279 Expect.equals("TypeTest.dart", subs); | |
280 Expect.equals(257, error.line); | |
281 Expect.equals(13, error.column); | |
282 } | |
283 try { | |
284 // Throws a TypeError if type checks are enabled. | |
285 int i = "hello" ? 1 : 0; /// static type warning | |
286 } catch (TypeError error) { | |
287 result++; | |
288 Expect.equals("bool", error.dstType); | |
289 Expect.equals("String", error.srcType); | |
290 Expect.equals("boolean expression", error.dstName); | |
291 int pos = error.url.lastIndexOf("/", error.url.length); | |
292 if (pos == -1) { | |
293 pos = error.url.lastIndexOf("\\", error.url.length); | |
294 } | |
295 String subs = error.url.substring(pos + 1, error.url.length); | |
296 Expect.equals("TypeTest.dart", subs); | |
297 Expect.equals(273, error.line); | |
298 Expect.equals(15, error.column); | |
299 } | |
300 try { | |
301 // Throws a TypeError if type checks are enabled. | |
302 if ("hello") {}; /// static type warning | |
303 } catch (TypeError error) { | |
304 result++; | |
305 Expect.equals("bool", error.dstType); | |
306 Expect.equals("String", error.srcType); | |
307 Expect.equals("boolean expression", error.dstName); | |
308 int pos = error.url.lastIndexOf("/", error.url.length); | |
309 if (pos == -1) { | |
310 pos = error.url.lastIndexOf("\\", error.url.length); | |
311 } | |
312 String subs = error.url.substring(pos + 1, error.url.length); | |
313 Expect.equals("TypeTest.dart", subs); | |
314 Expect.equals(289, error.line); | |
315 Expect.equals(11, error.column); | |
316 } | |
317 try { | |
318 // Throws a TypeError if type checks are enabled. | |
319 if ("hello" || false) {}; /// static type warning | |
320 } catch (TypeError error) { | |
321 result++; | |
322 Expect.equals("bool", error.dstType); | |
323 Expect.equals("String", error.srcType); | |
324 Expect.equals("boolean expression", error.dstName); | |
325 int pos = error.url.lastIndexOf("/", error.url.length); | |
326 if (pos == -1) { | |
327 pos = error.url.lastIndexOf("\\", error.url.length); | |
328 } | |
329 String subs = error.url.substring(pos + 1, error.url.length); | |
330 Expect.equals("TypeTest.dart", subs); | |
331 Expect.equals(305, error.line); | |
332 Expect.equals(11, error.column); | |
333 } | |
334 try { | |
335 // Throws a TypeError if type checks are enabled. | |
336 if (false || "hello") {}; /// static type warning | |
337 } catch (TypeError error) { | |
338 result++; | |
339 Expect.equals("bool", error.dstType); | |
340 Expect.equals("String", error.srcType); | |
341 Expect.equals("boolean expression", error.dstName); | |
342 int pos = error.url.lastIndexOf("/", error.url.length); | |
343 if (pos == -1) { | |
344 pos = error.url.lastIndexOf("\\", error.url.length); | |
345 } | |
346 String subs = error.url.substring(pos + 1, error.url.length); | |
347 Expect.equals("TypeTest.dart", subs); | |
348 Expect.equals(321, error.line); | |
349 Expect.equals(20, error.column); | |
350 } | |
351 try { | |
352 // TODO: I don't this should throw a TypeError, as null is a valid bool | |
353 // value. It might well should throw some other exception however. | |
354 // if (null) {}; // Throws a TypeError if type checks are enabled. | |
355 } catch (TypeError error) { | |
356 result++; | |
357 Expect.equals("bool", error.dstType); | |
358 Expect.equals("Null", error.srcType); | |
359 Expect.equals("boolean expression", error.dstName); | |
360 int pos = error.url.lastIndexOf("/", error.url.length); | |
361 if (pos == -1) { | |
362 pos = error.url.lastIndexOf("\\", error.url.length); | |
363 } | |
364 String subs = error.url.substring(pos + 1, error.url.length); | |
365 Expect.equals("TypeTest.dart", subs); | |
366 Expect.equals(337, error.line); | |
367 Expect.equals(11, error.column); | |
368 } | |
369 return result; | |
370 } | |
371 | |
372 | |
373 static int testFactory() { | |
374 int result = 0; | |
375 try { | |
376 var x = new C(); | |
377 } catch (TypeError error) { | |
378 result++; | |
379 Expect.equals("C", error.dstType); | |
380 Expect.equals("Smi", error.srcType); | |
381 Expect.equals("function result", error.dstName); | |
382 int pos = error.url.lastIndexOf("/", error.url.length); | |
383 if (pos == -1) { | |
384 pos = error.url.lastIndexOf("\\", error.url.length); | |
385 } | |
386 String subs = error.url.substring(pos + 1, error.url.length); | |
387 Expect.equals("TypeTest.dart", subs); | |
388 Expect.equals(472, error.line); | |
389 Expect.equals(12, error.column); | |
390 } | |
391 return result; | |
392 } | |
393 | |
394 static int testListAssigment() { | |
395 int result = 0; | |
396 { | |
397 var a = new List(5); | |
398 List a0 = a; | |
399 List<Object> ao = a; | |
400 List<int> ai = a; | |
401 List<num> an = a; | |
402 List<String> as = a; | |
403 } | |
404 { | |
405 var a = new List<Object>(5); | |
406 List a0 = a; | |
407 List<Object> ao = a; | |
408 try { | |
409 List<int> ai = a; | |
410 } catch (TypeError error) { | |
411 result++; | |
412 } | |
413 try { | |
414 List<num> an = a; | |
415 } catch (TypeError error) { | |
416 result++; | |
417 } | |
418 try { | |
419 List<String> as = a; | |
420 } catch (TypeError error) { | |
421 result++; | |
422 } | |
423 } | |
424 { | |
425 var a = new List<int>(5); | |
426 List a0 = a; | |
427 List<Object> ao = a; | |
428 List<int> ai = a; | |
429 List<num> an = a; | |
430 try { | |
431 List<String> as = a; | |
432 } catch (TypeError error) { | |
433 result++; | |
434 } | |
435 } | |
436 { | |
437 var a = new List<num>(5); | |
438 List a0 = a; | |
439 List<Object> ao = a; | |
440 try { | |
441 List<int> ai = a; | |
442 } catch (TypeError error) { | |
443 result++; | |
444 } | |
445 List<num> an = a; | |
446 try { | |
447 List<String> as = a; | |
448 } catch (TypeError error) { | |
449 result++; | |
450 } | |
451 } | |
452 { | |
453 var a = new List<String>(5); | |
454 List a0 = a; | |
455 List<Object> ao = a; | |
456 try { | |
457 List<int> ai = a; | |
458 } catch (TypeError error) { | |
459 result++; | |
460 } | |
461 try { | |
462 List<num> an = a; | |
463 } catch (TypeError error) { | |
464 result++; | |
465 } | |
466 List<String> as = a; | |
467 } | |
468 return result; | |
469 } | |
470 | |
471 static testMain() { | |
472 Expect.equals(1, test()); | |
473 Expect.equals(1, testSideEffect()); | |
474 Expect.equals(1, testArgument()); | |
475 Expect.equals(1, testReturn()); | |
476 Expect.equals(1, testField()); | |
477 Expect.equals(1, testAnyFunction()); | |
478 //Expect.equals(1, testVoidFunction()); - Function type checking issue | |
479 //Expect.equals(1, testFunctionNum()); - Function type checking issue | |
480 Expect.equals(8, testBoolCheck()); | |
481 //Expect.equals(1, testFactory()); - Not doing a test on factories | |
482 Expect.equals(8, testListAssigment()); | |
483 } | |
484 } | |
485 | |
486 | |
487 class C { | |
488 factory C() { | |
489 return 1; // Implicit result type is 'C', not int. | |
490 } | |
491 } | |
492 | |
493 | |
494 main() { | |
495 TypeTest.testMain(); | |
496 } | |
OLD | NEW |