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 // Dart test program testing type checks. | 5 // Dart test program testing type checks. |
6 | 6 |
7 class TypeTest { | 7 class TypeTest { |
8 static test() { | 8 static test() { |
9 int result = 0; | 9 int result = 0; |
10 try { | 10 try { |
11 // Throws a TypeError if type checks are enabled. | 11 // Throws a TypeError if type checks are enabled. |
12 int i = "hello"; /// static type warning | 12 int i = "hello"; /// static type warning |
13 } catch (TypeError error) { | 13 } catch (TypeError error) { |
14 result = 1; | 14 result = 1; |
15 Expect.equals("int", error.dstType); | 15 Expect.equals("int", error.dstType); |
16 Expect.equals("String", error.srcType); | 16 Expect.equals("String", error.srcType); |
17 Expect.equals("i", error.dstName); | 17 Expect.equals("i", error.dstName); |
18 int pos = error.url.lastIndexOf("/", error.url.length); | 18 int pos = error.url.lastIndexOf("/", error.url.length); |
19 if (pos == -1) { | 19 if (pos == -1) { |
20 pos = error.url.lastIndexOf("\\", error.url.length); | 20 pos = error.url.lastIndexOf("\\", error.url.length); |
21 } | 21 } |
22 String subs = error.url.substring(pos + 1, error.url.length); | 22 String subs = error.url.substring(pos + 1, error.url.length); |
23 Expect.equals("TypeTest.dart", subs); | 23 Expect.equals("type_test.dart", subs); |
24 Expect.equals(12, error.line); | 24 Expect.equals(12, error.line); |
25 Expect.equals(15, error.column); | 25 Expect.equals(15, error.column); |
26 } | 26 } |
27 return result; | 27 return result; |
28 } | 28 } |
29 | 29 |
30 static testSideEffect() { | 30 static testSideEffect() { |
31 int result = 0; | 31 int result = 0; |
32 int index() { | 32 int index() { |
33 result++; | 33 result++; |
(...skipping 21 matching lines...) Expand all Loading... |
55 } catch (TypeError error) { | 55 } catch (TypeError error) { |
56 result = 1; | 56 result = 1; |
57 Expect.equals("int", error.dstType); | 57 Expect.equals("int", error.dstType); |
58 Expect.equals("String", error.srcType); | 58 Expect.equals("String", error.srcType); |
59 Expect.equals("i", error.dstName); | 59 Expect.equals("i", error.dstName); |
60 int pos = error.url.lastIndexOf("/", error.url.length); | 60 int pos = error.url.lastIndexOf("/", error.url.length); |
61 if (pos == -1) { | 61 if (pos == -1) { |
62 pos = error.url.lastIndexOf("\\", error.url.length); | 62 pos = error.url.lastIndexOf("\\", error.url.length); |
63 } | 63 } |
64 String subs = error.url.substring(pos + 1, error.url.length); | 64 String subs = error.url.substring(pos + 1, error.url.length); |
65 Expect.equals("TypeTest.dart", subs); | 65 Expect.equals("type_test.dart", subs); |
66 Expect.equals(49, error.line); | 66 Expect.equals(49, error.line); |
67 Expect.equals(15, error.column); | 67 Expect.equals(15, error.column); |
68 } | 68 } |
69 return result; | 69 return result; |
70 } | 70 } |
71 | 71 |
72 static testReturn() { | 72 static testReturn() { |
73 int result = 0; | 73 int result = 0; |
74 int f(String s) { | 74 int f(String s) { |
75 // String not assignable to int on return | 75 // String not assignable to int on return |
76 return s; /// static type warning | 76 return s; /// static type warning |
77 } | 77 } |
78 try { | 78 try { |
79 // Throws a TypeError if type checks are enabled. | 79 // Throws a TypeError if type checks are enabled. |
80 int i = f("hello"); | 80 int i = f("hello"); |
81 } catch (TypeError error) { | 81 } catch (TypeError error) { |
82 result = 1; | 82 result = 1; |
83 Expect.equals("int", error.dstType); | 83 Expect.equals("int", error.dstType); |
84 Expect.equals("String", error.srcType); | 84 Expect.equals("String", error.srcType); |
85 Expect.equals("function result", error.dstName); | 85 Expect.equals("function result", error.dstName); |
86 int pos = error.url.lastIndexOf("/", error.url.length); | 86 int pos = error.url.lastIndexOf("/", error.url.length); |
87 if (pos == -1) { | 87 if (pos == -1) { |
88 pos = error.url.lastIndexOf("\\", error.url.length); | 88 pos = error.url.lastIndexOf("\\", error.url.length); |
89 } | 89 } |
90 String subs = error.url.substring(pos + 1, error.url.length); | 90 String subs = error.url.substring(pos + 1, error.url.length); |
91 Expect.equals("TypeTest.dart", subs); | 91 Expect.equals("type_test.dart", subs); |
92 Expect.equals(74, error.line); | 92 Expect.equals(74, error.line); |
93 Expect.equals(14, error.column); | 93 Expect.equals(14, error.column); |
94 } | 94 } |
95 return result; | 95 return result; |
96 } | 96 } |
97 | 97 |
98 static int field; | 98 static int field; |
99 static testField() { | 99 static testField() { |
100 int result = 0; | 100 int result = 0; |
101 try { | 101 try { |
102 // Throws a TypeError if type checks are enabled. | 102 // Throws a TypeError if type checks are enabled. |
103 field = "hello"; /// static type warning | 103 field = "hello"; /// static type warning |
104 } catch (TypeError error) { | 104 } catch (TypeError error) { |
105 result = 1; | 105 result = 1; |
106 Expect.equals("int", error.dstType); | 106 Expect.equals("int", error.dstType); |
107 Expect.equals("String", error.srcType); | 107 Expect.equals("String", error.srcType); |
108 Expect.equals("field", error.dstName); | 108 Expect.equals("field", error.dstName); |
109 int pos = error.url.lastIndexOf("/", error.url.length); | 109 int pos = error.url.lastIndexOf("/", error.url.length); |
110 if (pos == -1) { | 110 if (pos == -1) { |
111 pos = error.url.lastIndexOf("\\", error.url.length); | 111 pos = error.url.lastIndexOf("\\", error.url.length); |
112 } | 112 } |
113 String subs = error.url.substring(pos + 1, error.url.length); | 113 String subs = error.url.substring(pos + 1, error.url.length); |
114 Expect.equals("TypeTest.dart", subs); | 114 Expect.equals("type_test.dart", subs); |
115 Expect.equals(99, error.line); | 115 Expect.equals(99, error.line); |
116 Expect.equals(15, error.column); | 116 Expect.equals(15, error.column); |
117 } | 117 } |
118 return result; | 118 return result; |
119 } | 119 } |
120 | 120 |
121 static testAnyFunction() { | 121 static testAnyFunction() { |
122 int result = 0; | 122 int result = 0; |
123 Function anyFunction; | 123 Function anyFunction; |
124 f() { }; | 124 f() { }; |
125 anyFunction = f; // No error. | 125 anyFunction = f; // No error. |
126 try { | 126 try { |
127 // Throws a TypeError if type checks are enabled. | 127 // Throws a TypeError if type checks are enabled. |
128 int i = f; /// static type warning | 128 int i = f; /// static type warning |
129 } catch (TypeError error) { | 129 } catch (TypeError error) { |
130 result = 1; | 130 result = 1; |
131 Expect.equals("int", error.dstType); | 131 Expect.equals("int", error.dstType); |
132 Expect.equals("() => var", error.srcType); // TODO(regis): => Dynamic. | 132 Expect.equals("() => var", error.srcType); // TODO(regis): => Dynamic. |
133 Expect.equals("i", error.dstName); | 133 Expect.equals("i", error.dstName); |
134 int pos = error.url.lastIndexOf("/", error.url.length); | 134 int pos = error.url.lastIndexOf("/", error.url.length); |
135 if (pos == -1) { | 135 if (pos == -1) { |
136 pos = error.url.lastIndexOf("\\", error.url.length); | 136 pos = error.url.lastIndexOf("\\", error.url.length); |
137 } | 137 } |
138 String subs = error.url.substring(pos + 1, error.url.length); | 138 String subs = error.url.substring(pos + 1, error.url.length); |
139 Expect.equals("TypeTest.dart", subs); | 139 Expect.equals("type_test.dart", subs); |
140 Expect.equals(123, error.line); | 140 Expect.equals(123, error.line); |
141 Expect.equals(15, error.column); | 141 Expect.equals(15, error.column); |
142 } | 142 } |
143 return result; | 143 return result; |
144 } | 144 } |
145 | 145 |
146 static testVoidFunction() { | 146 static testVoidFunction() { |
147 int result = 0; | 147 int result = 0; |
148 Function anyFunction; | 148 Function anyFunction; |
149 void acceptVoidFunObj(void voidFunObj(Object obj)) { }; | 149 void acceptVoidFunObj(void voidFunObj(Object obj)) { }; |
(...skipping 11 matching lines...) Expand all Loading... |
161 } catch (TypeError error) { | 161 } catch (TypeError error) { |
162 result = 1; | 162 result = 1; |
163 Expect.equals("(Object) => Object", error.dstType); | 163 Expect.equals("(Object) => Object", error.dstType); |
164 Expect.equals("(Object) => void", error.srcType); | 164 Expect.equals("(Object) => void", error.srcType); |
165 Expect.equals("objFunObj", error.dstName); | 165 Expect.equals("objFunObj", error.dstName); |
166 int pos = error.url.lastIndexOf("/", error.url.length); | 166 int pos = error.url.lastIndexOf("/", error.url.length); |
167 if (pos == -1) { | 167 if (pos == -1) { |
168 pos = error.url.lastIndexOf("\\", error.url.length); | 168 pos = error.url.lastIndexOf("\\", error.url.length); |
169 } | 169 } |
170 String subs = error.url.substring(pos + 1, error.url.length); | 170 String subs = error.url.substring(pos + 1, error.url.length); |
171 Expect.equals("TypeTest.dart", subs); | 171 Expect.equals("type_test.dart", subs); |
172 Expect.equals(145, error.line); | 172 Expect.equals(145, error.line); |
173 Expect.equals(33, error.column); | 173 Expect.equals(33, error.column); |
174 } | 174 } |
175 return result; | 175 return result; |
176 } | 176 } |
177 | 177 |
178 static testFunctionNum() { | 178 static testFunctionNum() { |
179 int result = 0; | 179 int result = 0; |
180 Function anyFunction; | 180 Function anyFunction; |
181 void acceptFunNum(void funNum(num num)) { }; | 181 void acceptFunNum(void funNum(num num)) { }; |
(...skipping 14 matching lines...) Expand all Loading... |
196 } catch (TypeError error) { | 196 } catch (TypeError error) { |
197 result = 1; | 197 result = 1; |
198 Expect.equals("(num) => void", error.dstType); | 198 Expect.equals("(num) => void", error.dstType); |
199 Expect.equals("(String) => void", error.srcType); | 199 Expect.equals("(String) => void", error.srcType); |
200 Expect.equals("funNum", error.dstName); | 200 Expect.equals("funNum", error.dstName); |
201 int pos = error.url.lastIndexOf("/", error.url.length); | 201 int pos = error.url.lastIndexOf("/", error.url.length); |
202 if (pos == -1) { | 202 if (pos == -1) { |
203 pos = error.url.lastIndexOf("\\", error.url.length); | 203 pos = error.url.lastIndexOf("\\", error.url.length); |
204 } | 204 } |
205 String subs = error.url.substring(pos + 1, error.url.length); | 205 String subs = error.url.substring(pos + 1, error.url.length); |
206 Expect.equals("TypeTest.dart", subs); | 206 Expect.equals("type_test.dart", subs); |
207 Expect.equals(175, error.line); | 207 Expect.equals(175, error.line); |
208 Expect.equals(28, error.column); | 208 Expect.equals(28, error.column); |
209 } | 209 } |
210 return result; | 210 return result; |
211 } | 211 } |
212 | 212 |
213 static testBoolCheck() { | 213 static testBoolCheck() { |
214 int result = 0; | 214 int result = 0; |
215 try { | 215 try { |
216 // Throws a TypeError if type checks are enabled. | 216 // Throws a TypeError if type checks are enabled. |
217 bool i = !"hello"; /// static type warning | 217 bool i = !"hello"; /// static type warning |
218 } catch (TypeError error) { | 218 } catch (TypeError error) { |
219 result++; | 219 result++; |
220 Expect.equals("bool", error.dstType); | 220 Expect.equals("bool", error.dstType); |
221 Expect.equals("String", error.srcType); | 221 Expect.equals("String", error.srcType); |
222 Expect.equals("boolean expression", error.dstName); | 222 Expect.equals("boolean expression", error.dstName); |
223 int pos = error.url.lastIndexOf("/", error.url.length); | 223 int pos = error.url.lastIndexOf("/", error.url.length); |
224 if (pos == -1) { | 224 if (pos == -1) { |
225 pos = error.url.lastIndexOf("\\", error.url.length); | 225 pos = error.url.lastIndexOf("\\", error.url.length); |
226 } | 226 } |
227 String subs = error.url.substring(pos + 1, error.url.length); | 227 String subs = error.url.substring(pos + 1, error.url.length); |
228 Expect.equals("TypeTest.dart", subs); | 228 Expect.equals("type_test.dart", subs); |
229 Expect.equals(209, error.line); | 229 Expect.equals(209, error.line); |
230 Expect.equals(17, error.column); | 230 Expect.equals(17, error.column); |
231 } | 231 } |
232 try { | 232 try { |
233 // Throws a TypeError if type checks are enabled. | 233 // Throws a TypeError if type checks are enabled. |
234 while ("hello") {}; /// static type warning | 234 while ("hello") {}; /// static type warning |
235 } catch (TypeError error) { | 235 } catch (TypeError error) { |
236 result++; | 236 result++; |
237 Expect.equals("bool", error.dstType); | 237 Expect.equals("bool", error.dstType); |
238 Expect.equals("String", error.srcType); | 238 Expect.equals("String", error.srcType); |
239 Expect.equals("boolean expression", error.dstName); | 239 Expect.equals("boolean expression", error.dstName); |
240 int pos = error.url.lastIndexOf("/", error.url.length); | 240 int pos = error.url.lastIndexOf("/", error.url.length); |
241 if (pos == -1) { | 241 if (pos == -1) { |
242 pos = error.url.lastIndexOf("\\", error.url.length); | 242 pos = error.url.lastIndexOf("\\", error.url.length); |
243 } | 243 } |
244 String subs = error.url.substring(pos + 1, error.url.length); | 244 String subs = error.url.substring(pos + 1, error.url.length); |
245 Expect.equals("TypeTest.dart", subs); | 245 Expect.equals("type_test.dart", subs); |
246 Expect.equals(225, error.line); | 246 Expect.equals(225, error.line); |
247 Expect.equals(14, error.column); | 247 Expect.equals(14, error.column); |
248 } | 248 } |
249 try { | 249 try { |
250 // Throws a TypeError if type checks are enabled. | 250 // Throws a TypeError if type checks are enabled. |
251 do {} while ("hello"); /// static type warning | 251 do {} while ("hello"); /// static type warning |
252 } catch (TypeError error) { | 252 } catch (TypeError error) { |
253 result++; | 253 result++; |
254 Expect.equals("bool", error.dstType); | 254 Expect.equals("bool", error.dstType); |
255 Expect.equals("String", error.srcType); | 255 Expect.equals("String", error.srcType); |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 class C { | 487 class C { |
488 factory C() { | 488 factory C() { |
489 return 1; // Implicit result type is 'C', not int. | 489 return 1; // Implicit result type is 'C', not int. |
490 } | 490 } |
491 } | 491 } |
492 | 492 |
493 | 493 |
494 main() { | 494 main() { |
495 TypeTest.testMain(); | 495 TypeTest.testMain(); |
496 } | 496 } |
OLD | NEW |