Skip to content

Commit 1263ad9

Browse files
authored
Merge pull request #29 from rcardin/17-add-getright-and-getleft-methods-to-the-assertions-on-eithere-a
Added `asRight()` and `asLeft()` methods to `EitherAssert`
2 parents 57619ff + a80e523 commit 1263ad9

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldC
88
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnLeftInstanceOf
99
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnRightInstanceOf
1010
import org.assertj.core.api.AbstractObjectAssert
11+
import org.assertj.core.api.Assertions
1112
import org.assertj.core.internal.ComparisonStrategy
1213
import org.assertj.core.internal.StandardComparisonStrategy
1314

@@ -94,12 +95,28 @@ abstract class AbstractEitherAssert<
9495
*
9596
* @since 0.1.0
9697
*/
98+
@Deprecated(
99+
"hasRightValueSatisfying can be replaced using the method asRight() and chaining assertions on the returned object.",
100+
ReplaceWith("asRight().satisfies(requirement)"),
101+
)
97102
fun hasRightValueSatisfying(requirement: (RIGHT) -> Unit): SELF {
98103
assertIsRight()
99104
actual.onRight { requirement(it) }
100105
return myself
101106
}
102107

108+
/**
109+
* Verifies that the actual [Either] is not null and contains a right-sided value and returns an Object assertion
110+
* that allows chaining (object) assertions on the value.
111+
*
112+
* @since 0.2.0
113+
* @return a new [AbstractObjectAssert] for assertions chaining on the right-sided value of the [Either].
114+
*/
115+
fun asRight(): AbstractObjectAssert<*, RIGHT> {
116+
assertIsRight()
117+
return Assertions.assertThat(actual.getOrNull())
118+
}
119+
103120
private fun assertIsRight() {
104121
isNotNull
105122
if (!actual.isRight()) {
@@ -149,12 +166,28 @@ abstract class AbstractEitherAssert<
149166
* @return this assertion object.
150167
* @since 0.1.0
151168
*/
169+
@Deprecated(
170+
"hasLeftValueSatisfying can be replaced using the method asLeft() and chaining assertions on the returned object.",
171+
ReplaceWith("asLeft().satisfies(requirement)"),
172+
)
152173
fun hasLeftValueSatisfying(requirement: (LEFT) -> Unit): SELF {
153174
assertIsLeft()
154175
actual.onLeft { requirement(it) }
155176
return myself
156177
}
157178

179+
/**
180+
* Verifies that the actual [Either] is not null and contains a left-sided value and returns an Object assertion
181+
* that allows chaining (object) assertions on the value.
182+
*
183+
* @since 0.2.0
184+
* @return a new [AbstractObjectAssert] for assertions chaining on the left-sided value of the [Either].
185+
*/
186+
fun asLeft(): AbstractObjectAssert<*, LEFT> {
187+
assertIsLeft()
188+
return Assertions.assertThat(actual.leftOrNull())
189+
}
190+
158191
private fun assertIsLeft() {
159192
isNotNull
160193
if (!actual.isLeft()) {
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.EitherShouldBeLeft.Companion.shouldBeLeft
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_asLeft_Test {
13+
14+
@Test
15+
internal fun `should return a valid Object assert if the either contains a left-sided value`() {
16+
val actualLeftValue: Either<Int, Nothing> = 42.left()
17+
assertThat(actualLeftValue).asLeft().isEqualTo(42)
18+
}
19+
20+
@Test
21+
internal fun `should fail if the either contains a right-sided value`() {
22+
val actualRightValue: Either<Nothing, String> = "42".right()
23+
Assertions.assertThatThrownBy { assertThat(actualRightValue).asLeft() }
24+
.isInstanceOf(AssertionError::class.java)
25+
.hasMessage(shouldBeLeft(actualRightValue).create())
26+
}
27+
28+
@Test
29+
internal fun `should fail if the either is null`() {
30+
val actualLeftValue: Either<Int, Nothing>? = null
31+
Assertions.assertThatThrownBy { assertThat(actualLeftValue).asLeft() }
32+
.isInstanceOf(AssertionError::class.java)
33+
.hasMessage(FailureMessages.actualIsNull())
34+
}
35+
}
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)