Skip to content

Commit 6043d67

Browse files
Some unit tests and fixes
1 parent 28929ce commit 6043d67

File tree

14 files changed

+877
-15
lines changed

14 files changed

+877
-15
lines changed

ROADMAP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
- [x] Use JSpecify annotations
55
- [x] Regroup all formatting work under single "LineFormatter"
66
- [x] Builder with options (depth symbols, emoji, child limit)
7+
- [x] Unit tests, using @TempDir
78
- [ ] Max depth options
89
- [ ] Better "isLast" detection algorithm
9-
- [ ] Unit tests, using @TempDir
1010
- [ ] Unify dir-in-a-row into a single entry option
1111

1212
## Other ideas

pom.xml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
3+
44
<modelVersion>4.0.0</modelVersion>
5-
5+
66
<groupId>io.github.computerdaddyguy</groupId>
77
<artifactId>jfiletreeprettyprinter</artifactId>
88
<version>0.0.1-SNAPSHOT</version>
@@ -12,8 +12,10 @@
1212
<maven.compiler.source>21</maven.compiler.source>
1313
<maven.compiler.target>21</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
15+
1616
<jspecify.version>1.0.0</jspecify.version>
17+
<junit.version>5.13.4</junit.version>
18+
<assertj.version>3.27.4</assertj.version>
1719
</properties>
1820

1921
<dependencies>
@@ -22,6 +24,19 @@
2224
<artifactId>jspecify</artifactId>
2325
<version>${jspecify.version}</version>
2426
</dependency>
27+
<dependency>
28+
<groupId>org.junit.jupiter</groupId>
29+
<artifactId>junit-jupiter-engine</artifactId>
30+
<version>${junit.version}</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.assertj</groupId>
35+
<artifactId>assertj-core</artifactId>
36+
<version>${assertj.version}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
2540
</dependencies>
2641

2742
</project>

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/visitor/ChildVisitCounter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public boolean exceedsCurrentLimit() {
6060
return records.isEmpty() ? false : records.getLast().exceeds();
6161
}
6262

