Skip to content

Commit bba9d27

Browse files
committed
Added the containsOnLeft assertion
1 parent 4dbaef6 commit bba9d27

File tree

3 files changed

+143
-47
lines changed

3 files changed

+143
-47
lines changed

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

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package `in`.rcard.assertj.arrowcore
33
import arrow.core.Either
44
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeLeft.Companion.shouldBeLeft
55
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
6+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnLeft
67
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnRight
78
import org.assertj.core.api.AbstractObjectAssert
89
import org.assertj.core.internal.ComparisonStrategy
@@ -11,64 +12,83 @@ import org.assertj.core.internal.StandardComparisonStrategy
1112
/**
1213
* Assertions for [Either].
1314
*
14-
* @param SELF the "self" type of this assertion class.
15-
* @param LEFT type of the left value contained in the [Either].
15+
* @param SELF the "self" type of this assertion class.
16+
* @param LEFT type of the left value contained in the [Either].
1617
* @param RIGHT type of the right value contained in the [Either].
1718
* @author Riccardo Cardin
1819
*/
19-
abstract class AbstractEitherAssert<SELF : AbstractEitherAssert<SELF, LEFT, RIGHT>, LEFT : Any, RIGHT : Any>(either: Either<LEFT, RIGHT>?) :
20-
AbstractObjectAssert<SELF, Either<LEFT, RIGHT>>(either, AbstractEitherAssert::class.java) {
20+
abstract class AbstractEitherAssert<
21+
SELF : AbstractEitherAssert<SELF, LEFT, RIGHT>, LEFT : Any, RIGHT : Any>(
22+
either: Either<LEFT, RIGHT>?
23+
) : AbstractObjectAssert<SELF, Either<LEFT, RIGHT>>(either, AbstractEitherAssert::class.java) {
2124

22-
private val comparisonStrategy: ComparisonStrategy = StandardComparisonStrategy.instance()
25+
private val comparisonStrategy: ComparisonStrategy = StandardComparisonStrategy.instance()
2326

24-
/**
25-
* Verifies that the actual [Either] is right.
26-
*
27-
* @return this assertion object.
28-
*/
29-
fun isRight(): SELF {
30-
isNotNull
31-
assertIsRight()
32-
return myself
33-
}
27+
/**
28+
* Verifies that the actual [Either] is right.
29+
*
30+
* @return this assertion object.
31+
*/
32+
fun isRight(): SELF {
33+
isNotNull
34+
assertIsRight()
35+
return myself
36+
}
37+
38+
/**
39+
* Verifies that the actual [Either] is left.
40+
*
41+
* @return this assertion object.
42+
*/
43+
fun isLeft(): SELF {
44+
assertIsLeft()
45+
return myself
46+
}
3447

35-
/**
36-
* Verifies that the actual [Either] is left.
37-
*
38-
* @return this assertion object.
39-
*/
40-
fun isLeft(): SELF {
41-
assertIsLeft()
42-
return myself
48+
/**
49+
* Verifies that the actual [Either] is [Either.Right] and contains the given value.
50+
*
51+
* @param expectedValue the expected value inside the [Either].
52+
* @return this assertion object.
53+
*/
54+
fun containsOnRight(expectedValue: RIGHT): SELF {
55+
assertIsRight()
56+
actual.onRight { right ->
57+
if (!comparisonStrategy.areEqual(right, expectedValue)) {
58+
throwAssertionError(shouldContainOnRight(actual, expectedValue))
59+
}
4360
}
61+
return myself
62+
}
4463

45-
private fun assertIsLeft() {
46-
isNotNull
47-
if (!actual.isLeft()) {
48-
throwAssertionError(shouldBeLeft(actual))
49-
}
64+
private fun assertIsRight() {
65+
isNotNull
66+
if (!actual.isRight()) {
67+
throwAssertionError(shouldBeRight(actual))
5068
}
69+
}
5170

52-
/**
53-
* Verifies that the actual [Either] is [Either.Right] and contains the given value.
54-
*
55-
* @param expectedValue the expected value inside the [Either].
56-
* @return this assertion object.
57-
*/
58-
fun containsOnRight(expectedValue: RIGHT): SELF {
59-
assertIsRight()
60-
actual.onRight { right ->
61-
if (!comparisonStrategy.areEqual(right, expectedValue)) throwAssertionError(
62-
shouldContainOnRight(actual, expectedValue)
63-
)
64-
}
65-
return myself
71+
/**
72+
* Verifies that the actual [Either] is [Either.Left]
73+
* and contains the given value.
74+
*
75+
* @param expectedValue the expected value inside the [Either].
76+
* @return this assertion object.
77+
*/
78+
fun containsOnLeft(expectedValue: LEFT): SELF {
79+
assertIsLeft()
80+
actual.onLeft { left ->
81+
if (!comparisonStrategy.areEqual(left, expectedValue)) {
82+
throwAssertionError(shouldContainOnLeft(actual, expectedValue))
83+
}
6684
}
85+
return myself
86+
}
6787

68-
private fun assertIsRight() {
69-
isNotNull
70-
if (!actual.isRight()) {
71-
throwAssertionError(shouldBeRight(actual))
72-
}
88+
private fun assertIsLeft() {
89+
isNotNull
90+
if (!actual.isLeft()) {
91+
throwAssertionError(shouldBeLeft(actual))
7392
}
93+
}
7494
}

src/main/kotlin/in/rcard/assertj/arrowcore/errors/EitherShouldContain.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class EitherShouldContain(message: String, actual: Either<Any, Any>, expected: A
1515

1616
private const val EXPECTING_TO_CONTAIN_ON_RIGHT =
1717
"%nExpecting:%n <%s>%nto contain:%n <%s> on the [RIGHT]%nbut did not."
18+
private const val EXPECTING_TO_CONTAIN_ON_LEFT =
19+
"%nExpecting:%n <%s>%nto contain:%n <%s> on the [LEFT]%nbut did not."
1820

1921
/**
2022
* Indicates that the provided [Either] does not contain the provided argument as right value.
@@ -33,5 +35,23 @@ class EitherShouldContain(message: String, actual: Either<Any, Any>, expected: A
3335
actual,
3436
expectedValue
3537
)
38+
39+
/**
40+
* Indicates that the provided [Either] does not contain the provided argument as right value.
41+
*
42+
* @param actual the [Either] which contains a value.
43+
* @param expectedValue the value we expect to be in the provided [Either] on the left side.
44+
* @param LEFT the type of the value contained in the [Either] on the left side.
45+
* @param RIGHT the type of the value contained in the [Either] on the right side.
46+
* @return an error message factory
47+
*/
48+
fun <LEFT : Any, RIGHT : Any> shouldContainOnLeft(
49+
actual: Either<LEFT, RIGHT>,
50+
expectedValue: RIGHT
51+
): EitherShouldContain = EitherShouldContain(
52+
EXPECTING_TO_CONTAIN_ON_LEFT,
53+
actual,
54+
expectedValue
55+
)
3656
}
3757
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnLeft
8+
import org.assertj.core.api.Assertions
9+
import org.assertj.core.util.FailureMessages.*
10+
import org.junit.jupiter.api.Test
11+
12+
class EitherAssert_containsOnLeft_Test {
13+
@Test
14+
fun `should fail when either is null`() {
15+
val leftValue: Either<String, Nothing>? = null
16+
Assertions.assertThatThrownBy {
17+
EitherAssert.assertThat(leftValue).containsOnLeft(
18+
"something"
19+
)
20+
}
21+
.isInstanceOf(AssertionError::class.java)
22+
.hasMessage(actualIsNull())
23+
}
24+
25+
@Test
26+
fun `should pass if either contains expected value on left side`() {
27+
val leftValue = "something".left()
28+
EitherAssert.assertThat(leftValue).containsOnLeft("something")
29+
}
30+
31+
@Test
32+
fun `should fail if either does not contain expected value on left side`() {
33+
val actual: Either<String, Nothing> = "something".left()
34+
val expectedValue = "nothing"
35+
Assertions.assertThatThrownBy {
36+
EitherAssert.assertThat(actual).containsOnLeft(
37+
expectedValue
38+
)
39+
}
40+
.isInstanceOf(AssertionError::class.java)
41+
.hasMessage(shouldContainOnLeft(actual, expectedValue).create())
42+
}
43+
44+
@Test
45+
fun `should fail if either is right`() {
46+
val actual: Either<String, String> = "nothing".right()
47+
val expectedValue = "something"
48+
Assertions.assertThatThrownBy {
49+
EitherAssert.assertThat(actual).containsOnLeft(
50+
expectedValue
51+
)
52+
}
53+
.isInstanceOf(AssertionError::class.java)
54+
.hasMessage(shouldBeLeft(actual).create())
55+
}
56+
}

0 commit comments

Comments
 (0)