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

Unified Diff: compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerSlowTest.java

Issue 8479037: Provide enclosing element (method) for local function object, issue 145 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month 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: compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerSlowTest.java
diff --git a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerSlowTest.java b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerSlowTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..1fa8281aad2947ca3d57f63de0ef5c9b0c3a07d3
--- /dev/null
+++ b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerSlowTest.java
@@ -0,0 +1,114 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.google.dart.compiler.type;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.Iterables;
+import com.google.dart.compiler.CompilerTestCase;
+import com.google.dart.compiler.ast.DartFunctionExpression;
+import com.google.dart.compiler.ast.DartInvocation;
+import com.google.dart.compiler.ast.DartNode;
+import com.google.dart.compiler.ast.DartNodeTraverser;
+import com.google.dart.compiler.ast.DartUnit;
+import com.google.dart.compiler.resolver.ClassElement;
+import com.google.dart.compiler.resolver.Element;
+import com.google.dart.compiler.resolver.ElementKind;
+import com.google.dart.compiler.resolver.EnclosingElement;
+import com.google.dart.compiler.resolver.MethodElement;
+
+/**
+ * Variant of {@link TypeAnalyzerTest}, which is based on {@link CompilerTestCase}. It is probably
+ * slower, not actually unit test, but easier to use if you need access to DartNode's.
+ */
+public class TypeAnalyzerSlowTest extends CompilerTestCase {
zundel 2011/11/07 19:26:53 maybe call this TypeAnalyzerCompiledTest?
+ /**
+ * Tests that we correctly provide {@link Element#getEnclosingElement()} for method of class.
+ */
+ public void test_resolveClassMethod() throws Exception {
+ AnalyzeLibraryResult libraryResult =
+ analyzeLibrary(
+ "Test.dart",
+ Joiner.on("\n").join(
+ "class Object {}",
+ "class Test {",
+ " foo() {",
+ " f();",
+ " }",
+ " f() {",
+ " }",
+ "}"));
+ DartUnit unit = libraryResult.getLibraryUnitResult().getUnits().iterator().next();
+ // find f() invocation
+ DartInvocation invocation = findInvocationSimple(unit, "f()");
+ assertNotNull(invocation);
+ // referenced Element should be resolved to MethodElement
+ Element methodElement = invocation.getReferencedElement();
+ assertNotNull(methodElement);
+ assertSame(ElementKind.METHOD, methodElement.getKind());
+ assertEquals("f", ((MethodElement) methodElement).getOriginalSymbolName());
+ // enclosing Element of MethodElement is ClassElement
+ EnclosingElement classElement = methodElement.getEnclosingElement();
+ assertNotNull(classElement);
+ assertSame(ElementKind.CLASS, classElement.getKind());
+ assertEquals("Test", ((ClassElement) classElement).getOriginalSymbolName());
+ }
+
+ /**
+ * Test that local {@link DartFunctionExpression} has {@link Element} with enclosing
+ * {@link Element}.
+ * <p>
+ * http://code.google.com/p/dart/issues/detail?id=145
+ */
+ public void test_resolveLocalFunction() throws Exception {
+ AnalyzeLibraryResult libraryResult =
+ analyzeLibrary(
+ "Test.dart",
+ Joiner.on("\n").join(
+ "class Object {}",
+ "class Test {",
+ " foo() {",
+ " f() {",
+ " }",
+ " f();",
+ " }",
+ "}"));
+ DartUnit unit = libraryResult.getLibraryUnitResult().getUnits().iterator().next();
+ // find f() invocation
+ DartInvocation invocation = findInvocationSimple(unit, "f()");
+ assertNotNull(invocation);
+ // referenced Element should be resolved to MethodElement
+ Element functionElement = invocation.getReferencedElement();
+ assertNotNull(functionElement);
+ assertSame(ElementKind.FUNCTION_OBJECT, functionElement.getKind());
+ assertEquals("f", ((MethodElement) functionElement).getOriginalSymbolName());
+ // enclosing Element of this FUNCTION_OBJECT is enclosing method
+ EnclosingElement enclosingMethodElement = functionElement.getEnclosingElement();
+ assertNotNull(enclosingMethodElement);
+ assertSame(ElementKind.METHOD, enclosingMethodElement.getKind());
+ assertEquals("foo", ((MethodElement) enclosingMethodElement).getName());
+ // use EnclosingElement methods implementations in MethodElement
+ assertEquals(false, enclosingMethodElement.isInterface());
+ assertEquals(true, Iterables.isEmpty(enclosingMethodElement.getMembers()));
+ assertEquals(null, enclosingMethodElement.lookupLocalElement("f"));
+ }
+
+ /**
+ * @return the {@link DartInvocation} with given source. This is inaccurate approach, but good
+ * enough for specific tests.
+ */
+ private static DartInvocation findInvocationSimple(DartNode rootNode,
+ final String invocationString) {
+ final DartInvocation invocationRef[] = new DartInvocation[1];
+ rootNode.accept(new DartNodeTraverser<Void>() {
+ @Override
+ public Void visitInvocation(DartInvocation node) {
+ if (node.toSource().equals(invocationString)) {
+ invocationRef[0] = node;
+ }
+ return super.visitInvocation(node);
+ }
+ });
+ return invocationRef[0];
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698