File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
main/kotlin/in/rcard/assertj/arrowcore
test/kotlin/in/rcard/assertj/arrowcore Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 11package `in`.rcard.assertj.arrowcore
22
33import arrow.core.Either
4+ import arrow.core.Option
45import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeLeft.Companion.shouldBeLeft
56import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
67import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnLeft
78import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnRight
89import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnLeftInstanceOf
910import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnRightInstanceOf
1011import org.assertj.core.api.AbstractObjectAssert
12+ import org.assertj.core.api.Assertions
1113import org.assertj.core.internal.ComparisonStrategy
1214import 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()) {
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments