Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/main/kotlin/in/rcard/assertj/arrowcore/AbstractOptionAssert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import `in`.rcard.assertj.arrowcore.errors.OptionShouldBePresent.Companion.shoul
import `in`.rcard.assertj.arrowcore.errors.OptionShouldContain.Companion.shouldContain
import `in`.rcard.assertj.arrowcore.errors.OptionShouldContainInstanceOf.Companion.shouldContainInstanceOf
import org.assertj.core.api.AbstractObjectAssert
import org.assertj.core.api.Assertions
import org.assertj.core.internal.ComparisonStrategy
import org.assertj.core.internal.StandardComparisonStrategy

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

private val comparisonStrategy: ComparisonStrategy = StandardComparisonStrategy.instance()

/**
* Verifies that there is a value present in the actual [Option].
*
* @return this assertion object.
*/
fun isDefined(): SELF {
assertValueIsPresent()
return myself
}

/**
* Verifies that the actual [Option] is empty.
*
Expand Down Expand Up @@ -96,12 +87,38 @@ abstract class AbstractOptionAssert<
* @return this assertion object.
* @since 0.1.0
*/
@Deprecated(
"hasValueSatisfying can be replaced using the method get() and chaining assertions on the returned object.",
ReplaceWith("get().satisfies(requirement)"),
)
fun hasValueSatisfying(requirement: (VALUE) -> Unit): SELF {
assertValueIsPresent()
actual.onSome { requirement(it) }
return myself
}

/**
* Verifies that the actual [Option] is not null and not empty and returns an Object assertion that allows
* chaining (object) assertions on the optional value.
*
* @since 0.2.0
* @return a new [AbstractObjectAssert] for assertions chaining on the value of the [Option].
*/
fun get(): AbstractObjectAssert<*, VALUE> {
isDefined()
return Assertions.assertThat(actual.getOrNull())
}

/**
* Verifies that there is a value present in the actual [Option].
*
* @return this assertion object.
*/
fun isDefined(): SELF {
assertValueIsPresent()
return myself
}

private fun assertValueIsPresent() {
isNotNull
if (actual.isNone()) throwAssertionError(shouldBePresent())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package `in`.rcard.assertj.arrowcore

import arrow.core.None
import arrow.core.Option
import arrow.core.some
import `in`.rcard.assertj.arrowcore.OptionAssert.Companion.assertThat
import `in`.rcard.assertj.arrowcore.errors.OptionShouldBePresent.Companion.shouldBePresent
import org.assertj.core.api.Assertions
import org.assertj.core.util.FailureMessages
import org.junit.jupiter.api.Test

internal class OptionAssert_get_Test {

@Test
internal fun `should return a valid Object assert if the Option is not empty`() {
val some: Option<String> = "42".some()
assertThat(some).get().isEqualTo("42")
}

@Test
internal fun `should fail if the Option is empty`() {
val none: Option<String> = None
Assertions.assertThatThrownBy { assertThat(none).get() }
.isInstanceOf(AssertionError::class.java)
.hasMessage(shouldBePresent().create())
}

@Test
internal fun `should fail if the Option is null`() {
val nullOption: Option<String>? = null
Assertions.assertThatThrownBy { assertThat(nullOption).get() }
.isInstanceOf(AssertionError::class.java)
.hasMessage(FailureMessages.actualIsNull())
}
}