Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Unified Diff: pkg/analyzer/test/generated/all_the_rest_test.dart

Issue 1121313004: Move validation logic for constant evaluation into its own class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/test/generated/all_the_rest_test.dart
diff --git a/pkg/analyzer/test/generated/all_the_rest_test.dart b/pkg/analyzer/test/generated/all_the_rest_test.dart
index e16bff6bcaef5012f81762d55a1c375fd35f0296..a50be78850ec01c914bf56bb621c71fbad3f1603 100644
--- a/pkg/analyzer/test/generated/all_the_rest_test.dart
+++ b/pkg/analyzer/test/generated/all_the_rest_test.dart
@@ -466,6 +466,84 @@ abstract class AbstractScannerTest {
}
}
+/**
+ * Implementation of [ConstantEvaluationValidator] used during unit tests;
+ * verifies that any nodes referenced during constant evaluation are present in
+ * the dependency graph.
+ */
+class ConstantEvaluationValidator_ForTest
+ implements ConstantEvaluationValidator {
+ ConstantValueComputer computer;
+
+ AstNode _nodeBeingEvaluated;
+
+ @override
+ void beforeComputeValue(AstNode constNode) {
+ _nodeBeingEvaluated = constNode;
+ }
+
+ @override
+ void beforeGetConstantInitializers(ConstructorElement constructor) {
+ // If we are getting the constant initializers for a node in the graph,
+ // make sure we properly recorded the dependency.
+ ConstructorDeclaration node =
+ computer.findConstructorDeclaration(constructor);
+ if (node != null && computer.referenceGraph.nodes.contains(node)) {
+ expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, node),
+ isTrue);
+ }
+ }
+
+ @override
+ void beforeGetEvaluationResult(AstNode node) {
+ // If we are getting the evaluation result for a node in the graph,
+ // make sure we properly recorded the dependency.
+ if (computer.referenceGraph.nodes.contains(node)) {
+ expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, node),
+ isTrue);
+ }
+ }
+
+ @override
+ void beforeGetFieldEvaluationResult(FieldElementImpl field) {
+ // If we are getting the constant value for a node in the graph, make sure
+ // we properly recorded the dependency.
+ VariableDeclaration node = computer.findVariableDeclaration(field);
+ if (node != null && computer.referenceGraph.nodes.contains(node)) {
+ expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, node),
+ isTrue);
+ }
+ }
+
+ @override
+ void beforeGetParameterDefault(ParameterElement parameter) {
+ // Find the ConstructorElement and figure out which
+ // parameter we're talking about.
+ ConstructorElement constructor =
+ parameter.getAncestor((element) => element is ConstructorElement);
+ int parameterIndex;
+ List<ParameterElement> parameters = constructor.parameters;
+ int numParameters = parameters.length;
+ for (parameterIndex = 0; parameterIndex < numParameters; parameterIndex++) {
+ if (identical(parameters[parameterIndex], parameter)) {
+ break;
+ }
+ }
+ expect(parameterIndex < numParameters, isTrue);
+ // If we are getting the default parameter for a constructor in the graph,
+ // make sure we properly recorded the dependency on the parameter.
+ ConstructorDeclaration constructorNode =
+ computer.constructorDeclarationMap[constructor];
+ if (constructorNode != null) {
+ FormalParameter parameterNode =
+ constructorNode.parameters.parameters[parameterIndex];
+ expect(computer.referenceGraph.nodes.contains(parameterNode), isTrue);
+ expect(computer.referenceGraph.containsPath(
+ _nodeBeingEvaluated, parameterNode), isTrue);
+ }
+ }
+}
+
@reflectiveTest
class ConstantEvaluatorTest extends ResolverTestCase {
void fail_constructor() {
@@ -1433,28 +1511,28 @@ class B extends A {
const B b = const B();''');
}
- void test_dependencyOnInitializedNonStaticConst() {
- // Even though non-static consts are not allowed by the language, we need
- // to handle them for error recovery purposes.
+ void test_dependencyOnInitializedFinal() {
// a depends on A() depends on A.x
_assertProperDependencies('''
class A {
const A();
- const int x = 1;
+ final int x = 1;
}
const A a = const A();
-''', [CompileTimeErrorCode.CONST_INSTANCE_FIELD]);
+''');
}
- void test_dependencyOnInitializedFinal() {
+ void test_dependencyOnInitializedNonStaticConst() {
+ // Even though non-static consts are not allowed by the language, we need
+ // to handle them for error recovery purposes.
// a depends on A() depends on A.x
_assertProperDependencies('''
class A {
const A();
- final int x = 1;
+ const int x = 1;
}
const A a = const A();
-''');
+''', [CompileTimeErrorCode.CONST_INSTANCE_FIELD]);
}
void test_dependencyOnNonFactoryRedirect() {
@@ -1558,24 +1636,6 @@ const A a = const A();
_assertIntField(fields, "i", 123);
}
- void test_non_static_const_initialized_at_declaration() {
- // Even though non-static consts are not allowed by the language, we need
- // to handle them for error recovery purposes.
- CompilationUnit compilationUnit = resolveSource('''
-class A {
- const int i = 123;
- const A();
-}
-
-const A a = const A();
-''');
- EvaluationResultImpl result =
- _evaluateInstanceCreationExpression(compilationUnit, 'a');
- Map<String, DartObjectImpl> fields = _assertType(result, "A");
- expect(fields, hasLength(1));
- _assertIntField(fields, "i", 123);
- }
-
void test_fromEnvironment_bool_default_false() {
expect(_assertValidBool(_check_fromEnvironment_bool(null, "false")), false);
}
@@ -2077,6 +2137,24 @@ const c_num = const C<num>();''');
expect(ConstantValueComputer.isValidPublicSymbol("foo.void"), isFalse);
}
+ void test_non_static_const_initialized_at_declaration() {
+ // Even though non-static consts are not allowed by the language, we need
+ // to handle them for error recovery purposes.
+ CompilationUnit compilationUnit = resolveSource('''
+class A {
+ const int i = 123;
+ const A();
+}
+
+const A a = const A();
+''');
+ EvaluationResultImpl result =
+ _evaluateInstanceCreationExpression(compilationUnit, 'a');
+ Map<String, DartObjectImpl> fields = _assertType(result, "A");
+ expect(fields, hasLength(1));
+ _assertIntField(fields, "i", 123);
+ }
+
void test_symbolLiteral_void() {
CompilationUnit compilationUnit =
resolveSource("const voidSymbol = #void;");
@@ -2294,8 +2372,12 @@ class A {
}
ConstantValueComputer _makeConstantValueComputer() {
- return new ValidatingConstantValueComputer(
- analysisContext2.typeProvider, analysisContext2.declaredVariables);
+ ConstantEvaluationValidator_ForTest validator =
+ new ConstantEvaluationValidator_ForTest();
+ validator.computer = new ConstantValueComputer(
+ analysisContext2.typeProvider, analysisContext2.declaredVariables,
+ validator);
+ return validator.computer;
}
void _validate(bool shouldBeValid, VariableDeclarationList declarationList) {
@@ -2312,27 +2394,6 @@ class A {
}
}
-class ConstantValueComputerTest_ValidatingConstantVisitor
- extends ConstantVisitor {
- final DirectedGraph<AstNode> _referenceGraph;
- final AstNode _nodeBeingEvaluated;
-
- ConstantValueComputerTest_ValidatingConstantVisitor(TypeProvider typeProvider,
- this._referenceGraph, this._nodeBeingEvaluated,
- ErrorReporter errorReporter)
- : super.con1(typeProvider, errorReporter);
-
- @override
- void beforeGetEvaluationResult(AstNode node) {
- super.beforeGetEvaluationResult(node);
- // If we are getting the evaluation result for a node in the graph,
- // make sure we properly recorded the dependency.
- if (_referenceGraph.nodes.contains(node)) {
- expect(_referenceGraph.containsPath(_nodeBeingEvaluated, node), isTrue);
- }
- }
-}
-
@reflectiveTest
class ConstantVisitorTest extends ResolverTestCase {
void test_visitConditionalExpression_false() {
@@ -2343,8 +2404,8 @@ class ConstantVisitorTest extends ResolverTestCase {
GatheringErrorListener errorListener = new GatheringErrorListener();
ErrorReporter errorReporter =
new ErrorReporter(errorListener, _dummySource());
- _assertValue(0, expression.accept(
- new ConstantVisitor.con1(new TestTypeProvider(), errorReporter)));
+ _assertValue(0, expression
+ .accept(new ConstantVisitor(new TestTypeProvider(), errorReporter)));
errorListener.assertNoErrors();
}
@@ -2366,7 +2427,7 @@ class ConstantVisitorTest extends ResolverTestCase {
GatheringErrorListener errorListener = new GatheringErrorListener();
ErrorReporter errorReporter =
new ErrorReporter(errorListener, _dummySource());
- expression.accept(new ConstantVisitor.con1(typeProvider, errorReporter));
+ expression.accept(new ConstantVisitor(typeProvider, errorReporter));
errorListener
.assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]);
}
@@ -2380,8 +2441,8 @@ class ConstantVisitorTest extends ResolverTestCase {
GatheringErrorListener errorListener = new GatheringErrorListener();
ErrorReporter errorReporter =
new ErrorReporter(errorListener, _dummySource());
- DartObjectImpl result = expression.accept(
- new ConstantVisitor.con1(new TestTypeProvider(), errorReporter));
+ DartObjectImpl result = expression
+ .accept(new ConstantVisitor(new TestTypeProvider(), errorReporter));
expect(result, isNull);
errorListener
.assertErrorsWithCodes([CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL]);
@@ -2395,8 +2456,8 @@ class ConstantVisitorTest extends ResolverTestCase {
GatheringErrorListener errorListener = new GatheringErrorListener();
ErrorReporter errorReporter =
new ErrorReporter(errorListener, _dummySource());
- DartObjectImpl result = expression.accept(
- new ConstantVisitor.con1(new TestTypeProvider(), errorReporter));
+ DartObjectImpl result = expression
+ .accept(new ConstantVisitor(new TestTypeProvider(), errorReporter));
expect(result, isNull);
errorListener
.assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]);
@@ -2410,8 +2471,8 @@ class ConstantVisitorTest extends ResolverTestCase {
GatheringErrorListener errorListener = new GatheringErrorListener();
ErrorReporter errorReporter =
new ErrorReporter(errorListener, _dummySource());
- DartObjectImpl result = expression.accept(
- new ConstantVisitor.con1(new TestTypeProvider(), errorReporter));
+ DartObjectImpl result = expression
+ .accept(new ConstantVisitor(new TestTypeProvider(), errorReporter));
expect(result, isNull);
errorListener
.assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]);
@@ -2425,8 +2486,8 @@ class ConstantVisitorTest extends ResolverTestCase {
GatheringErrorListener errorListener = new GatheringErrorListener();
ErrorReporter errorReporter =
new ErrorReporter(errorListener, _dummySource());
- _assertValue(1, expression.accept(
- new ConstantVisitor.con1(new TestTypeProvider(), errorReporter)));
+ _assertValue(1, expression
+ .accept(new ConstantVisitor(new TestTypeProvider(), errorReporter)));
errorListener.assertNoErrors();
}
@@ -2496,8 +2557,8 @@ const b = 3;''');
findTopLevelConstantExpression(compilationUnit, name);
GatheringErrorListener errorListener = new GatheringErrorListener();
ErrorReporter errorReporter = new ErrorReporter(errorListener, source);
- DartObjectImpl result = expression.accept(new ConstantVisitor.con2(
- typeProvider, lexicalEnvironment, errorReporter));
+ DartObjectImpl result = expression.accept(new ConstantVisitor(
+ typeProvider, errorReporter, null, lexicalEnvironment));
errorListener.assertNoErrors();
return result;
}
@@ -8448,76 +8509,6 @@ class UriResolver_SourceFactoryTest_test_fromEncoding_valid
}
}
-class ValidatingConstantValueComputer extends ConstantValueComputer {
- AstNode _nodeBeingEvaluated;
- ValidatingConstantValueComputer(
- TypeProvider typeProvider, DeclaredVariables declaredVariables)
- : super(typeProvider, declaredVariables);
-
- @override
- void beforeComputeValue(AstNode constNode) {
- super.beforeComputeValue(constNode);
- _nodeBeingEvaluated = constNode;
- }
-
- @override
- void beforeGetFieldEvaluationResult(FieldElementImpl field) {
- super.beforeGetFieldEvaluationResult(field);
- // If we are getting the constant value for a node in the graph, make sure
- // we properly recorded the dependency.
- VariableDeclaration node = findVariableDeclaration(field);
- if (node != null && referenceGraph.nodes.contains(node)) {
- expect(referenceGraph.containsPath(_nodeBeingEvaluated, node), isTrue);
- }
- }
-
- @override
- void beforeGetConstantInitializers(ConstructorElement constructor) {
- super.beforeGetConstantInitializers(constructor);
- // If we are getting the constant initializers for a node in the graph,
- // make sure we properly recorded the dependency.
- ConstructorDeclaration node = findConstructorDeclaration(constructor);
- if (node != null && referenceGraph.nodes.contains(node)) {
- expect(referenceGraph.containsPath(_nodeBeingEvaluated, node), isTrue);
- }
- }
-
- @override
- void beforeGetParameterDefault(ParameterElement parameter) {
- super.beforeGetParameterDefault(parameter);
- // Find the ConstructorElement and figure out which
- // parameter we're talking about.
- ConstructorElement constructor =
- parameter.getAncestor((element) => element is ConstructorElement);
- int parameterIndex;
- List<ParameterElement> parameters = constructor.parameters;
- int numParameters = parameters.length;
- for (parameterIndex = 0; parameterIndex < numParameters; parameterIndex++) {
- if (identical(parameters[parameterIndex], parameter)) {
- break;
- }
- }
- expect(parameterIndex < numParameters, isTrue);
- // If we are getting the default parameter for a constructor in the graph,
- // make sure we properly recorded the dependency on the parameter.
- ConstructorDeclaration constructorNode =
- constructorDeclarationMap[constructor];
- if (constructorNode != null) {
- FormalParameter parameterNode =
- constructorNode.parameters.parameters[parameterIndex];
- expect(referenceGraph.nodes.contains(parameterNode), isTrue);
- expect(referenceGraph.containsPath(_nodeBeingEvaluated, parameterNode),
- isTrue);
- }
- }
-
- @override
- ConstantVisitor createConstantVisitor(ErrorReporter errorReporter) {
- return new ConstantValueComputerTest_ValidatingConstantVisitor(
- typeProvider, referenceGraph, _nodeBeingEvaluated, errorReporter);
- }
-}
-
/**
* Instances of `XmlValidator` traverse an [XmlNode] structure and validate the node
* hierarchy.
« pkg/analyzer/lib/src/generated/constant.dart ('K') | « pkg/analyzer/lib/src/generated/resolver.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698