Result.match should return Result/ResultVoid (#225)
* ResultTest: group match tests and add missing errorVoid test * Result{Void}.match() returns Result/ResultVoid
This commit is contained in:
parent
d1f4c61f00
commit
892e3ec9f5
7 changed files with 101 additions and 39 deletions
|
@ -67,8 +67,9 @@ class Err<T> implements Result<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void match(final Consumer<T> onSuccess, final Consumer<Throwable> onError) {
|
public Result<T> match(final Consumer<T> onSuccess, final Consumer<Throwable> onError) {
|
||||||
onError.accept(error);
|
onError.accept(error);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -26,11 +26,12 @@ public class ErrVoid implements ResultVoid {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void match(
|
public ResultVoid match(
|
||||||
final Runnable onSuccess,
|
final Runnable onSuccess,
|
||||||
final Consumer<Throwable> onError
|
final Consumer<Throwable> onError
|
||||||
) {
|
) {
|
||||||
onError.accept(error);
|
onError.accept(error);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -657,9 +657,10 @@ public interface Result<T> extends BaseResult, ThrowableFunctor<T, ThrowableFunc
|
||||||
*
|
*
|
||||||
* @param onSuccess the Consumer to pass the value of a successful Result to
|
* @param onSuccess the Consumer to pass the value of a successful Result to
|
||||||
* @param onError the Consumer to pass the error from an error Result to
|
* @param onError the Consumer to pass the error from an error Result to
|
||||||
|
* @return the original Result
|
||||||
*/
|
*/
|
||||||
@API(status = STABLE)
|
@API(status = STABLE)
|
||||||
void match(Consumer<T> onSuccess, Consumer<Throwable> onError);
|
Result<T> match(Consumer<T> onSuccess, Consumer<Throwable> onError);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide the value within the Result, if it is a success, to the Consumer, and returns this Result.
|
* Provide the value within the Result, if it is a success, to the Consumer, and returns this Result.
|
||||||
|
|
|
@ -27,9 +27,10 @@ public interface ResultVoid extends BaseResult {
|
||||||
*
|
*
|
||||||
* @param onSuccess the Consumer to pass the value of a successful Result to
|
* @param onSuccess the Consumer to pass the value of a successful Result to
|
||||||
* @param onError the Consumer to pass the error from an error Result to
|
* @param onError the Consumer to pass the error from an error Result to
|
||||||
|
* @return the original ResultVoid
|
||||||
*/
|
*/
|
||||||
@API(status = STABLE)
|
@API(status = STABLE)
|
||||||
void match(Runnable onSuccess, Consumer<Throwable> onError);
|
ResultVoid match(Runnable onSuccess, Consumer<Throwable> onError);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to restore an error {@code ResultVoid} to a success.
|
* Attempts to restore an error {@code ResultVoid} to a success.
|
||||||
|
|
|
@ -62,8 +62,9 @@ class Success<T> implements Result<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void match(final Consumer<T> onSuccess, final Consumer<Throwable> onError) {
|
public Result<T> match(final Consumer<T> onSuccess, final Consumer<Throwable> onError) {
|
||||||
onSuccess.accept(value);
|
onSuccess.accept(value);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -37,8 +37,9 @@ public class SuccessVoid implements ResultVoid {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void match(final Runnable onSuccess, final Consumer<Throwable> onError) {
|
public ResultVoid match(final Runnable onSuccess, final Consumer<Throwable> onError) {
|
||||||
onSuccess.run();
|
onSuccess.run();
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -106,17 +106,6 @@ class ResultTest implements WithAssertions {
|
||||||
assertThat(result.isError()).isFalse();
|
assertThat(result.isError()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void whenOkayVoid_match_isNull() {
|
|
||||||
//when
|
|
||||||
var result = Result.ok();
|
|
||||||
//then
|
|
||||||
result.match(
|
|
||||||
() -> assertThat(true).isTrue(),
|
|
||||||
error -> fail("not an error")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenOk_isOkay() {
|
void whenOk_isOkay() {
|
||||||
//when
|
//when
|
||||||
|
@ -133,17 +122,6 @@ class ResultTest implements WithAssertions {
|
||||||
assertThat(result.isError()).isFalse();
|
assertThat(result.isError()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void whenOkay_matchSuccess() {
|
|
||||||
//given
|
|
||||||
final Result<String> result = Result.ok("good");
|
|
||||||
//then
|
|
||||||
result.match(
|
|
||||||
success -> assertThat(success).isEqualTo("good"),
|
|
||||||
error -> fail("not an error")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenError_isError() {
|
void whenError_isError() {
|
||||||
//when
|
//when
|
||||||
|
@ -160,17 +138,6 @@ class ResultTest implements WithAssertions {
|
||||||
assertThat(result.isError()).isTrue();
|
assertThat(result.isError()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void whenError_matchError() {
|
|
||||||
//given
|
|
||||||
final Result<Integer> result = anError(new Exception("bad"));
|
|
||||||
//then
|
|
||||||
result.match(
|
|
||||||
success -> fail("not a success"),
|
|
||||||
error -> assertThat(error.getMessage()).isEqualTo("bad")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void okay_toString() {
|
void okay_toString() {
|
||||||
//given
|
//given
|
||||||
|
@ -245,6 +212,95 @@ class ResultTest implements WithAssertions {
|
||||||
.flatMap(s -> Result.of(() -> {throw e;}));
|
.flatMap(s -> Result.of(() -> {throw e;}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("match")
|
||||||
|
class MatchTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenOkay_matchSuccess() {
|
||||||
|
//given
|
||||||
|
Result<String> ok = Result.ok("good");
|
||||||
|
//then
|
||||||
|
ok.match(
|
||||||
|
success -> assertThat(success).isEqualTo("good"),
|
||||||
|
error -> fail("not an error")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenError_matchError() {
|
||||||
|
//given
|
||||||
|
Result<Integer> error = anError(new Exception("bad"));
|
||||||
|
//then
|
||||||
|
error.match(
|
||||||
|
success -> fail("not a success"),
|
||||||
|
e -> assertThat(e.getMessage()).isEqualTo("bad")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenOkayVoid_matchOkay() {
|
||||||
|
//when
|
||||||
|
var ok = Result.ok();
|
||||||
|
//then
|
||||||
|
ok.match(
|
||||||
|
() -> assertThat(true).isTrue(),
|
||||||
|
error -> fail("not an error")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenErrorVoid_matchError() {
|
||||||
|
//when
|
||||||
|
var error = Result.error(new RuntimeException());
|
||||||
|
//then
|
||||||
|
error.match(
|
||||||
|
() -> fail("not a success"),
|
||||||
|
e -> assertThat(true).isTrue()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenOkay_match_returnsSelf() {
|
||||||
|
//given
|
||||||
|
final Result<String> ok = Result.ok("good");
|
||||||
|
//when
|
||||||
|
Result<String> result = ok.match(s -> {}, e -> {});
|
||||||
|
//then
|
||||||
|
assertThat(result).isSameAs(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenError_match_returnsSelf() {
|
||||||
|
//given
|
||||||
|
final Result<Integer> error = anError(new Exception("bad"));
|
||||||
|
//then
|
||||||
|
Result<Integer> result = error.match(s -> {}, e -> {});
|
||||||
|
//then
|
||||||
|
assertThat(result).isSameAs(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenOkayVoid_match_returnsSelf() {
|
||||||
|
//given
|
||||||
|
ResultVoid ok = Result.ok();
|
||||||
|
//when
|
||||||
|
ResultVoid result = ok.match(() -> {}, e -> {});
|
||||||
|
//then
|
||||||
|
assertThat(result).isSameAs(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenErrorVoid_match_returnsSelf() {
|
||||||
|
//given
|
||||||
|
ResultVoid error = Result.error(new RuntimeException());
|
||||||
|
//when
|
||||||
|
ResultVoid result = error.match(() -> {}, e -> {});
|
||||||
|
//then
|
||||||
|
assertThat(result).isSameAs(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
@DisplayName("flatMap")
|
@DisplayName("flatMap")
|
||||||
class FlatMapTests {
|
class FlatMapTests {
|
||||||
|
|
Loading…
Reference in a new issue