Skip to content

Commit f18acf5

Browse files
committed
Added isLeft assertion and refactored using the BasicErrorMessageFactory approach for errors, instead of the raw strings
1 parent ef0aff9 commit f18acf5

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package `in`.rcard.assertj.arrowcore
22

33
import arrow.core.Either
4+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeLeft.Companion.shouldBeLeft
5+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
46
import org.assertj.core.api.AbstractObjectAssert
57

68
/**
@@ -19,10 +21,24 @@ abstract class AbstractEitherAssert<SELF : AbstractEitherAssert<SELF, LEFT, RIGH
1921
*
2022
* @return this assertion object.
2123
*/
22-
fun isRight() {
24+
fun isRight(): SELF {
2325
isNotNull
2426
if (!actual.isRight()) {
25-
failWithMessage("%nExpected Either to be right but was <%s>", actual)
27+
throwAssertionError(shouldBeRight(actual))
2628
}
29+
return myself
30+
}
31+
32+
/**
33+
* Verifies that the actual [Either] is left.
34+
*
35+
* @return this assertion object.
36+
*/
37+
fun isLeft(): SELF {
38+
isNotNull
39+
if (!actual.isLeft()) {
40+
throwAssertionError(shouldBeLeft(actual))
41+
}
42+
return myself
2743
}
2844
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package `in`.rcard.assertj.arrowcore.errors
2+
3+
import arrow.core.Either
4+
import org.assertj.core.error.BasicErrorMessageFactory
5+
6+
/**
7+
* Build error message when an [Either] should be left.
8+
*
9+
* @author Riccardo Cardin
10+
*/
11+
class EitherShouldBeLeft(actual: Either<*, *>) :
12+
BasicErrorMessageFactory("%nExpecting an Either to be left but was <$actual>.") {
13+
companion object {
14+
fun shouldBeLeft(actual: Either<*, *>): EitherShouldBeLeft {
15+
return EitherShouldBeLeft(actual)
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package `in`.rcard.assertj.arrowcore.errors
2+
3+
import arrow.core.Either
4+
import org.assertj.core.error.BasicErrorMessageFactory
5+
6+
/**
7+
* Build error message when an [Either] should be right.
8+
*
9+
* @author Riccardo Cardin
10+
*/
11+
class EitherShouldBeRight(actual: Either<*, *>) :
12+
BasicErrorMessageFactory("%nExpecting an Either to be right but was <$actual>.") {
13+
companion object {
14+
fun shouldBeRight(actual: Either<*, *>): EitherShouldBeRight {
15+
return EitherShouldBeRight(actual)
16+
}
17+
}
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.errors.EitherShouldBeLeft.Companion.shouldBeLeft
7+
import org.assertj.core.api.Assertions
8+
import org.assertj.core.util.FailureMessages
9+
import org.junit.jupiter.api.Test
10+
11+
class EitherAssert_isLeft_Test {
12+
13+
@Test
14+
fun `should fail if either is null`() {
15+
// GIVEN
16+
val leftValue: Either<Nothing, Nothing>? = null
17+
// WHEN/THEN
18+
Assertions.assertThatThrownBy { EitherAssert.assertThat(leftValue).isLeft() }
19+
.isInstanceOf(AssertionError::class.java)
20+
.hasMessage(FailureMessages.actualIsNull())
21+
}
22+
23+
@Test
24+
fun `should pass if either is left`() {
25+
// GIVEN
26+
val leftValue = "Error".left()
27+
// WHEN/THEN
28+
EitherAssert.assertThat(leftValue).isLeft()
29+
}
30+
31+
@Test
32+
fun `should fail if either is right`() {
33+
// GIVEN
34+
val rightValue = 42.right()
35+
// WHEN/THEN
36+
Assertions.assertThatThrownBy { EitherAssert.assertThat(rightValue).isLeft() }
37+
.isInstanceOf(AssertionError::class.java)
38+
.hasMessage(shouldBeLeft(rightValue).create())
39+
}
40+
}

src/test/kotlin/in/rcard/assertj/arrowcore/EitherAssert_isRight_Test.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package `in`.rcard.assertj.arrowcore
33
import arrow.core.Either
44
import arrow.core.left
55
import arrow.core.right
6+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
67
import org.assertj.core.api.Assertions
78
import org.assertj.core.util.FailureMessages.actualIsNull
89
import org.junit.jupiter.api.Test
@@ -34,6 +35,6 @@ class EitherAssert_isRight_Test {
3435
// WHEN/THEN
3536
Assertions.assertThatThrownBy { EitherAssert.assertThat(leftValue).isRight() }
3637
.isInstanceOf(AssertionError::class.java)
37-
.hasMessage("\nExpected Either to be right but was <Either.Left(42)>")
38+
.hasMessage(shouldBeRight(leftValue).create())
3839
}
3940
}

0 commit comments

Comments
 (0)