Skip to content

Commit 947caa6

Browse files
committed
Added containsOnLeftInstanceOf assertion
1 parent b00599e commit 947caa6

File tree

4 files changed

+150
-38
lines changed

4 files changed

+150
-38
lines changed

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

Lines changed: 13 additions & 1 deletion
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.shouldContainOnLeftInstanceOf
89
import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnRightInstanceOf
910
import org.assertj.core.api.AbstractObjectAssert
1011
import org.assertj.core.internal.ComparisonStrategy
@@ -19,7 +20,8 @@ import org.assertj.core.internal.StandardComparisonStrategy
1920
* @author Riccardo Cardin
2021
*/
2122
abstract class AbstractEitherAssert<
22-
SELF : AbstractEitherAssert<SELF, LEFT, RIGHT>, LEFT : Any, RIGHT : Any,>(
23+
SELF : AbstractEitherAssert<SELF, LEFT, RIGHT>, LEFT : Any, RIGHT : Any,
24+
>(
2325
either: Either<LEFT, RIGHT>?,
2426
) : AbstractObjectAssert<SELF, Either<LEFT, RIGHT>>(either, AbstractEitherAssert::class.java) {
2527

@@ -102,6 +104,16 @@ abstract class AbstractEitherAssert<
102104
return myself
103105
}
104106

107+
fun containsLeftInstanceOf(expectedClass: Class<*>): SELF {
108+
assertIsLeft()
109+
actual.onLeft { left ->
110+
if (!expectedClass.isInstance(left)) {
111+
throwAssertionError(shouldContainOnLeftInstanceOf(actual, expectedClass))
112+
}
113+
}
114+
return myself
115+
}
116+
105117
private fun assertIsLeft() {
106118
isNotNull
107119
if (!actual.isLeft()) {

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

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,70 @@ import org.assertj.core.error.BasicErrorMessageFactory
1010
* @author Riccardo Cardin
1111
*/
1212
internal class EitherShouldContainInstanceOf(message: String) : BasicErrorMessageFactory(message) {
13-
companion object {
14-
private const val EXPECTING_TO_CONTAIN_DIFFERENT_INSTANCE =
15-
"%nExpecting:%n <%s>%nto contain a value that is an instance of:%n <%s>%nbut did contain an instance of:%n <%s>"
13+
companion object {
14+
private const val EXPECTING_TO_CONTAIN_DIFFERENT_INSTANCE =
15+
"%nExpecting:%n <%s>%nto contain a value that is an instance of:%n <%s>%nbut did contain an instance of:%n <%s>"
1616

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."
19-
/**
20-
* Indicates that a value should be present in a right-sided [Either].
21-
*
22-
* @param actual Either to be checked.
23-
* @param expectedClass expected class of a right value
24-
* @return an error message factory.
25-
* @throws java.lang.NullPointerException if either is null.
26-
*/
27-
internal fun shouldContainOnRightInstanceOf(
28-
actual: Either<Any, Any>,
29-
expectedClass: Class<*>
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-
})
47-
}
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."
19+
20+
private const val EXPECTING_TO_CONTAIN_BUT_IS_RIGHT =
21+
"%nExpecting:%n <%s>%nto contain on left side:%n <%s>%nbut was right-sided."
22+
23+
/**
24+
* Indicates that a value should be present in a right-sided [Either].
25+
*
26+
* @param actual Either to be checked.
27+
* @param expectedClass expected class of a right value
28+
* @return an error message factory.
29+
* @throws java.lang.NullPointerException if either is null.
30+
*/
31+
internal fun shouldContainOnRightInstanceOf(
32+
actual: Either<Any, Any>,
33+
expectedClass: Class<*>,
34+
): EitherShouldContainInstanceOf =
35+
actual.fold(
36+
{
37+
EitherShouldContainInstanceOf(
38+
EXPECTING_TO_CONTAIN_BUT_IS_LEFT.format(
39+
actual.javaClass.simpleName,
40+
expectedClass.name,
41+
),
42+
)
43+
},
44+
{ rightValue ->
45+
EitherShouldContainInstanceOf(
46+
EXPECTING_TO_CONTAIN_DIFFERENT_INSTANCE.format(
47+
actual.javaClass.simpleName,
48+
expectedClass.name,
49+
rightValue.javaClass.name,
50+
),
51+
)
52+
},
53+
)
54+
55+
internal fun shouldContainOnLeftInstanceOf(
56+
actual: Either<Any, Any>,
57+
expectedClass: Class<*>,
58+
): EitherShouldContainInstanceOf =
59+
actual.fold(
60+
{ leftValue ->
61+
EitherShouldContainInstanceOf(
62+
EXPECTING_TO_CONTAIN_DIFFERENT_INSTANCE.format(
63+
actual.javaClass.simpleName,
64+
expectedClass.name,
65+
leftValue.javaClass.name,
66+
),
67+
)
68+
},
69+
{
70+
EitherShouldContainInstanceOf(
71+
EXPECTING_TO_CONTAIN_BUT_IS_RIGHT.format(
72+
actual.javaClass.simpleName,
73+
expectedClass.name,
74+
),
75+
)
76+
},
77+
)
78+
}
4879
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Companion.shouldContainOnLeftInstanceOf
9+
import org.assertj.core.api.Assertions
10+
import org.assertj.core.util.FailureMessages
11+
import org.junit.jupiter.api.Test
12+
13+
class EitherAssert_containsOnLeftInstanceOf_Test {
14+
@Test
15+
fun `should fail if either is null`() {
16+
val actual: Either<Any, Any>? = null
17+
Assertions.assertThatThrownBy {
18+
assertThat(actual).containsLeftInstanceOf(
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 right`() {
28+
val actual: Either<Any, String> = "some".right()
29+
Assertions.assertThatThrownBy {
30+
assertThat(actual).containsLeftInstanceOf(
31+
Any::class.java,
32+
)
33+
}
34+
.isInstanceOf(AssertionError::class.java)
35+
.hasMessage(shouldBeLeft(actual).create())
36+
}
37+
38+
@Test
39+
fun `should pass if either contains required type on left`() {
40+
val actual: Either<String, Nothing> = "something".left()
41+
assertThat(actual)
42+
.containsLeftInstanceOf(String::class.java)
43+
}
44+
45+
@Test
46+
fun `should pass if either contains required type subclass on left`() {
47+
val actual = Child().left()
48+
assertThat(actual).containsLeftInstanceOf(Parent::class.java)
49+
}
50+
51+
@Test
52+
fun `should fail if either contains other type on left than required`() {
53+
val actual: Either<String, Nothing> = "something".left()
54+
Assertions.assertThatThrownBy {
55+
assertThat(actual).containsLeftInstanceOf(Int::class.java)
56+
}
57+
.isInstanceOf(AssertionError::class.java)
58+
.hasMessage(
59+
shouldContainOnLeftInstanceOf(
60+
actual,
61+
Int::class.java,
62+
).create(),
63+
)
64+
}
65+
66+
private open class Parent
67+
68+
private class Child : Parent()
69+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class EitherAssert_containsOnRightInstanceOf_Test {
5959
val actual: Either<Nothing, String> = "something".right()
6060
assertThat(actual).containsRightInstanceOf(String::class.java)
6161
}
62-
}
6362

64-
private open class Parent
63+
private open class Parent
6564

66-
private class Child : Parent()
65+
private class Child : Parent()
66+
}

0 commit comments

Comments
 (0)