Merge pull request #34 from kemitix/clean-up-result-tests

Clean up Result tests
This commit is contained in:
Paul Campbell 2018-07-09 18:53:49 +01:00 committed by GitHub
commit 3871568ac4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,6 +10,8 @@ import java.io.IOException;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import static org.assertj.core.api.Assumptions.assumeThat;
public class ResultTest implements WithAssertions { public class ResultTest implements WithAssertions {
@Test @Test
@ -25,23 +27,23 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void successHashCode() { public void successHashCodesAreUnique() {
assertThat(Result.ok(1).hashCode()).isNotEqualTo(Result.ok(2).hashCode()); assertThat(Result.ok(1).hashCode()).isNotEqualTo(Result.ok(2).hashCode());
} }
@Test @Test
public void errorHashCode() { public void errorHashCodesAreUnique() {
// despite having 'equivalent' exceptions, the exceptions are distinct instances, so should be considered unique // despite having 'equivalent' exceptions, the exceptions are distinct instances, so should be considered unique
//given
final RuntimeException exception1 = new RuntimeException("message"); final RuntimeException exception1 = new RuntimeException("message");
final RuntimeException exception2 = new RuntimeException("message"); final RuntimeException exception2 = new RuntimeException("message");
assertThat(exception1.hashCode()).isNotEqualTo(exception2.hashCode()); assumeThat(exception1.hashCode()).isNotEqualTo(exception2.hashCode());
final Result<Object> error1 = Result.error(exception1); //then
final Result<Object> error2 = Result.error(exception2); assertThat(Result.error(exception1).hashCode()).isNotEqualTo(Result.error(exception2).hashCode());
assertThat(error1.hashCode()).isNotEqualTo(error2.hashCode());
} }
@Test @Test
public void createSuccess_isSuccess() { public void whenOk_isOkay() {
//when //when
final Result<String> result = Result.ok("good"); final Result<String> result = Result.ok("good");
//then //then
@ -49,7 +51,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void createSuccess_isNotError() { public void whenOkay_isNotError() {
//when //when
final Result<String> result = Result.ok("good"); final Result<String> result = Result.ok("good");
//then //then
@ -57,7 +59,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void createSuccess_matchSuccess() { public void whenOkay_matchSuccess() {
//given //given
final Result<String> result = Result.ok("good"); final Result<String> result = Result.ok("good");
//then //then
@ -68,7 +70,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void createError_isError() { public void whenError_isError() {
//when //when
final Result<String> result = Result.error(new Exception()); final Result<String> result = Result.error(new Exception());
//then //then
@ -76,7 +78,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void createError_isNotSuccess() { public void whenError_isNotSuccess() {
//when //when
final Result<String> result = Result.error(new Exception()); final Result<String> result = Result.error(new Exception());
//then //then
@ -84,7 +86,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void createError_matchError() { public void whenError_matchError() {
//given //given
final Result<Object> result = Result.error(new Exception("bad")); final Result<Object> result = Result.error(new Exception("bad"));
//then //then
@ -95,7 +97,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void successFlatMap_success_isSuccess() { public void okay_whenFlatMapToOkay_isOkay() {
//given //given
final Result<String> result = Result.ok("good"); final Result<String> result = Result.ok("good");
//when //when
@ -109,7 +111,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void successFlatMap_error_isError() { public void okay_whenFlatMapToError_isError() {
//given //given
final Result<String> result = Result.ok("good"); final Result<String> result = Result.ok("good");
//when //when
@ -119,7 +121,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void errorFlatMap_success_isError() { public void error_whenFlatMapToOkay_isError() {
//given //given
final Result<String> result = Result.error(new Exception("bad")); final Result<String> result = Result.error(new Exception("bad"));
//when //when
@ -129,7 +131,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void errorFlatMap_error_isError() { public void error_whenFlatMapToError_isError() {
//given //given
final Result<String> result = Result.error(new Exception("bad")); final Result<String> result = Result.error(new Exception("bad"));
//when //when
@ -139,7 +141,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_whenMap_isSuccess() { public void okay_whenMap_isOkay() {
//given //given
final Result<Integer> okResult = Result.ok(1); final Result<Integer> okResult = Result.ok(1);
//when //when
@ -168,7 +170,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void successMaybe_whenPasses_isSuccessJust() { public void okay_whenMaybe_wherePasses_isOkayJust() {
//given //given
final Result<Integer> okResult = Result.ok(1); final Result<Integer> okResult = Result.ok(1);
//when //when
@ -182,7 +184,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void successMaybe_whenFails_isSuccessNothing() { public void okay_whenMaybe_whereFails_isOkayNothing() {
//given //given
final Result<Integer> okResult = Result.ok(1); final Result<Integer> okResult = Result.ok(1);
//when //when
@ -196,7 +198,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void errorMaybe_whenPasses_isError() { public void error_whenMaybe_wherePasses_isError() {
//given //given
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
final Result<Integer> errorResult = Result.error(exception); final Result<Integer> errorResult = Result.error(exception);
@ -211,7 +213,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void errorMaybe_whenFails_isError() { public void error_whenMaybe_whereFails_isError() {
//given //given
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
final Result<Integer> errorResult = Result.error(exception); final Result<Integer> errorResult = Result.error(exception);
@ -226,7 +228,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void justMaybe_isSuccess() { public void just_whenFromMaybe_isOkay() {
//given //given
final Maybe<Integer> just = Maybe.just(1); final Maybe<Integer> just = Maybe.just(1);
//when //when
@ -240,7 +242,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void nothingMaybe_isError() { public void nothing_whenFromMaybe_isError() {
//given //given
final Maybe<Object> nothing = Maybe.nothing(); final Maybe<Object> nothing = Maybe.nothing();
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
@ -255,7 +257,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_toMaybe_isJust() { public void okay_whenToMaybe_isJust() {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
//when //when
@ -265,7 +267,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void error_toMaybe_isNothing() { public void error_whenToMaybe_isNothing() {
//given //given
final Result<Object> error = Result.error(new RuntimeException()); final Result<Object> error = Result.error(new RuntimeException());
//when //when
@ -275,7 +277,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_whenOrElseThrow_isValue() throws Throwable { public void okay_whenOrElseThrow_isValue() throws Throwable {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
//when //when
@ -290,12 +292,11 @@ public class ResultTest implements WithAssertions {
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
final Result<Integer> error = Result.error(exception); final Result<Integer> error = Result.error(exception);
//when //when
assertThatThrownBy(() -> error.orElseThrow()) assertThatThrownBy(() -> error.orElseThrow()).isSameAs(exception);
.isSameAs(exception);
} }
@Test @Test
public void JustSuccess_invert_thenSuccessJust() { public void justOkay_whenInvert_thenOkayJust() {
//given //given
final Maybe<Result<Integer>> justSuccess = Maybe.just(Result.ok(1)); final Maybe<Result<Integer>> justSuccess = Maybe.just(Result.ok(1));
//when //when
@ -308,7 +309,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void JustError_invert_thenError() { public void JustError_whenInvert_isError() {
//given //given
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
final Maybe<Result<Object>> justError = Maybe.just(Result.error(exception)); final Maybe<Result<Object>> justError = Maybe.just(Result.error(exception));
@ -322,7 +323,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void Nothing_invert_thenSuccessNothing() { public void nothing_whenInvert_thenOkayNothing() {
//given //given
final Maybe<Result<Integer>> nothing = Maybe.nothing(); final Maybe<Result<Integer>> nothing = Maybe.nothing();
//when //when
@ -341,7 +342,6 @@ public class ResultTest implements WithAssertions {
//when //when
final Result<Double> doubleResult = useCase.businessOperation("file a", "file bc"); final Result<Double> doubleResult = useCase.businessOperation("file a", "file bc");
//then //then
assertThat(doubleResult.isOkay()).isTrue();
doubleResult.match( doubleResult.match(
success -> assertThat(success).isEqualTo(7.5), success -> assertThat(success).isEqualTo(7.5),
error -> fail("not an error") error -> fail("not an error")
@ -355,7 +355,6 @@ public class ResultTest implements WithAssertions {
//when //when
final Result<Double> doubleResult = useCase.businessOperation("file def", "file ghij"); final Result<Double> doubleResult = useCase.businessOperation("file def", "file ghij");
//then //then
assertThat(doubleResult.isOkay()).isFalse();
doubleResult.match( doubleResult.match(
success -> fail("not okay"), success -> fail("not okay"),
error -> assertThat(error) error -> assertThat(error)
@ -381,7 +380,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_toString() { public void okay_toString() {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
//when //when
@ -391,7 +390,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void err_toString() { public void error_toString() {
//given //given
final Result<Integer> error = Result.error(new RuntimeException("failed")); final Result<Integer> error = Result.error(new RuntimeException("failed"));
//when //when
@ -401,7 +400,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void resultOf_okay_isOkay() { public void value_whenResultOf_isOkay() {
//given //given
final Callable<String> c = () -> "okay"; final Callable<String> c = () -> "okay";
//when //when
@ -414,7 +413,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void resultOf_error_isError() { public void exception_whenResultOf_isError() {
//given //given
final Callable<String> c = () -> { final Callable<String> c = () -> {
throw new IOException(); throw new IOException();
@ -429,7 +428,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_peek_consumes() { public void okay_whenPeek_isConsumed() {
//given //given
final Result<Integer> result = Result.ok(1); final Result<Integer> result = Result.ok(1);
final AtomicReference<Integer> consumed = new AtomicReference<>(0); final AtomicReference<Integer> consumed = new AtomicReference<>(0);
@ -441,7 +440,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void error_peek_doesNothing() { public void error_whenPeek_isNotConsumed() {
//given //given
final Result<Integer> result = Result.error(new RuntimeException()); final Result<Integer> result = Result.error(new RuntimeException());
final AtomicReference<Integer> consumed = new AtomicReference<>(0); final AtomicReference<Integer> consumed = new AtomicReference<>(0);
@ -453,7 +452,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_whenOnError_thenIgnore() { public void okay_whenOnError_isIgnored() {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
//when //when
@ -461,7 +460,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void error_whenOnError_thenConsume() { public void error_whenOnError_isConsumed() {
//given //given
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
final Result<Integer> error = Result.error(exception); final Result<Integer> error = Result.error(exception);
@ -473,17 +472,17 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_whenRecover_thenNoChange() { public void okay_whenRecover_thenNoChange() {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
//when //when
final Result<Integer> recovered = ok.recover(e -> Result.ok(2)); final Result<Integer> recovered = ok.recover(e -> Result.ok(2));
//then //then
recovered.peek(v -> assertThat(v).isEqualTo(1)); assertThat(recovered).isSameAs(ok);
} }
@Test @Test
public void error_whenRecover_thenSuccess() { public void error_whenRecover_isSuccess() {
//given //given
final Result<Integer> error = Result.error(new RuntimeException()); final Result<Integer> error = Result.error(new RuntimeException());
//when //when
@ -493,7 +492,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void error_whenRecoverFails_thenUpdatedError() { public void error_whenRecover_whereError_isUpdatedError() {
//given //given
final Result<Integer> error = Result.error(new RuntimeException("original")); final Result<Integer> error = Result.error(new RuntimeException("original"));
//when //when
@ -503,7 +502,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_andThenSuccess_thenSuccess() { public void okay_whenAndThen_whereSuccess_isUpdatedSuccess() {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
//when //when
@ -514,7 +513,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_andThenError_thenError() { public void okay_whenAndThen_whereError_isError() {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
@ -528,7 +527,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void error_andThenSuccess_thenError() { public void error_whereAndThen_whereSuccess_isError() {
//given //given
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
final Result<Object> error = Result.error(exception); final Result<Object> error = Result.error(exception);
@ -540,7 +539,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void error_andThenError_thenOriginalError() { public void error_whenAndThen_whereError_isOriginalError() {
//given //given
final RuntimeException exception1 = new RuntimeException(); final RuntimeException exception1 = new RuntimeException();
final Result<Object> error = Result.error(exception1); final Result<Object> error = Result.error(exception1);
@ -554,7 +553,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void success_whenThenWith_whenOkay_thenSuccess() { public void okay_whenThenWith_whereOkay_isOriginalSuccess() {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
//when //when
@ -562,12 +561,11 @@ public class ResultTest implements WithAssertions {
// do something with v // do something with v
}); });
//then //then
assertThat(result.isOkay()).isTrue(); assertThat(result).isSameAs(ok);
result.peek(v -> assertThat(v).isEqualTo(1));
} }
@Test @Test
public void success_whenThenWith_whenError_thenError() { public void okay_whenThenWith_whereError_thenError() {
//given //given
final Result<Integer> ok = Result.ok(1); final Result<Integer> ok = Result.ok(1);
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
@ -581,7 +579,7 @@ public class ResultTest implements WithAssertions {
} }
@Test @Test
public void error_whenThenWith_whenOkay_thenError() { public void error_whenThenWith_whereOkay_thenOriginalError() {
//given //given
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
final Result<Integer> error = Result.error(exception); final Result<Integer> error = Result.error(exception);
@ -590,8 +588,7 @@ public class ResultTest implements WithAssertions {
// do something with v // do something with v
}); });
//then //then
assertThat(result.isError()).isTrue(); assertThat(result).isSameAs(error);
result.onError(e -> assertThat(e).isSameAs(exception));
} }
@Test @Test
@ -604,8 +601,7 @@ public class ResultTest implements WithAssertions {
throw new RuntimeException(); throw new RuntimeException();
}); });
//then //then
assertThat(result.isError()).isTrue(); assertThat(result).isSameAs(error);
result.onError(e -> assertThat(e).isSameAs(exception));
} }
@RequiredArgsConstructor @RequiredArgsConstructor
@ -626,10 +622,11 @@ public class ResultTest implements WithAssertions {
} }
Result<Double> businessOperation(final String fileName1, final String fileName2) { Result<Double> businessOperation(final String fileName1, final String fileName2) {
return readIntFromFile(fileName1).flatMap(intFromFile1 -> return readIntFromFile(fileName1)
adjustValue(intFromFile1).flatMap(adjustedIntFromFile1 -> .andThen(intFromFile1 -> () -> adjustValue(intFromFile1))
readIntFromFile(fileName2).flatMap(intFromFile2 -> .flatMap(adjustedIntFromFile1 -> readIntFromFile(fileName2)
calculateAverage(adjustedIntFromFile1, intFromFile2)))); .flatMap(intFromFile2 -> adjustedIntFromFile1
.flatMap(aif1 -> calculateAverage(aif1, intFromFile2))));
} }
private Result<Integer> readIntFromFile(final String fileName) { private Result<Integer> readIntFromFile(final String fileName) {