@@ -3,6 +3,7 @@ package `in`.rcard.assertj.arrowcore
33import arrow.core.Either
44import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeLeft.Companion.shouldBeLeft
55import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
6+ import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnLeft
67import `in`.rcard.assertj.arrowcore.errors.EitherShouldContain.Companion.shouldContainOnRight
78import org.assertj.core.api.AbstractObjectAssert
89import 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}
0 commit comments