From 892e3ec9f5de4a12e258bc9544e87c70f9689e47 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Aug 2021 11:47:02 +0100 Subject: [PATCH] Result.match should return Result/ResultVoid (#225) * ResultTest: group match tests and add missing errorVoid test * Result{Void}.match() returns Result/ResultVoid --- src/main/java/net/kemitix/mon/result/Err.java | 3 +- .../java/net/kemitix/mon/result/ErrVoid.java | 3 +- .../java/net/kemitix/mon/result/Result.java | 3 +- .../net/kemitix/mon/result/ResultVoid.java | 3 +- .../java/net/kemitix/mon/result/Success.java | 3 +- .../net/kemitix/mon/result/SuccessVoid.java | 3 +- src/test/java/net/kemitix/mon/ResultTest.java | 122 +++++++++++++----- 7 files changed, 101 insertions(+), 39 deletions(-) diff --git a/src/main/java/net/kemitix/mon/result/Err.java b/src/main/java/net/kemitix/mon/result/Err.java index 0f9ec45..ca2d8dc 100644 --- a/src/main/java/net/kemitix/mon/result/Err.java +++ b/src/main/java/net/kemitix/mon/result/Err.java @@ -67,8 +67,9 @@ class Err implements Result { } @Override - public void match(final Consumer onSuccess, final Consumer onError) { + public Result match(final Consumer onSuccess, final Consumer onError) { onError.accept(error); + return this; } @Override diff --git a/src/main/java/net/kemitix/mon/result/ErrVoid.java b/src/main/java/net/kemitix/mon/result/ErrVoid.java index fd2da27..c5bff8b 100644 --- a/src/main/java/net/kemitix/mon/result/ErrVoid.java +++ b/src/main/java/net/kemitix/mon/result/ErrVoid.java @@ -26,11 +26,12 @@ public class ErrVoid implements ResultVoid { } @Override - public void match( + public ResultVoid match( final Runnable onSuccess, final Consumer onError ) { onError.accept(error); + return this; } @Override diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index f1111ed..3ecbada 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -657,9 +657,10 @@ public interface Result extends BaseResult, ThrowableFunctor onSuccess, Consumer onError); + Result match(Consumer onSuccess, Consumer onError); /** * Provide the value within the Result, if it is a success, to the Consumer, and returns this Result. diff --git a/src/main/java/net/kemitix/mon/result/ResultVoid.java b/src/main/java/net/kemitix/mon/result/ResultVoid.java index 0bf9259..9f2c503 100644 --- a/src/main/java/net/kemitix/mon/result/ResultVoid.java +++ b/src/main/java/net/kemitix/mon/result/ResultVoid.java @@ -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 onError); + ResultVoid match(Runnable onSuccess, Consumer onError); /** * Attempts to restore an error {@code ResultVoid} to a success. diff --git a/src/main/java/net/kemitix/mon/result/Success.java b/src/main/java/net/kemitix/mon/result/Success.java index 03d9096..afd0dfa 100644 --- a/src/main/java/net/kemitix/mon/result/Success.java +++ b/src/main/java/net/kemitix/mon/result/Success.java @@ -62,8 +62,9 @@ class Success implements Result { } @Override - public void match(final Consumer onSuccess, final Consumer onError) { + public Result match(final Consumer onSuccess, final Consumer onError) { onSuccess.accept(value); + return this; } @Override diff --git a/src/main/java/net/kemitix/mon/result/SuccessVoid.java b/src/main/java/net/kemitix/mon/result/SuccessVoid.java index 11b9598..ec4008b 100644 --- a/src/main/java/net/kemitix/mon/result/SuccessVoid.java +++ b/src/main/java/net/kemitix/mon/result/SuccessVoid.java @@ -37,8 +37,9 @@ public class SuccessVoid implements ResultVoid { } @Override - public void match(final Runnable onSuccess, final Consumer onError) { + public ResultVoid match(final Runnable onSuccess, final Consumer onError) { onSuccess.run(); + return this; } @Override diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index ba4f4c1..6567e60 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -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 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 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 ok = Result.ok("good"); + //then + ok.match( + success -> assertThat(success).isEqualTo("good"), + error -> fail("not an error") + ); + } + + @Test + void whenError_matchError() { + //given + Result 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 ok = Result.ok("good"); + //when + Result result = ok.match(s -> {}, e -> {}); + //then + assertThat(result).isSameAs(ok); + } + + @Test + void whenError_match_returnsSelf() { + //given + final Result error = anError(new Exception("bad")); + //then + Result 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 {