| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 analyzer.test.constant_test; | 5 library analyzer.test.constant_test; |
| 6 | 6 |
| 7 import 'package:analyzer/context/declared_variables.dart'; | 7 import 'package:analyzer/context/declared_variables.dart'; |
| 8 import 'package:analyzer/dart/ast/ast.dart'; | 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 9 import 'package:analyzer/dart/ast/token.dart'; | 9 import 'package:analyzer/dart/ast/token.dart'; |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 Source source = addSource(r''' | 268 Source source = addSource(r''' |
| 269 const int a = c; | 269 const int a = c; |
| 270 const int b = a; | 270 const int b = a; |
| 271 const int c = b;'''); | 271 const int c = b;'''); |
| 272 LibraryElement libraryElement = resolve2(source); | 272 LibraryElement libraryElement = resolve2(source); |
| 273 CompilationUnit unit = | 273 CompilationUnit unit = |
| 274 analysisContext.resolveCompilationUnit(source, libraryElement); | 274 analysisContext.resolveCompilationUnit(source, libraryElement); |
| 275 analysisContext.computeErrors(source); | 275 analysisContext.computeErrors(source); |
| 276 expect(unit, isNotNull); | 276 expect(unit, isNotNull); |
| 277 ConstantValueComputer computer = _makeConstantValueComputer(); | 277 ConstantValueComputer computer = _makeConstantValueComputer(); |
| 278 computer.add(unit, source, source); | 278 computer.add(unit); |
| 279 computer.computeValues(); | 279 computer.computeValues(); |
| 280 NodeList<CompilationUnitMember> members = unit.declarations; | 280 NodeList<CompilationUnitMember> members = unit.declarations; |
| 281 expect(members, hasLength(3)); | 281 expect(members, hasLength(3)); |
| 282 _validate(false, (members[0] as TopLevelVariableDeclaration).variables); | 282 _validate(false, (members[0] as TopLevelVariableDeclaration).variables); |
| 283 _validate(false, (members[1] as TopLevelVariableDeclaration).variables); | 283 _validate(false, (members[1] as TopLevelVariableDeclaration).variables); |
| 284 _validate(false, (members[2] as TopLevelVariableDeclaration).variables); | 284 _validate(false, (members[2] as TopLevelVariableDeclaration).variables); |
| 285 } finally { | 285 } finally { |
| 286 AnalysisEngine.instance.logger = Logger.NULL; | 286 AnalysisEngine.instance.logger = Logger.NULL; |
| 287 } | 287 } |
| 288 } | 288 } |
| 289 | 289 |
| 290 void test_computeValues_dependentVariables() { | 290 void test_computeValues_dependentVariables() { |
| 291 Source source = addSource(r''' | 291 Source source = addSource(r''' |
| 292 const int b = a; | 292 const int b = a; |
| 293 const int a = 0;'''); | 293 const int a = 0;'''); |
| 294 LibraryElement libraryElement = resolve2(source); | 294 LibraryElement libraryElement = resolve2(source); |
| 295 CompilationUnit unit = | 295 CompilationUnit unit = |
| 296 analysisContext.resolveCompilationUnit(source, libraryElement); | 296 analysisContext.resolveCompilationUnit(source, libraryElement); |
| 297 expect(unit, isNotNull); | 297 expect(unit, isNotNull); |
| 298 ConstantValueComputer computer = _makeConstantValueComputer(); | 298 ConstantValueComputer computer = _makeConstantValueComputer(); |
| 299 computer.add(unit, source, source); | 299 computer.add(unit); |
| 300 computer.computeValues(); | 300 computer.computeValues(); |
| 301 NodeList<CompilationUnitMember> members = unit.declarations; | 301 NodeList<CompilationUnitMember> members = unit.declarations; |
| 302 expect(members, hasLength(2)); | 302 expect(members, hasLength(2)); |
| 303 _validate(true, (members[0] as TopLevelVariableDeclaration).variables); | 303 _validate(true, (members[0] as TopLevelVariableDeclaration).variables); |
| 304 _validate(true, (members[1] as TopLevelVariableDeclaration).variables); | 304 _validate(true, (members[1] as TopLevelVariableDeclaration).variables); |
| 305 } | 305 } |
| 306 | 306 |
| 307 void test_computeValues_empty() { | 307 void test_computeValues_empty() { |
| 308 ConstantValueComputer computer = _makeConstantValueComputer(); | 308 ConstantValueComputer computer = _makeConstantValueComputer(); |
| 309 computer.computeValues(); | 309 computer.computeValues(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 324 const int b = a; | 324 const int b = a; |
| 325 const int d = c;'''); | 325 const int d = c;'''); |
| 326 LibraryElement libraryElement = resolve2(librarySource); | 326 LibraryElement libraryElement = resolve2(librarySource); |
| 327 CompilationUnit libraryUnit = | 327 CompilationUnit libraryUnit = |
| 328 analysisContext.resolveCompilationUnit(librarySource, libraryElement); | 328 analysisContext.resolveCompilationUnit(librarySource, libraryElement); |
| 329 expect(libraryUnit, isNotNull); | 329 expect(libraryUnit, isNotNull); |
| 330 CompilationUnit partUnit = | 330 CompilationUnit partUnit = |
| 331 analysisContext.resolveCompilationUnit(partSource, libraryElement); | 331 analysisContext.resolveCompilationUnit(partSource, libraryElement); |
| 332 expect(partUnit, isNotNull); | 332 expect(partUnit, isNotNull); |
| 333 ConstantValueComputer computer = _makeConstantValueComputer(); | 333 ConstantValueComputer computer = _makeConstantValueComputer(); |
| 334 computer.add(libraryUnit, librarySource, librarySource); | 334 computer.add(libraryUnit); |
| 335 computer.add(partUnit, partSource, librarySource); | 335 computer.add(partUnit); |
| 336 computer.computeValues(); | 336 computer.computeValues(); |
| 337 NodeList<CompilationUnitMember> libraryMembers = libraryUnit.declarations; | 337 NodeList<CompilationUnitMember> libraryMembers = libraryUnit.declarations; |
| 338 expect(libraryMembers, hasLength(2)); | 338 expect(libraryMembers, hasLength(2)); |
| 339 _validate( | 339 _validate( |
| 340 true, (libraryMembers[0] as TopLevelVariableDeclaration).variables); | 340 true, (libraryMembers[0] as TopLevelVariableDeclaration).variables); |
| 341 _validate( | 341 _validate( |
| 342 true, (libraryMembers[1] as TopLevelVariableDeclaration).variables); | 342 true, (libraryMembers[1] as TopLevelVariableDeclaration).variables); |
| 343 NodeList<CompilationUnitMember> partMembers = libraryUnit.declarations; | 343 NodeList<CompilationUnitMember> partMembers = libraryUnit.declarations; |
| 344 expect(partMembers, hasLength(2)); | 344 expect(partMembers, hasLength(2)); |
| 345 _validate(true, (partMembers[0] as TopLevelVariableDeclaration).variables); | 345 _validate(true, (partMembers[0] as TopLevelVariableDeclaration).variables); |
| 346 _validate(true, (partMembers[1] as TopLevelVariableDeclaration).variables); | 346 _validate(true, (partMembers[1] as TopLevelVariableDeclaration).variables); |
| 347 } | 347 } |
| 348 | 348 |
| 349 void test_computeValues_singleVariable() { | 349 void test_computeValues_singleVariable() { |
| 350 Source source = addSource("const int a = 0;"); | 350 Source source = addSource("const int a = 0;"); |
| 351 LibraryElement libraryElement = resolve2(source); | 351 LibraryElement libraryElement = resolve2(source); |
| 352 CompilationUnit unit = | 352 CompilationUnit unit = |
| 353 analysisContext.resolveCompilationUnit(source, libraryElement); | 353 analysisContext.resolveCompilationUnit(source, libraryElement); |
| 354 expect(unit, isNotNull); | 354 expect(unit, isNotNull); |
| 355 ConstantValueComputer computer = _makeConstantValueComputer(); | 355 ConstantValueComputer computer = _makeConstantValueComputer(); |
| 356 computer.add(unit, source, source); | 356 computer.add(unit); |
| 357 computer.computeValues(); | 357 computer.computeValues(); |
| 358 NodeList<CompilationUnitMember> members = unit.declarations; | 358 NodeList<CompilationUnitMember> members = unit.declarations; |
| 359 expect(members, hasLength(1)); | 359 expect(members, hasLength(1)); |
| 360 _validate(true, (members[0] as TopLevelVariableDeclaration).variables); | 360 _validate(true, (members[0] as TopLevelVariableDeclaration).variables); |
| 361 } | 361 } |
| 362 | 362 |
| 363 void test_computeValues_value_depends_on_enum() { | 363 void test_computeValues_value_depends_on_enum() { |
| 364 Source source = addSource(''' | 364 Source source = addSource(''' |
| 365 enum E { id0, id1 } | 365 enum E { id0, id1 } |
| 366 const E e = E.id0; | 366 const E e = E.id0; |
| 367 '''); | 367 '''); |
| 368 LibraryElement libraryElement = resolve2(source); | 368 LibraryElement libraryElement = resolve2(source); |
| 369 CompilationUnit unit = | 369 CompilationUnit unit = |
| 370 analysisContext.resolveCompilationUnit(source, libraryElement); | 370 analysisContext.resolveCompilationUnit(source, libraryElement); |
| 371 expect(unit, isNotNull); | 371 expect(unit, isNotNull); |
| 372 ConstantValueComputer computer = _makeConstantValueComputer(); | 372 ConstantValueComputer computer = _makeConstantValueComputer(); |
| 373 computer.add(unit, source, source); | 373 computer.add(unit); |
| 374 computer.computeValues(); | 374 computer.computeValues(); |
| 375 TopLevelVariableDeclaration declaration = unit.declarations | 375 TopLevelVariableDeclaration declaration = unit.declarations |
| 376 .firstWhere((member) => member is TopLevelVariableDeclaration); | 376 .firstWhere((member) => member is TopLevelVariableDeclaration); |
| 377 _validate(true, declaration.variables); | 377 _validate(true, declaration.variables); |
| 378 } | 378 } |
| 379 | 379 |
| 380 void test_dependencyOnConstructor() { | 380 void test_dependencyOnConstructor() { |
| 381 // x depends on "const A()" | 381 // x depends on "const A()" |
| 382 _assertProperDependencies(r''' | 382 _assertProperDependencies(r''' |
| 383 class A { | 383 class A { |
| (...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1193 } | 1193 } |
| 1194 | 1194 |
| 1195 void _assertProperDependencies(String sourceText, | 1195 void _assertProperDependencies(String sourceText, |
| 1196 [List<ErrorCode> expectedErrorCodes = ErrorCode.EMPTY_LIST]) { | 1196 [List<ErrorCode> expectedErrorCodes = ErrorCode.EMPTY_LIST]) { |
| 1197 Source source = addSource(sourceText); | 1197 Source source = addSource(sourceText); |
| 1198 LibraryElement element = resolve2(source); | 1198 LibraryElement element = resolve2(source); |
| 1199 CompilationUnit unit = | 1199 CompilationUnit unit = |
| 1200 analysisContext.resolveCompilationUnit(source, element); | 1200 analysisContext.resolveCompilationUnit(source, element); |
| 1201 expect(unit, isNotNull); | 1201 expect(unit, isNotNull); |
| 1202 ConstantValueComputer computer = _makeConstantValueComputer(); | 1202 ConstantValueComputer computer = _makeConstantValueComputer(); |
| 1203 computer.add(unit, source, source); | 1203 computer.add(unit); |
| 1204 computer.computeValues(); | 1204 computer.computeValues(); |
| 1205 assertErrors(source, expectedErrorCodes); | 1205 assertErrors(source, expectedErrorCodes); |
| 1206 } | 1206 } |
| 1207 | 1207 |
| 1208 Map<String, DartObjectImpl> _assertType( | 1208 Map<String, DartObjectImpl> _assertType( |
| 1209 EvaluationResultImpl result, String typeName) { | 1209 EvaluationResultImpl result, String typeName) { |
| 1210 expect(result.value, isNotNull); | 1210 expect(result.value, isNotNull); |
| 1211 DartObjectImpl value = result.value; | 1211 DartObjectImpl value = result.value; |
| 1212 expect(value.type.displayName, typeName); | 1212 expect(value.type.displayName, typeName); |
| 1213 return value.fields; | 1213 return value.fields; |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1373 VariableDeclaration varDecl = | 1373 VariableDeclaration varDecl = |
| 1374 findTopLevelDeclaration(compilationUnit, name); | 1374 findTopLevelDeclaration(compilationUnit, name); |
| 1375 ConstTopLevelVariableElementImpl varElement = varDecl.element; | 1375 ConstTopLevelVariableElementImpl varElement = varDecl.element; |
| 1376 return varElement.evaluationResult; | 1376 return varElement.evaluationResult; |
| 1377 } | 1377 } |
| 1378 | 1378 |
| 1379 ConstantValueComputer _makeConstantValueComputer() { | 1379 ConstantValueComputer _makeConstantValueComputer() { |
| 1380 ConstantEvaluationValidator_ForTest validator = | 1380 ConstantEvaluationValidator_ForTest validator = |
| 1381 new ConstantEvaluationValidator_ForTest(analysisContext2); | 1381 new ConstantEvaluationValidator_ForTest(analysisContext2); |
| 1382 validator.computer = new ConstantValueComputer( | 1382 validator.computer = new ConstantValueComputer( |
| 1383 analysisContext2, | |
| 1384 analysisContext2.typeProvider, | 1383 analysisContext2.typeProvider, |
| 1385 analysisContext2.declaredVariables, | 1384 analysisContext2.declaredVariables, |
| 1386 validator, | 1385 validator, |
| 1387 analysisContext2.typeSystem); | 1386 analysisContext2.typeSystem); |
| 1388 return validator.computer; | 1387 return validator.computer; |
| 1389 } | 1388 } |
| 1390 | 1389 |
| 1391 void _validate(bool shouldBeValid, VariableDeclarationList declarationList) { | 1390 void _validate(bool shouldBeValid, VariableDeclarationList declarationList) { |
| 1392 for (VariableDeclaration declaration in declarationList.variables) { | 1391 for (VariableDeclaration declaration in declarationList.variables) { |
| 1393 VariableElementImpl element = declaration.element as VariableElementImpl; | 1392 VariableElementImpl element = declaration.element as VariableElementImpl; |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1594 ErrorReporter errorReporter = new ErrorReporter(errorListener, source); | 1593 ErrorReporter errorReporter = new ErrorReporter(errorListener, source); |
| 1595 DartObjectImpl result = expression.accept(new ConstantVisitor( | 1594 DartObjectImpl result = expression.accept(new ConstantVisitor( |
| 1596 new ConstantEvaluationEngine(typeProvider, new DeclaredVariables(), | 1595 new ConstantEvaluationEngine(typeProvider, new DeclaredVariables(), |
| 1597 typeSystem: typeSystem), | 1596 typeSystem: typeSystem), |
| 1598 errorReporter, | 1597 errorReporter, |
| 1599 lexicalEnvironment: lexicalEnvironment)); | 1598 lexicalEnvironment: lexicalEnvironment)); |
| 1600 errorListener.assertNoErrors(); | 1599 errorListener.assertNoErrors(); |
| 1601 return result; | 1600 return result; |
| 1602 } | 1601 } |
| 1603 } | 1602 } |
| OLD | NEW |