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