OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
8 import 'package:analyzer/src/dart/analysis/driver.dart'; | 8 import 'package:analyzer/src/dart/analysis/driver.dart'; |
9 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 9 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
10 | 10 |
11 import '../dart/analysis/base.dart'; | 11 import '../dart/analysis/base.dart'; |
| 12 import '../task/strong/strong_test_helper.dart'; |
12 import 'element_text.dart'; | 13 import 'element_text.dart'; |
13 | 14 |
14 main() { | 15 main() { |
15 defineReflectiveSuite(() { | 16 defineReflectiveSuite(() { |
16 defineReflectiveTests(TopLevelInferenceTest); | 17 defineReflectiveTests(TopLevelInferenceTest); |
| 18 defineReflectiveTests(TopLevelInferenceErrorsTest); |
17 // defineReflectiveTests(ApplyCheckElementTextReplacements); | 19 // defineReflectiveTests(ApplyCheckElementTextReplacements); |
18 }); | 20 }); |
19 } | 21 } |
20 | 22 |
21 @reflectiveTest | 23 @reflectiveTest |
22 class ApplyCheckElementTextReplacements { | 24 class ApplyCheckElementTextReplacements { |
23 test_applyReplacements() { | 25 test_applyReplacements() { |
24 applyCheckElementTextReplacements(); | 26 applyCheckElementTextReplacements(); |
25 } | 27 } |
26 } | 28 } |
27 | 29 |
28 @reflectiveTest | 30 @reflectiveTest |
| 31 class TopLevelInferenceErrorsTest extends AbstractStrongTest { |
| 32 @override |
| 33 bool get enableNewAnalysisDriver => true; |
| 34 |
| 35 test_initializer_additive() async { |
| 36 await _assertErrorOnlyLeft(['+', '-']); |
| 37 } |
| 38 |
| 39 test_initializer_assign() async { |
| 40 var content = r''' |
| 41 var a = 1; |
| 42 var t1 = /*error:TOP_LEVEL_UNSUPPORTED*/a += 1; |
| 43 var t2 = (/*error:TOP_LEVEL_UNSUPPORTED*/a = 2); |
| 44 '''; |
| 45 await checkFile(content); |
| 46 } |
| 47 |
| 48 test_initializer_binary_onlyLeft() async { |
| 49 var content = r''' |
| 50 var a = 1; |
| 51 var t = (/*error:TOP_LEVEL_UNSUPPORTED*/a = 1) + (a = 2); |
| 52 '''; |
| 53 await checkFile(content); |
| 54 } |
| 55 |
| 56 test_initializer_bitwise() async { |
| 57 await _assertErrorOnlyLeft(['&', '|', '^']); |
| 58 } |
| 59 |
| 60 test_initializer_boolean() async { |
| 61 var content = r''' |
| 62 var a = 1; |
| 63 var t1 = ((a = 1) == 0) || ((a = 2) == 0); |
| 64 var t2 = ((a = 1) == 0) && ((a = 2) == 0); |
| 65 var t3 = !((a = 1) == 0); |
| 66 '''; |
| 67 await checkFile(content); |
| 68 } |
| 69 |
| 70 test_initializer_cascade() async { |
| 71 var content = r''' |
| 72 var a = 0; |
| 73 var t = (/*error:TOP_LEVEL_UNSUPPORTED*/a = 1)..isEven; |
| 74 '''; |
| 75 await checkFile(content); |
| 76 } |
| 77 |
| 78 test_initializer_classField_assign() async { |
| 79 var content = r''' |
| 80 class A { |
| 81 static var a = 1; |
| 82 static var t = /*error:TOP_LEVEL_UNSUPPORTED*/a += 1; |
| 83 } |
| 84 '''; |
| 85 await checkFile(content); |
| 86 } |
| 87 |
| 88 test_initializer_conditional() async { |
| 89 var content = r''' |
| 90 var a = 1; |
| 91 var b = true; |
| 92 var t = b ? |
| 93 (/*error:TOP_LEVEL_UNSUPPORTED*/a = 1) : |
| 94 (/*error:TOP_LEVEL_UNSUPPORTED*/a = 2); |
| 95 '''; |
| 96 await checkFile(content); |
| 97 } |
| 98 |
| 99 test_initializer_dependencyCycle() async { |
| 100 var content = r''' |
| 101 var a = /*error:TOP_LEVEL_CYCLE*/b; |
| 102 var b = /*error:TOP_LEVEL_CYCLE*/a; |
| 103 '''; |
| 104 await checkFile(content); |
| 105 } |
| 106 |
| 107 test_initializer_equality() async { |
| 108 var content = r''' |
| 109 var a = 1; |
| 110 var t1 = ((a = 1) == 0) == ((a = 2) == 0); |
| 111 var t2 = ((a = 1) == 0) != ((a = 2) == 0); |
| 112 '''; |
| 113 await checkFile(content); |
| 114 } |
| 115 |
| 116 test_initializer_functionLiteral_blockBody() async { |
| 117 var content = r''' |
| 118 var t = /*error:TOP_LEVEL_FUNCTION_LITERAL_BLOCK*/ |
| 119 /*info:INFERRED_TYPE_CLOSURE*/ |
| 120 (int p) {}; |
| 121 '''; |
| 122 await checkFile(content); |
| 123 } |
| 124 |
| 125 test_initializer_functionLiteral_expressionBody() async { |
| 126 var content = r''' |
| 127 var a = 0; |
| 128 var t = (int p) => (/*error:TOP_LEVEL_UNSUPPORTED*/a = 1); |
| 129 '''; |
| 130 await checkFile(content); |
| 131 } |
| 132 |
| 133 test_initializer_functionLiteral_parameters_withoutType() async { |
| 134 var content = r''' |
| 135 var t = (int a, |
| 136 /*error:TOP_LEVEL_FUNCTION_LITERAL_PARAMETER*/b, |
| 137 int c, |
| 138 /*error:TOP_LEVEL_FUNCTION_LITERAL_PARAMETER*/d) => 0; |
| 139 '''; |
| 140 await checkFile(content); |
| 141 } |
| 142 |
| 143 test_initializer_hasTypeAnnotation() async { |
| 144 var content = r''' |
| 145 var a = 1; |
| 146 int t = (a = 1); |
| 147 '''; |
| 148 await checkFile(content); |
| 149 } |
| 150 |
| 151 test_initializer_identifier() async { |
| 152 var content = r''' |
| 153 int top_function() => 0; |
| 154 var top_variable = 0; |
| 155 int get top_getter => 0; |
| 156 class A { |
| 157 static var static_field = 0; |
| 158 static int get static_getter => 0; |
| 159 static int static_method() => 0; |
| 160 int instance_method() => 0; |
| 161 } |
| 162 var t1 = top_function; |
| 163 var t2 = top_variable; |
| 164 var t3 = top_getter; |
| 165 var t4 = A.static_field; |
| 166 var t5 = A.static_getter; |
| 167 var t6 = A.static_method; |
| 168 var t7 = new A().instance_method; |
| 169 '''; |
| 170 await checkFile(content); |
| 171 } |
| 172 |
| 173 test_initializer_identifier_error() async { |
| 174 var content = r''' |
| 175 var a = 0; |
| 176 var b = (/*error:TOP_LEVEL_UNSUPPORTED*/a = 1); |
| 177 var c = /*error:TOP_LEVEL_IDENTIFIER_NO_TYPE*/b; |
| 178 '''; |
| 179 await checkFile(content); |
| 180 } |
| 181 |
| 182 test_initializer_ifNull() async { |
| 183 var content = r''' |
| 184 var a = 1; |
| 185 var t = /*error:TOP_LEVEL_UNSUPPORTED*/a ?? 2; |
| 186 '''; |
| 187 await checkFile(content); |
| 188 } |
| 189 |
| 190 test_initializer_instanceCreation_withoutTypeParameters() async { |
| 191 var content = r''' |
| 192 class A {} |
| 193 var t = new A(); |
| 194 '''; |
| 195 await checkFile(content); |
| 196 } |
| 197 |
| 198 test_initializer_instanceCreation_withTypeParameters() async { |
| 199 var content = r''' |
| 200 class A<T> {} |
| 201 var t1 = new /*error:TOP_LEVEL_TYPE_ARGUMENTS*/A(); |
| 202 var t2 = new A<int>(); |
| 203 '''; |
| 204 await checkFile(content); |
| 205 } |
| 206 |
| 207 test_initializer_instanceGetter() async { |
| 208 var content = r''' |
| 209 class A { |
| 210 int f = 1; |
| 211 } |
| 212 var a = new A()./*error:TOP_LEVEL_INSTANCE_GETTER*/f; |
| 213 '''; |
| 214 await checkFile(content); |
| 215 } |
| 216 |
| 217 test_initializer_methodInvocation_function() async { |
| 218 var content = r''' |
| 219 int f1() => null; |
| 220 T f2<T>() => null; |
| 221 var t1 = f1(); |
| 222 var t2 = /*error:TOP_LEVEL_TYPE_ARGUMENTS*/f2(); |
| 223 var t3 = f2<int>(); |
| 224 '''; |
| 225 await checkFile(content); |
| 226 } |
| 227 |
| 228 test_initializer_methodInvocation_method() async { |
| 229 var content = r''' |
| 230 class A { |
| 231 int m1() => null; |
| 232 T m2<T>() => null; |
| 233 } |
| 234 var a = new A(); |
| 235 var t1 = a.m1(); |
| 236 var t2 = a./*error:TOP_LEVEL_TYPE_ARGUMENTS*/m2(); |
| 237 var t3 = a.m2<int>(); |
| 238 '''; |
| 239 await checkFile(content); |
| 240 } |
| 241 |
| 242 test_initializer_multiplicative() async { |
| 243 await _assertErrorOnlyLeft(['*', '/', '%', '~/']); |
| 244 } |
| 245 |
| 246 test_initializer_postfixIncDec() async { |
| 247 var content = r''' |
| 248 var a = 1; |
| 249 var t1 = a++; |
| 250 var t2 = a--; |
| 251 '''; |
| 252 await checkFile(content); |
| 253 } |
| 254 |
| 255 test_initializer_prefixIncDec() async { |
| 256 var content = r''' |
| 257 var a = 1; |
| 258 var t1 = ++a; |
| 259 var t2 = --a; |
| 260 '''; |
| 261 await checkFile(content); |
| 262 } |
| 263 |
| 264 test_initializer_relational() async { |
| 265 await _assertErrorOnlyLeft(['>', '>=', '<', '<=']); |
| 266 } |
| 267 |
| 268 test_initializer_shift() async { |
| 269 await _assertErrorOnlyLeft(['<<', '>>']); |
| 270 } |
| 271 |
| 272 test_initializer_typedList() async { |
| 273 var content = r''' |
| 274 var a = 1; |
| 275 var t = <int>[a = 1]; |
| 276 '''; |
| 277 await checkFile(content); |
| 278 } |
| 279 |
| 280 test_initializer_typedMap() async { |
| 281 var content = r''' |
| 282 var a = 1; |
| 283 var t = <int, int>{(a = 1) : (a = 2)}; |
| 284 '''; |
| 285 await checkFile(content); |
| 286 } |
| 287 |
| 288 test_initializer_untypedList() async { |
| 289 var content = r''' |
| 290 var a = 1; |
| 291 var t = /*info:INFERRED_TYPE_LITERAL*/[ |
| 292 /*error:TOP_LEVEL_UNSUPPORTED*/a = 1, |
| 293 2, 3]; |
| 294 '''; |
| 295 await checkFile(content); |
| 296 } |
| 297 |
| 298 test_initializer_untypedMap() async { |
| 299 var content = r''' |
| 300 var a = 1; |
| 301 var t = /*info:INFERRED_TYPE_LITERAL*/{ |
| 302 (/*error:TOP_LEVEL_UNSUPPORTED*/a = 1) : |
| 303 (/*error:TOP_LEVEL_UNSUPPORTED*/a = 2)}; |
| 304 '''; |
| 305 await checkFile(content); |
| 306 } |
| 307 |
| 308 test_override_conflictFieldType() async { |
| 309 var content = r''' |
| 310 abstract class A { |
| 311 int aaa; |
| 312 } |
| 313 abstract class B { |
| 314 String aaa; |
| 315 } |
| 316 class C implements A, B { |
| 317 /*error:INVALID_METHOD_OVERRIDE*/var aaa; |
| 318 } |
| 319 '''; |
| 320 await checkFile(content); |
| 321 } |
| 322 |
| 323 @failingTest |
| 324 test_override_conflictParameterType_method() async { |
| 325 var content = r''' |
| 326 abstract class A { |
| 327 void mmm(int a); |
| 328 } |
| 329 abstract class B { |
| 330 void mmm(String a); |
| 331 } |
| 332 class C implements A, B { |
| 333 void mmm(/*error:TOP_LEVEL_INFERENCE_ERROR*/a) {} |
| 334 } |
| 335 '''; |
| 336 await checkFile(content); |
| 337 } |
| 338 |
| 339 Future<Null> _assertErrorOnlyLeft(List<String> operators) async { |
| 340 var err = '/*error:TOP_LEVEL_UNSUPPORTED*/'; |
| 341 String code = 'var a = 1;\n'; |
| 342 for (var i = 0; i < operators.length; i++) { |
| 343 String operator = operators[i]; |
| 344 code += 'var t$i = (${err}a = 1) $operator (a = 2);\n'; |
| 345 } |
| 346 await checkFile(code); |
| 347 } |
| 348 } |
| 349 |
| 350 @reflectiveTest |
29 class TopLevelInferenceTest extends BaseAnalysisDriverTest { | 351 class TopLevelInferenceTest extends BaseAnalysisDriverTest { |
30 void addFile(String path, String code) { | 352 void addFile(String path, String code) { |
31 provider.newFile(_p(path), code); | 353 provider.newFile(_p(path), code); |
32 } | 354 } |
33 | 355 |
34 test_initializer_additive() async { | 356 test_initializer_additive() async { |
35 var library = await _encodeDecodeLibrary(r''' | 357 var library = await _encodeDecodeLibrary(r''' |
36 var vPlusIntInt = 1 + 2; | 358 var vPlusIntInt = 1 + 2; |
37 var vPlusIntDouble = 1 + 2.0; | 359 var vPlusIntDouble = 1 + 2.0; |
38 var vPlusDoubleInt = 1.0 + 2; | 360 var vPlusDoubleInt = 1.0 + 2; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 var library = await _encodeDecodeLibrary(r''' | 454 var library = await _encodeDecodeLibrary(r''' |
133 var V = true ? 1 : 2.3; | 455 var V = true ? 1 : 2.3; |
134 '''); | 456 '''); |
135 checkElementText( | 457 checkElementText( |
136 library, | 458 library, |
137 r''' | 459 r''' |
138 num V; | 460 num V; |
139 '''); | 461 '''); |
140 } | 462 } |
141 | 463 |
| 464 test_initializer_equality() async { |
| 465 var library = await _encodeDecodeLibrary(r''' |
| 466 var vEq = 1 == 2; |
| 467 var vNotEq = 1 != 2; |
| 468 '''); |
| 469 checkElementText( |
| 470 library, |
| 471 r''' |
| 472 bool vEq; |
| 473 bool vNotEq; |
| 474 '''); |
| 475 } |
| 476 |
142 test_initializer_error_assign() async { | 477 test_initializer_error_assign() async { |
143 var library = await _encodeDecodeLibrary(r''' | 478 var library = await _encodeDecodeLibrary(r''' |
144 var a = 1; | 479 var a = 1; |
145 var t1 = (a = 2); | 480 var t1 = (a = 2); |
146 var t2 = (a += 2); | 481 var t2 = (a += 2); |
147 '''); | 482 '''); |
148 checkElementText( | 483 checkElementText( |
149 library, | 484 library, |
150 r''' | 485 r''' |
151 int a; | 486 int a; |
(...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 double vMultiplyDoubleInt; | 1258 double vMultiplyDoubleInt; |
924 double vMultiplyDoubleDouble; | 1259 double vMultiplyDoubleDouble; |
925 num vDivideIntInt; | 1260 num vDivideIntInt; |
926 num vDivideIntDouble; | 1261 num vDivideIntDouble; |
927 double vDivideDoubleInt; | 1262 double vDivideDoubleInt; |
928 double vDivideDoubleDouble; | 1263 double vDivideDoubleDouble; |
929 int vFloorDivide; | 1264 int vFloorDivide; |
930 '''); | 1265 '''); |
931 } | 1266 } |
932 | 1267 |
| 1268 @failingTest |
| 1269 test_initializer_onlyLeft() async { |
| 1270 var library = await _encodeDecodeLibrary(r''' |
| 1271 var a = 1; |
| 1272 var vEq = a == ((a = 2) == 0); |
| 1273 var vNotEq = a != ((a = 2) == 0); |
| 1274 '''); |
| 1275 checkElementText( |
| 1276 library, |
| 1277 r''' |
| 1278 int a; |
| 1279 bool vEq; |
| 1280 bool vNotEq; |
| 1281 '''); |
| 1282 } |
| 1283 |
933 test_initializer_parenthesized() async { | 1284 test_initializer_parenthesized() async { |
934 var library = await _encodeDecodeLibrary(r''' | 1285 var library = await _encodeDecodeLibrary(r''' |
935 var V = (42); | 1286 var V = (42); |
936 '''); | 1287 '''); |
937 checkElementText( | 1288 checkElementText( |
938 library, | 1289 library, |
939 r''' | 1290 r''' |
940 int V; | 1291 int V; |
941 '''); | 1292 '''); |
942 } | 1293 } |
(...skipping 1391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2334 | 2685 |
2335 Future<LibraryElement> _encodeDecodeLibrary(String text) async { | 2686 Future<LibraryElement> _encodeDecodeLibrary(String text) async { |
2336 String path = _p('/test.dart'); | 2687 String path = _p('/test.dart'); |
2337 provider.newFile(path, text); | 2688 provider.newFile(path, text); |
2338 UnitElementResult result = await driver.getUnitElement(path); | 2689 UnitElementResult result = await driver.getUnitElement(path); |
2339 return result.element.library; | 2690 return result.element.library; |
2340 } | 2691 } |
2341 | 2692 |
2342 String _p(String path) => provider.convertPath(path); | 2693 String _p(String path) => provider.convertPath(path); |
2343 } | 2694 } |
OLD | NEW |