Skip to content

Commit 6b6e9a4

Browse files
committed
fix(49): Moved Raise evaluation into the 'assertThat' function
1 parent 9d6d06e commit 6b6e9a4

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

src/main/kotlin/in/rcard/assertj/arrowcore/AbstractRaiseAssert.kt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package `in`.rcard.assertj.arrowcore
22

33
import arrow.core.raise.Raise
44
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeds.Companion.shouldFailButSucceedsWith
5+
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeds.Companion.shouldFailWithButSucceedsWith
56
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailWith.Companion.shouldFailWith
67
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedButFailed.Companion.shouldSucceedButFailed
78
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedWith.Companion.shouldSucceedWith
9+
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedWithButFailed.Companion.shouldSucceedWithButFailed
810
import org.assertj.core.api.AbstractAssert
911
import org.assertj.core.internal.ComparisonStrategy
1012
import org.assertj.core.internal.StandardComparisonStrategy
@@ -33,104 +35,104 @@ abstract class AbstractRaiseAssert<
3335
* Verifies that the function in the [Raise] context succeeds with the given value.
3436
* @param expectedValue the expected value returned by the function.
3537
*/
36-
fun succeedsWith(expectedValue: VALUE) {
38+
fun succeedsWith(expectedValue: VALUE) =
3739
when (actual) {
3840
is RaiseResult.Failure<ERROR> -> {
3941
throwAssertionError(
40-
shouldSucceedButFailed((actual as RaiseResult.Failure<ERROR>).error),
42+
shouldSucceedWithButFailed(expectedValue, (actual as RaiseResult.Failure<ERROR>).error),
4143
)
4244
}
4345

4446
is RaiseResult.FailureWithException -> {
45-
// TODO: Add a specific error message
47+
throw (actual as RaiseResult.FailureWithException).exception
4648
}
4749

4850
is RaiseResult.Success<VALUE> -> {
4951
val actualValue = (actual as RaiseResult.Success<VALUE>).value
5052
if (!comparisonStrategy.areEqual(actualValue, expectedValue)) {
5153
throwAssertionError(shouldSucceedWith(expectedValue, actualValue))
54+
} else {
55+
// Nothing to do
5256
}
5357
}
5458
}
55-
}
5659

5760
/**
5861
* Verifies that the function in the [Raise] context succeeded. No check on the value returned by the function is
5962
* performed.
6063
*
6164
* @see succeedsWith
6265
*/
63-
fun succeeds() {
66+
fun succeeds() =
6467
when (actual) {
6568
is RaiseResult.Failure<ERROR> ->
6669
throwAssertionError(
6770
shouldSucceedButFailed((actual as RaiseResult.Failure<ERROR>).error),
6871
)
6972

7073
is RaiseResult.FailureWithException -> {
71-
// TODO: Add a specific error message
74+
throw (actual as RaiseResult.FailureWithException).exception
7275
}
7376

7477
is RaiseResult.Success<VALUE> -> {
7578
// Nothing to do
7679
}
7780
}
78-
}
7981

8082
/**
8183
* Verifies that the function in the [Raise] context fails with the given error.
8284
* @param expectedError the expected error raised by the function.
8385
*/
84-
fun raises(expectedError: ERROR) {
86+
fun raises(expectedError: ERROR) =
8587
when (actual) {
8688
is RaiseResult.Failure<ERROR> -> {
8789
val actualError = (actual as RaiseResult.Failure<ERROR>).error
8890
if (!comparisonStrategy.areEqual(actualError, expectedError)) {
8991
throwAssertionError(shouldFailWith(expectedError, actualError))
92+
} else {
93+
// Nothing to do
9094
}
9195
}
9296

9397
is RaiseResult.FailureWithException -> {
94-
// TODO: Add a specific error message
98+
throw (actual as RaiseResult.FailureWithException).exception
9599
}
96100

97101
is RaiseResult.Success<VALUE> -> {
98102
throwAssertionError(
99-
shouldFailButSucceedsWith((actual as RaiseResult.Success<VALUE>).value),
103+
shouldFailWithButSucceedsWith(expectedError, (actual as RaiseResult.Success<VALUE>).value),
100104
)
101105
}
102106
}
103-
}
104107

105108
/**
106109
* Verifies that the function in the [Raise] context fails, no matter the type of the logical error.
107110
*
108111
* @see raises
109112
*/
110-
fun fails() {
113+
fun fails() =
111114
when (actual) {
112115
is RaiseResult.Failure<ERROR> -> {
113116
// Nothing to do
114117
}
115118

116119
is RaiseResult.FailureWithException -> {
117-
// TODO: Add a specific error message
120+
throw (actual as RaiseResult.FailureWithException).exception
118121
}
119122

120123
is RaiseResult.Success ->
121124
throwAssertionError(
122125
shouldFailButSucceedsWith((actual as RaiseResult.Success<VALUE>).value),
123126
)
124127
}
125-
}
126128
}
127129

