Skip to content

Commit 54c1fdf

Browse files
author
Simon Frost
committed
Add test for satisfying fun
1 parent 0bd47b9 commit 54c1fdf

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
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+
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(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(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(actual).hasRightValueSatisfying { assertThat(it).isEqualTo(24) } }
33+
.isInstanceOf(AssertionError::class.java)
34+
.hasMessage(("\nexpected: \"24\"\n but was: \"42\""))
35+
}
36+
37+
@Test
38+
fun should_pass_if_consumer_passes() {
39+
val actual: Either<Int, String> = Either.Left(42)
40+
EitherAssert(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+
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(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(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(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(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)