Search in sources :

Example 36 with FileContents

use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.

the class XpathQueryGeneratorTest method testConstructorWithTreeWalkerAuditEvent.

@Test
public void testConstructorWithTreeWalkerAuditEvent() {
    final Violation violation = new Violation(12, 1, "messages.properties", null, null, null, null, null, null);
    final TreeWalkerAuditEvent event = new TreeWalkerAuditEvent(new FileContents(fileText), "InputXpathQueryGenerator", violation, rootAst);
    final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(event, DEFAULT_TAB_WIDTH);
    final List<String> actual = queryGenerator.generate();
    final List<String> expected = Arrays.asList("/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathQueryGenerator']]", "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathQueryGenerator']]/MODIFIERS", "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathQueryGenerator']]/MODIFIERS" + "/LITERAL_PUBLIC");
    assertWithMessage("Generated queries do not match expected ones").that(actual).isEqualTo(expected);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Example 37 with FileContents

use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.

the class JavadocMethodCheck method processAST.

/**
 * Called to process an AST when visiting it.
 *
 * @param ast the AST to process. Guaranteed to not be PACKAGE_DEF or
 *             IMPORT tokens.
 */
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private void processAST(DetailAST ast) {
    if (shouldCheck(ast)) {
        final FileContents contents = getFileContents();
        final TextBlock textBlock = contents.getJavadocBefore(ast.getLineNo());
        if (textBlock != null) {
            checkComment(ast, textBlock);
        }
    }
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TextBlock(com.puppycrawl.tools.checkstyle.api.TextBlock)

Example 38 with FileContents

use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.

the class SuppressWithNearbyCommentFilterTest method testAcceptNullViolation.

@Test
public void testAcceptNullViolation() {
    final SuppressWithNearbyCommentFilter filter = new SuppressWithNearbyCommentFilter();
    final FileContents contents = new FileContents(new FileText(new File("filename"), Collections.singletonList("//SUPPRESS CHECKSTYLE ignore")));
    contents.reportSingleLineComment(1, 0);
    final TreeWalkerAuditEvent auditEvent = new TreeWalkerAuditEvent(contents, null, null, null);
    assertWithMessage("Filter should accept null violation").that(filter.accept(auditEvent)).isTrue();
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 39 with FileContents

use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.

the class SuppressWithNearbyCommentFilterTest method testAcceptNullFileContents.

@Test
public void testAcceptNullFileContents() {
    final SuppressWithNearbyCommentFilter filter = new SuppressWithNearbyCommentFilter();
    final FileContents contents = null;
    final TreeWalkerAuditEvent auditEvent = new TreeWalkerAuditEvent(contents, null, new Violation(1, null, null, null, null, Object.class, null), null);
    assertWithMessage("Filter should accept audit event").that(filter.accept(auditEvent)).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Example 40 with FileContents

use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.

the class SuppressionCommentFilterTest method testFindNearestMatchDontAllowSameColumn.

@Test
public void testFindNearestMatchDontAllowSameColumn() {
    final SuppressionCommentFilter suppressionCommentFilter = new SuppressionCommentFilter();
    final FileContents contents = new FileContents(new FileText(new File("filename"), Arrays.asList("//CHECKSTYLE:OFF: ConstantNameCheck", "line2")));
    contents.reportSingleLineComment(1, 0);
    final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, "filename", new Violation(1, null, null, null, null, Object.class, null), null);
    final boolean result = suppressionCommentFilter.accept(dummyEvent);
    assertWithMessage("Filter should not accept event").that(result).isFalse();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)44 FileText (com.puppycrawl.tools.checkstyle.api.FileText)18 File (java.io.File)15 Test (org.junit.jupiter.api.Test)11 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)10 TextBlock (com.puppycrawl.tools.checkstyle.api.TextBlock)9 TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)8 Violation (com.puppycrawl.tools.checkstyle.api.Violation)7 ArrayList (java.util.ArrayList)7 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)6 TypeNameCheck (com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck)3 RecognitionException (antlr.RecognitionException)2 TokenStreamException (antlr.TokenStreamException)2 List (java.util.List)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)1 LineColumn (com.puppycrawl.tools.checkstyle.api.LineColumn)1 LocalizedMessages (com.puppycrawl.tools.checkstyle.api.LocalizedMessages)1 Scope (com.puppycrawl.tools.checkstyle.api.Scope)1 CommentsIndentationCheck (com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck)1