Skip to content

Commit

Permalink
Embrace Given-When-Then test naming (#40)
Browse files Browse the repository at this point in the history
Signed-off-by: David Greven <[email protected]>
  • Loading branch information
grevend authored Sep 1, 2022
1 parent 6927609 commit 1f46104
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class CsFailureReasonTest {
@Test
void toFailure() {
void whenCallingToFailureOnCsFailureReasonThenReturnReasonWrappedInFailureInstance() {
assertThat(new TestReason().toFailure()).isFailure();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,203 +29,204 @@ class FailureTest {
private final Result<Integer, Integer> result = new Failure<>(reason);

@Test
void isFailure() {
void givenFailureResultWhenCallingIsFailureOnResultThenReturnTrue() {
assertThat(result).isInstanceOf(Result.class);
assertThat(result.isFailure()).isTrue();
assertThat(result).isFailure();
assertThat(result.isSuccess()).isFalse();
}

@Test
void reason() {
void givenFailureResultWhenCallingReasonOnResultThenReturnExpectedReason() {
assertThat(result).hasReason(reason);
}

@Test
void map() {
void givenFailureResultWhenCallingMapOnResultThenReturnMappedResult() {
assertThat(result.map(v -> v * 2)).hasReason(reason);
}

@Test
void mapNullPointerException() {
void givenMappingFunctionIsNullWhenCallingMapOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.map(null))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void mapAndTransformType() {
void givenFailureResultWhenCallingMapOnResultThenReturnMappedResultWithTransformedType() {
assertThat(result.map(v -> String.format("%s * 2 -> %s", v, v * 2)))
.hasReason(reason);
}

@Test
void mapFailure() {
void givenFailureResultWhenCallingMapFailureOnResultThenReturnResultWithMappedFailureReason() {
assertThat(result.mapFailure(r -> r * 2)).hasReason(reason * 2);
}

@Test
void mapFailureNullPointerException() {
void givenMappingFunctionIsNullWhenCallingMapFailureOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.mapFailure(null))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void tryMap() {
void givenFailureResultWhenCallingTryMapOnResultThenReturnMappedResult() {
assertThat(result.tryMap(v -> v * 2, reason * 2)).hasReason(reason);
}

@Test
void tryMapNullPointerException() {
void givenMappingFunctionsIsNullWhenCallingTryMapOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.tryMap(null, reason * 2))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void mapFailureAndTransformType() {
void givenFailureResultWhenCallingMapFailureOnResultThenReturnResultWithMappedFailureReasonAndTransformedType() {
assertThat(result.mapFailure(r -> String.format("%s * 2 -> %s", r, r * 2)))
.hasReason("21 * 2 -> 42");
}

@Test
void tryMapAndTransformType() {
void givenFailureResultWhenCallingTryMapOnResultThenReturnMappedResultAndTransformedType() {
assertThat(result.tryMap(v -> String.format("%s * 2 -> %s", v, v * 2),
reason * 2)).hasReason(reason);
}

@Test
void tryMapWithException() {
void givenThrowingMappingFunctionWhenCallingTryMapOnResultThenReturnFailureResult() {
assertThat(result.tryMap(v -> {
throw new IOException("-11");
}, reason * 2)).hasReason(reason);
}

@Test
void peek() {
void givenFailureResultWhenCallingPeekOnResultThenKeepSuccessValueOutput() {
AtomicInteger output = new AtomicInteger(-1);
result.peek(output::set);
assertThat(output).hasValue(-1);
}

@Test
void peekNullPointerException() {
void givenOutputFunctionIsNullWhenCallingPeekOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.peek(null))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void tryPeek() {
void givenFailureResultWhenCallingTryPeekOnResultThenKeepSuccessValueOutput() {
AtomicInteger output = new AtomicInteger(-1);
result.tryPeek(output::set, reason * 2);
assertThat(output).hasValue(-1);
}

@Test
void tryPeekWithException() {
void givenThrowingOutputFunctionWhenCallingTryPeekOnResultThenReturnFailureResult() {
assertThat(result.tryPeek(v -> {
throw new IOException("-11");
}, reason * 2)).hasReason(reason);
}

@Test
void tryPeekNullPointerException() {
void givenOutputFunctionIsNullWhenCallingTryPeekOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.tryPeek(null, reason * 2))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void recover() {
void givenFailureResultWhenCallingRecoverOnResultThenReturnSuccessResult() {
assertThat(result.recover(r -> r * 2)).hasValue(42);
}

@Test
void recoverNullPointerException() {
void givenRecoveryFunctionIsNullWhenCallingRecoverOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.recover(null))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void flatMap() {
void givenFailureResultWhenCallingFlatMapOnResultThenReturnCurrentFailureResult() {
assertThat(result.flatMap(v -> new Success<>(v * 2))).hasReason(reason);
assertThat(result.flatMap(v -> new Failure<>(v * 2))).hasReason(reason);
}

@Test
void flatMapNullPointerException() {
void givenMappingFunctionWhenCallingFlatMapOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.flatMap(null))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void unsafeFlattenNestedFailure() {
void givenOutermostNestedFailureResultWhenCallingUnsafeFlattenOnResultThenReturnFlattenedResult() {
int reason = 21;
Result<Integer, Result<Integer, Integer>> res = new Failure<>(new Failure<>(reason));
assertThat(res.unsafeFlatten()).hasReason(reason);
}

@Test
void unsafeFlattenSuccessNestedInFailure() {
void givenOutermostFailureResultWithNestedSuccessResultWhenCallingUnsafeFlattenOnResultThenReturnFlattenedResult() {
int value = 12;
Result<Integer, Result<Integer, Integer>> res = new Failure<>(new Success<>(value));
assertThat(res.unsafeFlatten()).hasValue(value);
}

@Test
void unsafeFlattenNonNestedFailure() {
void givenNonNestedFailureResultWhenCallingUnsafeFlattenOnResultThenReturnNonNestedFailureResult() {
int reason = 21;
Result<Integer, Integer> res = new Failure<>(reason);
assertThat(res.unsafeFlatten()).hasReason(reason);
}

@Test
void fold() {
void givenFailureResultWhenCallingFoldOnResultThenReturnExpectedValue() {
assertThat(result.<Integer>fold(v -> v * 2, r -> r + 9)).isEqualTo(30);
}

@ParameterizedTest
@NullableParamSource("FOLD")
void foldNullPointerException(Function<? super Integer, ? super Integer> successFunction,
Function<? super Integer, ? super Integer> failureFunction) {
void givenFoldingFunctionsAreNullWhenCallingFoldOnResultThenThrowNullPointerException(
Function<? super Integer, ? super Integer> successFunction,
Function<? super Integer, ? super Integer> failureFunction) {
assertThatThrownBy(() -> result.fold(successFunction, failureFunction))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void filter() {
void givenFailureResultWhenCallingFilterOnResultThenReturnCurrentFailureResult() {
assertThat(result.filter(Predicate.isEqual(12), 22)).hasReason(reason);
}

@Test
void filterNullPointerException() {
void givenPredicateIsNullWhenCallingFilterOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.filter(null, null))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void or() {
void givenFailureResultWhenCallingOrOnResultThenReturnSuccessResult() {
int value = 12;
assertThat(result.or(() -> new Success<>(value))).hasValue(value);
}

@Test
void orNullPointerException() {
void givenResultSupplierIsNullWhenCallingOrOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> result.or(null))
.isExactlyInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> result.or(() -> null))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void swap() {
void givenFailureResultWhenCallingSwapOnResultThenReturnSuccessResult() {
assertThat(result.swap()).hasValue(reason);
}

@Test
void toOptional() {
void givenFailureResultWhenCallingToOptionalOnResultThenReturnEmptyOptional() {
assertThat(result.toOptional()).isEmpty();
}

@Test
void stream() {
void givenFailureResultWhenCallingStreamOnResultThenReturnEmptyStream() {
assertThat(result.stream()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class ResultTest {
() -> 12, Collections.emptyMap());

@Test
void of() {
void givenSuccessValueWhenCallingOfOnResultThenReturnSuccessResult() {
int value = 12;
Result<Integer, String> result = Result.of(() -> value, "some");
assertThat(result).hasValue(value);
}

@Test
void ofThrowsToFailure() {
void givenThrowingValueSupplierWhenCallingOfOnResultThenReturnFailureResult() {
String reason = "some";
Result<Integer, String> result = Result.of(() -> {
throw new IOException();
Expand All @@ -46,20 +46,21 @@ void ofThrowsToFailure() {

@ParameterizedTest
@NullableParamSource("OF")
void ofNullPointerException(AnyThrowingSupplier<Integer> supplier, String reason) {
void givenValueSuppliersAndReasonsAreNullWhenCallingOfOnResultThenThrowNullPointerException(
AnyThrowingSupplier<Integer> supplier, String reason) {
assertThatThrownBy(() -> Result.of(supplier, reason))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void ofWithCsFailureReason() {
void givenFailureReasonTypeWhenCallingOfOnResultThenReturnSuccessResult() {
int value = 12;
Result<Integer, ? extends Throwable> result = Result.of(() -> value);
assertThat(result).hasValue(value);
}

@Test
void ofWithCsFailureReasonToFailure() {
void givenThrowingValueSupplierWithFailureReasonTypeWhenCallingOfOnResultThenReturnFailureResult() {
FailureException reason = new FailureException();
Result<Object, FailureException> result = Result.of(() -> {
throw reason;
Expand All @@ -68,21 +69,21 @@ void ofWithCsFailureReasonToFailure() {
}

@Test
void ofWithCsFailureReasonNullPointerException() {
void givenValueSupplierIsNullWhenCallingOfOnResultThenThrowNullPointerException() {
assertThatThrownBy(() -> Result.of(null))
.isExactlyInstanceOf(NullPointerException.class);
}

@Test
void ofWithMappedExceptions() {
void givenEmptyExceptionMappingWhenCallingOfOnResultThenReturnSuccessResult() {
int value = 12;
Result<Integer, FailureException> result = Result.of(() -> value, Collections.emptyMap(),
new FailureException());
assertThat(result).hasValue(value);
}

@Test
void ofWithNoMappedExceptions() {
void givenThrowingValueSupplierAndEmptyExceptionMappingWhenCallingOfOnResultThenReturnFailureResult() {
FailureException exception = new FailureException();
Result<Integer, FailureException> result = Result.of(() -> {
throw new IOException();
Expand All @@ -91,7 +92,7 @@ void ofWithNoMappedExceptions() {
}

@Test
void ofWithMappedExactExceptionToFailure() {
void givenThrowingValueSupplierAndExactExceptionMappingWhenCallingOfOnResultThenReturnMappedFailureResult() {
FailureException reason = new FailureException("IO", "IOException");
Map<Class<? extends Throwable>, FailureException> reasons = new HashMap<>();
reasons.put(IOException.class, reason);
Expand All @@ -102,7 +103,7 @@ void ofWithMappedExactExceptionToFailure() {
}

@Test
void ofWithMappedCauseExceptionToFailure() {
void givenThrowingValueSupplierAndCauseExceptionMappingWhenCallingOfOnResultThenReturnMappedFailureResult() {
FailureException reason = new FailureException("IO", "IOException");
Map<Class<? extends Throwable>, FailureException> reasons = new HashMap<>();
reasons.put(IllegalArgumentException.class, reason);
Expand All @@ -113,7 +114,7 @@ void ofWithMappedCauseExceptionToFailure() {
}

@Test
void ofWithMappedAssignableExceptionToFailure() {
void givenThrowingValueSupplierAndAssignableExceptionMappingWhenCallingOfOnResultThenReturnMappedFailureResult() {
FailureException reason = new FailureException("IO", "IOException");
Map<Class<? extends Throwable>, FailureException> reasons = new HashMap<>();
reasons.put(Throwable.class, reason);
Expand All @@ -124,7 +125,7 @@ void ofWithMappedAssignableExceptionToFailure() {
}

@Test
void ofWithMappedExceptionsMissingEntry() {
void givenThrowingValueSupplierAndMissingExceptionMappingEntryWhenCallingOfOnResultThenReturnMappedFailureResult() {
FailureException exception = new FailureException();
Map<Class<? extends Throwable>, FailureException> reasons = new HashMap<>();
reasons.put(NumberFormatException.class, new FailureException("IO", "IOException"));
Expand All @@ -136,7 +137,8 @@ void ofWithMappedExceptionsMissingEntry() {

@ParameterizedTest
@NullableParamSource("OF_WITH_MAPPED_EXCEPTIONS")
void ofWithMappedExceptionsNullPointerException(AnyThrowingSupplier<Integer> supplier, Map<Class<? extends Throwable>, FailureException> reasons) {
void givenValueSuppliersAndExceptionMappingsAreNullWhenCallingOfOnResultThenThrowNullPointerException(
AnyThrowingSupplier<Integer> supplier, Map<Class<? extends Throwable>, FailureException> reasons) {
assertThatThrownBy(() -> Result.of(supplier, reasons, new FailureException()));
}

Expand Down
Loading

0 comments on commit 1f46104

Please sign in to comment.