forked from phoedos/pmd
[apex] Support top-level enums in rules
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.Stack;
|
||||
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTMethod;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserClassOrInterface;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserEnum;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserTrigger;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ApexParserVisitorReducedAdapter;
|
||||
|
||||
@@ -41,6 +42,12 @@ public class ApexMultifileVisitor extends ApexParserVisitorReducedAdapter {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object visit(ASTUserEnum node, Object data) {
|
||||
return data; // ignore
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object visit(ASTMethod node, Object data) {
|
||||
stack.peek().addOperation(node.getQualifiedName().getOperation(), node.getSignature());
|
||||
|
||||
@@ -137,12 +137,17 @@ public abstract class AbstractApexRule extends AbstractRule
|
||||
|
||||
protected void visitAll(List<? extends Node> nodes, RuleContext ctx) {
|
||||
for (Object element : nodes) {
|
||||
// all nodes of type ApexRootNode...
|
||||
if (element instanceof ASTUserClass) {
|
||||
visit((ASTUserClass) element, ctx);
|
||||
} else if (element instanceof ASTUserInterface) {
|
||||
visit((ASTUserInterface) element, ctx);
|
||||
} else if (element instanceof ASTUserTrigger) {
|
||||
visit((ASTUserTrigger) element, ctx);
|
||||
} else if (element instanceof ASTUserEnum) {
|
||||
visit((ASTUserEnum) element, ctx);
|
||||
} else if (element instanceof ASTAnonymousClass) {
|
||||
visit((ASTAnonymousClass) element, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.apex.rule;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTAnonymousClass;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserEnum;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserInterface;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserTrigger;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ApexNode;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ApexParserTestBase;
|
||||
|
||||
import apex.jorje.semantic.ast.compilation.Compilation;
|
||||
|
||||
public class AbstractApexRuleTest extends ApexParserTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldVisitTopLevelClass() {
|
||||
run("class Foo { }");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldVisitTopLevelInterface() {
|
||||
run("interface Foo { }");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldVisitTopLevelTrigger() {
|
||||
run("trigger Foo on Account (before insert, before update) { }");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldVisitTopLevelEnum() {
|
||||
run("enum Foo { }");
|
||||
}
|
||||
|
||||
private void run(String code) {
|
||||
ApexNode<Compilation> node = parse(code);
|
||||
RuleContext ctx = new RuleContext();
|
||||
ctx.setLanguageVersion(apex.getDefaultVersion());
|
||||
TopLevelRule rule = new TopLevelRule();
|
||||
rule.apply(Collections.singletonList(node), ctx);
|
||||
assertEquals(1, ctx.getReport().size());
|
||||
}
|
||||
|
||||
private static class TopLevelRule extends AbstractApexRule {
|
||||
@Override
|
||||
public Object visit(ASTUserClass node, Object data) {
|
||||
addViolation(data, node);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTUserInterface node, Object data) {
|
||||
addViolation(data, node);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTUserTrigger node, Object data) {
|
||||
addViolation(data, node);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTUserEnum node, Object data) {
|
||||
addViolation(data, node);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTAnonymousClass node, Object data) {
|
||||
addViolation(data, node);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,14 @@ public class Foo {
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>top-level enum all is well</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public enum FooEnum { }
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>test class default is title case</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
@@ -121,6 +129,17 @@ public class Foo {
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>top-level enum default is title case</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-messages>
|
||||
<message>The enum name 'fooEnum' doesn't match '[A-Z][a-zA-Z0-9_]*'</message>
|
||||
</expected-messages>
|
||||
<code><![CDATA[
|
||||
public enum fooEnum { }
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>custom test class pattern</description>
|
||||
<rule-property name="testClassPattern">[a-zA-Z0-9_]+</rule-property>
|
||||
@@ -157,4 +176,22 @@ public class FOO_CLASS { }
|
||||
public interface FOO_INTERFACE { }
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>custom enum pattern</description>
|
||||
<rule-property name="enumPattern">E[a-zA-Z0-9_]+</rule-property>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public enum EFOO_ENUM { }
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>custom enum pattern</description>
|
||||
<rule-property name="enumPattern">E[a-zA-Z0-9_]+</rule-property>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public enum FooEnum { }
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
|
||||
Reference in New Issue
Block a user