Skip to content

Commit df16cb3

Browse files
committed
Added containsOnRightInstanceOf
1 parent 623c3cc commit df16cb3

File tree

3 files changed

+89
-71
lines changed

3 files changed

+89
-71
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeLeft.Companion.shouldBe
55
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
66
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnLeft
77
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnRight
8+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnRightInstanceOf
89
import org.assertj.core.api.AbstractObjectAssert
910
import org.assertj.core.internal.ComparisonStrategy
1011
import org.assertj.core.internal.StandardComparisonStrategy
@@ -61,6 +62,23 @@ abstract class AbstractEitherAssert<
6162
return myself
6263
}
6364

65+
/**
66+
* Verifies that the actual right-sided [Either] contains a value that is an
67+
* instance of the argument.
68+
*
69+
* @param expectedClass the expected class of the value inside the right-sided [Either].
70+
* @return this assertion object.
71+
*/
72+
fun containsRightInstanceOf(expectedClass: Class<*>): SELF {
73+
assertIsRight()
74+
actual.onRight { right ->
75+
if (!expectedClass.isInstance(right)) {
76+
throwAssertionError(shouldContainOnRightInstanceOf(actual, expectedClass))
77+
}
78+
}
79+
return myself
80+
}
81+
6482
private fun assertIsRight() {
6583
isNotNull
6684
if (!actual.isRight()) {
@@ -90,17 +108,4 @@ abstract class AbstractEitherAssert<
90108
throwAssertionError(shouldBeLeft(actual))
91109
}
92110
}
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-
}
106111
}

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ package `in`.rcard.assertj.arrowcore.errors
33
import arrow.core.Either
44
import org.assertj.core.error.BasicErrorMessageFactory
55

6+
/**
7+
* Build error message when an [Either] should contain a value being an instance of a specific
8+
* class.
9+
*
10+
* @author Riccardo Cardin
11+
*/
612
class EitherShouldContainInstanceOf(message: String) : BasicErrorMessageFactory(message) {
713
companion object {
814
private const val EXPECTING_TO_CONTAIN_DIFFERENT_INSTANCE =
915
"%nExpecting:%n <%s>%nto contain a value that is an instance of:%n <%s>%nbut did contain an instance of:%n <%s>"
1016

17+
private const val EXPECTING_TO_CONTAIN_BUT_IS_LEFT =
18+
"%nExpecting:%n <%s>%nto contain on right side:%n <%s>%nbut was left-sided."
1119
/**
1220
* Indicates that a value should be present in a right-sided [Either].
1321
*
@@ -17,20 +25,24 @@ class EitherShouldContainInstanceOf(message: String) : BasicErrorMessageFactory(
1725
* @throws java.lang.NullPointerException if either is null.
1826
*/
1927
fun shouldContainOnRightInstanceOf(
20-
actual: Either<*, *>,
28+
actual: Either<Any, Any>,
2129
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-
}
30+
): EitherShouldContainInstanceOf =
31+
actual.fold(
32+
{
33+
EitherShouldContainInstanceOf(
34+
java.lang.String.format(
35+
EXPECTING_TO_CONTAIN_BUT_IS_LEFT,
36+
actual.javaClass.simpleName,
37+
expectedClass.name))
38+
},
39+
{ rightValue ->
40+
EitherShouldContainInstanceOf(
41+
java.lang.String.format(
42+
EXPECTING_TO_CONTAIN_DIFFERENT_INSTANCE,
43+
actual.javaClass.simpleName,
44+
expectedClass.name,
45+
rightValue.javaClass.name))
46+
})
3547
}
3648
}

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

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,55 @@ import arrow.core.left
55
import arrow.core.right
66
import `in`.rcard.assertj.arrowcore.EitherAssert.Companion.assertThat
77
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
8+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnRightInstanceOf
89
import org.assertj.core.api.Assertions
910
import org.assertj.core.util.FailureMessages
1011
import org.junit.jupiter.api.Test
1112

1213
class EitherAssert_containsOnRightInstanceOf_Test {
1314

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(
15+
@Test
16+
fun `should fail if either is null`() {
17+
val actual: Either<Nothing, Nothing>? = null
18+
Assertions.assertThatThrownBy { assertThat(actual).containsRightInstanceOf(Any::class.java) }
19+
.isInstanceOf(AssertionError::class.java)
20+
.hasMessage(FailureMessages.actualIsNull())
21+
}
22+
23+
@Test
24+
fun `should fail if either is left`() {
25+
val actual: Either<String, Nothing> = "some".left()
26+
Assertions.assertThatThrownBy { assertThat(actual).containsRightInstanceOf(Any::class.java) }
27+
.isInstanceOf(AssertionError::class.java)
28+
.hasMessage(shouldBeRight(actual).create())
29+
}
30+
31+
@Test
32+
fun should_fail_if_either_contains_other_type_on_right_than_required() {
33+
val actual: Either<Any, Int> = 42.right()
34+
Assertions.assertThatThrownBy { assertThat(actual).containsRightInstanceOf(String::class.java) }
35+
.isInstanceOf(AssertionError::class.java)
36+
.hasMessage(
37+
shouldContainOnRightInstanceOf(
4738
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-
}
39+
String::class.java,
40+
)
41+
.create())
42+
}
43+
44+
@Test
45+
fun `should pass if either contains required type subclass on right`() {
46+
val actual = Child().right()
47+
assertThat(actual).containsRightInstanceOf(Parent::class.java)
48+
}
49+
50+
@Test
51+
fun `should pass if either contains required type on right`() {
52+
val actual: Either<Nothing, String> = "something".right()
53+
assertThat(actual).containsRightInstanceOf(String::class.java)
54+
}
55+
}
56+
57+
private open class Parent
58+
59+
private class Child : Parent()

0 commit comments

Comments
 (0)