Skip to content

Commit 623c3cc

Browse files
committed
WIP Adding containsOnRightInstanceOf
1 parent bba9d27 commit 623c3cc

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ abstract class AbstractEitherAssert<
6969
}
7070

7171
/**
72-
* Verifies that the actual [Either] is [Either.Left]
73-
* and contains the given value.
72+
* Verifies that the actual [Either] is [Either.Left] and contains the given value.
7473
*
7574
* @param expectedValue the expected value inside the [Either].
7675
* @return this assertion object.
@@ -91,4 +90,17 @@ abstract class AbstractEitherAssert<
9190
throwAssertionError(shouldBeLeft(actual))
9291
}
9392
}
93+
94+
/**
95+
* Verifies that the actual right-sided [Either] contains a value that is an
96+
* instance of the argument.
97+
*
98+
* @param expectedClass the expected class of the value inside the right-sided [Either].
99+
* @return this assertion object.
100+
*/
101+
fun containsRightInstanceOf(expectedClass: Class<*>): SELF {
102+
isNotNull
103+
isRight()
104+
return myself
105+
}
94106
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package `in`.rcard.assertj.arrowcore.errors
2+
3+
import arrow.core.Either
4+
import org.assertj.core.error.BasicErrorMessageFactory
5+
6+
class EitherShouldContainInstanceOf(message: String) : BasicErrorMessageFactory(message) {
7+
companion object {
8+
private const val EXPECTING_TO_CONTAIN_DIFFERENT_INSTANCE =
9+
"%nExpecting:%n <%s>%nto contain a value that is an instance of:%n <%s>%nbut did contain an instance of:%n <%s>"
10+
11+
/**
12+
* Indicates that a value should be present in a right-sided [Either].
13+
*
14+
* @param actual Either to be checked.
15+
* @param expectedClass expected class of a right value
16+
* @return an error message factory.
17+
* @throws java.lang.NullPointerException if either is null.
18+
*/
19+
fun shouldContainOnRightInstanceOf(
20+
actual: Either<*, *>,
21+
expectedClass: Class<*>
22+
): EitherShouldContainInstanceOf? {
23+
actual.onRight { rightValue ->
24+
return EitherShouldContainInstanceOf(
25+
java.lang.String.format(
26+
EXPECTING_TO_CONTAIN_DIFFERENT_INSTANCE,
27+
actual.javaClass.simpleName,
28+
expectedClass.name,
29+
rightValue.javaClass.name
30+
))
31+
}.onLeft {
32+
TODO()
33+
}
34+
}
35+
}
36+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
class EitherAssert_containsOnRightInstanceOf_Test {
13+
14+
@Test
15+
fun `should fail if either is null`() {
16+
val actual: Either<Nothing, Nothing>? = null
17+
Assertions.assertThatThrownBy {
18+
assertThat(actual).containsRightInstanceOf(
19+
Any::class.java
20+
)
21+
}
22+
.isInstanceOf(AssertionError::class.java)
23+
.hasMessage(FailureMessages.actualIsNull())
24+
}
25+
26+
@Test
27+
fun `should fail if either is left`() {
28+
val actual: Either<String, Nothing> = "some".left()
29+
Assertions.assertThatThrownBy {
30+
assertThat(actual).containsRightInstanceOf(
31+
Any::class.java
32+
)
33+
}
34+
.isInstanceOf(AssertionError::class.java)
35+
.hasMessage(shouldBeRight(actual).create())
36+
}
37+
38+
@Test
39+
fun should_fail_if_either_contains_other_type_on_right_than_required() {
40+
val actual: Either<Any, Int> = 42.right()
41+
Assertions.assertThatThrownBy {
42+
assertThat(actual).containsRightInstanceOf(String::class.java)
43+
}
44+
.isInstanceOf(AssertionError::class.java)
45+
.hasMessage(
46+
shouldContainOnRightInstanceOf(
47+
actual,
48+
Int::class.java,
49+
).create()
50+
)
51+
}
52+
53+
@Test
54+
fun `should pass if either contains required type on right`() {
55+
val actual: Either<Nothing, String> = "something".right()
56+
assertThat(actual).containsRightInstanceOf(String::class.java)
57+
}
58+
}

0 commit comments

Comments
 (0)