Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 engine.resolver_test; | 5 library engine.resolver_test; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/context/context.dart' as newContext; | 9 import 'package:analyzer/src/context/context.dart' as newContext; |
| 10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 runReflectiveTests(TypeProviderImplTest); | 61 runReflectiveTests(TypeProviderImplTest); |
| 62 runReflectiveTests(TypeResolverVisitorTest); | 62 runReflectiveTests(TypeResolverVisitorTest); |
| 63 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); | 63 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); |
| 64 runReflectiveTests(ErrorResolverTest); | 64 runReflectiveTests(ErrorResolverTest); |
| 65 runReflectiveTests(HintCodeTest); | 65 runReflectiveTests(HintCodeTest); |
| 66 runReflectiveTests(MemberMapTest); | 66 runReflectiveTests(MemberMapTest); |
| 67 runReflectiveTests(NonHintCodeTest); | 67 runReflectiveTests(NonHintCodeTest); |
| 68 runReflectiveTests(SimpleResolverTest); | 68 runReflectiveTests(SimpleResolverTest); |
| 69 runReflectiveTests(StrictModeTest); | 69 runReflectiveTests(StrictModeTest); |
| 70 runReflectiveTests(TypePropagationTest); | 70 runReflectiveTests(TypePropagationTest); |
| 71 runReflectiveTests(StrongModeDownwardsInferenceTest); | |
| 71 runReflectiveTests(StrongModeStaticTypeAnalyzer2Test); | 72 runReflectiveTests(StrongModeStaticTypeAnalyzer2Test); |
| 72 runReflectiveTests(StrongModeTypePropagationTest); | 73 runReflectiveTests(StrongModeTypePropagationTest); |
| 73 } | 74 } |
| 74 | 75 |
| 75 /** | 76 /** |
| 76 * The class `AnalysisContextFactory` defines utility methods used to create ana lysis contexts | 77 * The class `AnalysisContextFactory` defines utility methods used to create ana lysis contexts |
| 77 * for testing purposes. | 78 * for testing purposes. |
| 78 */ | 79 */ |
| 79 class AnalysisContextFactory { | 80 class AnalysisContextFactory { |
| 80 static String _DART_MATH = "dart:math"; | 81 static String _DART_MATH = "dart:math"; |
| (...skipping 12039 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 12120 int f() { | 12121 int f() { |
| 12121 num n = 1234; | 12122 num n = 1234; |
| 12122 return n & 0x0F; | 12123 return n & 0x0F; |
| 12123 }'''); | 12124 }'''); |
| 12124 computeLibrarySourceErrors(source); | 12125 computeLibrarySourceErrors(source); |
| 12125 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]); | 12126 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]); |
| 12126 } | 12127 } |
| 12127 } | 12128 } |
| 12128 | 12129 |
| 12129 /** | 12130 /** |
| 12131 * Strong mode static analyzer downwards inference tests | |
| 12132 */ | |
| 12133 @reflectiveTest | |
| 12134 class StrongModeDownwardsInferenceTest extends ResolverTestCase { | |
| 12135 TypeAssertions _assertions; | |
| 12136 AsserterBuilder<Element, DartType> _hasElement; | |
| 12137 AsserterBuilder<DartType, DartType> _isType; | |
| 12138 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, | |
| 12139 DartType> _isFunction2Of; | |
| 12140 AsserterBuilderBuilder<Asserter<DartType>, List<Asserter<DartType>>, | |
| 12141 DartType> _isInstantiationOf; | |
| 12142 Asserter<DartType> _isInt; | |
| 12143 Asserter<DartType> _isNum; | |
| 12144 Asserter<DartType> _isString; | |
| 12145 Asserter<DartType> _isDynamic; | |
| 12146 AsserterBuilder<Asserter<DartType>, InterfaceType> _isListOf; | |
| 12147 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>, | |
| 12148 InterfaceType> _isMapOf; | |
| 12149 | |
| 12150 @override | |
| 12151 void setUp() { | |
| 12152 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | |
| 12153 options.strongMode = true; | |
| 12154 resetWithOptions(options); | |
| 12155 _assertions = new TypeAssertions(typeProvider); | |
| 12156 _isType = _assertions.isType; | |
| 12157 _hasElement = _assertions.hasElement; | |
| 12158 _isInstantiationOf = _assertions.isInstantiationOf; | |
| 12159 _isInt = _assertions.isInt; | |
| 12160 _isNum = _assertions.isNum; | |
| 12161 _isString = _assertions.isString; | |
| 12162 _isDynamic = _assertions.isDynamic; | |
| 12163 _isListOf = _assertions.isListOf; | |
| 12164 _isMapOf = _assertions.isMapOf; | |
| 12165 _isFunction2Of = _assertions.isFunction2Of; | |
| 12166 } | |
| 12167 | |
| 12168 void test_cascadeExpression() { | |
| 12169 String code = r''' | |
| 12170 class A<T> { | |
| 12171 List<T> map(T a, List<T> mapper(T x)) => mapper(a); | |
| 12172 } | |
| 12173 | |
| 12174 void main () { | |
| 12175 A<int> a = new A()..map(0, (x) => [x]); | |
| 12176 } | |
| 12177 '''; | |
| 12178 CompilationUnit unit = resolveSource(code); | |
| 12179 List<Statement> statements = | |
| 12180 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12181 CascadeExpression fetch(int i) { | |
| 12182 VariableDeclarationStatement stmt = statements[i]; | |
| 12183 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12184 CascadeExpression exp = decl.initializer; | |
| 12185 return exp; | |
| 12186 } | |
| 12187 Element elementA = AstFinder.getClass(unit, "A").element; | |
| 12188 | |
| 12189 CascadeExpression cascade = fetch(0); | |
| 12190 _isInstantiationOf(_hasElement(elementA))([_isInt])(cascade.staticType); | |
| 12191 MethodInvocation invoke = cascade.cascadeSections[0]; | |
| 12192 FunctionExpression function = invoke.argumentList.arguments[1]; | |
| 12193 ExecutableElement f0 = function.element; | |
| 12194 _isListOf(_isInt)(f0.type.returnType); | |
| 12195 expect(f0.type.normalParameterTypes[0], typeProvider.intType); | |
| 12196 } | |
| 12197 | |
| 12198 void test_constructorInitializer_propagation() { | |
| 12199 String code = r''' | |
| 12200 class A { | |
| 12201 List<String> x; | |
| 12202 A() : this.x = []; | |
| 12203 } | |
| 12204 '''; | |
| 12205 CompilationUnit unit = resolveSource(code); | |
| 12206 ConstructorDeclaration constructor = | |
| 12207 AstFinder.getConstructorInClass(unit, "A", null); | |
| 12208 ConstructorFieldInitializer assignment = constructor.initializers[0]; | |
| 12209 Expression exp = assignment.expression; | |
| 12210 _isListOf(_isString)(exp.staticType); | |
| 12211 } | |
| 12212 | |
| 12213 void test_factoryConstructor_propagation() { | |
| 12214 String code = r''' | |
| 12215 class A<T> { | |
| 12216 factory A() { return new B(); } | |
| 12217 } | |
| 12218 class B<S> extends A<S> {} | |
| 12219 '''; | |
| 12220 CompilationUnit unit = resolveSource(code); | |
| 12221 | |
| 12222 ConstructorDeclaration constructor = | |
| 12223 AstFinder.getConstructorInClass(unit, "A", null); | |
| 12224 BlockFunctionBody body = constructor.body; | |
| 12225 ReturnStatement stmt = body.block.statements[0]; | |
| 12226 InstanceCreationExpression exp = stmt.expression; | |
| 12227 ClassElement elementB = AstFinder.getClass(unit, "B").element; | |
| 12228 ClassElement elementA = AstFinder.getClass(unit, "A").element; | |
| 12229 expect(exp.constructorName.type.type.element, elementB); | |
| 12230 _isInstantiationOf(_hasElement(elementB))( | |
| 12231 [_isType(elementA.typeParameters[0].type)])(exp.staticType); | |
| 12232 } | |
| 12233 | |
| 12234 void test_fieldDeclaration_propagation() { | |
| 12235 String code = r''' | |
| 12236 class A { | |
| 12237 List<String> f0 = ["hello"]; | |
| 12238 } | |
| 12239 '''; | |
| 12240 CompilationUnit unit = resolveSource(code); | |
| 12241 | |
| 12242 VariableDeclaration field = AstFinder.getFieldInClass(unit, "A", "f0"); | |
| 12243 | |
| 12244 _isListOf(_isString)(field.initializer.staticType); | |
| 12245 } | |
| 12246 | |
| 12247 void test_functionDeclaration_body_propagation() { | |
| 12248 String code = r''' | |
| 12249 typedef T Function2<S, T>(S x); | |
| 12250 | |
| 12251 List<int> test1() => []; | |
| 12252 | |
| 12253 Function2<int, int> test2 (int x) { | |
| 12254 Function2<String, int> inner() { | |
| 12255 return (x) => x.length; | |
| 12256 } | |
| 12257 return (x) => x; | |
| 12258 } | |
| 12259 '''; | |
| 12260 CompilationUnit unit = resolveSource(code); | |
| 12261 | |
| 12262 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt); | |
| 12263 | |
| 12264 FunctionDeclaration test1 = AstFinder.getTopLevelFunction(unit, "test1"); | |
| 12265 ExpressionFunctionBody body = test1.functionExpression.body; | |
| 12266 assertListOfInt(body.expression.staticType); | |
| 12267 | |
| 12268 List<Statement> statements = | |
| 12269 AstFinder.getStatementsInTopLevelFunction(unit, "test2"); | |
| 12270 | |
| 12271 FunctionDeclaration inner = | |
| 12272 (statements[0] as FunctionDeclarationStatement).functionDeclaration; | |
| 12273 BlockFunctionBody body0 = inner.functionExpression.body; | |
| 12274 ReturnStatement return0 = body0.block.statements[0]; | |
| 12275 Expression anon0 = return0.expression; | |
| 12276 FunctionType type0 = anon0.staticType; | |
| 12277 expect(type0.returnType, typeProvider.intType); | |
| 12278 expect(type0.normalParameterTypes[0], typeProvider.stringType); | |
| 12279 | |
| 12280 FunctionExpression anon1 = (statements[1] as ReturnStatement).expression; | |
| 12281 FunctionType type1 = anon1.element.type; | |
| 12282 expect(type1.returnType, typeProvider.intType); | |
| 12283 expect(type1.normalParameterTypes[0], typeProvider.intType); | |
| 12284 } | |
| 12285 | |
| 12286 void test_functionLiteral_assignment_typedArguments() { | |
| 12287 String code = r''' | |
| 12288 typedef T Function2<S, T>(S x); | |
| 12289 | |
| 12290 void main () { | |
| 12291 Function2<int, String> l0 = (int x) => null; | |
| 12292 Function2<int, String> l1 = (int x) => "hello"; | |
| 12293 Function2<int, String> l2 = (String x) => "hello"; | |
| 12294 Function2<int, String> l3 = (int x) => 3; | |
| 12295 Function2<int, String> l4 = (int x) {return 3;}; | |
| 12296 } | |
| 12297 '''; | |
| 12298 CompilationUnit unit = resolveSource(code); | |
| 12299 List<Statement> statements = | |
| 12300 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12301 DartType literal(int i) { | |
| 12302 VariableDeclarationStatement stmt = statements[i]; | |
| 12303 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12304 FunctionExpression exp = decl.initializer; | |
| 12305 return exp.element.type; | |
| 12306 } | |
| 12307 _isFunction2Of(_isInt, _isString)(literal(0)); | |
| 12308 _isFunction2Of(_isInt, _isString)(literal(1)); | |
| 12309 _isFunction2Of(_isString, _isString)(literal(2)); | |
| 12310 _isFunction2Of(_isInt, _isInt)(literal(3)); | |
| 12311 _isFunction2Of(_isInt, _isString)(literal(4)); | |
| 12312 } | |
| 12313 | |
| 12314 void test_functionLiteral_assignment_unTypedArguments() { | |
| 12315 String code = r''' | |
| 12316 typedef T Function2<S, T>(S x); | |
| 12317 | |
| 12318 void main () { | |
| 12319 Function2<int, String> l0 = (x) => null; | |
| 12320 Function2<int, String> l1 = (x) => "hello"; | |
| 12321 Function2<int, String> l2 = (x) => "hello"; | |
| 12322 Function2<int, String> l3 = (x) => 3; | |
| 12323 Function2<int, String> l4 = (x) {return 3;}; | |
| 12324 } | |
| 12325 '''; | |
| 12326 CompilationUnit unit = resolveSource(code); | |
| 12327 List<Statement> statements = | |
| 12328 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12329 DartType literal(int i) { | |
| 12330 VariableDeclarationStatement stmt = statements[i]; | |
| 12331 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12332 FunctionExpression exp = decl.initializer; | |
| 12333 return exp.element.type; | |
| 12334 } | |
| 12335 _isFunction2Of(_isInt, _isString)(literal(0)); | |
| 12336 _isFunction2Of(_isInt, _isString)(literal(1)); | |
| 12337 _isFunction2Of(_isInt, _isString)(literal(2)); | |
| 12338 _isFunction2Of(_isInt, _isInt)(literal(3)); | |
| 12339 _isFunction2Of(_isInt, _isString)(literal(4)); | |
| 12340 } | |
| 12341 | |
| 12342 void test_functionLiteral_body_propagation() { | |
| 12343 String code = r''' | |
| 12344 typedef T Function2<S, T>(S x); | |
| 12345 | |
| 12346 void main () { | |
| 12347 Function2<int, List<String>> l0 = (int x) => ["hello"]; | |
| 12348 Function2<int, List<String>> l1 = (String x) => ["hello"]; | |
| 12349 Function2<int, List<String>> l2 = (int x) => [3]; | |
| 12350 Function2<int, List<String>> l3 = (int x) {return [3];}; | |
| 12351 } | |
| 12352 '''; | |
| 12353 CompilationUnit unit = resolveSource(code); | |
| 12354 List<Statement> statements = | |
| 12355 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12356 Expression functionReturnValue(int i) { | |
| 12357 VariableDeclarationStatement stmt = statements[i]; | |
| 12358 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12359 FunctionExpression exp = decl.initializer; | |
| 12360 FunctionBody body = exp.body; | |
| 12361 if (body is ExpressionFunctionBody) { | |
| 12362 return body.expression; | |
| 12363 } else { | |
| 12364 Statement stmt = (body as BlockFunctionBody).block.statements[0]; | |
| 12365 return (stmt as ReturnStatement).expression; | |
| 12366 } | |
| 12367 } | |
| 12368 Asserter<InterfaceType> assertListOfString = _isListOf(_isString); | |
| 12369 assertListOfString(functionReturnValue(0).staticType); | |
| 12370 assertListOfString(functionReturnValue(1).staticType); | |
| 12371 assertListOfString(functionReturnValue(2).staticType); | |
| 12372 assertListOfString(functionReturnValue(3).staticType); | |
| 12373 } | |
| 12374 | |
| 12375 void test_functionLiteral_functionExpressionInvocation_typedArguments() { | |
| 12376 String code = r''' | |
| 12377 class Mapper<F, T> { | |
| 12378 T map(T mapper(F x)) => mapper(null); | |
| 12379 } | |
| 12380 | |
| 12381 void main () { | |
| 12382 (new Mapper<int, String>().map)((int x) => null); | |
| 12383 (new Mapper<int, String>().map)((int x) => "hello"); | |
| 12384 (new Mapper<int, String>().map)((String x) => "hello"); | |
| 12385 (new Mapper<int, String>().map)((int x) => 3); | |
| 12386 (new Mapper<int, String>().map)((int x) {return 3;}); | |
| 12387 } | |
| 12388 '''; | |
| 12389 CompilationUnit unit = resolveSource(code); | |
| 12390 List<Statement> statements = | |
| 12391 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12392 DartType literal(int i) { | |
| 12393 ExpressionStatement stmt = statements[i]; | |
| 12394 FunctionExpressionInvocation invk = stmt.expression; | |
| 12395 FunctionExpression exp = invk.argumentList.arguments[0]; | |
| 12396 return exp.element.type; | |
| 12397 } | |
| 12398 _isFunction2Of(_isInt, _isString)(literal(0)); | |
| 12399 _isFunction2Of(_isInt, _isString)(literal(1)); | |
| 12400 _isFunction2Of(_isString, _isString)(literal(2)); | |
| 12401 _isFunction2Of(_isInt, _isInt)(literal(3)); | |
| 12402 _isFunction2Of(_isInt, _isString)(literal(4)); | |
| 12403 } | |
| 12404 | |
| 12405 void test_functionLiteral_functionExpressionInvocation_unTypedArguments() { | |
| 12406 String code = r''' | |
| 12407 class Mapper<F, T> { | |
| 12408 T map(T mapper(F x)) => mapper(null); | |
| 12409 } | |
| 12410 | |
| 12411 void main () { | |
| 12412 (new Mapper<int, String>().map)((x) => null); | |
| 12413 (new Mapper<int, String>().map)((x) => "hello"); | |
| 12414 (new Mapper<int, String>().map)((x) => "hello"); | |
| 12415 (new Mapper<int, String>().map)((x) => 3); | |
| 12416 (new Mapper<int, String>().map)((x) {return 3;}); | |
| 12417 } | |
| 12418 '''; | |
| 12419 CompilationUnit unit = resolveSource(code); | |
| 12420 List<Statement> statements = | |
| 12421 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12422 DartType literal(int i) { | |
| 12423 ExpressionStatement stmt = statements[i]; | |
| 12424 FunctionExpressionInvocation invk = stmt.expression; | |
| 12425 FunctionExpression exp = invk.argumentList.arguments[0]; | |
| 12426 return exp.element.type; | |
| 12427 } | |
| 12428 _isFunction2Of(_isInt, _isString)(literal(0)); | |
| 12429 _isFunction2Of(_isInt, _isString)(literal(1)); | |
| 12430 _isFunction2Of(_isInt, _isString)(literal(2)); | |
| 12431 _isFunction2Of(_isInt, _isInt)(literal(3)); | |
| 12432 _isFunction2Of(_isInt, _isString)(literal(4)); | |
| 12433 } | |
| 12434 | |
| 12435 void test_functionLiteral_functionInvocation_typedArguments() { | |
| 12436 String code = r''' | |
| 12437 String map(String mapper(int x)) => mapper(null); | |
| 12438 | |
| 12439 void main () { | |
| 12440 map((int x) => null); | |
| 12441 map((int x) => "hello"); | |
| 12442 map((String x) => "hello"); | |
| 12443 map((int x) => 3); | |
| 12444 map((int x) {return 3;}); | |
| 12445 } | |
| 12446 '''; | |
| 12447 CompilationUnit unit = resolveSource(code); | |
| 12448 List<Statement> statements = | |
| 12449 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12450 DartType literal(int i) { | |
| 12451 ExpressionStatement stmt = statements[i]; | |
| 12452 MethodInvocation invk = stmt.expression; | |
| 12453 FunctionExpression exp = invk.argumentList.arguments[0]; | |
| 12454 return exp.element.type; | |
| 12455 } | |
| 12456 _isFunction2Of(_isInt, _isString)(literal(0)); | |
| 12457 _isFunction2Of(_isInt, _isString)(literal(1)); | |
| 12458 _isFunction2Of(_isString, _isString)(literal(2)); | |
| 12459 _isFunction2Of(_isInt, _isInt)(literal(3)); | |
| 12460 _isFunction2Of(_isInt, _isString)(literal(4)); | |
| 12461 } | |
| 12462 | |
| 12463 void test_functionLiteral_functionInvocation_unTypedArguments() { | |
| 12464 String code = r''' | |
| 12465 String map(String mapper(int x)) => mapper(null); | |
| 12466 | |
| 12467 void main () { | |
| 12468 map((x) => null); | |
| 12469 map((x) => "hello"); | |
| 12470 map((x) => "hello"); | |
| 12471 map((x) => 3); | |
| 12472 map((x) {return 3;}); | |
| 12473 } | |
| 12474 '''; | |
| 12475 CompilationUnit unit = resolveSource(code); | |
| 12476 List<Statement> statements = | |
| 12477 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12478 DartType literal(int i) { | |
| 12479 ExpressionStatement stmt = statements[i]; | |
| 12480 MethodInvocation invk = stmt.expression; | |
| 12481 FunctionExpression exp = invk.argumentList.arguments[0]; | |
| 12482 return exp.element.type; | |
| 12483 } | |
| 12484 _isFunction2Of(_isInt, _isString)(literal(0)); | |
| 12485 _isFunction2Of(_isInt, _isString)(literal(1)); | |
| 12486 _isFunction2Of(_isInt, _isString)(literal(2)); | |
| 12487 _isFunction2Of(_isInt, _isInt)(literal(3)); | |
| 12488 _isFunction2Of(_isInt, _isString)(literal(4)); | |
| 12489 } | |
| 12490 | |
| 12491 void test_functionLiteral_methodInvocation_typedArguments() { | |
| 12492 String code = r''' | |
| 12493 class Mapper<F, T> { | |
| 12494 T map(T mapper(F x)) => mapper(null); | |
| 12495 } | |
| 12496 | |
| 12497 void main () { | |
| 12498 new Mapper<int, String>().map((int x) => null); | |
| 12499 new Mapper<int, String>().map((int x) => "hello"); | |
| 12500 new Mapper<int, String>().map((String x) => "hello"); | |
| 12501 new Mapper<int, String>().map((int x) => 3); | |
| 12502 new Mapper<int, String>().map((int x) {return 3;}); | |
| 12503 } | |
| 12504 '''; | |
| 12505 CompilationUnit unit = resolveSource(code); | |
| 12506 List<Statement> statements = | |
| 12507 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12508 DartType literal(int i) { | |
| 12509 ExpressionStatement stmt = statements[i]; | |
| 12510 MethodInvocation invk = stmt.expression; | |
| 12511 FunctionExpression exp = invk.argumentList.arguments[0]; | |
| 12512 return exp.element.type; | |
| 12513 } | |
| 12514 _isFunction2Of(_isInt, _isString)(literal(0)); | |
| 12515 _isFunction2Of(_isInt, _isString)(literal(1)); | |
| 12516 _isFunction2Of(_isString, _isString)(literal(2)); | |
| 12517 _isFunction2Of(_isInt, _isInt)(literal(3)); | |
| 12518 _isFunction2Of(_isInt, _isString)(literal(4)); | |
| 12519 } | |
| 12520 | |
| 12521 void test_functionLiteral_methodInvocation_unTypedArguments() { | |
| 12522 String code = r''' | |
| 12523 class Mapper<F, T> { | |
| 12524 T map(T mapper(F x)) => mapper(null); | |
| 12525 } | |
| 12526 | |
| 12527 void main () { | |
| 12528 new Mapper<int, String>().map((x) => null); | |
| 12529 new Mapper<int, String>().map((x) => "hello"); | |
| 12530 new Mapper<int, String>().map((x) => "hello"); | |
| 12531 new Mapper<int, String>().map((x) => 3); | |
| 12532 new Mapper<int, String>().map((x) {return 3;}); | |
| 12533 } | |
| 12534 '''; | |
| 12535 CompilationUnit unit = resolveSource(code); | |
| 12536 List<Statement> statements = | |
| 12537 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12538 DartType literal(int i) { | |
| 12539 ExpressionStatement stmt = statements[i]; | |
| 12540 MethodInvocation invk = stmt.expression; | |
| 12541 FunctionExpression exp = invk.argumentList.arguments[0]; | |
| 12542 return exp.element.type; | |
| 12543 } | |
| 12544 _isFunction2Of(_isInt, _isString)(literal(0)); | |
| 12545 _isFunction2Of(_isInt, _isString)(literal(1)); | |
| 12546 _isFunction2Of(_isInt, _isString)(literal(2)); | |
| 12547 _isFunction2Of(_isInt, _isInt)(literal(3)); | |
| 12548 _isFunction2Of(_isInt, _isString)(literal(4)); | |
| 12549 } | |
| 12550 | |
| 12551 void test_functionLiteral_unTypedArgument_propagation() { | |
| 12552 String code = r''' | |
| 12553 typedef T Function2<S, T>(S x); | |
| 12554 | |
| 12555 void main () { | |
| 12556 Function2<int, int> l0 = (x) => x; | |
| 12557 Function2<int, int> l1 = (x) => x+1; | |
| 12558 Function2<int, String> l2 = (x) => x; | |
| 12559 Function2<int, String> l3 = (x) => x.toLowerCase(); | |
| 12560 Function2<String, String> l4 = (x) => x.toLowerCase(); | |
| 12561 } | |
| 12562 '''; | |
| 12563 CompilationUnit unit = resolveSource(code); | |
| 12564 List<Statement> statements = | |
| 12565 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12566 Expression functionReturnValue(int i) { | |
| 12567 VariableDeclarationStatement stmt = statements[i]; | |
| 12568 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12569 FunctionExpression exp = decl.initializer; | |
| 12570 FunctionBody body = exp.body; | |
| 12571 if (body is ExpressionFunctionBody) { | |
| 12572 return body.expression; | |
| 12573 } else { | |
| 12574 Statement stmt = (body as BlockFunctionBody).block.statements[0]; | |
| 12575 return (stmt as ReturnStatement).expression; | |
| 12576 } | |
| 12577 } | |
| 12578 expect(functionReturnValue(0).staticType, typeProvider.intType); | |
| 12579 expect(functionReturnValue(1).staticType, typeProvider.intType); | |
| 12580 expect(functionReturnValue(2).staticType, typeProvider.intType); | |
| 12581 expect(functionReturnValue(3).staticType, typeProvider.dynamicType); | |
| 12582 expect(functionReturnValue(4).staticType, typeProvider.stringType); | |
| 12583 } | |
| 12584 | |
| 12585 void test_instanceCreation() { | |
| 12586 String code = r''' | |
| 12587 class A<S, T> { | |
| 12588 S x; | |
| 12589 T y; | |
| 12590 A(this.x, this.y); | |
| 12591 A.named(this.x, this.y); | |
| 12592 } | |
| 12593 | |
| 12594 class B<S, T> extends A<T, S> { | |
| 12595 B(S y, T x) : super(x, y); | |
| 12596 B.named(S y, T x) : super.named(x, y); | |
| 12597 } | |
| 12598 | |
| 12599 class C<S> extends B<S, S> { | |
| 12600 C(S a) : super(a, a); | |
| 12601 C.named(S a) : super.named(a, a); | |
| 12602 } | |
| 12603 | |
| 12604 class D<S, T> extends B<T, int> { | |
| 12605 D(T a) : super(a, 3); | |
| 12606 D.named(T a) : super.named(a, 3); | |
| 12607 } | |
| 12608 | |
| 12609 class E<S, T> extends A<C<S>, T> { | |
| 12610 E(T a) : super(null, a); | |
| 12611 } | |
| 12612 | |
| 12613 class F<S, T> extends A<S, T> { | |
| 12614 F(S x, T y, {List<S> a, List<T> b}) : super(x, y); | |
| 12615 F.named(S x, T y, [S a, T b]) : super(a, b); | |
| 12616 } | |
| 12617 | |
| 12618 void test0() { | |
| 12619 A<int, String> a0 = new A(3, "hello"); | |
|
Jennifer Messerly
2015/11/23 22:11:58
formatting is a little off in this string literal.
Leaf
2015/12/01 21:49:12
Done.
| |
| 12620 A<int, String> a1 = new A.named(3, "hello"); | |
| 12621 A<int, String> a2 = new A<int, String>(3, "hello"); | |
| 12622 A<int, String> a3 = new A<int, String>.named(3, "hello"); | |
| 12623 A<int, String> a4 = new A<int, dynamic>(3, "hello"); | |
| 12624 A<int, String> a5 = new A<dynamic, dynamic>.named(3, "hello"); | |
| 12625 } | |
| 12626 void test1() { | |
| 12627 A<int, String> a0 = new A("hello", 3); | |
| 12628 A<int, String> a1 = new A.named("hello", 3); | |
| 12629 } | |
| 12630 void test2() { | |
| 12631 A<int, String> a0 = new B("hello", 3); | |
| 12632 A<int, String> a1 = new B.named("hello", 3); | |
| 12633 A<int, String> a2 = new B<String, int>("hello", 3); | |
| 12634 A<int, String> a3 = new B<String, int>.named("hello", 3); | |
| 12635 A<int, String> a4 = new B<String, dynamic>("hello", 3); | |
| 12636 A<int, String> a5 = new B<dynamic, dynamic>.named("hello", 3); | |
| 12637 } | |
| 12638 void test3() { | |
| 12639 A<int, String> a0 = new B(3, "hello"); | |
| 12640 A<int, String> a1 = new B.named(3, "hello"); | |
| 12641 } | |
| 12642 void test4() { | |
| 12643 A<int, int> a0 = new C(3); | |
| 12644 A<int, int> a1 = new C.named(3); | |
| 12645 A<int, int> a2 = new C<int>(3); | |
| 12646 A<int, int> a3 = new C<int>.named(3); | |
| 12647 A<int, int> a4 = new C<dynamic>(3); | |
| 12648 A<int, int> a5 = new C<dynamic>.named(3); | |
| 12649 } | |
| 12650 void test5() { | |
| 12651 A<int, int> a0 = new C("hello"); | |
| 12652 A<int, int> a1 = new C.named("hello"); | |
| 12653 } | |
| 12654 void test6() { | |
| 12655 A<int, String> a0 = new D("hello"); | |
| 12656 A<int, String> a1 = new D.named("hello"); | |
| 12657 A<int, String> a2 = new D<int, String>("hello"); | |
| 12658 A<int, String> a3 = new D<String, String>.named("hello"); | |
| 12659 A<int, String> a4 = new D<num, dynamic>("hello"); | |
| 12660 A<int, String> a5 = new D<dynamic, dynamic>.named("hello"); | |
| 12661 } | |
| 12662 void test7() { | |
| 12663 A<int, String> a0 = new D(3); | |
| 12664 A<int, String> a1 = new D.named(3); | |
| 12665 } | |
| 12666 void test8() { // Currently we only allow variable constraints. Test that we reject. | |
|
Jennifer Messerly
2015/11/23 22:11:58
long line here
Leaf
2015/12/01 21:49:12
Done.
| |
| 12667 A<C<int>, String> a0 = new E("hello"); | |
| 12668 } | |
| 12669 void test9() { // Check named and optional arguments | |
| 12670 A<int, String> a0 = new F(3, "hello", a: [3], b: ["hello"]); | |
| 12671 A<int, String> a1 = new F(3, "hello", a: ["hello"], b:[3]); | |
| 12672 A<int, String> a2 = new F.named(3, "hello", 3, "hello"); | |
| 12673 A<int, String> a3 = new F.named(3, "hello"); | |
| 12674 A<int, String> a4 = new F.named(3, "hello", "hello", 3); | |
| 12675 A<int, String> a5 = new F.named(3, "hello", "hello"); | |
| 12676 } | |
| 12677 } | |
| 12678 '''; | |
| 12679 CompilationUnit unit = resolveSource(code); | |
| 12680 | |
| 12681 Expression rhs(VariableDeclarationStatement stmt) { | |
| 12682 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12683 Expression exp = decl.initializer; | |
| 12684 return exp; | |
| 12685 } | |
| 12686 | |
| 12687 void hasType(Asserter<DartType> assertion, Expression exp) => | |
| 12688 assertion(exp.staticType); | |
| 12689 | |
| 12690 Element elementA = AstFinder.getClass(unit, "A").element; | |
| 12691 Element elementB = AstFinder.getClass(unit, "B").element; | |
| 12692 Element elementC = AstFinder.getClass(unit, "C").element; | |
| 12693 Element elementD = AstFinder.getClass(unit, "D").element; | |
| 12694 Element elementE = AstFinder.getClass(unit, "E").element; | |
| 12695 Element elementF = AstFinder.getClass(unit, "F").element; | |
| 12696 | |
| 12697 AsserterBuilder<List<Asserter<DartType>>, DartType> assertAOf = | |
| 12698 _isInstantiationOf(_hasElement(elementA)); | |
| 12699 AsserterBuilder<List<Asserter<DartType>>, DartType> assertBOf = | |
| 12700 _isInstantiationOf(_hasElement(elementB)); | |
| 12701 AsserterBuilder<List<Asserter<DartType>>, DartType> assertCOf = | |
| 12702 _isInstantiationOf(_hasElement(elementC)); | |
| 12703 AsserterBuilder<List<Asserter<DartType>>, DartType> assertDOf = | |
| 12704 _isInstantiationOf(_hasElement(elementD)); | |
| 12705 AsserterBuilder<List<Asserter<DartType>>, DartType> assertEOf = | |
| 12706 _isInstantiationOf(_hasElement(elementE)); | |
| 12707 AsserterBuilder<List<Asserter<DartType>>, DartType> assertFOf = | |
| 12708 _isInstantiationOf(_hasElement(elementF)); | |
| 12709 | |
| 12710 { | |
| 12711 List<Statement> statements = | |
| 12712 AstFinder.getStatementsInTopLevelFunction(unit, "test0"); | |
| 12713 | |
| 12714 hasType(assertAOf([_isInt, _isString]), rhs(statements[0])); | |
| 12715 hasType(assertAOf([_isInt, _isString]), rhs(statements[0])); | |
| 12716 hasType(assertAOf([_isInt, _isString]), rhs(statements[1])); | |
| 12717 hasType(assertAOf([_isInt, _isString]), rhs(statements[2])); | |
| 12718 hasType(assertAOf([_isInt, _isString]), rhs(statements[3])); | |
| 12719 hasType(assertAOf([_isInt, _isDynamic]), rhs(statements[4])); | |
| 12720 hasType(assertAOf([_isDynamic, _isDynamic]), rhs(statements[5])); | |
| 12721 } | |
| 12722 | |
| 12723 { | |
| 12724 List<Statement> statements = | |
| 12725 AstFinder.getStatementsInTopLevelFunction(unit, "test1"); | |
| 12726 hasType(assertAOf([_isInt, _isString]), rhs(statements[0])); | |
| 12727 hasType(assertAOf([_isInt, _isString]), rhs(statements[1])); | |
| 12728 } | |
| 12729 | |
| 12730 { | |
| 12731 List<Statement> statements = | |
| 12732 AstFinder.getStatementsInTopLevelFunction(unit, "test2"); | |
| 12733 hasType(assertBOf([_isString, _isInt]), rhs(statements[0])); | |
| 12734 hasType(assertBOf([_isString, _isInt]), rhs(statements[1])); | |
| 12735 hasType(assertBOf([_isString, _isInt]), rhs(statements[2])); | |
| 12736 hasType(assertBOf([_isString, _isInt]), rhs(statements[3])); | |
| 12737 hasType(assertBOf([_isString, _isDynamic]), rhs(statements[4])); | |
| 12738 hasType(assertBOf([_isDynamic, _isDynamic]), rhs(statements[5])); | |
| 12739 } | |
| 12740 | |
| 12741 { | |
| 12742 List<Statement> statements = | |
| 12743 AstFinder.getStatementsInTopLevelFunction(unit, "test3"); | |
| 12744 hasType(assertBOf([_isString, _isInt]), rhs(statements[0])); | |
| 12745 hasType(assertBOf([_isString, _isInt]), rhs(statements[1])); | |
| 12746 } | |
| 12747 | |
| 12748 { | |
| 12749 List<Statement> statements = | |
| 12750 AstFinder.getStatementsInTopLevelFunction(unit, "test4"); | |
| 12751 hasType(assertCOf([_isInt]), rhs(statements[0])); | |
| 12752 hasType(assertCOf([_isInt]), rhs(statements[1])); | |
| 12753 hasType(assertCOf([_isInt]), rhs(statements[2])); | |
| 12754 hasType(assertCOf([_isInt]), rhs(statements[3])); | |
| 12755 hasType(assertCOf([_isDynamic]), rhs(statements[4])); | |
| 12756 hasType(assertCOf([_isDynamic]), rhs(statements[5])); | |
| 12757 } | |
| 12758 | |
| 12759 { | |
| 12760 List<Statement> statements = | |
| 12761 AstFinder.getStatementsInTopLevelFunction(unit, "test5"); | |
| 12762 hasType(assertCOf([_isInt]), rhs(statements[0])); | |
| 12763 hasType(assertCOf([_isInt]), rhs(statements[1])); | |
| 12764 } | |
| 12765 | |
| 12766 { | |
| 12767 // The first type parameter is not constrained by the | |
| 12768 // context. We could choose a tighter type, but currently | |
| 12769 // we just use dynamic. | |
| 12770 List<Statement> statements = | |
| 12771 AstFinder.getStatementsInTopLevelFunction(unit, "test6"); | |
| 12772 hasType(assertDOf([_isDynamic, _isString]), rhs(statements[0])); | |
| 12773 hasType(assertDOf([_isDynamic, _isString]), rhs(statements[1])); | |
| 12774 hasType(assertDOf([_isInt, _isString]), rhs(statements[2])); | |
| 12775 hasType(assertDOf([_isString, _isString]), rhs(statements[3])); | |
| 12776 hasType(assertDOf([_isNum, _isDynamic]), rhs(statements[4])); | |
| 12777 hasType(assertDOf([_isDynamic, _isDynamic]), rhs(statements[5])); | |
| 12778 } | |
| 12779 | |
| 12780 { | |
| 12781 List<Statement> statements = | |
| 12782 AstFinder.getStatementsInTopLevelFunction(unit, "test7"); | |
| 12783 hasType(assertDOf([_isDynamic, _isString]), rhs(statements[0])); | |
| 12784 hasType(assertDOf([_isDynamic, _isString]), rhs(statements[1])); | |
| 12785 } | |
| 12786 | |
| 12787 { | |
| 12788 List<Statement> statements = | |
| 12789 AstFinder.getStatementsInTopLevelFunction(unit, "test8"); | |
| 12790 hasType(assertEOf([_isDynamic, _isDynamic]), rhs(statements[0])); | |
| 12791 } | |
| 12792 | |
| 12793 { | |
| 12794 List<Statement> statements = | |
| 12795 AstFinder.getStatementsInTopLevelFunction(unit, "test9"); | |
| 12796 hasType(assertFOf([_isInt, _isString]), rhs(statements[0])); | |
| 12797 hasType(assertFOf([_isInt, _isString]), rhs(statements[1])); | |
| 12798 hasType(assertFOf([_isInt, _isString]), rhs(statements[2])); | |
| 12799 hasType(assertFOf([_isInt, _isString]), rhs(statements[3])); | |
| 12800 hasType(assertFOf([_isInt, _isString]), rhs(statements[4])); | |
| 12801 hasType(assertFOf([_isInt, _isString]), rhs(statements[5])); | |
| 12802 } | |
| 12803 } | |
| 12804 | |
| 12805 void test_listLiteral_nested() { | |
| 12806 String code = r''' | |
| 12807 void main () { | |
| 12808 List<List<int>> l0 = [[]]; | |
| 12809 Iterable<List<int>> l1 = [[3]]; | |
| 12810 Iterable<List<int>> l2 = [[3], [4]]; | |
| 12811 List<List<int>> l3 = [["hello", 3], []]; | |
| 12812 } | |
| 12813 '''; | |
| 12814 CompilationUnit unit = resolveSource(code); | |
| 12815 List<Statement> statements = | |
| 12816 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12817 ListLiteral literal(int i) { | |
| 12818 VariableDeclarationStatement stmt = statements[i]; | |
| 12819 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12820 ListLiteral exp = decl.initializer; | |
| 12821 return exp; | |
| 12822 } | |
| 12823 | |
| 12824 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt); | |
| 12825 Asserter<InterfaceType> assertListOfListOfInt = _isListOf(assertListOfInt); | |
| 12826 | |
| 12827 assertListOfListOfInt(literal(0).staticType); | |
| 12828 assertListOfListOfInt(literal(1).staticType); | |
| 12829 assertListOfListOfInt(literal(2).staticType); | |
| 12830 assertListOfListOfInt(literal(3).staticType); | |
| 12831 | |
| 12832 assertListOfInt(literal(1).elements[0].staticType); | |
| 12833 assertListOfInt(literal(2).elements[0].staticType); | |
| 12834 assertListOfInt(literal(3).elements[0].staticType); | |
| 12835 } | |
| 12836 | |
| 12837 void test_listLiteral_simple() { | |
| 12838 String code = r''' | |
| 12839 void main () { | |
| 12840 List<int> l0 = []; | |
| 12841 List<int> l1 = [3]; | |
| 12842 List<int> l2 = ["hello"]; | |
| 12843 List<int> l3 = ["hello", 3]; | |
| 12844 } | |
| 12845 '''; | |
| 12846 CompilationUnit unit = resolveSource(code); | |
| 12847 List<Statement> statements = | |
| 12848 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12849 DartType literal(int i) { | |
| 12850 VariableDeclarationStatement stmt = statements[i]; | |
| 12851 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12852 ListLiteral exp = decl.initializer; | |
| 12853 return exp.staticType; | |
| 12854 } | |
| 12855 | |
| 12856 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt); | |
| 12857 | |
| 12858 assertListOfInt(literal(0)); | |
| 12859 assertListOfInt(literal(1)); | |
| 12860 assertListOfInt(literal(2)); | |
| 12861 assertListOfInt(literal(3)); | |
| 12862 } | |
| 12863 | |
| 12864 void test_listLiteral_simple_const() { | |
| 12865 String code = r''' | |
| 12866 void main () { | |
| 12867 const List<int> c0 = const []; | |
| 12868 const List<int> c1 = const [3]; | |
| 12869 const List<int> c2 = const ["hello"]; | |
| 12870 const List<int> c3 = const ["hello", 3]; | |
| 12871 } | |
| 12872 '''; | |
| 12873 CompilationUnit unit = resolveSource(code); | |
| 12874 List<Statement> statements = | |
| 12875 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12876 DartType literal(int i) { | |
| 12877 VariableDeclarationStatement stmt = statements[i]; | |
| 12878 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12879 ListLiteral exp = decl.initializer; | |
| 12880 return exp.staticType; | |
| 12881 } | |
| 12882 | |
| 12883 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt); | |
| 12884 | |
| 12885 assertListOfInt(literal(0)); | |
| 12886 assertListOfInt(literal(1)); | |
| 12887 assertListOfInt(literal(2)); | |
| 12888 assertListOfInt(literal(3)); | |
| 12889 } | |
| 12890 | |
| 12891 void test_listLiteral_simple_disabled() { | |
| 12892 String code = r''' | |
| 12893 void main () { | |
| 12894 List<int> l0 = <num>[]; | |
| 12895 List<int> l1 = <num>[3]; | |
| 12896 List<int> l2 = <String>["hello"]; | |
| 12897 List<int> l3 = <dynamic>["hello", 3]; | |
| 12898 } | |
| 12899 '''; | |
| 12900 CompilationUnit unit = resolveSource(code); | |
| 12901 List<Statement> statements = | |
| 12902 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12903 DartType literal(int i) { | |
| 12904 VariableDeclarationStatement stmt = statements[i]; | |
| 12905 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12906 ListLiteral exp = decl.initializer; | |
| 12907 return exp.staticType; | |
| 12908 } | |
| 12909 | |
| 12910 _isListOf(_isNum)(literal(0)); | |
| 12911 _isListOf(_isNum)(literal(1)); | |
| 12912 _isListOf(_isString)(literal(2)); | |
| 12913 _isListOf(_isDynamic)(literal(3)); | |
| 12914 } | |
| 12915 | |
| 12916 void test_listLiteral_simple_subtype() { | |
| 12917 String code = r''' | |
| 12918 void main () { | |
| 12919 Iterable<int> l0 = []; | |
| 12920 Iterable<int> l1 = [3]; | |
| 12921 Iterable<int> l2 = ["hello"]; | |
| 12922 Iterable<int> l3 = ["hello", 3]; | |
| 12923 } | |
| 12924 '''; | |
| 12925 CompilationUnit unit = resolveSource(code); | |
| 12926 List<Statement> statements = | |
| 12927 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12928 DartType literal(int i) { | |
| 12929 VariableDeclarationStatement stmt = statements[i]; | |
| 12930 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12931 ListLiteral exp = decl.initializer; | |
| 12932 return exp.staticType; | |
| 12933 } | |
| 12934 | |
| 12935 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt); | |
| 12936 | |
| 12937 assertListOfInt(literal(0)); | |
| 12938 assertListOfInt(literal(1)); | |
| 12939 assertListOfInt(literal(2)); | |
| 12940 assertListOfInt(literal(3)); | |
| 12941 } | |
| 12942 | |
| 12943 void test_mapLiteral_nested() { | |
| 12944 String code = r''' | |
| 12945 void main () { | |
| 12946 Map<int, List<String>> l0 = {}; | |
| 12947 Map<int, List<String>> l1 = {3: ["hello"]}; | |
| 12948 Map<int, List<String>> l2 = {"hello": ["hello"]}; | |
| 12949 Map<int, List<String>> l3 = {3: [3]}; | |
| 12950 Map<int, List<String>> l4 = {3:["hello"], "hello": [3]}; | |
| 12951 } | |
| 12952 '''; | |
| 12953 CompilationUnit unit = resolveSource(code); | |
| 12954 List<Statement> statements = | |
| 12955 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12956 MapLiteral literal(int i) { | |
| 12957 VariableDeclarationStatement stmt = statements[i]; | |
| 12958 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12959 MapLiteral exp = decl.initializer; | |
| 12960 return exp; | |
| 12961 } | |
| 12962 | |
| 12963 Asserter<InterfaceType> assertListOfString = _isListOf(_isString); | |
| 12964 Asserter<InterfaceType> assertMapOfIntToListOfString = | |
| 12965 _isMapOf(_isInt, assertListOfString); | |
| 12966 | |
| 12967 assertMapOfIntToListOfString(literal(0).staticType); | |
| 12968 assertMapOfIntToListOfString(literal(1).staticType); | |
| 12969 assertMapOfIntToListOfString(literal(2).staticType); | |
| 12970 assertMapOfIntToListOfString(literal(3).staticType); | |
| 12971 assertMapOfIntToListOfString(literal(4).staticType); | |
| 12972 | |
| 12973 assertListOfString(literal(1).entries[0].value.staticType); | |
| 12974 assertListOfString(literal(2).entries[0].value.staticType); | |
| 12975 assertListOfString(literal(3).entries[0].value.staticType); | |
| 12976 assertListOfString(literal(4).entries[0].value.staticType); | |
| 12977 } | |
| 12978 | |
| 12979 void test_mapLiteral_simple() { | |
| 12980 String code = r''' | |
| 12981 void main () { | |
| 12982 Map<int, String> l0 = {}; | |
| 12983 Map<int, String> l1 = {3: "hello"}; | |
| 12984 Map<int, String> l2 = {"hello": "hello"}; | |
| 12985 Map<int, String> l3 = {3: 3}; | |
| 12986 Map<int, String> l4 = {3:"hello", "hello": 3}; | |
| 12987 } | |
| 12988 '''; | |
| 12989 CompilationUnit unit = resolveSource(code); | |
| 12990 List<Statement> statements = | |
| 12991 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 12992 DartType literal(int i) { | |
| 12993 VariableDeclarationStatement stmt = statements[i]; | |
| 12994 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 12995 MapLiteral exp = decl.initializer; | |
| 12996 return exp.staticType; | |
| 12997 } | |
| 12998 | |
| 12999 Asserter<InterfaceType> assertMapOfIntToString = | |
| 13000 _isMapOf(_isInt, _isString); | |
| 13001 | |
| 13002 assertMapOfIntToString(literal(0)); | |
| 13003 assertMapOfIntToString(literal(1)); | |
| 13004 assertMapOfIntToString(literal(2)); | |
| 13005 assertMapOfIntToString(literal(3)); | |
| 13006 } | |
| 13007 | |
| 13008 void test_mapLiteral_simple_disabled() { | |
| 13009 String code = r''' | |
| 13010 void main () { | |
| 13011 Map<int, String> l0 = <int, dynamic>{}; | |
| 13012 Map<int, String> l1 = <int, dynamic>{3: "hello"}; | |
| 13013 Map<int, String> l2 = <int, dynamic>{"hello": "hello"}; | |
| 13014 Map<int, String> l3 = <int, dynamic>{3: 3}; | |
| 13015 } | |
| 13016 '''; | |
| 13017 CompilationUnit unit = resolveSource(code); | |
| 13018 List<Statement> statements = | |
| 13019 AstFinder.getStatementsInTopLevelFunction(unit, "main"); | |
| 13020 DartType literal(int i) { | |
| 13021 VariableDeclarationStatement stmt = statements[i]; | |
| 13022 VariableDeclaration decl = stmt.variables.variables[0]; | |
| 13023 MapLiteral exp = decl.initializer; | |
| 13024 return exp.staticType; | |
| 13025 } | |
| 13026 | |
| 13027 Asserter<InterfaceType> assertMapOfIntToDynamic = | |
| 13028 _isMapOf(_isInt, _isDynamic); | |
| 13029 | |
| 13030 assertMapOfIntToDynamic(literal(0)); | |
| 13031 assertMapOfIntToDynamic(literal(1)); | |
| 13032 assertMapOfIntToDynamic(literal(2)); | |
| 13033 assertMapOfIntToDynamic(literal(3)); | |
| 13034 } | |
| 13035 | |
| 13036 void test_methodDeclaration_body_propagation() { | |
| 13037 String code = r''' | |
| 13038 class A { | |
| 13039 List<String> m0(int x) => ["hello"]; | |
| 13040 List<String> m1(int x) {return [3];}; | |
| 13041 } | |
| 13042 '''; | |
| 13043 CompilationUnit unit = resolveSource(code); | |
| 13044 Expression methodReturnValue(String methodName) { | |
| 13045 MethodDeclaration method = | |
| 13046 AstFinder.getMethodInClass(unit, "A", methodName); | |
| 13047 FunctionBody body = method.body; | |
| 13048 if (body is ExpressionFunctionBody) { | |
| 13049 return body.expression; | |
| 13050 } else { | |
| 13051 Statement stmt = (body as BlockFunctionBody).block.statements[0]; | |
| 13052 return (stmt as ReturnStatement).expression; | |
| 13053 } | |
| 13054 } | |
| 13055 Asserter<InterfaceType> assertListOfString = _isListOf(_isString); | |
| 13056 assertListOfString(methodReturnValue("m0").staticType); | |
| 13057 assertListOfString(methodReturnValue("m1").staticType); | |
| 13058 } | |
| 13059 | |
| 13060 void test_redirectingConstructor_propagation() { | |
| 13061 String code = r''' | |
| 13062 class A { | |
| 13063 A() : this.named([]); | |
| 13064 A.named(List<String> x); | |
| 13065 } | |
| 13066 '''; | |
| 13067 CompilationUnit unit = resolveSource(code); | |
| 13068 | |
| 13069 ConstructorDeclaration constructor = | |
| 13070 AstFinder.getConstructorInClass(unit, "A", null); | |
| 13071 RedirectingConstructorInvocation invocation = constructor.initializers[0]; | |
| 13072 Expression exp = invocation.argumentList.arguments[0]; | |
| 13073 _isListOf(_isString)(exp.staticType); | |
| 13074 } | |
| 13075 | |
| 13076 void test_superConstructorInvocation_propagation() { | |
| 13077 String code = r''' | |
| 13078 class B { | |
| 13079 B(List<String>); | |
| 13080 } | |
| 13081 class A extends B { | |
| 13082 A() : super([]); | |
| 13083 } | |
| 13084 '''; | |
| 13085 CompilationUnit unit = resolveSource(code); | |
| 13086 | |
| 13087 ConstructorDeclaration constructor = | |
| 13088 AstFinder.getConstructorInClass(unit, "A", null); | |
| 13089 SuperConstructorInvocation invocation = constructor.initializers[0]; | |
| 13090 Expression exp = invocation.argumentList.arguments[0]; | |
| 13091 _isListOf(_isString)(exp.staticType); | |
| 13092 } | |
| 13093 } | |
| 13094 | |
| 13095 /** | |
| 12130 * Strong mode static analyzer end to end tests | 13096 * Strong mode static analyzer end to end tests |
| 12131 */ | 13097 */ |
| 12132 @reflectiveTest | 13098 @reflectiveTest |
| 12133 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { | 13099 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { |
| 12134 void setUp() { | 13100 void setUp() { |
| 12135 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 13101 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
| 12136 options.strongMode = true; | 13102 options.strongMode = true; |
| 12137 resetWithOptions(options); | 13103 resetWithOptions(options); |
| 12138 } | 13104 } |
| 12139 | 13105 |
| (...skipping 3053 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 15193 | 16159 |
| 15194 void _resolveTestUnit(String code) { | 16160 void _resolveTestUnit(String code) { |
| 15195 testCode = code; | 16161 testCode = code; |
| 15196 testSource = addSource(testCode); | 16162 testSource = addSource(testCode); |
| 15197 LibraryElement library = resolve2(testSource); | 16163 LibraryElement library = resolve2(testSource); |
| 15198 assertNoErrors(testSource); | 16164 assertNoErrors(testSource); |
| 15199 verify([testSource]); | 16165 verify([testSource]); |
| 15200 testUnit = resolveCompilationUnit(testSource, library); | 16166 testUnit = resolveCompilationUnit(testSource, library); |
| 15201 } | 16167 } |
| 15202 } | 16168 } |
| OLD | NEW |