Skip to content

Commit 3bf97f7

Browse files
committed
Added isRight assertion
1 parent 46d08dc commit 3bf97f7

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ import arrow.core.Either
44
import org.assertj.core.api.AbstractObjectAssert
55

66
abstract class AbstractEitherAssert<SELF : AbstractEitherAssert<SELF, LEFT, RIGHT>, LEFT, RIGHT>(either: Either<LEFT, RIGHT>?) :
7-
AbstractObjectAssert<SELF, Either<LEFT, RIGHT>>(either, AbstractEitherAssert::class.java)
7+
AbstractObjectAssert<SELF, Either<LEFT, RIGHT>>(either, AbstractEitherAssert::class.java) {
8+
fun isRight() {
9+
isNotNull
10+
if (!actual.isRight()) {
11+
failWithMessage("%nExpected Either to be right but was <%s>", actual)
12+
}
13+
}
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package `in`.rcard.assertj.arrowcore
2+
3+
import arrow.core.Either
4+
import arrow.core.left
5+
import arrow.core.right
6+
import org.assertj.core.api.Assertions
7+
import org.assertj.core.util.FailureMessages.actualIsNull
8+
import org.junit.jupiter.api.Test
9+
10+
class EitherAssert_isRight_Test {
11+
12+
@Test
13+
fun `should fail if either is null`() {
14+
// GIVEN
15+
val rightValue: Either<Nothing, Nothing>? = null
16+
// WHEN/THEN
17+
Assertions.assertThatThrownBy { EitherAssert.assertThat(rightValue).isRight() }
18+
.isInstanceOf(AssertionError::class.java)
19+
.hasMessage(actualIsNull())
20+
}
21+
22+
@Test
23+
fun `should pass if either is right`() {
24+
// GIVEN
25+
val rightValue = 42.right()
26+
// WHEN/THEN
27+
EitherAssert.assertThat(rightValue).isRight()
28+
}
29+
30+
@Test
31+
fun `should fail if either is left`() {
32+
// GIVEN
33+
val leftValue = "42".left()
34+
// WHEN/THEN
35+
Assertions.assertThatThrownBy { EitherAssert.assertThat(leftValue).isRight() }
36+
.isInstanceOf(AssertionError::class.java)
37+
.hasMessage("\nExpected Either to be right but was <Either.Left(42)>")
38+
}
39+
}

0 commit comments

Comments
 (0)