Search in sources :

Example 16 with RuleContext

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;
}
Also used : ParametricRuleViolation(net.sourceforge.pmd.lang.rule.ParametricRuleViolation) FooRule(net.sourceforge.pmd.FooRule) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) HashMap(java.util.HashMap) Node(net.sourceforge.pmd.lang.ast.Node)

Example 17 with RuleContext

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");
}
Also used : ParametricRuleViolation(net.sourceforge.pmd.lang.rule.ParametricRuleViolation) FooRule(net.sourceforge.pmd.FooRule) RuleContext(net.sourceforge.pmd.RuleContext) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode)

Example 18 with RuleContext

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);
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) PMD(net.sourceforge.pmd.PMD) RuleSets(net.sourceforge.pmd.RuleSets) StringReader(java.io.StringReader) PMDException(net.sourceforge.pmd.PMDException) IOException(java.io.IOException) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 19 with RuleContext

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());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) RuleContext(net.sourceforge.pmd.RuleContext) Node(net.sourceforge.pmd.lang.ast.Node) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) Parser(net.sourceforge.pmd.lang.Parser) Test(org.junit.Test)

Example 20 with RuleContext

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));
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) ArrayList(java.util.ArrayList) AbstractEcmascriptRule(net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule) Test(org.junit.Test)

Aggregations

RuleContext (net.sourceforge.pmd.RuleContext)51 Test (org.junit.Test)19 RuleSets (net.sourceforge.pmd.RuleSets)17 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)15 Report (net.sourceforge.pmd.Report)14 Node (net.sourceforge.pmd.lang.ast.Node)13 RuleSet (net.sourceforge.pmd.RuleSet)11 StringReader (java.io.StringReader)9 RuleViolation (net.sourceforge.pmd.RuleViolation)9 IOException (java.io.IOException)8 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)8 PMDException (net.sourceforge.pmd.PMDException)8 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)8 ArrayList (java.util.ArrayList)7 SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)7 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)6 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)6 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)5 FooRule (net.sourceforge.pmd.FooRule)4 Parser (net.sourceforge.pmd.lang.Parser)4