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:
Paul Campbell 2021-08-19 11:47:02 +01:00 committed by GitHub
parent d1f4c61f00
commit 892e3ec9f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 101 additions and 39 deletions

View file

@ -67,8 +67,9 @@ class Err<T> implements Result<T> {
}
@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);
return this;
}
@Override

View file

@ -26,11 +26,12 @@ public class ErrVoid implements ResultVoid {
}
@Override
public void match(
public ResultVoid match(
final Runnable onSuccess,
final Consumer<Throwable> onError
) {
onError.accept(error);
return this;
}
@Override

View file

@ -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 onError the Consumer to pass the error from an error Result to
* @return the original Result
*/
@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.

View file

@ -27,9 +27,10 @@ public interface ResultVoid extends BaseResult {
*
* @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
* @return the original ResultVoid
*/
@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.

View file

@ -62,8 +62,9 @@ class Success<T> implements Result<T> {
}
@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);
return this;
}
@Override

View file

@ -37,8 +37,9 @@ public class SuccessVoid implements ResultVoid {
}
@Override
public void match(final Runnable onSuccess, final Consumer<Throwable> onError) {
public ResultVoid match(final Runnable onSuccess, final Consumer<Throwable> onError) {
onSuccess.run();
return this;
}
@Override

View file

@ -106,17 +106,6 @@ class ResultTest implements WithAssertions {
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
void whenOk_isOkay() {
//when
@ -133,17 +122,6 @@ class ResultTest implements WithAssertions {
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
void whenError_isError() {
//when
@ -160,17 +138,6 @@ class ResultTest implements WithAssertions {
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
void okay_toString() {
//given
@ -245,6 +212,95 @@ class ResultTest implements WithAssertions {
.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
@DisplayName("flatMap")
class FlatMapTests {