1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle;
21
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.stream.Stream;
29
30 import org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.Assumptions;
32 import org.junit.jupiter.params.ParameterizedTest;
33 import org.junit.jupiter.params.provider.MethodSource;
34
35 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
36
37 class IndentationTrailingCommentsVerticalAlignmentTest {
38
39 private static final String INDENTATION_TEST_FILES_PATH =
40 "com/puppycrawl/tools/checkstyle/checks/indentation/indentation";
41
42 private static final int TAB_WIDTH = 4;
43
44 private static final Set<String> ALLOWED_VIOLATION_FILES = Set.of(
45
46 "InputIndentationAnnArrInit.java",
47 "InputIndentationDifficultAnnotations.java",
48 "InputIndentationZeroArrayInit.java",
49 "InputIndentationAnnArrInitWithEmoji.java",
50 "InputIndentationOddLineWrappingAndArrayInit.java",
51 "InputIndentationArrayInitIndentWithEmoji.java",
52 "InputIndentationInvalidArrayInitIndent.java",
53 "InputIndentationInvalidArrayInitIndentTwoDimensional.java",
54 "InputIndentationAnnArrInit2.java",
55 "InputIndentationInvalidArrayInitIndent1.java",
56
57
58 "InputIndentationAnnotationClosingParenthesisEndsInSameIndentationAsOpening.java",
59 "InputIndentationAnnotationFieldDefinition.java",
60 "InputIndentationAnnotationScopeIndentationCheck.java",
61 "InputIndentationAnonymousClassInMethodCurlyOnNewLine.java",
62 "InputIndentationAnonymousClasses.java",
63 "InputIndentationBraceAdjustment.java",
64 "InputIndentationChainedMethodCalls.java",
65 "InputIndentationCtorCall.java",
66 "InputIndentationCustomAnnotation.java",
67 "InputIndentationFromGuava1.java",
68 "InputIndentationIfAndParameter.java",
69 "InputIndentationInvalidArrayInitIndentWithoutTrailingComments.java",
70 "InputIndentationInvalidClassDefIndent.java",
71 "InputIndentationInvalidClassDefIndent1.java",
72 "InputIndentationInvalidForIndent.java",
73 "InputIndentationInvalidIfIndent.java",
74 "InputIndentationInvalidIfIndent2.java",
75 "InputIndentationInvalidLabelIndent.java",
76 "InputIndentationInvalidMethodIndent2.java",
77 "InputIndentationLambda.java",
78 "InputIndentationLambda1.java",
79 "InputIndentationLambda2.java",
80 "InputIndentationLineWrappedRecordDeclaration.java",
81 "InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java",
82 "InputIndentationNewChildren.java",
83 "InputIndentationNewWithForceStrictCondition.java",
84 "InputIndentationStrictCondition.java",
85 "InputIndentationTryResourcesNotStrict.java",
86 "InputIndentationTryResourcesNotStrict1.java",
87 "InputIndentationTryWithResourcesStrict.java",
88 "InputIndentationTryWithResourcesStrict1.java",
89 "InputIndentationValidBlockIndent1.java",
90 "InputIndentationValidClassDefIndent.java",
91 "InputIndentationValidClassDefIndent1.java",
92 "InputIndentationCorrectIfAndParameter1.java",
93 "InputIndentationPackageDeclaration3.java"
94 );
95
96 @ParameterizedTest
97 @MethodSource("indentationTestFiles")
98 public void testTrailingCommentsAlignment(Path testFile) throws IOException {
99 final String fileName = testFile.getFileName().toString();
100 if (ALLOWED_VIOLATION_FILES.contains(fileName)) {
101 Assumptions.assumeTrue(false, "Skipping file: " + fileName);
102 }
103
104 final List<String> lines = Files.readAllLines(testFile);
105 int expectedStartIndex = -1;
106
107 for (int idx = 0; idx < lines.size(); idx++) {
108 final String line = lines.get(idx);
109 final int commentStartIndex = line.indexOf("//indent:");
110 if (commentStartIndex > 0) {
111 final String codePart = line.substring(0, commentStartIndex);
112 if (!codePart.isBlank()) {
113 final int actualStartIndex =
114 CommonUtil.lengthExpandedTabs(line, commentStartIndex, TAB_WIDTH);
115 if (expectedStartIndex == -1) {
116 expectedStartIndex = actualStartIndex;
117 }
118 else {
119 Assertions.assertEquals(expectedStartIndex, actualStartIndex,
120 "Trailing comment alignment mismatch in file: "
121 + testFile + " on line " + (idx + 1));
122 }
123 }
124 }
125 }
126 }
127
128 private static Stream<Path> indentationTestFiles() {
129 final Path resourcesDir = Paths.get("src", "test", "resources");
130 final Path indentationDir = resourcesDir.resolve(INDENTATION_TEST_FILES_PATH);
131
132 Stream<Path> testFiles;
133 try {
134 testFiles = Files.walk(indentationDir)
135 .filter(path -> {
136 final String fileName = path.getFileName().toString();
137 return fileName.startsWith("InputIndentation")
138 && fileName.endsWith(".java");
139 }
140 );
141 }
142 catch (IOException exception) {
143 Assertions.fail("Failed to find indentation test files", exception);
144 testFiles = Stream.empty();
145 }
146 return testFiles;
147 }
148 }