Add toString() method in ArgumentValue

See gh-1196

Signed-off-by: Dennis Griese <d.griese1987@gmail.com>
This commit is contained in:
Dennis
2025-04-23 12:57:43 +02:00
committed by rstoyanchev
parent 46df905acd
commit fbd114bd10
2 changed files with 31 additions and 0 deletions

View File

@@ -137,6 +137,23 @@ public final class ArgumentValue<T> {
return result;
}
/**
* Returns a non-empty string representation of this {@code ArgumentValue}
* suitable for debugging.
*
* @return the string representation of this instance
*/
@Override
public String toString() {
if (this.omitted) {
return "ArgumentValue.omitted";
}
if (this.value == null){
return "ArgumentValue.empty";
}
return "ArgumentValue[%s]".formatted(this.value);
}
/**
* Static factory method for an argument value that was provided, even if

View File

@@ -80,4 +80,18 @@ class ArgumentValueTests {
assertThat(called.get()).isFalse();
}
@Test
void toStringShouldReturnOmittedWhenOmitted() {
assertThat(ArgumentValue.omitted()).hasToString("ArgumentValue.omitted");
}
@Test
void toStringShouldReturnEmptyWhenNull() {
assertThat(ArgumentValue.ofNullable(null)).hasToString("ArgumentValue.empty");
}
@Test
void toStringShouldReturnValueWhenValue() {
assertThat(ArgumentValue.ofNullable("hello")).hasToString("ArgumentValue[hello]");
}
}