Skip to content

Commit 98677bc

Browse files
Fix sonar
1 parent 7078f6d commit 98677bc

File tree

2 files changed

+31
-61
lines changed

2 files changed

+31
-61
lines changed

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/renderer/RenderingOptions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.file.Path;
55
import java.util.function.Function;
66
import org.jspecify.annotations.NullMarked;
7+
import org.jspecify.annotations.Nullable;
78

89
@NullMarked
910
public interface RenderingOptions {
@@ -30,6 +31,7 @@ public interface RenderingOptions {
3031
* The line extension function.
3132
* @return
3233
*/
34+
@Nullable
3335
Function<Path, String> getLineExtension();
3436

3537
}

src/test/java/io/github/computerdaddyguy/jfiletreeprettyprinter/LineExtensionTest.java

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,13 @@ void example_dir_match() {
6969
@Test
7070
void compact_dir_first_dir() {
7171

72-
// @formatter:off
73-
var path = FileStructureCreator
74-
.forTargetPath(root)
75-
.createAndEnterDirectory("dirA")
76-
.createAndEnterDirectory("dirB")
77-
.createAndEnterDirectory("dirC")
78-
.createFiles("file", 3)
79-
.up()
80-
.up()
81-
.up()
82-
.createDirectory("dirX")
83-
.getPath();
84-
// @formatter:on
85-
8672
Function<Path, String> lineExtension = p -> {
8773
if (PathUtils.hasName(p, "dirA")) {
8874
return " // 1";
8975
}
9076
return null;
9177
};
9278

93-
var printer = FileTreePrettyPrinter.builder()
94-
.customizeOptions(options -> options.withLineExtension(lineExtension))
95-
.customizeOptions(options -> options.withCompactDirectories(true))
96-
.build();
97-
98-
var result = printer.prettyPrint(path);
9979
var expected = """
10080
targetPath/
10181
├─ dirA/ // 1
@@ -104,39 +84,20 @@ void compact_dir_first_dir() {
10484
│ ├─ file2
10585
│ └─ file3
10686
└─ dirX/""";
107-
assertThat(result).isEqualTo(expected);
87+
88+
compact_dir(lineExtension, expected);
10889
}
10990

11091
@Test
11192
void compact_dir_middle_dir() {
11293

113-
// @formatter:off
114-
var path = FileStructureCreator
115-
.forTargetPath(root)
116-
.createAndEnterDirectory("dirA")
117-
.createAndEnterDirectory("dirB")
118-
.createAndEnterDirectory("dirC")
119-
.createFiles("file", 3)
120-
.up()
121-
.up()
122-
.up()
123-
.createDirectory("dirX")
124-
.getPath();
125-
// @formatter:on
126-
12794
Function<Path, String> lineExtension = p -> {
12895
if (PathUtils.hasName(p, "dirB")) {
12996
return " // 2";
13097
}
13198
return null;
13299
};
133100

134-
var printer = FileTreePrettyPrinter.builder()
135-
.customizeOptions(options -> options.withLineExtension(lineExtension))
136-
.customizeOptions(options -> options.withCompactDirectories(true))
137-
.build();
138-
139-
var result = printer.prettyPrint(path);
140101
var expected = """
141102
targetPath/
142103
├─ dirA/dirB/ // 2
@@ -145,46 +106,53 @@ void compact_dir_middle_dir() {
145106
│ ├─ file2
146107
│ └─ file3
147108
└─ dirX/""";
148-
assertThat(result).isEqualTo(expected);
109+
110+
compact_dir(lineExtension, expected);
149111
}
150112

151113
@Test
152114
void compact_dir_last_dir() {
153115

116+
Function<Path, String> lineExtension = p -> {
117+
if (PathUtils.hasName(p, "dirC")) {
118+
return " // 3";
119+
}
120+
return null;
121+
};
122+
123+
var expected = """
124+
targetPath/
125+
├─ dirA/dirB/dirC/ // 3
126+
│ ├─ file1
127+
│ ├─ file2
128+
│ └─ file3
129+
└─ dirX/""";
130+
131+
compact_dir(lineExtension, expected);
132+
}
133+
134+
private void compact_dir(Function<Path, String> lineExtension, String expected) {
135+
154136
// @formatter:off
155137
var path = FileStructureCreator
156138
.forTargetPath(root)
157139
.createAndEnterDirectory("dirA")
158-
.createAndEnterDirectory("dirB")
159-
.createAndEnterDirectory("dirC")
160-
.createFiles("file", 3)
161-
.up()
162-
.up()
140+
.createAndEnterDirectory("dirB")
141+
.createAndEnterDirectory("dirC")
142+
.createFiles("file", 3)
143+
.up()
144+
.up()
163145
.up()
164146
.createDirectory("dirX")
165147
.getPath();
166148
// @formatter:on
167149

168-
Function<Path, String> lineExtension = p -> {
169-
if (PathUtils.hasName(p, "dirC")) {
170-
return " // 3";
171-
}
172-
return null;
173-
};
174-
175150
var printer = FileTreePrettyPrinter.builder()
176151
.customizeOptions(options -> options.withLineExtension(lineExtension))
177152
.customizeOptions(options -> options.withCompactDirectories(true))
178153
.build();
179154

180155
var result = printer.prettyPrint(path);
181-
var expected = """
182-
targetPath/
183-
├─ dirA/dirB/dirC/ // 3
184-
│ ├─ file1
185-
│ ├─ file2
186-
│ └─ file3
187-
└─ dirX/""";
188156
assertThat(result).isEqualTo(expected);
189157
}
190158

0 commit comments

Comments
 (0)