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 static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.stream.Stream;
30
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 "InputIndentationInvalidLabelIndent.java",
47 "InputIndentationInvalidMethodIndent2.java",
48 "InputIndentationLambda2.java",
49 "InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java",
50 "InputIndentationNewChildren.java",
51 "InputIndentationNewWithForceStrictCondition.java",
52 "InputIndentationStrictCondition.java",
53 "InputIndentationTryResourcesNotStrict.java",
54 "InputIndentationTryResourcesNotStrict1.java",
55 "InputIndentationTryWithResourcesStrict.java",
56 "InputIndentationTryWithResourcesStrict1.java",
57 "InputIndentationValidClassDefIndent.java",
58 "InputIndentationValidClassDefIndent1.java",
59 "InputIndentationCorrectIfAndParameter1.java",
60 "InputIndentationPackageDeclaration3.java"
61 );
62
63 @MethodSource("indentationTestFiles")
64 @ParameterizedTest
65 public void testTrailingCommentsAlignment(Path testFile) throws IOException {
66 final String fileName = testFile.getFileName().toString();
67 if (ALLOWED_VIOLATION_FILES.contains(fileName)) {
68 Assumptions.assumeTrue(false, "Skipping file: " + fileName);
69 }
70
71 final List<String> lines = Files.readAllLines(testFile);
72 int expectedStartIndex = -1;
73
74 for (int idx = 0; idx < lines.size(); idx++) {
75 final String line = lines.get(idx);
76 if (line.trim().startsWith("import ") || line.trim().startsWith("package ")) {
77 continue;
78 }
79 final int commentStartIndex = line.indexOf("//indent:");
80 if (commentStartIndex > 0) {
81 final String codePart = line.substring(0, commentStartIndex);
82 if (!codePart.isBlank()) {
83 int actualStartIndex =
84 CommonUtil.lengthExpandedTabs(line, commentStartIndex, TAB_WIDTH);
85
86
87 final long extraWidth = codePart.codePoints().filter(
88 Character::isSupplementaryCodePoint).count();
89 actualStartIndex -= Math.toIntExact(extraWidth);
90
91 if (expectedStartIndex == -1) {
92 expectedStartIndex = actualStartIndex;
93 }
94 else {
95 assertWithMessage("Trailing comment alignment mismatch in file: "
96 + testFile + " on line " + (idx + 1))
97 .that(actualStartIndex)
98 .isEqualTo(expectedStartIndex);
99 }
100 }
101 }
102 }
103 }
104
105 private static Stream<Path> indentationTestFiles() {
106 final Path resourcesDir = Path.of("src", "test", "resources");
107 final Path indentationDir = resourcesDir.resolve(INDENTATION_TEST_FILES_PATH);
108 Stream<Path> result;
109 try (Stream<Path> testFiles = Files.walk(indentationDir)) {
110 final List<Path> collected = testFiles
111 .filter(path -> {
112 final String fileName = path.getFileName().toString();
113 return fileName.startsWith("InputIndentation")
114 && fileName.endsWith(".java");
115 }).toList();
116 result = collected.stream();
117 }
118 catch (IOException exception) {
119 assertWithMessage("Failed to find indentation test files")
120 .that(exception)
121 .isNull();
122 result = Stream.empty();
123 }
124 return result;
125 }
126 }