Skip to content

Commit 7ccc11f

Browse files
committed
[30] Added the RaiseAssert.fails() assertion
1 parent 64919da commit 7ccc11f

File tree

6 files changed

+103
-41
lines changed

6 files changed

+103
-41
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package `in`.rcard.assertj.arrowcore
22

33
import arrow.core.raise.Raise
44
import arrow.core.raise.fold
5-
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeded.Companion.shouldFailButSucceeded
5+
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeds.Companion.shouldFailButSucceedsWith
6+
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeds.Companion.shouldFailWithButSucceedsWith
67
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailWith.Companion.shouldFailWith
78
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedButFailed.Companion.shouldSucceedButFailed
89
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldSucceedWith.Companion.shouldSucceedWith
@@ -62,7 +63,7 @@ abstract class AbstractRaiseAssert<
6263
*
6364
* @see succeedsWith
6465
*/
65-
fun succeeded() {
66+
fun succeeds() {
6667
fold(
6768
block = actual,
6869
recover = { actualError: ERROR -> throwAssertionError(shouldSucceedButFailed(actualError)) },
@@ -84,7 +85,24 @@ abstract class AbstractRaiseAssert<
8485
},
8586
transform = { actualValue ->
8687
throwAssertionError(
87-
shouldFailButSucceeded(expectedError, actualValue),
88+
shouldFailWithButSucceedsWith(expectedError, actualValue),
89+
)
90+
},
91+
)
92+
}
93+
94+
/**
95+
* Verifies that the function in the [Raise] context fails, no matter the type of the logical error.
96+
*
97+
* @see raises
98+
*/
99+
fun fails() {
100+
fold(
101+
block = actual,
102+
recover = { _ -> },
103+
transform = { actualValue ->
104+
throwAssertionError(
105+
shouldFailButSucceedsWith(actualValue),
88106
)
89107
},
90108
)

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

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package `in`.rcard.assertj.arrowcore.errors
2+
3+
import org.assertj.core.error.BasicErrorMessageFactory
4+
5+
private const val SHOULD_FAIL_WITH_BUT_SUCCEEDS_WITH_MESSAGE: String =
6+
"%nExpecting the lambda to fail with:%n <%s> %nbut it succeeded with:%n <%s>"
7+
8+
private const val SHOULD_FAIL_BUT_SUCCEEDS_WITH_MESSAGE: String =
9+
"%nExpecting the lambda to fail<%s>, but it succeeded with:%n <%s>"
10+
11+
/**
12+
* Build error message when a lambda should fail with a logic error but it succeeded with a value.
13+
*
14+
* @author Riccardo Cardin
15+
* @since 0.2.0
16+
*/
17+
internal class RaiseShouldFailButSucceeds private constructor(
18+
expectedError: Any,
19+
actualSucceededValue: Any,
20+
message: String,
21+
) : BasicErrorMessageFactory(message, expectedError, actualSucceededValue) {
22+
companion object {
23+
internal fun shouldFailWithButSucceedsWith(
24+
expectedError: Any,
25+
actualSucceededValue: Any,
26+
): RaiseShouldFailButSucceeds =
27+
RaiseShouldFailButSucceeds(expectedError, actualSucceededValue, SHOULD_FAIL_WITH_BUT_SUCCEEDS_WITH_MESSAGE)
28+
29+
internal fun shouldFailButSucceedsWith(actualSucceededValue: Any): RaiseShouldFailButSucceeds =
30+
RaiseShouldFailButSucceeds("", actualSucceededValue, SHOULD_FAIL_BUT_SUCCEEDS_WITH_MESSAGE)
31+
}
32+
}
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 `in`.rcard.assertj.arrowcore.RaiseAssert.Companion.assertThat
4+
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeds.Companion.shouldFailButSucceedsWith
5+
import org.assertj.core.api.Assertions
6+
import org.junit.jupiter.api.Test
7+
8+
@Suppress("ktlint:standard:class-naming")
9+
internal class RaiseAssert_fails_Test {
10+
@Test
11+
internal fun `should pass if lambda raises a logical error`() {
12+
assertThat { Dummy.aFunctionThatRaisesAnError() }.fails()
13+
}
14+
15+
@Test
16+
internal fun `should fail if lambda succeeds with a value instead of failing`() {
17+
Assertions
18+
.assertThatThrownBy {
19+
assertThat { Dummy.aFunctionWithContext(42) }.fails()
20+
}.isInstanceOf(AssertionError::class.java)
21+
.hasMessage(
22+
shouldFailButSucceedsWith(42).create(),
23+
)
24+
}
25+
26+
@Test
27+
internal fun `should fail if lambda throws an exception`() {
28+
Assertions
29+
.assertThatThrownBy {
30+
assertThat { Dummy.aFunctionThatThrowsAnException() }.fails()
31+
}.isInstanceOf(RuntimeException::class.java)
32+
.hasMessage("AN EXCEPTION")
33+
}
34+
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
package `in`.rcard.assertj.arrowcore
22

33
import `in`.rcard.assertj.arrowcore.RaiseAssert.Companion.assertThat
4-
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeded.Companion.shouldFailButSucceeded
4+
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailButSucceeds.Companion.shouldFailWithButSucceedsWith
55
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldFailWith.Companion.shouldFailWith
66
import org.assertj.core.api.Assertions
77
import org.junit.jupiter.api.Test
88

99
internal class RaiseAssert_raises_Test {
10-
1110
@Test
1211
internal fun `should pass if lambda raises a logical error`() {
1312
assertThat { Dummy.aFunctionThatRaisesAnError() }.raises("LOGICAL ERROR")
1413
}
1514

1615
@Test
1716
internal fun `should fail if lambda raises a logical error different from the expected`() {
18-
Assertions.assertThatThrownBy {
19-
assertThat { Dummy.aFunctionThatRaisesAnError() }.raises("ANOTHER LOGICAL ERROR")
20-
}.isInstanceOf(AssertionError::class.java)
17+
Assertions
18+
.assertThatThrownBy {
19+
assertThat { Dummy.aFunctionThatRaisesAnError() }.raises("ANOTHER LOGICAL ERROR")
20+
}.isInstanceOf(AssertionError::class.java)
2121
.hasMessage(shouldFailWith("ANOTHER LOGICAL ERROR", "LOGICAL ERROR").create())
2222
}
2323

2424
@Test
2525
internal fun `should fail if lambda succeeds with a value instead of failing`() {
26-
Assertions.assertThatThrownBy {
27-
assertThat { Dummy.aFunctionWithContext(42) }.raises("LOGICAL ERROR")
28-
}.isInstanceOf(AssertionError::class.java)
26+
Assertions
27+
.assertThatThrownBy {
28+
assertThat { Dummy.aFunctionWithContext(42) }.raises("LOGICAL ERROR")
29+
}.isInstanceOf(AssertionError::class.java)
2930
.hasMessage(
30-
shouldFailButSucceeded("LOGICAL ERROR", 42).create(),
31+
shouldFailWithButSucceedsWith("LOGICAL ERROR", 42).create(),
3132
)
3233
}
3334

3435
@Test
3536
internal fun `should fail if lambda throws an exception`() {
36-
Assertions.assertThatThrownBy {
37-
assertThat { Dummy.aFunctionThatThrowsAnException() }.raises("LOGICAL ERROR")
38-
}.isInstanceOf(RuntimeException::class.java)
37+
Assertions
38+
.assertThatThrownBy {
39+
assertThat { Dummy.aFunctionThatThrowsAnException() }.raises("LOGICAL ERROR")
40+
}.isInstanceOf(RuntimeException::class.java)
3941
.hasMessage("AN EXCEPTION")
4042
}
4143
}

src/test/kotlin/in/rcard/assertj/arrowcore/RaiseAssert_suceeded_Test.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import org.junit.jupiter.api.Test
99
internal class RaiseAssert_suceeded_Test {
1010
@Test
1111
internal fun `should pass if lambda succeeds`() {
12-
assertThat { Dummy.aFunctionWithContext(42) }.succeeded()
12+
assertThat { Dummy.aFunctionWithContext(42) }.succeeds()
1313
}
1414

1515
@Test
1616
internal fun `should fail if lambda raises an error instead of succeeding`() {
1717
Assertions
1818
.assertThatThrownBy {
19-
assertThat { Dummy.aFunctionThatRaisesAnError() }.succeeded()
19+
assertThat { Dummy.aFunctionThatRaisesAnError() }.succeeds()
2020
}.isInstanceOf(AssertionError::class.java)
2121
.hasMessage(
2222
shouldSucceedButFailed("LOGICAL ERROR").create(),

0 commit comments

Comments
 (0)