REVERT ME Remove some diff
This commit is contained in:
@ -1,14 +0,0 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast;
|
||||
|
||||
/**
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
public abstract class BaseScalaTest {
|
||||
|
||||
protected final ScalaParsingHelper scala = ScalaParsingHelper.DEFAULT.withResourceContext(getClass());
|
||||
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.ast;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.PMDException;
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.RuleSet;
|
||||
import net.sourceforge.pmd.RuleSetFactory;
|
||||
import net.sourceforge.pmd.RuleSets;
|
||||
import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
|
||||
import net.sourceforge.pmd.lang.scala.ScalaLanguageModule;
|
||||
|
||||
public final class ScalaParsingHelper extends BaseParsingHelper<ScalaParsingHelper, ASTSource> {
|
||||
|
||||
public static final ScalaParsingHelper DEFAULT = new ScalaParsingHelper(Params.getDefaultProcess());
|
||||
|
||||
private ScalaParsingHelper(@NotNull Params params) {
|
||||
super(ScalaLanguageModule.NAME, ASTSource.class, params);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected ScalaParsingHelper clone(@NotNull Params params) {
|
||||
return new ScalaParsingHelper(params);
|
||||
}
|
||||
|
||||
|
||||
public Report getReportForTestString(Rule rule, String testSourceCode) {
|
||||
PMD p = new PMD();
|
||||
RuleContext ctx = new RuleContext();
|
||||
Report report = new Report();
|
||||
ctx.setReport(report);
|
||||
ctx.setSourceCodeFile(new File("test.scala"));
|
||||
RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(rule);
|
||||
try {
|
||||
p.getSourceCodeProcessor().processSourceCode(new StringReader(testSourceCode), new RuleSets(rules), ctx);
|
||||
} catch (PMDException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
public Report getReportForResource(Rule rule, String resourcePath) {
|
||||
return getReportForTestString(rule, readResource(resourcePath));
|
||||
}
|
||||
}
|
@ -4,27 +4,42 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.scala.rule;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.io.File;
|
||||
import java.io.StringReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.PMDException;
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.RuleSet;
|
||||
import net.sourceforge.pmd.RuleSetFactory;
|
||||
import net.sourceforge.pmd.RuleSets;
|
||||
import net.sourceforge.pmd.internal.util.IteratorUtil;
|
||||
import net.sourceforge.pmd.lang.scala.ast.ASTSource;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersionHandler;
|
||||
import net.sourceforge.pmd.lang.Parser;
|
||||
import net.sourceforge.pmd.lang.scala.ScalaLanguageModule;
|
||||
import net.sourceforge.pmd.lang.scala.ast.ASTTermApply;
|
||||
import net.sourceforge.pmd.lang.scala.ast.ASTTermName;
|
||||
import net.sourceforge.pmd.lang.scala.ast.BaseScalaTest;
|
||||
import net.sourceforge.pmd.lang.scala.ast.ScalaNode;
|
||||
|
||||
public class ScalaRuleTest extends BaseScalaTest {
|
||||
|
||||
public class ScalaRuleTest {
|
||||
private static final String SCALA_TEST = "/parserFiles/helloworld.scala";
|
||||
|
||||
@Test
|
||||
public void testRuleVisits() {
|
||||
public void testRuleVisits() throws Exception {
|
||||
LanguageVersionHandler scalaVersionHandler = LanguageRegistry.getLanguage(ScalaLanguageModule.NAME)
|
||||
.getDefaultVersion().getLanguageVersionHandler();
|
||||
Parser parser = scalaVersionHandler.getParser(scalaVersionHandler.getDefaultParserOptions());
|
||||
ScalaNode<?> root = (ScalaNode<?>) parser.parse(null,
|
||||
new StringReader(IOUtils.toString(getClass().getResourceAsStream(SCALA_TEST), "UTF-8")));
|
||||
final AtomicInteger visited = new AtomicInteger();
|
||||
ScalaRule rule = new ScalaRule() {
|
||||
|
||||
@ -34,13 +49,12 @@ public class ScalaRuleTest extends BaseScalaTest {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
};
|
||||
ASTSource root = scala.parseResource(SCALA_TEST);
|
||||
rule.apply(Collections.singletonList(root), null);
|
||||
rule.apply(Arrays.asList(root), null);
|
||||
Assert.assertEquals(12, visited.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDummyRule() {
|
||||
public void testDummyRule() throws Exception {
|
||||
ScalaRule rule = new ScalaRule() {
|
||||
@Override
|
||||
public RuleContext visit(ASTTermApply node, RuleContext data) {
|
||||
@ -51,9 +65,21 @@ public class ScalaRuleTest extends BaseScalaTest {
|
||||
return data;
|
||||
}
|
||||
};
|
||||
Report report = scala.getReportForResource(rule, SCALA_TEST);
|
||||
Report report = getReportForTestString(rule,
|
||||
IOUtils.toString(getClass().getResourceAsStream(SCALA_TEST), "UTF-8"));
|
||||
|
||||
Assert.assertEquals(1, IteratorUtil.count(report.iterator()));
|
||||
}
|
||||
|
||||
private static Report getReportForTestString(Rule r, String test) throws PMDException {
|
||||
PMD p = new PMD();
|
||||
RuleContext ctx = new RuleContext();
|
||||
Report report = new Report();
|
||||
ctx.setReport(report);
|
||||
ctx.setSourceCodeFile(new File("test.scala"));
|
||||
RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(r);
|
||||
p.getSourceCodeProcessor().processSourceCode(new StringReader(test), new RuleSets(rules), ctx);
|
||||
return report;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,19 +6,29 @@ package net.sourceforge.pmd.lang.scala.rule;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.PMDException;
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.RuleSet;
|
||||
import net.sourceforge.pmd.RuleSetFactory;
|
||||
import net.sourceforge.pmd.RuleSets;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.rule.XPathRule;
|
||||
import net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery;
|
||||
import net.sourceforge.pmd.lang.scala.ScalaLanguageModule;
|
||||
import net.sourceforge.pmd.lang.scala.ast.BaseScalaTest;
|
||||
|
||||
public class XPathRuleTest extends BaseScalaTest {
|
||||
import net.sourceforge.pmd.testframework.RuleTst;
|
||||
|
||||
public class XPathRuleTest extends RuleTst {
|
||||
private static final String SCALA_TEST = "/parserFiles/helloworld.scala";
|
||||
|
||||
XPathRule rule;
|
||||
@ -31,13 +41,24 @@ public class XPathRuleTest extends BaseScalaTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrintHelloWorld() {
|
||||
public void testPrintHelloWorld() throws Exception {
|
||||
String xpath = "//TermApply/TermName[@Image=\"println\"]";
|
||||
rule.setXPath(xpath);
|
||||
rule.setVersion(XPathRuleQuery.XPATH_2_0);
|
||||
Report report = scala.getReportForResource(rule, SCALA_TEST);
|
||||
Report report = getReportForTestString(rule,
|
||||
IOUtils.toString(getClass().getResourceAsStream(SCALA_TEST), "UTF-8"));
|
||||
RuleViolation rv = report.iterator().next();
|
||||
assertEquals(2, rv.getBeginLine());
|
||||
}
|
||||
|
||||
private static Report getReportForTestString(Rule r, String test) throws PMDException {
|
||||
PMD p = new PMD();
|
||||
RuleContext ctx = new RuleContext();
|
||||
Report report = new Report();
|
||||
ctx.setReport(report);
|
||||
ctx.setSourceCodeFile(new File("test.scala"));
|
||||
RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(r);
|
||||
p.getSourceCodeProcessor().processSourceCode(new StringReader(test), new RuleSets(rules), ctx);
|
||||
return report;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user