Skip to content

Commit

Permalink
Refactor tests for javascript, jsp, plsql, scala, visualforce, xml
Browse files Browse the repository at this point in the history
using BaseParsingHelper

Revert "REVERT ME Remove some diff"

This reverts commit f728100.
  • Loading branch information
adangel committed Jan 10, 2020
1 parent a7c5a1f commit 4269fd1
Show file tree
Hide file tree
Showing 62 changed files with 784 additions and 1,167 deletions.
10 changes: 10 additions & 0 deletions pmd-javascript/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,15 @@
<artifactId>pmd-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-lang-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import org.mozilla.javascript.ast.AstRoot;

public class ASTAstRoot extends AbstractEcmascriptNode<AstRoot> {
import net.sourceforge.pmd.lang.ast.RootNode;

public class ASTAstRoot extends AbstractEcmascriptNode<AstRoot> implements RootNode {
public ASTAstRoot(AstRoot astRoot) {
super(astRoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ASTFunctionNodeTest extends EcmascriptParserTestBase {

@Test
public void testGetBody() {
ASTAstRoot node = parse("function foo() { var a = 'a'; }");
ASTAstRoot node = js.parse("function foo() { var a = 'a'; }");
ASTFunctionNode fn = node.getFirstDescendantOfType(ASTFunctionNode.class);
Assert.assertFalse(fn.isClosure());
EcmascriptNode<?> body = fn.getBody();
Expand All @@ -20,7 +20,7 @@ public void testGetBody() {

@Test
public void testGetBodyFunctionClosureExpression() {
ASTAstRoot node = parse18("(function(x) x*x)");
ASTAstRoot node = js18.parse("(function(x) x*x)");
ASTFunctionNode fn = node.getFirstDescendantOfType(ASTFunctionNode.class);
Assert.assertTrue(fn.isClosure());
EcmascriptNode<?> body = fn.getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class ASTTryStatementTest extends EcmascriptParserTestBase {

private ASTTryStatement getTryStmt(String js) {
EcmascriptNode<AstRoot> node = parse(js);
EcmascriptNode<AstRoot> node = this.js.parse(js);
List<ASTTryStatement> trys = node.findDescendantsOfType(ASTTryStatement.class);
Assert.assertEquals(1, trys.size());
ASTTryStatement tryStmt = trys.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
@Test
public void testLineNumbers() {
final String SOURCE_CODE = "function a() {" + PMD.EOL + " alert('hello');" + PMD.EOL + "}" + PMD.EOL;
EcmascriptNode<AstRoot> node = parse(SOURCE_CODE);
EcmascriptNode<AstRoot> node = js.parse(SOURCE_CODE);
assertEquals(1, node.getBeginLine());
assertEquals(1, node.getBeginColumn());
assertEquals(3, node.getEndLine());
Expand Down Expand Up @@ -69,7 +69,7 @@ public Object visit(ASTScope node, Object data) {

MyEcmascriptRule rule = new MyEcmascriptRule();
RuleContext ctx = new RuleContext();
rule.apply(Arrays.asList(parse(source)), ctx);
rule.apply(Arrays.asList(js.parse(source)), ctx);

assertEquals("Scope from 2 to 4", output.get(0));
assertEquals("Scope from 4 to 6", output.get(1));
Expand All @@ -80,7 +80,7 @@ public Object visit(ASTScope node, Object data) {
*/
@Test
public void testArrayAccess() {
EcmascriptNode<AstRoot> node = parse("function a() { b['a'] = 1; c[1] = 2; }");
EcmascriptNode<AstRoot> node = js.parse("function a() { b['a'] = 1; c[1] = 2; }");
List<ASTElementGet> arrays = node.findDescendantsOfType(ASTElementGet.class);
assertEquals("b", arrays.get(0).getTarget().getImage());
assertEquals("a", arrays.get(0).getElement().getImage());
Expand All @@ -94,9 +94,9 @@ public void testArrayAccess() {
*/
@Test
public void testArrayMethod() {
EcmascriptNode<AstRoot> rootNode = parse(
"function test(){\n" + " a(); // OK\n" + " b.c(); // OK\n" + " d[0](); // OK\n"
+ " e[0].f(); // OK\n" + " y.z[0](); // FAIL ==> java.lang.NullPointerException\n" + "}");
EcmascriptNode<AstRoot> rootNode = js.parse(
"function test(){\n" + " a(); // OK\n" + " b.c(); // OK\n" + " d[0](); // OK\n"
+ " e[0].f(); // OK\n" + " y.z[0](); // FAIL ==> java.lang.NullPointerException\n" + "}");

List<ASTFunctionCall> calls = rootNode.findDescendantsOfType(ASTFunctionCall.class);
List<String> results = new ArrayList<>();
Expand Down Expand Up @@ -129,7 +129,7 @@ private String getName(Node node) {
*/
@Test
public void testCaseAsIdentifier() {
ASTAstRoot rootNode = parse("function f(a){\n" + " a.case.flag = 1;\n" + " return;\n" + "}");
ASTAstRoot rootNode = js.parse("function f(a){\n" + " a.case.flag = 1;\n" + " return;\n" + "}");
ASTBlock block = rootNode.getFirstDescendantOfType(ASTBlock.class);
assertFalse(block.jjtGetChild(0) instanceof ASTEmptyExpression);
assertTrue(block.jjtGetChild(0) instanceof ASTExpressionStatement);
Expand Down Expand Up @@ -163,9 +163,9 @@ public void testSuppresionComment() {
*/
@Test
public void testVoidKeyword() {
ASTAstRoot rootNode = parse("function f(matchFn, fieldval, n){\n"
+ " return (matchFn)?(matcharray = eval(matchFn+\"('\"+fieldval+\"','\"+n.id+\"')\")):void(0);\n"
+ "}\n");
ASTAstRoot rootNode = js.parse("function f(matchFn, fieldval, n){\n"
+ " return (matchFn)?(matcharray = eval(matchFn+\"('\"+fieldval+\"','\"+n.id+\"')\")):void(0);\n"
+ "}\n");
ASTUnaryExpression unary = rootNode.getFirstDescendantOfType(ASTUnaryExpression.class);
assertEquals("void", unary.getImage());
}
Expand All @@ -175,9 +175,9 @@ public void testVoidKeyword() {
*/
@Test
public void testXorAssignment() {
ASTAstRoot rootNode = parse("function f() { var x = 2; x ^= 2; x &= 2; x |= 2; "
+ "x &&= true; x ||= false; x *= 2; x /= 2; x %= 2; x += 2; x -= 2; "
+ "x <<= 2; x >>= 2; x >>>= 2; }");
ASTAstRoot rootNode = js.parse("function f() { var x = 2; x ^= 2; x &= 2; x |= 2; "
+ "x &&= true; x ||= false; x *= 2; x /= 2; x %= 2; x += 2; x -= 2; "
+ "x <<= 2; x >>= 2; x >>>= 2; }");
ASTAssignment infix = rootNode.getFirstDescendantOfType(ASTAssignment.class);
assertEquals("^=", infix.getImage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,21 @@

package net.sourceforge.pmd.lang.ecmascript.ast;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;

import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.ecmascript.EcmascriptParserOptions;
import net.sourceforge.pmd.lang.ecmascript.EcmascriptParserOptions.Version;

public abstract class EcmascriptParserTestBase {
public ASTAstRoot parse(String code) {
EcmascriptParser parser = new EcmascriptParser(new EcmascriptParserOptions());
Reader sourceCode = new StringReader(code);
return (ASTAstRoot) parser.parse(sourceCode);
}

public ASTAstRoot parse18(String code) {
protected final JsParsingHelper js = JsParsingHelper.DEFAULT.withResourceContext(getClass());

protected final JsParsingHelper js18 = JsParsingHelper.DEFAULT.withResourceContext(getClass())
.withParserOptions(parserVersion(Version.VERSION_1_8));

public ParserOptions parserVersion(EcmascriptParserOptions.Version version) {
EcmascriptParserOptions parserOptions = new EcmascriptParserOptions();
parserOptions.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8);
EcmascriptParser parser = new EcmascriptParser(parserOptions);
Reader sourceCode = new StringReader(code);
return (ASTAstRoot) parser.parse(sourceCode);
parserOptions.setRhinoLanguageVersion(version);
return parserOptions;
}

public String dump(EcmascriptNode<?> node) {
DumpFacade dumpFacade = new DumpFacade();
StringWriter writer = new StringWriter();
dumpFacade.initializeWith(writer, "", true, node);
dumpFacade.visit(node, "");
return writer.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.lang.ecmascript.ast;

import org.jetbrains.annotations.NotNull;

import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
import net.sourceforge.pmd.lang.ecmascript.EcmascriptLanguageModule;

public final class JsParsingHelper extends BaseParsingHelper<JsParsingHelper, ASTAstRoot> {

public static final JsParsingHelper DEFAULT = new JsParsingHelper(Params.getDefaultProcess());

private JsParsingHelper(@NotNull Params params) {
super(EcmascriptLanguageModule.NAME, ASTAstRoot.class, params);
}

@NotNull
@Override
protected JsParsingHelper clone(@NotNull Params params) {
return new JsParsingHelper(params);
}
}
10 changes: 10 additions & 0 deletions pmd-jsp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,20 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-lang-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,8 @@

package net.sourceforge.pmd.lang.jsp.ast;

import static org.junit.Assert.assertEquals;

import java.io.StringReader;
import java.util.HashSet;
import java.util.Set;

import net.sourceforge.pmd.lang.ast.JavaCharStream;
import net.sourceforge.pmd.lang.ast.Node;

public abstract class AbstractJspNodesTst {

public <T extends JspNode> void assertNumberOfNodes(Class<T> clazz, String source, int number) {
Set<T> nodes = getNodes(clazz, source);
assertEquals("Exactly " + number + " element(s) expected", number, nodes.size());
}

/**
* Run the JSP parser on the source, and return the nodes of type clazz.
*
* @param clazz
* @param source
* @return Set
*/
public <T extends JspNode> Set<T> getNodes(Class<T> clazz, String source) {
JspParser parser = new JspParser(new JavaCharStream(new StringReader(source)));
Node rootNode = parser.CompilationUnit();
Set<T> nodes = new HashSet<>();
addNodeAndSubnodes(rootNode, nodes, clazz);
return nodes;
}

/**
* Return a subset of allNodes, containing the items in allNodes that are of
* the given type.
*
* @param clazz
* @param allNodes
* @return Set
*/
public <T extends JspNode> Set<T> getNodesOfType(Class<T> clazz, Set<JspNode> allNodes) {
Set<T> result = new HashSet<>();
for (Node node : allNodes) {
if (clazz.equals(node.getClass())) {
result.add((T) node);
}
}
return result;
}

/**
* Add the given node and its subnodes to the set of nodes. If clazz is not
* null, only nodes of the given class are put in the set of nodes.
*/
private <T extends JspNode> void addNodeAndSubnodes(Node node, Set<T> nodes, Class<T> clazz) {
if (null != node) {
if ((null == clazz) || (clazz.equals(node.getClass()))) {
nodes.add((T) node);
}
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
addNodeAndSubnodes(node.jjtGetChild(i), nodes, clazz);
}
}
}
protected JspParsingHelper jsp = JspParsingHelper.DEFAULT.withResourceContext(getClass());

}
Loading

0 comments on commit 4269fd1

Please sign in to comment.