use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class SummaryHTMLRendererTest method createEmptyReportWithSuppression.
private Report createEmptyReportWithSuppression() {
Report rep = new Report();
Map<Integer, String> suppressions = new HashMap<>();
suppressions.put(1, "test");
rep.suppress(suppressions);
RuleContext ctx = new RuleContext();
ParametricRuleViolation<Node> violation = new ParametricRuleViolation<>(new FooRule(), ctx, null, "suppress test");
violation.setLines(1, 1);
rep.addRuleViolation(violation);
return rep;
}
use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class AbstractRendererTst method newRuleViolation.
private RuleViolation newRuleViolation(int endColumn) {
DummyNode node = createNode(endColumn);
RuleContext ctx = new RuleContext();
ctx.setSourceCodeFilename(getSourceCodeFilename());
return new ParametricRuleViolation<Node>(new FooRule(), ctx, node, "blah");
}
use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class RuleTst method runTestFromString.
public void runTestFromString(String code, Rule rule, Report report, LanguageVersion languageVersion, boolean isUseAuxClasspath) {
try {
PMD p = new PMD();
p.getConfiguration().setDefaultLanguageVersion(languageVersion);
p.getConfiguration().setIgnoreIncrementalAnalysis(true);
if (isUseAuxClasspath) {
// configure the "auxclasspath" option for unit testing
p.getConfiguration().prependClasspath(".");
}
RuleContext ctx = new RuleContext();
ctx.setReport(report);
ctx.setSourceCodeFilename("n/a");
ctx.setLanguageVersion(languageVersion);
ctx.setIgnoreExceptions(false);
RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(rule);
p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class StdCyclomaticComplexityRuleTest method entryStackMustBeEmpty.
/**
* Make sure the entry stack is empty, if show classes complexity is
* disabled.
*
* @see <a href="https://sourceforge.net/p/pmd/bugs/1501/">bug #1501</a>
*/
@Test
public void entryStackMustBeEmpty() {
StdCyclomaticComplexityRule rule = new StdCyclomaticComplexityRule();
rule.setProperty(StdCyclomaticComplexityRule.SHOW_CLASSES_COMPLEXITY_DESCRIPTOR, Boolean.FALSE);
RuleContext ctx = new RuleContext();
LanguageVersion javaLanguageVersion = LanguageRegistry.getLanguage(ApexLanguageModule.NAME).getDefaultVersion();
ParserOptions parserOptions = javaLanguageVersion.getLanguageVersionHandler().getDefaultParserOptions();
Parser parser = javaLanguageVersion.getLanguageVersionHandler().getParser(parserOptions);
Node node = parser.parse("test", new StringReader("public class SampleClass {}"));
rule.apply(Arrays.asList(node), ctx);
Assert.assertTrue(rule.entryStack.isEmpty());
}
use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class EcmascriptParserTest method testLineNumbersWithinEcmascriptRules.
/**
* https://sourceforge.net/p/pmd/bugs/1149/
*/
@Test
public void testLineNumbersWithinEcmascriptRules() {
String source = "function f(x){\n" + " if (x) {\n" + " return 1;\n" + " } else {\n" + " return 0;\n" + " }\n" + "}";
final List<String> output = new ArrayList<>();
class MyEcmascriptRule extends AbstractEcmascriptRule {
public Object visit(ASTScope node, Object data) {
output.add("Scope from " + node.getBeginLine() + " to " + node.getEndLine());
return super.visit(node, data);
}
}
MyEcmascriptRule rule = new MyEcmascriptRule();
RuleContext ctx = new RuleContext();
rule.apply(Arrays.asList(parse(source)), ctx);
assertEquals("Scope from 2 to 4", output.get(0));
assertEquals("Scope from 4 to 6", output.get(1));
}
Aggregations