Search in sources :

Example 41 with FileText

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

the class XpathQueryGeneratorTest method testTextBlocksWithNewLine.

@Test
public void testTextBlocksWithNewLine() throws Exception {
    final File testFile = new File(getNonCompilablePath("InputXpathQueryGeneratorTextBlockNewLine.java"));
    final FileText testFileText = new FileText(testFile, StandardCharsets.UTF_8.name());
    final DetailAST detailAst = JavaParser.parseFile(testFile, JavaParser.Options.WITHOUT_COMMENTS);
    final int tabWidth = 8;
    final int lineNumber = 6;
    final int columnNumber = 25;
    final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(detailAst, lineNumber, columnNumber, testFileText, tabWidth);
    final List<String> actual = queryGenerator.generate();
    final List<String> expected = Collections.singletonList("/COMPILATION_UNIT/CLASS_DEF" + "[./IDENT[@text='InputXpathQueryGeneratorTextBlockNewLine']]/OBJBLOCK/" + "VARIABLE_DEF[./IDENT[@text='testOne']]/ASSIGN/EXPR/" + "TEXT_BLOCK_LITERAL_BEGIN/TEXT_BLOCK_CONTENT[@text='\\n        " + "&amp;1line\\n\\n        &gt;2line\\n        &lt;3line\\n        ']");
    assertWithMessage("Generated queries do not match expected ones").that(expected).isEqualTo(actual);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 42 with FileText

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

the class XpathQueryGeneratorTest method testTabWidthBeforeMethodDef.

@Test
public void testTabWidthBeforeMethodDef() throws Exception {
    final File testFile = new File(getPath("InputXpathQueryGeneratorTabWidth.java"));
    final FileText testFileText = new FileText(testFile, StandardCharsets.UTF_8.name());
    final DetailAST detailAst = JavaParser.parseFile(testFile, JavaParser.Options.WITHOUT_COMMENTS);
    final int lineNumber = 4;
    final int columnNumber = 13;
    final int tabWidth = 4;
    final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(detailAst, lineNumber, columnNumber, testFileText, tabWidth);
    final List<String> actual = queryGenerator.generate();
    final List<String> expected = Arrays.asList("/COMPILATION_UNIT/CLASS_DEF" + "[./IDENT[@text='InputXpathQueryGeneratorTabWidth']]/OBJBLOCK" + "/METHOD_DEF[./IDENT[@text='toString']]", "/COMPILATION_UNIT/CLASS_DEF" + "[./IDENT[@text='InputXpathQueryGeneratorTabWidth']]/OBJBLOCK" + "/METHOD_DEF[./IDENT[@text='toString']]/MODIFIERS", "/COMPILATION_UNIT/CLASS_DEF" + "[./IDENT[@text='InputXpathQueryGeneratorTabWidth']]/OBJBLOCK" + "/METHOD_DEF[./IDENT[@text='toString']]/MODIFIERS/LITERAL_PUBLIC");
    assertWithMessage("Generated queries do not match expected ones").that(actual).isEqualTo(expected);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 43 with FileText

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

the class SuppressWithPlainTextCommentFilter method accept.

@Override
public boolean accept(AuditEvent event) {
    boolean accepted = true;
    if (event.getViolation() != null) {
        final FileText fileText = getFileText(event.getFileName());
        if (fileText != null) {
            final List<Suppression> suppressions = getSuppressions(fileText);
            accepted = getNearestSuppression(suppressions, event) == null;
        }
    }
    return accepted;
}
Also used : FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Example 44 with FileText

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

the class AstTreeStringPrinterTest method testPrintAst.

@Test
public void testPrintAst() throws Exception {
    final FileText text = new FileText(new File(getPath("InputAstTreeStringPrinterComments.java")).getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
    final String actual = toLfLineEnding(AstTreeStringPrinter.printAst(text, JavaParser.Options.WITHOUT_COMMENTS));
    final String expected = toLfLineEnding(Files.readString(Paths.get(getPath("ExpectedAstTreeStringPrinter.txt"))));
    assertWithMessage("Print AST output is invalid").that(actual).isEqualTo(expected);
}
Also used : FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 45 with FileText

use of com.puppycrawl.tools.checkstyle.api.FileText 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)

Aggregations

FileText (com.puppycrawl.tools.checkstyle.api.FileText)49 File (java.io.File)41 Test (org.junit.jupiter.api.Test)29 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)18 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)15 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)7 Violation (com.puppycrawl.tools.checkstyle.api.Violation)7 ArrayList (java.util.ArrayList)6 TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)5 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)4 TypeNameCheck (com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 TestLoggingReporter (com.puppycrawl.tools.checkstyle.internal.testmodules.TestLoggingReporter)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 RecognitionException (antlr.RecognitionException)1 TokenStreamException (antlr.TokenStreamException)1 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)1 ClassPath (com.google.common.reflect.ClassPath)1 LineColumn (com.puppycrawl.tools.checkstyle.api.LineColumn)1