Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ public int hashCode() {
return Objects.hash(this.value, this.exception);
}

@Override
public String toString() {
if (this.value != null) {
return "Exceptional[" + this.value + "]";
} else if (this.exception != null) {
return "Exceptional[" + this.exception + "]";
} else {
return "Exceptional.empty";
}
}

/**
* Obtains the empty {@link Exceptional}, without a value and a {@link Exception}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,4 +787,19 @@ void shouldSeeCausingExceptionInExceptional() {
.isEqualTo("Not supported mate");
}
}

/**
* Ensure {@link Exceptional#toString()} renders the value, exception, or empty state.
*/
@Test
void shouldRenderToString() {
assertThat(Exceptional.of(42).toString())
.isEqualTo("Exceptional[42]");

assertThat(Exceptional.ofException(new IllegalStateException("boom")).toString())
.isEqualTo("Exceptional[java.lang.IllegalStateException: boom]");

assertThat(Exceptional.empty().toString())
.isEqualTo("Exceptional.empty");
}
}
2 changes: 1 addition & 1 deletion base-io/src/main/java/build/base/io/PathSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public Iterator<Path> iterator() {

@Override
public String toString() {
return "PathSet.empty()";
return "[]";
}
}
}
5 changes: 5 additions & 0 deletions base-io/src/main/java/build/base/io/PathSetBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,10 @@ public boolean equals(final Object object) {
public int hashCode() {
return Objects.hash(this.paths);
}

@Override
public String toString() {
return this.paths.toString();
}
}
}
38 changes: 38 additions & 0 deletions base-io/src/test/java/build/base/io/PathSetTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,42 @@ void shouldComparePathSetsWithEquals() {
assertThat(pathSet1.hashCode())
.isEqualTo(pathSet2.hashCode());
}

/**
* Ensure {@link PathSet#toString()} renders the contained {@link Path}s.
*/
@Test
void shouldRenderToString() {
final var first = Paths.get("/usr/local/bin");
final var second = Paths.get("/usr/bin");

final var pathSet = PathSetBuilder
.create(first, second)
.build();

assertThat(pathSet.toString())
.isEqualTo(Stream.of(first, second).map(Path::toString).toList().toString());
}

/**
* Ensure an empty {@link PathSet#toString()} renders an empty list.
*/
@Test
void shouldRenderEmptyToString() {
final var pathSet = PathSetBuilder
.create()
.build();

assertThat(pathSet.toString())
.isEqualTo("[]");
}

/**
* Ensure {@link PathSet#empty()} renders the same as an empty built {@link PathSet}.
*/
@Test
void shouldRenderEmptyPathSetToString() {
assertThat(PathSet.empty().toString())
.isEqualTo("[]");
}
}
Loading