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

Unified Diff: editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ConstantFinderTest.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/ConstantFinderTest.java
diff --git a/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ConstantFinderTest.java b/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ConstantFinderTest.java
index 21700382d03c66288caed48c05fe280cd58016d0..2291ef2942433495c479158b9b8239eb2f6275d8 100644
--- a/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ConstantFinderTest.java
+++ b/editor/tools/plugins/com.google.dart.engine_test/src/com/google/dart/engine/internal/constant/ConstantFinderTest.java
@@ -14,55 +14,126 @@
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.InstanceCreationExpression;
import com.google.dart.engine.ast.SimpleIdentifier;
import com.google.dart.engine.ast.VariableDeclaration;
+import com.google.dart.engine.element.ClassElement;
+import com.google.dart.engine.element.ConstructorElement;
import com.google.dart.engine.element.VariableElement;
import com.google.dart.engine.scanner.Keyword;
+import static com.google.dart.engine.ast.AstFactory.blockFunctionBody;
+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.integer;
+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.HashMap;
+import java.util.HashSet;
public class ConstantFinderTest extends EngineTestCase {
+ private AstNode node;
+
+ public void test_visitConstructorDeclaration_const() {
+ ConstructorElement element = setupConstructorDeclaration("A", true);
+ assertSame(node, findConstantDeclarations().get(element));
+ }
+
+ public void test_visitConstructorDeclaration_nonConst() {
+ setupConstructorDeclaration("A", false);
+ assertTrue(findConstantDeclarations().isEmpty());
+ }
+
+ public void test_visitInstanceCreationExpression_const() {
+ setupInstanceCreationExpression("A", true);
+ assertTrue(findConstructorInvocations().contains(node));
+ }
+
+ public void test_visitInstanceCreationExpression_nonConst() {
+ setupInstanceCreationExpression("A", false);
+ assertTrue(findConstructorInvocations().isEmpty());
+ }
+
public void test_visitVariableDeclaration_const() {
- VariableDeclaration declaration = variableDeclaration("v", integer(0));
- SimpleIdentifier name = declaration.getName();
- VariableElement element = localVariableElement(name);
- name.setStaticElement(element);
- variableDeclarationList(Keyword.CONST, declaration);
- ConstantFinder finder = new ConstantFinder();
- declaration.accept(finder);
- HashMap<VariableElement, VariableDeclaration> variableMap = finder.getVariableMap();
- assertNotNull(variableMap);
- assertSame(declaration, variableMap.get(element));
+ VariableElement element = setupVariableDeclaration("v", true, true);
+ assertSame(node, findVariableDeclarations().get(element));
}
public void test_visitVariableDeclaration_noInitializer() {
- VariableDeclaration declaration = variableDeclaration("v");
- SimpleIdentifier name = declaration.getName();
- VariableElement element = localVariableElement(name);
- name.setStaticElement(element);
- variableDeclarationList(Keyword.CONST, declaration);
- ConstantFinder finder = new ConstantFinder();
- declaration.accept(finder);
- HashMap<VariableElement, VariableDeclaration> variableMap = finder.getVariableMap();
- assertNotNull(variableMap);
- assertTrue(variableMap.isEmpty());
+ setupVariableDeclaration("v", true, false);
+ assertTrue(findVariableDeclarations().isEmpty());
}
public void test_visitVariableDeclaration_nonConst() {
- VariableDeclaration declaration = variableDeclaration("v", integer(0));
- SimpleIdentifier name = declaration.getName();
- VariableElement element = localVariableElement(name);
- name.setStaticElement(element);
- variableDeclarationList(null, declaration);
+ setupVariableDeclaration("v", false, true);
+ assertTrue(findVariableDeclarations().isEmpty());
+ }
+
+ private HashMap<ConstructorElement, ConstructorDeclaration> findConstantDeclarations() {
+ ConstantFinder finder = new ConstantFinder();
+ node.accept(finder);
+ HashMap<ConstructorElement, ConstructorDeclaration> constructorMap = finder.getConstructorMap();
+ assertNotNull(constructorMap);
+ return constructorMap;
+ }
+
+ private HashSet<InstanceCreationExpression> findConstructorInvocations() {
+ ConstantFinder finder = new ConstantFinder();
+ node.accept(finder);
+ HashSet<InstanceCreationExpression> constructorInvocations = finder.getConstructorInvocations();
+ assertNotNull(constructorInvocations);
+ return constructorInvocations;
+ }
+
+ private HashMap<VariableElement, VariableDeclaration> findVariableDeclarations() {
ConstantFinder finder = new ConstantFinder();
- declaration.accept(finder);
+ node.accept(finder);
HashMap<VariableElement, VariableDeclaration> variableMap = finder.getVariableMap();
assertNotNull(variableMap);
- assertTrue(variableMap.isEmpty());
+ return variableMap;
+ }
+
+ private ConstructorElement setupConstructorDeclaration(String name, boolean isConst) {
+ Keyword constKeyword = isConst ? Keyword.CONST : null;
+ ConstructorDeclaration constructorDeclaration = constructorDeclaration(
+ constKeyword,
+ null,
+ null,
+ name,
+ formalParameterList(),
+ null,
+ blockFunctionBody());
+ ClassElement classElement = classElement(name);
+ ConstructorElement element = constructorElement(classElement, name, isConst);
+ constructorDeclaration.setElement(element);
+ node = constructorDeclaration;
+ return element;
+ }
+
+ private void setupInstanceCreationExpression(String name, boolean isConst) {
+ node = instanceCreationExpression(
+ isConst ? Keyword.CONST : null,
+ typeName(identifier(name)));
+ }
+
+ private VariableElement setupVariableDeclaration(String name, boolean isConst,
+ boolean isInitialized) {
+ VariableDeclaration variableDeclaration = isInitialized ? variableDeclaration(name, integer(0))
+ : variableDeclaration(name);
+ SimpleIdentifier identifier = variableDeclaration.getName();
+ VariableElement element = localVariableElement(identifier);
+ identifier.setStaticElement(element);
+ variableDeclarationList(isConst ? Keyword.CONST : null, variableDeclaration);
+ node = variableDeclaration;
+ return element;
}
}

Powered by Google App Engine
This is Rietveld 408576698