Skip to content

Commit b3ece2b

Browse files
committed
[add-getRight-and-getLeft-methods] Added asRight() assertion to EitherAssert
1 parent 57619ff commit b3ece2b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package `in`.rcard.assertj.arrowcore
22

33
import arrow.core.Either
4+
import arrow.core.Option
45
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeLeft.Companion.shouldBeLeft
56
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
67
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnLeft
78
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnRight
89
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnLeftInstanceOf
910
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnRightInstanceOf
1011
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

@@ -100,6 +102,18 @@ abstract class AbstractEitherAssert<
100102
return myself
101103
}
102104

105+
/**
106+
* Verifies that the actual [Either] is not null and contains a right-sided value and returns an Object assertion
107+
* that allows chaining (object) assertions on the value.
108+
*
109+
* @since 0.2.0
110+
* @return a new [AbstractObjectAssert] for assertions chaining on the right-sided value of the [Either].
111+
*/
112+
fun asRight(): AbstractObjectAssert<*, RIGHT> {
113+
assertIsRight()
114+
return Assertions.assertThat(actual.getOrNull())
115+
}
116+
103117
private fun assertIsRight() {
104118
isNotNull
105119
if (!actual.isRight()) {
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.EitherShouldBeRight.Companion.shouldBeRight
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_asRight_Test {
13+
14+
@Test
15+
internal fun `should return a valid Object assert if the either contains a right-sided value`() {
16+
val actualRightValue: Either<Nothing, Int> = 42.right()
17+
assertThat(actualRightValue).asRight().isEqualTo(42)
18+
}
19+
20+
@Test
21+
internal fun `should fail if the either contains a left-sided value`() {
22+
val actualLeftValue: Either<String, Nothing> = "42".left()
23+
Assertions.assertThatThrownBy { assertThat(actualLeftValue).asRight() }
24+
.isInstanceOf(AssertionError::class.java)
25+
.hasMessage(shouldBeRight(actualLeftValue).create())
26+
}
27+
28+
@Test
29+
internal fun `should fail if the either is null`() {
30+
val actualRightValue: Either<Nothing, Int>? = null
31+
Assertions.assertThatThrownBy { assertThat(actualRightValue).asRight() }
32+
.isInstanceOf(AssertionError::class.java)
33+
.hasMessage(FailureMessages.actualIsNull())
34+
}
35+
}

0 commit comments

Comments
 (0)