Skip to content

Commit 2136051

Browse files
committed
Added isEmpty assert to Option type
1 parent fd7aac6 commit 2136051

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed

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

Lines changed: 12 additions & 0 deletions
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.OptionShouldBeEmpty.Companion.shouldBeEmpty
45
import `in`.rcard.assertj.arrowcore.errors.OptionShouldBePresent.Companion.shouldBePresent
56
import org.assertj.core.api.AbstractObjectAssert
67

@@ -27,4 +28,15 @@ abstract class AbstractOptionAssert<
2728
if (actual.isEmpty()) throwAssertionError(shouldBePresent())
2829
return myself
2930
}
31+
32+
/**
33+
* Verifies that the actual [Option] is empty.
34+
*
35+
* @return this assertion object.
36+
*/
37+
fun isEmpty(): SELF {
38+
isNotNull
39+
if (actual.isDefined()) throwAssertionError(shouldBeEmpty(actual))
40+
return myself
41+
}
3042
}

src/main/kotlin/in/rcard/assertj/arrowcore/errors/EitherShouldContain.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ internal class EitherShouldContain(message: String, actual: Either<Any, Any>, ex
2929
*/
3030
internal fun <LEFT : Any, RIGHT : Any> shouldContainOnRight(
3131
actual: Either<LEFT, RIGHT>,
32-
expectedValue: RIGHT
32+
expectedValue: RIGHT,
3333
): EitherShouldContain = EitherShouldContain(
3434
EXPECTING_TO_CONTAIN_ON_RIGHT,
3535
actual,
36-
expectedValue
36+
expectedValue,
3737
)
3838

3939
/**
@@ -47,11 +47,11 @@ internal class EitherShouldContain(message: String, actual: Either<Any, Any>, ex
4747
*/
4848
internal fun <LEFT : Any, RIGHT : Any> shouldContainOnLeft(
4949
actual: Either<LEFT, RIGHT>,
50-
expectedValue: RIGHT
50+
expectedValue: RIGHT,
5151
): EitherShouldContain = EitherShouldContain(
5252
EXPECTING_TO_CONTAIN_ON_LEFT,
5353
actual,
54-
expectedValue
54+
expectedValue,
5555
)
5656
}
5757
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 an [Option] should be empty.
8+
*
9+
* @author Riccardo Cardin
10+
*/
11+
internal class OptionShouldBeEmpty(expected: Option<*>) :
12+
BasicErrorMessageFactory("%nExpecting an Option to be empty but was <%s>.", expected.orNull()) {
13+
companion object {
14+
internal fun shouldBeEmpty(actual: Option<*>): OptionShouldBeEmpty =
15+
OptionShouldBeEmpty(actual)
16+
}
17+
}

src/main/kotlin/in/rcard/assertj/arrowcore/errors/OptionShouldBePresent.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class OptionShouldBePresent :
1717
*
1818
* @return a error message factory.
1919
*/
20-
fun shouldBePresent(): OptionShouldBePresent {
21-
return OptionShouldBePresent()
22-
}
20+
fun shouldBePresent(): OptionShouldBePresent = OptionShouldBePresent()
2321
}
2422
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.OptionShouldBeEmpty.Companion.shouldBeEmpty
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_isEmpty_Test {
13+
14+
@Test
15+
internal fun `should pass if Option is empty`() {
16+
assertThat(None).isEmpty()
17+
}
18+
19+
@Test
20+
internal fun `should fail when Option is null`() {
21+
val nullOption: Option<Nothing>? = null
22+
Assertions.assertThatThrownBy { assertThat(nullOption).isEmpty() }
23+
.isInstanceOf(AssertionError::class.java)
24+
.hasMessage(FailureMessages.actualIsNull())
25+
}
26+
27+
@Test
28+
internal fun `should fail if Option is present`() {
29+
val actual: Option<String> = "not-empty".some()
30+
Assertions.assertThatThrownBy { assertThat(actual).isEmpty() }
31+
.isInstanceOf(AssertionError::class.java)
32+
.hasMessage(shouldBeEmpty(actual).create())
33+
}
34+
}

0 commit comments

Comments
 (0)