128-
sealed interface RaiseResult<ERROR : Any, VALUE : Any> {
129-
data class Success<VALUE : Any>(
130+
sealed interface RaiseResult<out ERROR : Any, out VALUE : Any> {
131+
data class Success<out VALUE : Any>(
130132
val value: VALUE,
131133
) : RaiseResult<Nothing, VALUE>
132134

133-
data class Failure<ERROR : Any>(
135+
data class Failure<out ERROR : Any>(
134136
val error: ERROR,
135137
) : RaiseResult<ERROR, Nothing>
136138

src/main/kotlin/in/rcard/assertj/arrowcore/RaiseAssert.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RaiseAssert<ERROR : Any, VALUE : Any>(
2525
companion object {
2626
inline fun <ERROR : Any, VALUE : Any> assertThat(
2727
@BuilderInference lambda: Raise<ERROR>.() -> VALUE,
28-
): RaiseAssert<out ERROR, out VALUE> {
28+
): RaiseAssert<ERROR, VALUE> {
2929
val raiseResult =
3030
fold(
3131
block = lambda,
@@ -41,7 +41,7 @@ class RaiseAssert<ERROR : Any, VALUE : Any>(
4141
* @param shouldRaiseThrowable the function to be executed in the [Raise] context.
4242
* @return the [AbstractThrowableAssert] to be used to verify the exception.
4343
*/
44-
fun <ERROR : Any, VALUE : Any> assertThatThrownBy(
44+
inline fun <ERROR : Any, VALUE : Any> assertThatThrownBy(
4545
@BuilderInference shouldRaiseThrowable: Raise<ERROR>.() -> VALUE,
4646
): AbstractThrowableAssert<*, out Throwable> {
4747
val throwable: Throwable? =

src/main/kotlin/in/rcard/assertj/arrowcore/errors/RaiseShouldThrowAnException.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ private const val SHOULD_THROW_AN_EXCEPTION_MESSAGE = "%nExpecting code to throw
1010
* @author Riccardo Cardin
1111
* @since 0.2.0
1212
*/
13-
internal class RaiseShouldThrowAnException private constructor() :
14-
BasicErrorMessageFactory(SHOULD_THROW_AN_EXCEPTION_MESSAGE) {
15-
13+
class RaiseShouldThrowAnException private constructor() : BasicErrorMessageFactory(SHOULD_THROW_AN_EXCEPTION_MESSAGE) {
1614
companion object {
17-
internal fun shouldThrowAnException(): RaiseShouldThrowAnException =
18-
RaiseShouldThrowAnException()
15+
fun shouldThrowAnException(): RaiseShouldThrowAnException = RaiseShouldThrowAnException()
1916
}
20-
}
17+
}

src/test/kotlin/in/rcard/assertj/arrowcore/Dummy.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ internal object Dummy {
1010
fun Raise<String>.aFunctionThatThrowsAnException(): Int = throw RuntimeException("AN EXCEPTION")
1111

1212
suspend fun Raise<String>.aSuspendFunctionWithContext(input: Int): Int = input
13+
14+
suspend fun Raise<String>.aSuspendFunctionThatThrowsAnException(): Int = throw RuntimeException("AN EXCEPTION")
1315
}

src/test/kotlin/in/rcard/assertj/arrowcore/RaiseAssert_assertThatThrownBy_Test.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package `in`.rcard.assertj.arrowcore
33
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionThatRaisesAnError
44
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionThatThrowsAnException
55
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionWithContext
6+
import `in`.rcard.assertj.arrowcore.Dummy.aSuspendFunctionThatThrowsAnException
67
import `in`.rcard.assertj.arrowcore.RaiseAssert.Companion.assertThatThrownBy
78
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldThrowAnException.Companion.shouldThrowAnException
9+
import kotlinx.coroutines.test.runTest
810
import org.assertj.core.api.Assertions
911
import org.junit.jupiter.api.Test
1012

@@ -16,6 +18,14 @@ internal class RaiseAssert_assertThatThrownBy_Test {
1618
.hasMessage("AN EXCEPTION")
1719
}
1820

21+
@Test
22+
internal fun `should succeed if the suspending lambda throws an exception`() =
23+
runTest {
24+
assertThatThrownBy { aSuspendFunctionThatThrowsAnException() }
25+
.isInstanceOf(RuntimeException::class.java)
26+
.hasMessage("AN EXCEPTION")
27+
}
28+
1929
@Test
2030
internal fun `should fail if the lambda succeeds with a value`() {
2131
Assertions

src/test/kotlin/in/rcard/assertj/arrowcore/RaiseAssert_assertThat_Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class RaiseAssert_assertThat_Test {
1414
}
1515

1616
@Test
17-
fun `should create an assertion instance for suspending lambdas`() =
17+
internal fun `should create an assertion instance for suspending lambdas`() =
1818
runTest {
1919
val assertion = RaiseAssert.assertThat { aSuspendFunctionWithContext(42) }
2020
then(assertion).isNotNull.isInstanceOf(RaiseAssert::class.java)

0 commit comments

Comments
 (0)