| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.ImmutableSet; |
| 20 |
import com.google.javascript.jscomp.NodeTraversal.AbstractNodeTypePruningCallback; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
import com.google.javascript.rhino.Token; |
| 23 |
|
| 24 |
import junit.framework.TestCase; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Set; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class NodeTraversalTest extends TestCase { |
| 34 |
public void testPruningCallbackShouldTraverse1() { |
| 35 |
PruningCallback include = |
| 36 |
new PruningCallback(ImmutableSet.of(Token.SCRIPT, Token.VAR), true); |
| 37 |
|
| 38 |
Node script = new Node(Token.SCRIPT); |
| 39 |
assertTrue(include.shouldTraverse(null, script, null)); |
| 40 |
assertTrue(include.shouldTraverse(null, new Node(Token.VAR), null)); |
| 41 |
assertFalse(include.shouldTraverse(null, new Node(Token.NAME), null)); |
| 42 |
assertFalse(include.shouldTraverse(null, new Node(Token.ADD), null)); |
| 43 |
} |
| 44 |
|
| 45 |
public void testPruningCallbackShouldTraverse2() { |
| 46 |
PruningCallback include = |
| 47 |
new PruningCallback(ImmutableSet.of(Token.SCRIPT, Token.VAR), false); |
| 48 |
|
| 49 |
Node script = new Node(Token.SCRIPT); |
| 50 |
assertFalse(include.shouldTraverse(null, script, null)); |
| 51 |
assertFalse(include.shouldTraverse(null, new Node(Token.VAR), null)); |
| 52 |
assertTrue(include.shouldTraverse(null, new Node(Token.NAME), null)); |
| 53 |
assertTrue(include.shouldTraverse(null, new Node(Token.ADD), null)); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static class PruningCallback extends AbstractNodeTypePruningCallback { |
| 61 |
public PruningCallback(Set<Integer> nodeTypes, boolean include) { |
| 62 |
super(nodeTypes, include); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 67 |
throw new UnsupportedOperationException(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public void testReport() { |
| 72 |
final List<JSError> errors = new ArrayList<JSError>(); |
| 73 |
|
| 74 |
Compiler compiler = new Compiler(new BasicErrorManager() { |
| 75 |
|
| 76 |
@Override public void report(CheckLevel level, JSError error) { |
| 77 |
errors.add(error); |
| 78 |
} |
| 79 |
|
| 80 |
@Override public void println(CheckLevel level, JSError error) { |
| 81 |
} |
| 82 |
|
| 83 |
@Override protected void printSummary() { |
| 84 |
} |
| 85 |
}); |
| 86 |
compiler.initCompilerOptionsIfTesting(); |
| 87 |
|
| 88 |
NodeTraversal t = new NodeTraversal(compiler, null); |
| 89 |
DiagnosticType dt = DiagnosticType.warning("FOO", "{0}, {1} - {2}"); |
| 90 |
|
| 91 |
t.report(null, dt, "Foo", "Bar", "Hello"); |
| 92 |
assertEquals(1, errors.size()); |
| 93 |
assertEquals("Foo, Bar - Hello", errors.get(0).description); |
| 94 |
} |
| 95 |
|
| 96 |
public void testUnexpectedException() { |
| 97 |
final String TEST_EXCEPTION = "test me"; |
| 98 |
|
| 99 |
NodeTraversal.Callback cb = new NodeTraversal.AbstractPostOrderCallback() { |
| 100 |
@Override |
| 101 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 102 |
throw new RuntimeException(TEST_EXCEPTION); |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
Compiler compiler = new Compiler(); |
| 107 |
NodeTraversal t = new NodeTraversal(compiler, cb); |
| 108 |
String code = "function foo() {}"; |
| 109 |
Node tree = parse(compiler, code); |
| 110 |
|
| 111 |
try { |
| 112 |
t.traverse(tree); |
| 113 |
fail("Expected RuntimeException"); |
| 114 |
} catch (RuntimeException e) { |
| 115 |
assertTrue(e.getMessage().startsWith( |
| 116 |
"INTERNAL COMPILER ERROR.\n" + |
| 117 |
"Please report this problem.\n" + |
| 118 |
"test me")); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
public void testGetScopeRoot() { |
| 124 |
Compiler compiler = new Compiler(); |
| 125 |
NodeTraversal t = new NodeTraversal(compiler, |
| 126 |
new NodeTraversal.ScopedCallback() { |
| 127 |
|
| 128 |
@Override |
| 129 |
public void enterScope(NodeTraversal t) { |
| 130 |
Node root1 = t.getScopeRoot(); |
| 131 |
Node root2 = t.getScope().getRootNode(); |
| 132 |
assertEquals(root1, root2); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void exitScope(NodeTraversal t) { |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 146 |
} |
| 147 |
} |
| 148 |
); |
| 149 |
|
| 150 |
String code = "" + |
| 151 |
"var a; " + |
| 152 |
"function foo() {" + |
| 153 |
" var b" + |
| 154 |
"}"; |
| 155 |
Node tree = parse(compiler, code); |
| 156 |
t.traverse(tree); |
| 157 |
} |
| 158 |
|
| 159 |
public void testGetCurrentNode() { |
| 160 |
Compiler compiler = new Compiler(); |
| 161 |
ScopeCreator creator = new SyntacticScopeCreator(compiler); |
| 162 |
ExpectNodeOnEnterScope callback = new ExpectNodeOnEnterScope(); |
| 163 |
NodeTraversal t = new NodeTraversal(compiler, callback, creator); |
| 164 |
|
| 165 |
String code = "" + |
| 166 |
"var a; " + |
| 167 |
"function foo() {" + |
| 168 |
" var b;" + |
| 169 |
"}"; |
| 170 |
|
| 171 |
Node tree = parse(compiler, code); |
| 172 |
Scope topScope = creator.createScope(tree, null); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
callback.expect(tree.getFirstChild(), tree); |
| 177 |
t.traverseWithScope(tree.getFirstChild(), topScope); |
| 178 |
callback.assertEntered(); |
| 179 |
|
| 180 |
|
| 181 |
callback.expect(tree.getFirstChild(), tree.getFirstChild()); |
| 182 |
t.traverse(tree.getFirstChild()); |
| 183 |
callback.assertEntered(); |
| 184 |
|
| 185 |
|
| 186 |
Node fn = tree.getFirstChild().getNext(); |
| 187 |
Scope fnScope = creator.createScope(fn, topScope); |
| 188 |
callback.expect(fn, fn); |
| 189 |
t.traverseAtScope(fnScope); |
| 190 |
callback.assertEntered(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private static class ExpectNodeOnEnterScope implements |
| 196 |
NodeTraversal.ScopedCallback { |
| 197 |
private Node node; |
| 198 |
private Node scopeRoot; |
| 199 |
private boolean entered = false; |
| 200 |
|
| 201 |
private void expect(Node node, Node scopeRoot) { |
| 202 |
this.node = node; |
| 203 |
this.scopeRoot = scopeRoot; |
| 204 |
entered = false; |
| 205 |
} |
| 206 |
|
| 207 |
private void assertEntered() { |
| 208 |
assertTrue(entered); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public void enterScope(NodeTraversal t) { |
| 213 |
assertEquals(node, t.getCurrentNode()); |
| 214 |
assertEquals(scopeRoot, t.getScopeRoot()); |
| 215 |
entered = true; |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public void exitScope(NodeTraversal t) { |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
private static Node parse(Compiler compiler, String js) { |
| 233 |
Node n = compiler.parseTestCode(js); |
| 234 |
assertEquals(0, compiler.getErrorCount()); |
| 235 |
return n; |
| 236 |
} |
| 237 |
} |