Search in sources :

Example 1 with ASTStatement

use of net.sourceforge.pmd.lang.apex.ast.ASTStatement in project pmd by pmd.

the class ApexUnitTestClassShouldHaveAssertsRule method checkForAssertStatements.

private Object checkForAssertStatements(ApexNode<?> node, Object data) {
    final List<ASTBlockStatement> blockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
    final List<ASTStatement> statements = new ArrayList<>();
    final List<ASTMethodCallExpression> methodCalls = new ArrayList<>();
    for (ASTBlockStatement blockStatement : blockStatements) {
        statements.addAll(blockStatement.findDescendantsOfType(ASTStatement.class));
        methodCalls.addAll(blockStatement.findDescendantsOfType(ASTMethodCallExpression.class));
    }
    boolean isAssertFound = false;
    for (final ASTMethodCallExpression methodCallExpression : methodCalls) {
        if (ASSERT_METHODS.contains(methodCallExpression.getFullMethodName().toLowerCase(Locale.ROOT))) {
            isAssertFound = true;
            break;
        }
    }
    if (!isAssertFound) {
        addViolation(data, node);
    }
    return data;
}
Also used : ASTBlockStatement(net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement) ArrayList(java.util.ArrayList) ASTStatement(net.sourceforge.pmd.lang.apex.ast.ASTStatement) ASTMethodCallExpression(net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)

Aggregations

ArrayList (java.util.ArrayList)1 ASTBlockStatement (net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement)1 ASTMethodCallExpression (net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)1 ASTStatement (net.sourceforge.pmd.lang.apex.ast.ASTStatement)1