Skip to content

Commit fd7aac6

Browse files
committed
Added the isDefined assertion
1 parent b0590dc commit fd7aac6

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package `in`.rcard.assertj.arrowcore
22

33
import arrow.core.Option
4+
import `in`.rcard.assertj.arrowcore.errors.OptionShouldBePresent.Companion.shouldBePresent
45
import org.assertj.core.api.AbstractObjectAssert
56

67
/**
@@ -14,4 +15,16 @@ abstract class AbstractOptionAssert<
1415
SELF : AbstractOptionAssert<SELF, VALUE>, VALUE : Any,
1516
>(
1617
option: Option<VALUE>?,
17-
) : AbstractObjectAssert<SELF, Option<VALUE>>(option, AbstractOptionAssert::class.java)
18+
) : AbstractObjectAssert<SELF, Option<VALUE>>(option, AbstractOptionAssert::class.java) {
19+
20+
/**
21+
* Verifies that there is a value present in the actual [Option].
22+
*
23+
* @return this assertion object.
24+
*/
25+
fun isDefined(): SELF {
26+
isNotNull
27+
if (actual.isEmpty()) throwAssertionError(shouldBePresent())
28+
return myself
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package `in`.rcard.assertj.arrowcore.errors
2+
3+
import arrow.core.Option
4+
import org.assertj.core.error.BasicErrorMessageFactory
5+
6+
/**
7+
* Build error message when a value should be present in an [Option].
8+
*
9+
* @author Riccardo Cardin
10+
*/
11+
class OptionShouldBePresent :
12+
BasicErrorMessageFactory("%nExpecting Option to contain a value but it didn't.".format()) {
13+
companion object {
14+
15+
/**
16+
* Indicates that a value should be present in an empty [Option].
17+
*
18+
* @return a error message factory.
19+
*/
20+
fun shouldBePresent(): OptionShouldBePresent {
21+
return OptionShouldBePresent()
22+
}
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.actualIsNull
10+
import org.junit.jupiter.api.Test
11+
12+
class OptionAssert_isDefined_Test {
13+
@Test
14+
fun `should pass when Option is present`() {
15+
assertThat("present".some()).isDefined()
16+
}
17+
18+
@Test
19+
fun `should fail when Option is empty`() {
20+
Assertions.assertThatThrownBy { assertThat(None).isDefined() }
21+
.isInstanceOf(AssertionError::class.java)
22+
.hasMessage(shouldBePresent().create())
23+
}
24+
25+
@Test
26+
fun `should fail when Option is null`() {
27+
val nullOption: Option<Nothing>? = null
28+
Assertions.assertThatThrownBy { assertThat(nullOption).isDefined() }
29+
.isInstanceOf(AssertionError::class.java)
30+
.hasMessage(actualIsNull())
31+
}
32+
}

0 commit comments

Comments
 (0)