Skip to content

Commit

Permalink
Add some integration tests for PureFunctionIdentifier.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131879793
  • Loading branch information
tadeegan authored and blickly committed Aug 31, 2016
1 parent bd774e2 commit e73f806
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/com/google/javascript/jscomp/ChainCalls.java
Expand Up @@ -21,7 +21,6 @@
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; import com.google.javascript.jscomp.NodeTraversal.ScopedCallback;
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;

import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
Expand Down
Expand Up @@ -22,7 +22,6 @@
import com.google.javascript.jscomp.DefinitionsRemover.Definition; import com.google.javascript.jscomp.DefinitionsRemover.Definition;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;

import java.util.Collection; import java.util.Collection;


/** /**
Expand Down
5 changes: 4 additions & 1 deletion src/com/google/javascript/jscomp/DefinitionsRemover.java
Expand Up @@ -21,7 +21,6 @@
import com.google.javascript.rhino.IR; import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token; import com.google.javascript.rhino.Token;

import java.util.Set; import java.util.Set;


/** /**
Expand Down Expand Up @@ -156,6 +155,10 @@ public void remove() {
public boolean isExtern() { public boolean isExtern() {
return isExtern; return isExtern;
} }

public String toString() {
return getLValue().getQualifiedName() + " = " + getRValue().toString();
}
} }


/** /**
Expand Down
Expand Up @@ -21,7 +21,6 @@
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;

import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
Expand Down
Expand Up @@ -22,7 +22,6 @@
import com.google.javascript.jscomp.DefinitionsRemover.Definition; import com.google.javascript.jscomp.DefinitionsRemover.Definition;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;

import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
Expand Down
79 changes: 77 additions & 2 deletions test/com/google/javascript/jscomp/PureFunctionIdentifierTest.java
Expand Up @@ -23,7 +23,6 @@
import com.google.javascript.jscomp.CompilerOptions.LanguageMode; import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;

import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;


Expand Down Expand Up @@ -668,6 +667,37 @@ public void testCall() throws Exception {
ImmutableList.of("f.call")); ImmutableList.of("f.call"));
} }


public void testApplyToUnknownDefinition() throws Exception {
checkMarkedCalls(
"var dict = {'func': function() {}};"
+ "function f() { var s = dict['func'];}"
+ "f.apply()",
ImmutableList.of("f.apply"));

// Not marked becuase the definition cannot be found so unknown side effects.
checkMarkedCalls(
"var dict = {'func': function() {}};"
+ "function f() { var s = dict['func'].apply();}"
+ "f.apply()",
ImmutableList.<String>of());

// Not marked becuase the definition cannot be found so unknown side effects.
checkMarkedCalls(
"var pure = function() {};"
+ "var dict = {'func': function() {}};"
+ "function f() { var s = (dict['func'] || pure)();}"
+ "f()",
ImmutableList.<String>of());

// Not marked becuase the definition cannot be found so unknown side effects.
checkMarkedCalls(
"var pure = function() {};"
+ "var dict = {'func': function() {}};"
+ "function f() { var s = (condition ? dict['func'] : pure)();}"
+ "f()",
ImmutableList.<String>of());
}

public void testInference1() throws Exception { public void testInference1() throws Exception {
checkMarkedCalls("function f() {return g()}" + checkMarkedCalls("function f() {return g()}" +
"function g() {return 42}" + "function g() {return 42}" +
Expand Down Expand Up @@ -1036,6 +1066,51 @@ public void testInheritance2() throws Exception {
checkMarkedCalls(source, ImmutableList.of("goog.inherits", "I", "A")); checkMarkedCalls(source, ImmutableList.of("goog.inherits", "I", "A"));
} }


public void testAmbiguousDefinitions() throws Exception {
String s =
"var globalVar = 1;"
+ "A.f = function() {globalVar = 2;};"
+ "A.f = function() {};"
+ "function sideEffectCaller() { A.f() };"
+ "sideEffectCaller();";
// Can't tell which f is being called so it assumes both.
checkMarkedCalls(s, ImmutableList.<String>of());
}

public void testAmbiguousDefinitionsCall() throws Exception {
String s =
"var globalVar = 1;"
+ "A.f = function() {globalVar = 2;};"
+ "A.f = function() {};"
+ "function sideEffectCaller() { A.f.call(null); };"
+ "sideEffectCaller();";
// Can't tell which f is being called so it assumes both.
checkMarkedCalls(s, ImmutableList.<String>of());
}

public void testAmbiguousDefinitionsAllPropagationTypes() throws Exception {
String s =
"var globalVar = 1;"
+ "/**@constructor*/A.f = function() { this.x = 5; };"
+ "/**@constructor*/B.f = function() {};"
+ "function sideEffectCaller() { new C.f() };"
+ "sideEffectCaller();";
// Can't tell which f is being called so it assumes both.
checkMarkedCalls(s, ImmutableList.<String>of("C.f", "sideEffectCaller"));
}

public void testAmbiguousDefinitionsCallWithThis() throws Exception {
String s =
"var globalVar = 1;"
+ "A.modifiesThis = function() { this.x = 5; };"
+ "/**@constructor*/function Constructor() { Constructor.modifiesThis.call(this); };"
+ "Constructor.prototype.modifiesThis = function() {};"
+ "new Constructor();"
+ "A.modifiesThis();";
// Can't tell which modifiesThis is being called so it assumes both.
checkMarkedCalls(s, ImmutableList.<String>of("Constructor"));
}

public void testCallBeforeDefinition() throws Exception { public void testCallBeforeDefinition() throws Exception {
checkMarkedCalls("f(); function f(){}", checkMarkedCalls("f(); function f(){}",
ImmutableList.of("f")); ImmutableList.of("f"));
Expand Down Expand Up @@ -1199,7 +1274,7 @@ public void testCallFunctionForGorH() throws Exception {
checkMarkedCalls(source, ImmutableList.of("(f : (g || h))", "i")); checkMarkedCalls(source, ImmutableList.of("(f : (g || h))", "i"));
} }


public void testCallFunctionFOrGWithSideEffects() throws Exception { public void testCallFunctionForGWithSideEffects() throws Exception {
String source = "var x = 0;\n" + String source = "var x = 0;\n" +
"function f(){x = 10}\n" + "function f(){x = 10}\n" +
"function g(){}\n" + "function g(){}\n" +
Expand Down

0 comments on commit e73f806

Please sign in to comment.