Skip to content

Commit cc7701c

Browse files
committed
[add-getRight-and-getLeft-methods] Added asLeft() assertion to EitherAssert
1 parent 7c979b4 commit cc7701c

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ abstract class AbstractEitherAssert<
168168
return myself
169169
}
170170

171+
/**
172+
* Verifies that the actual [Either] is not null and contains a left-sided value and returns an Object assertion
173+
* that allows chaining (object) assertions on the value.
174+
*
175+
* @since 0.2.0
176+
* @return a new [AbstractObjectAssert] for assertions chaining on the left-sided value of the [Either].
177+
*/
178+
fun asLeft(): AbstractObjectAssert<*, LEFT> {
179+
assertIsLeft()
180+
return Assertions.assertThat(actual.leftOrNull())
181+
}
182+
171183
private fun assertIsLeft() {
172184
isNotNull
173185
if (!actual.isLeft()) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package `in`.rcard.assertj.arrowcore
2+
3+
import arrow.core.Either
4+
import arrow.core.left
5+
import arrow.core.right
6+
import `in`.rcard.assertj.arrowcore.EitherAssert.Companion.assertThat
7+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeLeft.Companion.shouldBeLeft
8+
import org.assertj.core.api.Assertions
9+
import org.assertj.core.util.FailureMessages
10+
import org.junit.jupiter.api.Test
11+
12+
internal class EitherAssert_asLeft_Test {
13+
14+
@Test
15+
internal fun `should return a valid Object assert if the either contains a left-sided value`() {
16+
val actualLeftValue: Either<Int, Nothing> = 42.left()
17+
assertThat(actualLeftValue).asLeft().isEqualTo(42)
18+
}
19+
20+
@Test
21+
internal fun `should fail if the either contains a right-sided value`() {
22+
val actualRightValue: Either<Nothing, String> = "42".right()
23+
Assertions.assertThatThrownBy { assertThat(actualRightValue).asLeft() }
24+
.isInstanceOf(AssertionError::class.java)
25+
.hasMessage(shouldBeLeft(actualRightValue).create())
26+
}
27+
28+
@Test
29+
internal fun `should fail if the either is null`() {
30+
val actualLeftValue: Either<Int, Nothing>? = null
31+
Assertions.assertThatThrownBy { assertThat(actualLeftValue).asLeft() }
32+
.isInstanceOf(AssertionError::class.java)
33+
.hasMessage(FailureMessages.actualIsNull())
34+
}
35+
}

0 commit comments

Comments
 (0)