Skip to content

Commit 1933f7e

Browse files
authored
Merge pull request #59 from rcardin/58-add-methods-to-extract-the-result-and-the-error-of-the-execution-of-a-raisee-a-function
feat(58): Added methods to extract the result and the error of the execution of a function in the `Raise<E>` context.
2 parents f207d37 + 568da03 commit 1933f7e

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedButFailed.Companion
88
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedWith.Companion.shouldSucceedWith
99
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedWithButFailed.Companion.shouldSucceedWithButFailed
1010
import org.assertj.core.api.AbstractAssert
11+
import org.assertj.core.api.AbstractObjectAssert
12+
import org.assertj.core.api.Assertions
1113
import org.assertj.core.internal.ComparisonStrategy
1214
import org.assertj.core.internal.StandardComparisonStrategy
1315

@@ -125,6 +127,32 @@ abstract class AbstractRaiseAssert<
125127
shouldFailButSucceedsWith((actual as RaiseResult.Success<VALUE>).value),
126128
)
127129
}
130+
131+
/**
132+
* Verifies that the actual function in the [Raise] context succeeds and returns an Object assertion
133+
* that allows chaining (object) assertions on the returned value.
134+
*
135+
* @since 1.1.0
136+
* @return a new [AbstractObjectAssert] for assertions chaining on the result value of the function
137+
* in the [Raise] context.
138+
*/
139+
fun result(): AbstractObjectAssert<*, VALUE> {
140+
succeeds()
141+
return Assertions.assertThat((actual as RaiseResult.Success<VALUE>).value)
142+
}
143+
144+
/**
145+
* Verifies that the actual function in the [Raise] context fails and returns an Object assertion
146+
* that allows chaining (object) assertions on the raised error.
147+
*
148+
* @since 1.1.0
149+
* @return a new [AbstractObjectAssert] for assertions chaining on the raised error of the function
150+
* in the [Raise] context.
151+
*/
152+
fun error(): AbstractObjectAssert<*, ERROR> {
153+
fails()
154+
return Assertions.assertThat((actual as RaiseResult.Failure<ERROR>).error)
155+
}
128156
}
129157

130158
sealed interface RaiseResult<out ERROR : Any, out VALUE : Any> {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package `in`.rcard.assertj.arrowcore
2+
3+
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionThatRaisesAnError
4+
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionThatThrowsAnException
5+
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionWithContext
6+
import `in`.rcard.assertj.arrowcore.RaiseAssert.Companion.assertThat
7+
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeds.Companion.shouldFailButSucceedsWith
8+
import org.assertj.core.api.Assertions
9+
import org.junit.jupiter.api.Test
10+
11+
internal class RaiseAssert_error_Test {
12+
@Test
13+
internal fun `should return a valid Object Assert if the Raise raises an error`() {
14+
assertThat { aFunctionThatRaisesAnError() }.error().isEqualTo("LOGICAL ERROR")
15+
}
16+
17+
@Test
18+
internal fun `should fails if the Raise returns a value`() {
19+
Assertions
20+
.assertThatThrownBy { assertThat { aFunctionWithContext(42) }.error() }
21+
.isInstanceOf(AssertionError::class.java)
22+
.hasMessage(shouldFailButSucceedsWith(42).create())
23+
}
24+
25+
@Test
26+
internal fun `should rethrow the inner exception`() {
27+
Assertions
28+
.assertThatThrownBy { assertThat { aFunctionThatThrowsAnException() }.error() }
29+
.isInstanceOf(RuntimeException::class.java)
30+
.hasMessage("AN EXCEPTION")
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package `in`.rcard.assertj.arrowcore
2+
3+
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionThatRaisesAnError
4+
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionThatThrowsAnException
5+
import `in`.rcard.assertj.arrowcore.Dummy.aFunctionWithContext
6+
import `in`.rcard.assertj.arrowcore.RaiseAssert.Companion.assertThat
7+
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedButFailed.Companion.shouldSucceedButFailed
8+
import org.assertj.core.api.Assertions
9+
import org.junit.jupiter.api.Test
10+
11+
internal class RaiseAssert_result_Test {
12+
@Test
13+
internal fun `should return a valid Object Assert if the Raise returns a result`() {
14+
assertThat { aFunctionWithContext(42) }.result().isEqualTo(42)
15+
}
16+
17+
@Test
18+
internal fun `should fails if the Raise raised an error`() {
19+
Assertions
20+
.assertThatThrownBy { assertThat { aFunctionThatRaisesAnError() }.result() }
21+
.isInstanceOf(AssertionError::class.java)
22+
.hasMessage(shouldSucceedButFailed("LOGICAL ERROR").create())
23+
}
24+
25+
@Test
26+
internal fun `should rethrow the inner exception`() {
27+
Assertions
28+
.assertThatThrownBy { assertThat { aFunctionThatThrowsAnException() }.result() }
29+
.isInstanceOf(RuntimeException::class.java)
30+
.hasMessage("AN EXCEPTION")
31+
}
32+
}

0 commit comments

Comments
 (0)