parent
5c975bd08c
commit
abc0b3c94e
@ -72,5 +72,16 @@
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<!-- Test classes of pmd-core, which contain dummy language modules. -->
|
||||
<groupId>net.sourceforge.pmd</groupId>
|
||||
<artifactId>pmd-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
<classifier>tests</classifier>
|
||||
<type>test-jar</type>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -1,80 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.test.lang;
|
||||
|
||||
import net.sourceforge.pmd.annotation.InternalApi;
|
||||
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.ast.AstInfo;
|
||||
import net.sourceforge.pmd.lang.ast.Parser;
|
||||
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
|
||||
import net.sourceforge.pmd.lang.ast.RootNode;
|
||||
import net.sourceforge.pmd.lang.document.TextRegion;
|
||||
import net.sourceforge.pmd.lang.impl.SimpleLanguageModuleBase;
|
||||
import net.sourceforge.pmd.test.lang.ast.DummyNode;
|
||||
|
||||
/**
|
||||
* Dummy language used for testing PMD.
|
||||
*
|
||||
* @deprecated Don't use this directly. We can probably remove this in favour of plaintextlanguage
|
||||
* when https://github.com/pmd/pmd/issues/3918 is merged
|
||||
*/
|
||||
@Deprecated
|
||||
@InternalApi
|
||||
public class DummyLanguageModule extends SimpleLanguageModuleBase {
|
||||
|
||||
public static final String NAME = "Dummy";
|
||||
public static final String TERSE_NAME = "dummy";
|
||||
|
||||
public DummyLanguageModule() {
|
||||
super(LanguageMetadata.withId(TERSE_NAME).name(NAME).extensions("dummy")
|
||||
.addVersion("1.0")
|
||||
.addVersion("1.1")
|
||||
.addVersion("1.2")
|
||||
.addVersion("1.3")
|
||||
.addVersion("1.4")
|
||||
.addVersion("1.5", "5")
|
||||
.addVersion("1.6", "6")
|
||||
.addDefaultVersion("1.7", "7")
|
||||
.addVersion("1.8", "8"), new Handler());
|
||||
}
|
||||
|
||||
public static DummyLanguageModule getInstance() {
|
||||
return (DummyLanguageModule) LanguageRegistry.PMD.getLanguageByFullName(NAME);
|
||||
}
|
||||
|
||||
public static class Handler extends AbstractPmdLanguageVersionHandler {
|
||||
|
||||
@Override
|
||||
public Parser getParser() {
|
||||
return DummyRootNode::new;
|
||||
}
|
||||
}
|
||||
|
||||
public static class DummyRootNode extends DummyNode implements RootNode {
|
||||
|
||||
|
||||
private final AstInfo<DummyRootNode> astInfo;
|
||||
|
||||
public DummyRootNode(ParserTask task) {
|
||||
this.astInfo = new AstInfo<>(task, this);
|
||||
withCoords(task.getTextDocument().getEntireRegion());
|
||||
setImage("Foo");
|
||||
}
|
||||
|
||||
@Override
|
||||
public DummyRootNode withCoords(TextRegion region) {
|
||||
super.withCoords(region);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public AstInfo<DummyRootNode> getAstInfo() {
|
||||
return astInfo;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.test.lang.ast;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.impl.AbstractNode;
|
||||
import net.sourceforge.pmd.lang.document.TextRegion;
|
||||
|
||||
public class DummyNode extends AbstractNode<DummyNode, DummyNode> {
|
||||
|
||||
private String image;
|
||||
private TextRegion region = TextRegion.caretAt(0);
|
||||
|
||||
public DummyNode withCoords(TextRegion region) {
|
||||
this.region = Objects.requireNonNull(region);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DummyNode newChild() {
|
||||
DummyNode child = new DummyNode();
|
||||
addChild(child, getNumChildren());
|
||||
return child;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextRegion getTextRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public String toString() {
|
||||
return "dummyNode";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getXPathNodeName() {
|
||||
return "dummyNode";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
}
|
@ -55,11 +55,9 @@ public abstract class RuleTst {
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a rule in a certain ruleset by name
|
||||
*
|
||||
* todo make this static
|
||||
* Find a rule in a certain ruleset by name.
|
||||
*/
|
||||
public Rule findRule(String ruleSet, String ruleName) {
|
||||
public static Rule findRule(String ruleSet, String ruleName) {
|
||||
try {
|
||||
RuleSet parsedRset = new RuleSetLoader().warnDeprecated(false).loadFromResource(ruleSet);
|
||||
Rule rule = parsedRset.getRuleByName(ruleName);
|
||||
@ -139,8 +137,8 @@ public abstract class RuleTst {
|
||||
*
|
||||
* @return The rule once it has been reinitialised
|
||||
*/
|
||||
protected Rule reinitializeRule(Rule rule) {
|
||||
return findRule(rule.getRuleSetName(), rule.getName());
|
||||
private Rule reinitializeRule(Rule rule) {
|
||||
return rule.deepCopy();
|
||||
}
|
||||
|
||||
|
||||
|
@ -19,7 +19,7 @@ import net.sourceforge.pmd.Rule;
|
||||
*/
|
||||
public abstract class SimpleAggregatorTst extends RuleTst {
|
||||
|
||||
private List<Rule> rules = new ArrayList<>();
|
||||
private final List<Rule> rules = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Configure the rule tests to be executed. Override this method in
|
||||
|
@ -1 +0,0 @@
|
||||
net.sourceforge.pmd.test.lang.DummyLanguageModule
|
@ -4,9 +4,11 @@
|
||||
|
||||
package net.sourceforge.pmd.testframework;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ -18,26 +20,20 @@ import org.mockito.Mockito;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.DummyLanguageModule;
|
||||
import net.sourceforge.pmd.lang.LanguageProcessor;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.ast.DummyNode.DummyRootNode;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.document.TextRegion;
|
||||
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
|
||||
import net.sourceforge.pmd.test.lang.DummyLanguageModule;
|
||||
import net.sourceforge.pmd.test.lang.DummyLanguageModule.DummyRootNode;
|
||||
import net.sourceforge.pmd.test.schema.RuleTestDescriptor;
|
||||
|
||||
class RuleTstTest {
|
||||
private LanguageVersion dummyLanguage = DummyLanguageModule.getInstance().getDefaultVersion();
|
||||
private final LanguageVersion dummyLanguage = DummyLanguageModule.getInstance().getDefaultVersion();
|
||||
|
||||
private Rule rule = mock(Rule.class);
|
||||
private final Rule rule = mock(Rule.class);
|
||||
|
||||
private RuleTst ruleTester = new RuleTst() {
|
||||
@Override
|
||||
public Rule findRule(String ruleSet, String ruleName) {
|
||||
return rule;
|
||||
}
|
||||
};
|
||||
private final RuleTst ruleTester = spy(RuleTst.class);
|
||||
|
||||
@Test
|
||||
void shouldCallStartAndEnd() {
|
||||
@ -68,16 +64,17 @@ class RuleTstTest {
|
||||
when(rule.getTargetSelector()).thenReturn(RuleTargetSelector.forRootOnly());
|
||||
when(rule.deepCopy()).thenReturn(rule);
|
||||
|
||||
final String code = "the\ncode";
|
||||
final String code = "(a)(b)\n(c)";
|
||||
Mockito.doAnswer(invocation -> {
|
||||
RuleContext context = invocation.getArgument(1, RuleContext.class);
|
||||
DummyRootNode node = invocation.getArgument(0, DummyRootNode.class);
|
||||
|
||||
assertEquals(3, node.getNumChildren());
|
||||
// the violations are reported out of order
|
||||
// line 2
|
||||
context.addViolation(node.newChild().withCoords(TextRegion.fromOffsetLength("the\n".length(), "code".length())));
|
||||
context.addViolation(node.getChild(2));
|
||||
// line 1
|
||||
context.addViolation(node.newChild().withCoords(TextRegion.fromOffsetLength(0, "the".length())));
|
||||
context.addViolation(node.getChild(1));
|
||||
return null;
|
||||
}).when(rule).apply(any(Node.class), Mockito.any(RuleContext.class));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user