Skip to content

Commit 89f50d3

Browse files
committed
[dont-use-arrow-deprecated-methods] Added get() method to Option assertions and deprecated 'hasValueSatisfying'.
1 parent 147871e commit 89f50d3

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import `in`.rcard.assertj.arrowcore.errors.OptionShouldBePresent.Companion.shoul
66
import `in`.rcard.assertj.arrowcore.errors.OptionShouldContain.Companion.shouldContain
77
import `in`.rcard.assertj.arrowcore.errors.OptionShouldContainInstanceOf.Companion.shouldContainInstanceOf
88
import org.assertj.core.api.AbstractObjectAssert
9+
import org.assertj.core.api.Assertions
910
import org.assertj.core.internal.ComparisonStrategy
1011
import org.assertj.core.internal.StandardComparisonStrategy
1112

@@ -26,16 +27,6 @@ abstract class AbstractOptionAssert<
2627

2728
private val comparisonStrategy: ComparisonStrategy = StandardComparisonStrategy.instance()
2829

29-
/**
30-
* Verifies that there is a value present in the actual [Option].
31-
*
32-
* @return this assertion object.
33-
*/
34-
fun isDefined(): SELF {
35-
assertValueIsPresent()
36-
return myself
37-
}
38-
3930
/**
4031
* Verifies that the actual [Option] is empty.
4132
*
@@ -96,12 +87,38 @@ abstract class AbstractOptionAssert<
9687
* @return this assertion object.
9788
* @since 0.1.0
9889
*/
90+
@Deprecated(
91+
"hasValueSatisfying can be replaced using the method get() and chaining assertions on the returned object.",
92+
ReplaceWith("get().satisfies(requirement)"),
93+
)
9994
fun hasValueSatisfying(requirement: (VALUE) -> Unit): SELF {
10095
assertValueIsPresent()
10196
actual.onSome { requirement(it) }
10297
return myself
10398
}
10499

100+
/**
101+
* Verifies that the actual [Option] is not null and not empty and returns an Object assertion that allows
102+
* chaining (object) assertions on the optional value.
103+
*
104+
* @since 0.2.0
105+
* @return a new [AbstractObjectAssert] for assertions chaining on the value of the [Option].
106+
*/
107+
fun get(): AbstractObjectAssert<*, VALUE> {
108+
isDefined()
109+
return Assertions.assertThat(actual.getOrNull())
110+
}
111+
112+
/**
113+
* Verifies that there is a value present in the actual [Option].
114+
*
115+
* @return this assertion object.
116+
*/
117+
fun isDefined(): SELF {
118+
assertValueIsPresent()
119+
return myself
120+
}
121+
105122
private fun assertValueIsPresent() {
106123
isNotNull
107124
if (actual.isNone()) throwAssertionError(shouldBePresent())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package `in`.rcard.assertj.arrowcore
2+
3+
import arrow.core.None
4+
import arrow.core.Option
5+
import arrow.core.some
6+
import `in`.rcard.assertj.arrowcore.OptionAssert.Companion.assertThat
7+
import `in`.rcard.assertj.arrowcore.errors.OptionShouldBePresent.Companion.shouldBePresent
8+
import org.assertj.core.api.Assertions
9+
import org.assertj.core.util.FailureMessages
10+
import org.junit.jupiter.api.Test
11+
12+
internal class OptionAssert_get_Test {
13+
14+
@Test
15+
internal fun `should return a valid Object assert if the Option is not empty`() {
16+
val some: Option<String> = "42".some()
17+
assertThat(some).get().isEqualTo("42")
18+
}
19+
20+
@Test
21+
internal fun `should fail if the Option is empty`() {
22+
val none: Option<String> = None
23+
Assertions.assertThatThrownBy { assertThat(none).get() }
24+
.isInstanceOf(AssertionError::class.java)
25+
.hasMessage(shouldBePresent().create())
26+
}
27+
28+
@Test
29+
internal fun `should fail if the Option is null`() {
30+
val nullOption: Option<String>? = null
31+
Assertions.assertThatThrownBy { assertThat(nullOption).get() }
32+
.isInstanceOf(AssertionError::class.java)
33+
.hasMessage(FailureMessages.actualIsNull())
34+
}
35+
}

0 commit comments

Comments
 (0)