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

Unified Diff: editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ReferenceFinderTest.java

Issue 270813004: Evaluate constant instance creation expressions in ConstantValueComputer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 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: editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ReferenceFinderTest.java
diff --git a/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ReferenceFinderTest.java b/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ReferenceFinderTest.java
index a3b82c12d79b69524bb87bfb9030026bbd8631f5..1f3ab031f843a72cd8180ea3439bb0ee756b5ba9 100644
--- a/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ReferenceFinderTest.java
+++ b/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ReferenceFinderTest.java
@@ -15,69 +15,154 @@ package com.google.dart.engine.internal.constant;
import com.google.dart.engine.EngineTestCase;
import com.google.dart.engine.ast.AstNode;
+import com.google.dart.engine.ast.ConstructorDeclaration;
+import com.google.dart.engine.ast.ConstructorInitializer;
+import com.google.dart.engine.ast.InstanceCreationExpression;
import com.google.dart.engine.ast.SimpleIdentifier;
+import com.google.dart.engine.ast.TypeName;
import com.google.dart.engine.ast.VariableDeclaration;
+import com.google.dart.engine.element.ConstructorElement;
import com.google.dart.engine.element.VariableElement;
+import com.google.dart.engine.internal.element.ClassElementImpl;
+import com.google.dart.engine.internal.element.ConstructorElementImpl;
import com.google.dart.engine.internal.element.VariableElementImpl;
import com.google.dart.engine.scanner.Keyword;
+import com.google.dart.engine.scanner.KeywordToken;
import com.google.dart.engine.utilities.collection.DirectedGraph;
+import static com.google.dart.engine.ast.AstFactory.constructorDeclaration;
+import static com.google.dart.engine.ast.AstFactory.formalParameterList;
import static com.google.dart.engine.ast.AstFactory.identifier;
+import static com.google.dart.engine.ast.AstFactory.instanceCreationExpression;
+import static com.google.dart.engine.ast.AstFactory.typeName;
import static com.google.dart.engine.ast.AstFactory.variableDeclaration;
import static com.google.dart.engine.ast.AstFactory.variableDeclarationList;
+import static com.google.dart.engine.element.ElementFactory.classElement;
+import static com.google.dart.engine.element.ElementFactory.constructorElement;
import static com.google.dart.engine.element.ElementFactory.localVariableElement;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class ReferenceFinderTest extends EngineTestCase {
+ private DirectedGraph<AstNode> referenceGraph;
+ private HashMap<VariableElement, VariableDeclaration> variableDeclarationMap;
+ private HashMap<ConstructorElement, ConstructorDeclaration> constructorDeclarationMap;
+ private VariableDeclaration head;
+ private AstNode tail;
+
+ public void test_visitInstanceCreationExpression_const() {
+ visitNode(makeTailConstructor("A", true, true, true));
+ assertOneArc(tail);
+ }
+
+ public void test_visitInstanceCreationExpression_nonConstDeclaration() {
+ // In the source:
+ // const x = const A();
+ // x depends on "const A()" even if the A constructor isn't declared as const.
+ visitNode(makeTailConstructor("A", false, true, true));
+ assertOneArc(tail);
+ }
+
+ public void test_visitInstanceCreationExpression_nonConstUsage() {
+ visitNode(makeTailConstructor("A", true, false, true));
+ assertNoArcs();
+ }
+
+ public void test_visitInstanceCreationExpression_notInMap() {
+ // In the source:
+ // const x = const A();
+ // x depends on "const A()" even if the AST for the A constructor isn't available.
+ visitNode(makeTailConstructor("A", true, true, false));
+ assertOneArc(tail);
+ }
+
public void test_visitSimpleIdentifier_const() {
- VariableDeclaration head = variableDeclaration("v1");
- VariableDeclaration tail = variableDeclaration("v2");
- VariableElementImpl tailElement = localVariableElement("v2");
- tailElement.setConst(true);
- variableDeclarationList(Keyword.CONST, head, tail);
- DirectedGraph<AstNode> referenceGraph = new DirectedGraph<AstNode>();
- HashMap<VariableElement, VariableDeclaration> declarationMap = new HashMap<VariableElement, VariableDeclaration>();
- declarationMap.put(tailElement, tail);
- ReferenceFinder finder = new ReferenceFinder(head, referenceGraph, declarationMap);
- SimpleIdentifier identifier = identifier("v2");
- identifier.setStaticElement(tailElement);
- identifier.accept(finder);
- Set<AstNode> tails = referenceGraph.getTails(head);
- assertSizeOfSet(1, tails);
- assertSame(tail, tails.iterator().next());
+ visitNode(makeTailVariable("v2", true, true));
+ assertOneArc(tail);
}
public void test_visitSimpleIdentifier_nonConst() {
- VariableDeclaration head = variableDeclaration("v1");
- VariableDeclaration tail = variableDeclaration("v2");
- VariableElementImpl tailElement = localVariableElement("v2");
- tailElement.setConst(false);
- variableDeclarationList(Keyword.VAR, head, tail);
- DirectedGraph<AstNode> referenceGraph = new DirectedGraph<AstNode>();
- HashMap<VariableElement, VariableDeclaration> declarationMap = new HashMap<VariableElement, VariableDeclaration>();
- ReferenceFinder finder = new ReferenceFinder(head, referenceGraph, declarationMap);
- SimpleIdentifier identifier = identifier("v2");
- identifier.setStaticElement(tailElement);
- identifier.accept(finder);
- Set<AstNode> tails = referenceGraph.getTails(head);
- assertSizeOfSet(0, tails);
+ visitNode(makeTailVariable("v2", false, true));
+ assertNoArcs();
}
public void test_visitSimpleIdentifier_notInMap() {
- VariableDeclaration head = variableDeclaration("v1");
- VariableDeclaration tail = variableDeclaration("v2");
- VariableElementImpl tailElement = localVariableElement("v2");
- tailElement.setConst(true);
- variableDeclarationList(Keyword.CONST, head, tail);
- DirectedGraph<AstNode> referenceGraph = new DirectedGraph<AstNode>();
- HashMap<VariableElement, VariableDeclaration> declarationMap = new HashMap<VariableElement, VariableDeclaration>();
- ReferenceFinder finder = new ReferenceFinder(head, referenceGraph, declarationMap);
- SimpleIdentifier identifier = identifier("v2");
- identifier.setStaticElement(tailElement);
- identifier.accept(finder);
+ visitNode(makeTailVariable("v2", true, false));
+ assertNoArcs();
+ }
+
+ @Override
+ protected void setUp() {
+ referenceGraph = new DirectedGraph<AstNode>();
+ variableDeclarationMap = new HashMap<VariableElement, VariableDeclaration>();
+ constructorDeclarationMap = new HashMap<ConstructorElement, ConstructorDeclaration>();
+ head = variableDeclaration("v1");
+ }
+
+ private void assertNoArcs() {
Set<AstNode> tails = referenceGraph.getTails(head);
assertSizeOfSet(0, tails);
}
+
+ private void assertOneArc(AstNode tail) {
+ Set<AstNode> tails = referenceGraph.getTails(head);
+ assertSizeOfSet(1, tails);
+ assertSame(tail, tails.iterator().next());
+ }
+
+ private ReferenceFinder createReferenceFinder(AstNode source) {
+ return new ReferenceFinder(
+ source,
+ referenceGraph,
+ variableDeclarationMap,
+ constructorDeclarationMap);
+ }
+
+ private InstanceCreationExpression makeTailConstructor(String name, boolean isConstDeclaration,
+ boolean isConstUsage, boolean inMap) {
+ ArrayList<ConstructorInitializer> initializers = new ArrayList<ConstructorInitializer>();
+ ConstructorDeclaration constructorDeclaration = constructorDeclaration(
+ identifier(name),
+ null,
+ formalParameterList(),
+ initializers);
+ if (isConstDeclaration) {
+ constructorDeclaration.setConstKeyword(new KeywordToken(Keyword.CONST, 0));
+ }
+ ClassElementImpl classElement = classElement(name);
+ SimpleIdentifier identifier = identifier(name);
+ TypeName type = typeName(identifier);
+ InstanceCreationExpression instanceCreationExpression = instanceCreationExpression(isConstUsage
+ ? Keyword.CONST : Keyword.NEW, type);
+ tail = instanceCreationExpression;
+ ConstructorElementImpl constructorElement = constructorElement(
+ classElement,
+ name,
+ isConstDeclaration);
+ if (inMap) {
+ constructorDeclarationMap.put(constructorElement, constructorDeclaration);
+ }
+ instanceCreationExpression.setStaticElement(constructorElement);
+ return instanceCreationExpression;
+ }
+
+ private SimpleIdentifier makeTailVariable(String name, boolean isConst, boolean inMap) {
+ VariableDeclaration variableDeclaration = variableDeclaration(name);
+ tail = variableDeclaration;
+ VariableElementImpl variableElement = localVariableElement(name);
+ variableElement.setConst(isConst);
+ variableDeclarationList(isConst ? Keyword.CONST : Keyword.VAR, variableDeclaration);
+ if (inMap) {
+ variableDeclarationMap.put(variableElement, variableDeclaration);
+ }
+ SimpleIdentifier identifier = identifier(name);
+ identifier.setStaticElement(variableElement);
+ return identifier;
+ }
+
+ private void visitNode(AstNode node) {
+ node.accept(createReferenceFinder(head));
+ }
}

Powered by Google App Engine
This is Rietveld 408576698