| 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.all_the_rest_test; | 5 library engine.all_the_rest_test; |
| 6 | 6 |
| 7 import 'package:analyzer/file_system/physical_file_system.dart'; | 7 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 8 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
| 9 import 'package:analyzer/src/generated/constant.dart'; | 9 import 'package:analyzer/src/generated/constant.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| (...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 861 analysisContext.resolveCompilationUnit(source, library); | 861 analysisContext.resolveCompilationUnit(source, library); |
| 862 expect(unit, isNotNull); | 862 expect(unit, isNotNull); |
| 863 NodeList<CompilationUnitMember> declarations = unit.declarations; | 863 NodeList<CompilationUnitMember> declarations = unit.declarations; |
| 864 expect(declarations, hasLength(1)); | 864 expect(declarations, hasLength(1)); |
| 865 CompilationUnitMember declaration = declarations[0]; | 865 CompilationUnitMember declaration = declarations[0]; |
| 866 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableDeclaration, | 866 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableDeclaration, |
| 867 TopLevelVariableDeclaration, declaration); | 867 TopLevelVariableDeclaration, declaration); |
| 868 NodeList<VariableDeclaration> variables = | 868 NodeList<VariableDeclaration> variables = |
| 869 (declaration as TopLevelVariableDeclaration).variables.variables; | 869 (declaration as TopLevelVariableDeclaration).variables.variables; |
| 870 expect(variables, hasLength(1)); | 870 expect(variables, hasLength(1)); |
| 871 ConstantEvaluator evaluator = | 871 ConstantEvaluator evaluator = new ConstantEvaluator( |
| 872 new ConstantEvaluator(source, analysisContext.typeProvider); | 872 source, analysisContext.typeProvider, analysisContext.typeSystem); |
| 873 return evaluator.evaluate(variables[0].initializer); | 873 return evaluator.evaluate(variables[0].initializer); |
| 874 } | 874 } |
| 875 } | 875 } |
| 876 | 876 |
| 877 @reflectiveTest | 877 @reflectiveTest |
| 878 class ConstantFinderTest extends EngineTestCase { | 878 class ConstantFinderTest extends EngineTestCase { |
| 879 AstNode _node; | 879 AstNode _node; |
| 880 TypeProvider _typeProvider; | 880 TypeProvider _typeProvider; |
| 881 AnalysisContext _context; | 881 AnalysisContext _context; |
| 882 Source _source; | 882 Source _source; |
| (...skipping 1471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2354 ConstTopLevelVariableElementImpl varElement = varDecl.element; | 2354 ConstTopLevelVariableElementImpl varElement = varDecl.element; |
| 2355 return varElement.evaluationResult; | 2355 return varElement.evaluationResult; |
| 2356 } | 2356 } |
| 2357 | 2357 |
| 2358 ConstantValueComputer _makeConstantValueComputer() { | 2358 ConstantValueComputer _makeConstantValueComputer() { |
| 2359 ConstantEvaluationValidator_ForTest validator = | 2359 ConstantEvaluationValidator_ForTest validator = |
| 2360 new ConstantEvaluationValidator_ForTest(); | 2360 new ConstantEvaluationValidator_ForTest(); |
| 2361 validator.computer = new ConstantValueComputer( | 2361 validator.computer = new ConstantValueComputer( |
| 2362 analysisContext2, | 2362 analysisContext2, |
| 2363 analysisContext2.typeProvider, | 2363 analysisContext2.typeProvider, |
| 2364 analysisContext2.typeSystem, |
| 2364 analysisContext2.declaredVariables, | 2365 analysisContext2.declaredVariables, |
| 2365 validator); | 2366 validator); |
| 2366 return validator.computer; | 2367 return validator.computer; |
| 2367 } | 2368 } |
| 2368 | 2369 |
| 2369 void _validate(bool shouldBeValid, VariableDeclarationList declarationList) { | 2370 void _validate(bool shouldBeValid, VariableDeclarationList declarationList) { |
| 2370 for (VariableDeclaration declaration in declarationList.variables) { | 2371 for (VariableDeclaration declaration in declarationList.variables) { |
| 2371 VariableElementImpl element = declaration.element as VariableElementImpl; | 2372 VariableElementImpl element = declaration.element as VariableElementImpl; |
| 2372 expect(element, isNotNull); | 2373 expect(element, isNotNull); |
| 2373 EvaluationResultImpl result = element.evaluationResult; | 2374 EvaluationResultImpl result = element.evaluationResult; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 2386 Expression thenExpression = AstFactory.integer(1); | 2387 Expression thenExpression = AstFactory.integer(1); |
| 2387 Expression elseExpression = AstFactory.integer(0); | 2388 Expression elseExpression = AstFactory.integer(0); |
| 2388 ConditionalExpression expression = AstFactory.conditionalExpression( | 2389 ConditionalExpression expression = AstFactory.conditionalExpression( |
| 2389 AstFactory.booleanLiteral(false), thenExpression, elseExpression); | 2390 AstFactory.booleanLiteral(false), thenExpression, elseExpression); |
| 2390 GatheringErrorListener errorListener = new GatheringErrorListener(); | 2391 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 2391 ErrorReporter errorReporter = | 2392 ErrorReporter errorReporter = |
| 2392 new ErrorReporter(errorListener, _dummySource()); | 2393 new ErrorReporter(errorListener, _dummySource()); |
| 2393 _assertValue( | 2394 _assertValue( |
| 2394 0, | 2395 0, |
| 2395 expression.accept(new ConstantVisitor( | 2396 expression.accept(new ConstantVisitor( |
| 2396 new ConstantEvaluationEngine( | 2397 new ConstantEvaluationEngine(new TestTypeProvider(), |
| 2397 new TestTypeProvider(), new DeclaredVariables()), | 2398 new TypeSystemImpl(), new DeclaredVariables()), |
| 2398 errorReporter))); | 2399 errorReporter))); |
| 2399 errorListener.assertNoErrors(); | 2400 errorListener.assertNoErrors(); |
| 2400 } | 2401 } |
| 2401 | 2402 |
| 2402 void test_visitConditionalExpression_nonBooleanCondition() { | 2403 void test_visitConditionalExpression_nonBooleanCondition() { |
| 2403 Expression thenExpression = AstFactory.integer(1); | 2404 Expression thenExpression = AstFactory.integer(1); |
| 2404 Expression elseExpression = AstFactory.integer(0); | 2405 Expression elseExpression = AstFactory.integer(0); |
| 2405 NullLiteral conditionExpression = AstFactory.nullLiteral(); | 2406 NullLiteral conditionExpression = AstFactory.nullLiteral(); |
| 2406 ConditionalExpression expression = AstFactory.conditionalExpression( | 2407 ConditionalExpression expression = AstFactory.conditionalExpression( |
| 2407 conditionExpression, thenExpression, elseExpression); | 2408 conditionExpression, thenExpression, elseExpression); |
| 2408 GatheringErrorListener errorListener = new GatheringErrorListener(); | 2409 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 2409 ErrorReporter errorReporter = | 2410 ErrorReporter errorReporter = |
| 2410 new ErrorReporter(errorListener, _dummySource()); | 2411 new ErrorReporter(errorListener, _dummySource()); |
| 2411 DartObjectImpl result = expression.accept(new ConstantVisitor( | 2412 DartObjectImpl result = expression.accept(new ConstantVisitor( |
| 2412 new ConstantEvaluationEngine( | 2413 new ConstantEvaluationEngine(new TestTypeProvider(), |
| 2413 new TestTypeProvider(), new DeclaredVariables()), | 2414 new TypeSystemImpl(), new DeclaredVariables()), |
| 2414 errorReporter)); | 2415 errorReporter)); |
| 2415 expect(result, isNull); | 2416 expect(result, isNull); |
| 2416 errorListener | 2417 errorListener |
| 2417 .assertErrorsWithCodes([CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL]); | 2418 .assertErrorsWithCodes([CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL]); |
| 2418 } | 2419 } |
| 2419 | 2420 |
| 2420 void test_visitConditionalExpression_nonConstantElse() { | 2421 void test_visitConditionalExpression_nonConstantElse() { |
| 2421 Expression thenExpression = AstFactory.integer(1); | 2422 Expression thenExpression = AstFactory.integer(1); |
| 2422 Expression elseExpression = AstFactory.identifier3("x"); | 2423 Expression elseExpression = AstFactory.identifier3("x"); |
| 2423 ConditionalExpression expression = AstFactory.conditionalExpression( | 2424 ConditionalExpression expression = AstFactory.conditionalExpression( |
| 2424 AstFactory.booleanLiteral(true), thenExpression, elseExpression); | 2425 AstFactory.booleanLiteral(true), thenExpression, elseExpression); |
| 2425 GatheringErrorListener errorListener = new GatheringErrorListener(); | 2426 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 2426 ErrorReporter errorReporter = | 2427 ErrorReporter errorReporter = |
| 2427 new ErrorReporter(errorListener, _dummySource()); | 2428 new ErrorReporter(errorListener, _dummySource()); |
| 2428 DartObjectImpl result = expression.accept(new ConstantVisitor( | 2429 DartObjectImpl result = expression.accept(new ConstantVisitor( |
| 2429 new ConstantEvaluationEngine( | 2430 new ConstantEvaluationEngine(new TestTypeProvider(), |
| 2430 new TestTypeProvider(), new DeclaredVariables()), | 2431 new TypeSystemImpl(), new DeclaredVariables()), |
| 2431 errorReporter)); | 2432 errorReporter)); |
| 2432 expect(result, isNull); | 2433 expect(result, isNull); |
| 2433 errorListener | 2434 errorListener |
| 2434 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]); | 2435 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]); |
| 2435 } | 2436 } |
| 2436 | 2437 |
| 2437 void test_visitConditionalExpression_nonConstantThen() { | 2438 void test_visitConditionalExpression_nonConstantThen() { |
| 2438 Expression thenExpression = AstFactory.identifier3("x"); | 2439 Expression thenExpression = AstFactory.identifier3("x"); |
| 2439 Expression elseExpression = AstFactory.integer(0); | 2440 Expression elseExpression = AstFactory.integer(0); |
| 2440 ConditionalExpression expression = AstFactory.conditionalExpression( | 2441 ConditionalExpression expression = AstFactory.conditionalExpression( |
| 2441 AstFactory.booleanLiteral(true), thenExpression, elseExpression); | 2442 AstFactory.booleanLiteral(true), thenExpression, elseExpression); |
| 2442 GatheringErrorListener errorListener = new GatheringErrorListener(); | 2443 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 2443 ErrorReporter errorReporter = | 2444 ErrorReporter errorReporter = |
| 2444 new ErrorReporter(errorListener, _dummySource()); | 2445 new ErrorReporter(errorListener, _dummySource()); |
| 2445 DartObjectImpl result = expression.accept(new ConstantVisitor( | 2446 DartObjectImpl result = expression.accept(new ConstantVisitor( |
| 2446 new ConstantEvaluationEngine( | 2447 new ConstantEvaluationEngine(new TestTypeProvider(), |
| 2447 new TestTypeProvider(), new DeclaredVariables()), | 2448 new TypeSystemImpl(), new DeclaredVariables()), |
| 2448 errorReporter)); | 2449 errorReporter)); |
| 2449 expect(result, isNull); | 2450 expect(result, isNull); |
| 2450 errorListener | 2451 errorListener |
| 2451 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]); | 2452 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]); |
| 2452 } | 2453 } |
| 2453 | 2454 |
| 2454 void test_visitConditionalExpression_true() { | 2455 void test_visitConditionalExpression_true() { |
| 2455 Expression thenExpression = AstFactory.integer(1); | 2456 Expression thenExpression = AstFactory.integer(1); |
| 2456 Expression elseExpression = AstFactory.integer(0); | 2457 Expression elseExpression = AstFactory.integer(0); |
| 2457 ConditionalExpression expression = AstFactory.conditionalExpression( | 2458 ConditionalExpression expression = AstFactory.conditionalExpression( |
| 2458 AstFactory.booleanLiteral(true), thenExpression, elseExpression); | 2459 AstFactory.booleanLiteral(true), thenExpression, elseExpression); |
| 2459 GatheringErrorListener errorListener = new GatheringErrorListener(); | 2460 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 2460 ErrorReporter errorReporter = | 2461 ErrorReporter errorReporter = |
| 2461 new ErrorReporter(errorListener, _dummySource()); | 2462 new ErrorReporter(errorListener, _dummySource()); |
| 2462 _assertValue( | 2463 _assertValue( |
| 2463 1, | 2464 1, |
| 2464 expression.accept(new ConstantVisitor( | 2465 expression.accept(new ConstantVisitor( |
| 2465 new ConstantEvaluationEngine( | 2466 new ConstantEvaluationEngine(new TestTypeProvider(), |
| 2466 new TestTypeProvider(), new DeclaredVariables()), | 2467 new TypeSystemImpl(), new DeclaredVariables()), |
| 2467 errorReporter))); | 2468 errorReporter))); |
| 2468 errorListener.assertNoErrors(); | 2469 errorListener.assertNoErrors(); |
| 2469 } | 2470 } |
| 2470 | 2471 |
| 2471 void test_visitSimpleIdentifier_className() { | 2472 void test_visitSimpleIdentifier_className() { |
| 2472 CompilationUnit compilationUnit = resolveSource(''' | 2473 CompilationUnit compilationUnit = resolveSource(''' |
| 2473 const a = C; | 2474 const a = C; |
| 2474 class C {} | 2475 class C {} |
| 2475 '''); | 2476 '''); |
| 2476 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null); | 2477 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2529 } | 2530 } |
| 2530 | 2531 |
| 2531 DartObjectImpl _evaluateConstant(CompilationUnit compilationUnit, String name, | 2532 DartObjectImpl _evaluateConstant(CompilationUnit compilationUnit, String name, |
| 2532 Map<String, DartObjectImpl> lexicalEnvironment) { | 2533 Map<String, DartObjectImpl> lexicalEnvironment) { |
| 2533 Source source = compilationUnit.element.source; | 2534 Source source = compilationUnit.element.source; |
| 2534 Expression expression = | 2535 Expression expression = |
| 2535 findTopLevelConstantExpression(compilationUnit, name); | 2536 findTopLevelConstantExpression(compilationUnit, name); |
| 2536 GatheringErrorListener errorListener = new GatheringErrorListener(); | 2537 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 2537 ErrorReporter errorReporter = new ErrorReporter(errorListener, source); | 2538 ErrorReporter errorReporter = new ErrorReporter(errorListener, source); |
| 2538 DartObjectImpl result = expression.accept(new ConstantVisitor( | 2539 DartObjectImpl result = expression.accept(new ConstantVisitor( |
| 2539 new ConstantEvaluationEngine(typeProvider, new DeclaredVariables()), | 2540 new ConstantEvaluationEngine( |
| 2541 typeProvider, typeSystem, new DeclaredVariables()), |
| 2540 errorReporter, | 2542 errorReporter, |
| 2541 lexicalEnvironment: lexicalEnvironment)); | 2543 lexicalEnvironment: lexicalEnvironment)); |
| 2542 errorListener.assertNoErrors(); | 2544 errorListener.assertNoErrors(); |
| 2543 return result; | 2545 return result; |
| 2544 } | 2546 } |
| 2545 } | 2547 } |
| 2546 | 2548 |
| 2547 @reflectiveTest | 2549 @reflectiveTest |
| 2548 class ContentCacheTest { | 2550 class ContentCacheTest { |
| 2549 void test_setContents() { | 2551 void test_setContents() { |
| (...skipping 6748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9298 if (_expectedExternalScriptName == null) { | 9300 if (_expectedExternalScriptName == null) { |
| 9299 expect(scriptSource, isNull, reason: "script $scriptIndex"); | 9301 expect(scriptSource, isNull, reason: "script $scriptIndex"); |
| 9300 } else { | 9302 } else { |
| 9301 expect(scriptSource, isNotNull, reason: "script $scriptIndex"); | 9303 expect(scriptSource, isNotNull, reason: "script $scriptIndex"); |
| 9302 String actualExternalScriptName = scriptSource.shortName; | 9304 String actualExternalScriptName = scriptSource.shortName; |
| 9303 expect(actualExternalScriptName, _expectedExternalScriptName, | 9305 expect(actualExternalScriptName, _expectedExternalScriptName, |
| 9304 reason: "script $scriptIndex"); | 9306 reason: "script $scriptIndex"); |
| 9305 } | 9307 } |
| 9306 } | 9308 } |
| 9307 } | 9309 } |
| OLD | NEW |