Skip to content

Commit f000fb7

Browse files
authored
Merge pull request #15 from sFreezer/satisfying
Add satisfying fun
2 parents dcd478c + 9473cf4 commit f000fb7

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import `in`.rcard.assertj.arrowcore.errors.EitherShouldContainInstanceOf.Compani
1010
import org.assertj.core.api.AbstractObjectAssert
1111
import org.assertj.core.internal.ComparisonStrategy
1212
import org.assertj.core.internal.StandardComparisonStrategy
13+
import java.util.function.Consumer
1314

1415
/**
1516
* Assertions for [Either].
@@ -81,6 +82,17 @@ abstract class AbstractEitherAssert<
8182
return myself
8283
}
8384

85+
/**
86+
* Verifies that the actual [Either] contains a right-sided value and gives this value to the given
87+
* consumer for further assertions. Should be used as a way of deeper asserting on the
88+
* containing object, as further requirement(s) for the value.
89+
*/
90+
fun hasRightValueSatisfying(requirement: (RIGHT) -> Unit): SELF {
91+
assertIsRight()
92+
actual.onRight { requirement(it) }
93+
return myself
94+
}
95+
8496
private fun assertIsRight() {
8597
isNotNull
8698
if (!actual.isRight()) {
@@ -114,6 +126,17 @@ abstract class AbstractEitherAssert<
114126
return myself
115127
}
116128

129+
/**
130+
* Verifies that the actual [Either] contains a left-sided value and gives this value to the given
131+
* consumer for further assertions. Should be used as a way of deeper asserting on the
132+
* containing object, as further requirement(s) for the value.
133+
*/
134+
fun hasLeftValueSatisfying(requirement: (LEFT) -> Unit): SELF {
135+
assertIsLeft()
136+
actual.onLeft { requirement(it) }
137+
return myself
138+
}
139+
117140
private fun assertIsLeft() {
118141
isNotNull
119142
if (!actual.isLeft()) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import `in`.rcard.assertj.arrowcore.errors.OptionShouldContainInstanceOf.Compani
88
import org.assertj.core.api.AbstractObjectAssert
99
import org.assertj.core.internal.ComparisonStrategy
1010
import org.assertj.core.internal.StandardComparisonStrategy
11+
import java.util.function.Consumer
1112

1213
/**
1314
* Assertions for [Option].
@@ -85,6 +86,20 @@ abstract class AbstractOptionAssert<
8586
return myself
8687
}
8788

89+
/**
90+
* Verifies that the actual [Option] contains a value and gives this value to the given
91+
* consumer for further assertions. Should be used as a way of deeper asserting on the
92+
* containing object, as further requirement(s) for the value.
93+
*
94+
* @param requirement to further assert on the object contained inside the [Option].
95+
* @return this assertion object.
96+
*/
97+
fun hasValueSatisfying(requirement: (VALUE) -> Unit): SELF {
98+
assertValueIsPresent()
99+
actual.onSome { requirement(it) }
100+
return myself
101+
}
102+
88103
private fun assertValueIsPresent() {
89104
isNotNull
90105
if (actual.isEmpty()) throwAssertionError(shouldBePresent())
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package `in`.rcard.assertj.arrowcore
2+
3+
import arrow.core.Either
4+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeLeft
5+
import org.assertj.core.api.Assertions
6+
import org.assertj.core.api.Assertions.assertThat
7+
import org.assertj.core.util.FailureMessages.actualIsNull
8+
import org.junit.jupiter.api.Test
9+
10+
11+
internal class EitherAssert_hasLeftValueSatisfying_Test {
12+
13+
@Test
14+
internal fun `should fail when either is null`() {
15+
val actual: Either<Int, String>? = null
16+
Assertions.assertThatThrownBy { EitherAssert.assertThat(actual).hasLeftValueSatisfying { } }
17+
.isInstanceOf(AssertionError::class.java)
18+
.hasMessage(actualIsNull())
19+
}
20+
21+
@Test
22+
internal fun `should fail if either is right`() {
23+
val actual: Either<Int, String> = Either.Right("something")
24+
Assertions.assertThatThrownBy { EitherAssert.assertThat(actual).hasLeftValueSatisfying { } }
25+
.isInstanceOf(AssertionError::class.java)
26+
.hasMessage(EitherShouldBeLeft.shouldBeLeft(actual).create())
27+
}
28+
29+
@Test
30+
internal fun `should fail if consumer fails`() {
31+
val actual: Either<Int, String> = Either.Left(42)
32+
Assertions.assertThatThrownBy { EitherAssert.assertThat(actual).hasRightValueSatisfying { assertThat(it).isEqualTo(24) } }
33+
.isInstanceOf(AssertionError::class.java)
34+
.hasMessage(("\nexpected: \"24\"\n but was: \"42\""))
35+
}
36+
37+
@Test
38+
internal fun `should pass if consumer passes`() {
39+
val actual: Either<Int, String> = Either.Left(42)
40+
EitherAssert.assertThat(actual).hasLeftValueSatisfying { assertThat(it).isEqualTo(42) }
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package `in`.rcard.assertj.arrowcore
2+
3+
import arrow.core.Either
4+
import `in`.rcard.assertj.arrowcore.errors.EitherShouldBeRight.Companion.shouldBeRight
5+
import org.assertj.core.api.Assertions.assertThat
6+
import org.assertj.core.api.Assertions.assertThatThrownBy
7+
import org.assertj.core.util.FailureMessages.actualIsNull
8+
import org.junit.jupiter.api.Test
9+
10+
11+
internal class EitherAssert_hasRightValueSatisfying_Test {
12+
13+
@Test
14+
internal fun `should fail when either is null`() {
15+
val actual: Either<Int, String>? = null
16+
assertThatThrownBy { EitherAssert.assertThat(actual).hasRightValueSatisfying { } }
17+
.isInstanceOf(AssertionError::class.java)
18+
.hasMessage(actualIsNull())
19+
}
20+
21+
@Test
22+
internal fun `should fail if either is left`() {
23+
val actual: Either<Int, String> = Either.Left(42)
24+
assertThatThrownBy { EitherAssert.assertThat(actual).hasRightValueSatisfying { } }
25+
.isInstanceOf(AssertionError::class.java)
26+
.hasMessage(shouldBeRight(actual).create())
27+
}
28+
29+
@Test
30+
internal fun `should fail if consumer fails`() {
31+
val actual: Either<Int, String> = Either.Right("something")
32+
assertThatThrownBy { EitherAssert.assertThat(actual).hasRightValueSatisfying { assertThat(it).isEqualTo("something else") } }
33+
.isInstanceOf(AssertionError::class.java)
34+
.hasMessage(("\nexpected: \"something else\"\n but was: \"something\""))
35+
}
36+
37+
@Test
38+
internal fun `should pass if consumer passes`() {
39+
val actual: Either<Int, String> = Either.Right("something")
40+
EitherAssert.assertThat(actual).hasRightValueSatisfying { assertThat(it).isEqualTo("something") }
41+
}
42+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package `in`.rcard.assertj.arrowcore
2+
3+
import arrow.core.None
4+
import arrow.core.Option
5+
import `in`.rcard.assertj.arrowcore.OptionAssert.Companion.assertThat
6+
import `in`.rcard.assertj.arrowcore.errors.OptionShouldBePresent.Companion.shouldBePresent
7+
import org.assertj.core.api.Assertions.assertThat
8+
import org.assertj.core.api.Assertions.assertThatThrownBy
9+
import org.assertj.core.util.FailureMessages.actualIsNull
10+
import org.junit.jupiter.api.Test
11+
12+
13+
internal class OptionAssert_hasValueSatisfying_Test {
14+
15+
@Test
16+
internal fun `should fail when option is null`() {
17+
assertThatThrownBy {
18+
val nullOption: Option<String>? = null
19+
assertThat(nullOption).hasValueSatisfying { }
20+
}
21+
.isInstanceOf(AssertionError::class.java)
22+
.hasMessage(actualIsNull())
23+
}
24+
25+
@Test
26+
internal fun `should fail when option is none`() {
27+
assertThatThrownBy {
28+
assertThat(None).hasValueSatisfying { }
29+
}
30+
.isInstanceOf(AssertionError::class.java)
31+
.hasMessage(shouldBePresent().create())
32+
}
33+
34+
35+
@Test
36+
internal fun `should pass when consumer passes`() {
37+
assertThat(Option("something")).hasValueSatisfying {
38+
assertThat(it).isEqualTo("something")
39+
.startsWith("some")
40+
.endsWith("thing")
41+
}
42+
assertThat(Option(10)).hasValueSatisfying {
43+
assertThat(it).isGreaterThan(9)
44+
}
45+
}
46+
47+
@Test
48+
internal fun `should fail from consumer`() {
49+
assertThatThrownBy {
50+
assertThat(Option("something"))
51+
.hasValueSatisfying { assertThat(it).isEqualTo("something else") }
52+
}
53+
.isInstanceOf(AssertionError::class.java)
54+
.hasMessage("\nexpected: \"something else\"\n but was: \"something\"")
55+
}
56+
}

0 commit comments

Comments
 (0)