63+
public boolean hasSomeNotVisitedChildren() {
64+
return records.isEmpty() ? false : records.getLast().notVisited().size() > 0;
65+
}
66+
6367
private class ChildVisitCounterRecord {
6468

6569
private final int maxChildVisitCount;
@@ -78,7 +82,7 @@ void registerChildVisit(Path visited) {
7882
}
7983

8084
boolean exceeds() {
81-
if (maxChildVisitCount < 0) {
85+
if (maxChildVisitCount <= 0) {
8286
return false;
8387
}
8488
return alreadyVisited.size() >= maxChildVisitCount;

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/visitor/DefaultFileTreePrettyPrintVisitor.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ public String getResult() {
3535

3636
@Override
3737
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
38+
// COUNTER
39+
if (counter.exceedsCurrentLimit()) {
40+
return FileVisitResult.SKIP_SIBLINGS;
41+
}
42+
3843
// DEPTH
3944
updateDepth(dir);
4045

4146
// FILE
42-
appendLine(lineRenderer.renderDirectoryBegin(depth, dir, attrs));
47+
appendNewLine(lineRenderer.renderDirectoryBegin(depth, dir, attrs));
4348
depth = depth.append(DepthSymbol.NON_LAST_FILE); // assume not last until proven otherwise
4449

4550
// COUNTER
@@ -61,7 +66,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
6166
updateDepth(file);
6267

6368
// FILE
64-
appendLine(lineRenderer.renderFile(depth, file, attrs));
69+
appendNewLine(lineRenderer.renderFile(depth, file, attrs));
6570

6671
return FileVisitResult.CONTINUE;
6772
}
@@ -73,7 +78,7 @@ public FileVisitResult visitFileFailed(Path file, @Nullable IOException exc) thr
7378

7479
// FILE
7580
if (exc != null) {
76-
appendLine(lineRenderer.renderFileException(depth, file, exc));
81+
appendNewLine(lineRenderer.renderFileException(depth, file, exc));
7782
}
7883

7984
return FileVisitResult.CONTINUE;
@@ -83,17 +88,17 @@ public FileVisitResult visitFileFailed(Path file, @Nullable IOException exc) thr
8388
public FileVisitResult postVisitDirectory(Path dir, @Nullable IOException exc) throws IOException {
8489

8590
if (exc != null) {
86-
appendLine(lineRenderer.renderDirectoryException(depth, dir, exc));
91+
appendNewLine(lineRenderer.renderDirectoryException(depth, dir, exc));
8792
}
8893

8994
// DEPTH
9095
depth = depth.pop();
9196

9297
// COUNTER
93-
var limitReached = counter.exceedsCurrentLimit();
98+
var limitReached = counter.hasSomeNotVisitedChildren();
9499
if (limitReached) {
95100
depth = depth.append(DepthSymbol.LAST_FILE);
96-
appendLine(lineRenderer.renderLimitReached(depth, counter.notVisitedInCurrentDir()));
101+
appendNewLine(lineRenderer.renderLimitReached(depth, counter.notVisitedInCurrentDir()));
97102
depth = depth.pop();
98103
}
99104
counter.exitCurrentDirectory();
@@ -120,9 +125,12 @@ private boolean isLastChild(Path path) {
120125
return siblings != null && siblings[siblings.length - 1].toPath().equals(path);
121126
}
122127

123-
private void appendLine(@Nullable String str) {
128+
private void appendNewLine(@Nullable String str) {
124129
if (str != null) {
125-
buff.append(str).append('\n');
130+
if (buff.length() > 0) {
131+
buff.append('\n');
132+
}
133+
buff.append(str);
126134
}
127135
}
128136

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/visitor/renderer/depth/DefaultDepthFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@NullMarked
1010
class DefaultDepthFormatter implements DepthFormatter {
1111

12-
static final DepthFormatter CLASSIC_ASCII = create("|--", "`--", " ", " ", 0, 1);
12+
static final DepthFormatter CLASSIC_ASCII = create("|--", "`--", "| ", " ", 0, 1);
1313
static final DepthFormatter UNICODE_BOX_DRAWING = create("├─", "└─", "│ ", " ", 0, 1);
1414

1515
private final Function<DepthSymbol, String> printFunction;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package io.github.computerdaddyguy.jfiletreeprettyprinter;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import io.github.computerdaddyguy.jfiletreeprettyprinter.visitor.RenderingOptions.DepthFormat;
6+
import java.nio.file.Path;
7+
import java.util.function.Function;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.io.TempDir;
10+
11+
abstract class AbstractDirectoryPrettyPrintTest {
12+
13+
@TempDir
14+
protected static Path root;
15+
16+
protected static Path targetPath;
17+
18+
protected FileTreePrettyPrinter customize(Function<PrettyPrintOptions, PrettyPrintOptions> customizer) {
19+
var builder = FileTreePrettyPrinter.builder();
20+
builder.customizeOptions(customizer);
21+
return builder.build();
22+
}
23+
24+
protected void run(Function<PrettyPrintOptions, PrettyPrintOptions> customizer, String expected) {
25+
var printer = customize(customizer);
26+
var actual = printer.prettyPrint(targetPath);
27+
assertThat(actual).isEqualTo(expected);
28+
}
29+
30+
// ---------------------------------------------------------------------------------
31+
// DEFAULT
32+
33+
@Test
34+
void defaultOptions() {
35+
run(
36+
options -> options,
37+
defaultOptionsExpected()
38+
);
39+
}
40+
41+
abstract String defaultOptionsExpected();
42+
43+
// ---------------------------------------------------------------------------------
44+
// EMOJI
45+
46+
@Test
47+
void withEmoji() {
48+
run(
49+
options -> options.withEmojis(true),
50+
withEmojiExpected()
51+
);
52+
}
53+
54+
abstract String withEmojiExpected();
55+
56+
// ---------------------------------------------------------------------------------
57+
// DEPTH FORMAT CLASSIC_ASCII
58+
59+
@Test
60+
void withDepthFormatClassicAscii() {
61+
run(
62+
options -> options.withDepthFormat(DepthFormat.CLASSIC_ASCII),
63+
withDepthFormatClassicAsciiExpected()
64+
);
65+
}
66+
67+
abstract String withDepthFormatClassicAsciiExpected();
68+
69+
// ---------------------------------------------------------------------------------
70+
// LIMIT 0
71+
72+
@Test
73+
void withLimit0() {
74+
run(
75+
options -> options.withChildrenLimit(0),
76+
withLimit0Expected()
77+
);
78+
}
79+
80+
abstract String withLimit0Expected();
81+
82+
// ---------------------------------------------------------------------------------
83+
// LIMIT 1
84+
85+
@Test
86+
void withLimit1() {
87+
run(
88+
options -> options.withChildrenLimit(1),
89+
withLimit1Expected()
90+
);
91+
}
92+
93+
abstract String withLimit1Expected();
94+
95+
// ---------------------------------------------------------------------------------
96+
// LIMIT 2
97+
98+
@Test
99+
void withLimit2() {
100+
run(
101+
options -> options.withChildrenLimit(2),
102+
withLimit2Expected()
103+
);
104+
}
105+
106+
abstract String withLimit2Expected();
107+
108+
// ---------------------------------------------------------------------------------
109+
// LIMIT 3
110+
111+
@Test
112+
void withLimit3() {
113+
run(
114+
options -> options.withChildrenLimit(3),
115+
withLimit3Expected()
116+
);
117+
}
118+
119+
abstract String withLimit3Expected();
120+
121+
// ---------------------------------------------------------------------------------
122+
123+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.github.computerdaddyguy.jfiletreeprettyprinter;
2+
3+
import io.github.computerdaddyguy.jfiletreeprettyprinter.util.FileStructureCreator;
4+
import java.io.IOException;
5+
import org.junit.jupiter.api.BeforeAll;
6+
7+
class Directory_1f_Test extends AbstractDirectoryPrettyPrintTest {
8+
9+
@BeforeAll
10+
static void setup() throws IOException {
11+
// @formatter:off
12+
targetPath = FileStructureCreator
13+
.forTargetPath(root)
14+
.createFile("file1")
15+
.end()
16+
.getCurrentDir()
17+
;
18+
// @formatter:on
19+
}
20+
21+
@Override
22+
String defaultOptionsExpected() {
23+
return """
24+
targetPath/
25+
└─ file1""";
26+
}
27+
28+
@Override
29+
String withEmojiExpected() {
30+
return """
31+
📂 targetPath/
32+
└─ file1""";
33+
}
34+
35+
@Override
36+
String withDepthFormatClassicAsciiExpected() {
37+
return """
38+
targetPath/
39+
`-- file1""";
40+
}
41+
42+
@Override
43+
String withLimit0Expected() {
44+
return """
45+
targetPath/
46+
└─ file1""";
47+
}
48+
49+
@Override
50+
String withLimit1Expected() {
51+
return """
52+
targetPath/
53+
└─ file1""";
54+
}
55+
56+
@Override
57+
String withLimit2Expected() {
58+
return """
59+
targetPath/
60+
└─ file1""";
61+
}
62+
63+
@Override
64+
String withLimit3Expected() {
65+
return """
66+
targetPath/
67+
└─ file1""";
68+
}
69+
70+
}

0 commit comments

Comments
 (0)