-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assertions for io.strikt:strikt-core (#65)
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
description = "ForkHandles Library Testing Helpers (Strikt)" | ||
|
||
dependencies { | ||
implementation(project(":result4k")) | ||
implementation("io.strikt:strikt-core:_") | ||
|
||
testImplementation(project(path= ":result4k", configuration= "testArtifacts")) | ||
} |
30 changes: 30 additions & 0 deletions
30
result4k/strikt/src/main/kotlin/dev/forkhandles/result4k/strikt/matchers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dev.forkhandles.result4k.strikt | ||
|
||
import dev.forkhandles.result4k.Failure | ||
import dev.forkhandles.result4k.Success | ||
import strikt.api.Assertion | ||
import strikt.assertions.isA | ||
import strikt.assertions.isEqualTo | ||
|
||
fun Assertion.Builder<*>.isSuccess() = | ||
isA<Success<*>>() | ||
|
||
fun Assertion.Builder<*>.isFailure() = | ||
isA<Failure<*>>() | ||
|
||
@JvmName("isSuccessOfInstance") | ||
inline fun <reified T> Assertion.Builder<*>.isSuccess() = | ||
isA<Success<T>>().and { get { value }.isA<T>() } | ||
|
||
@JvmName("isFailureOfInstance") | ||
inline fun <reified E> Assertion.Builder<*>.isFailure() = | ||
isA<Failure<E>>().and { get { reason }.isA<E>() } | ||
|
||
@JvmName("isSuccessOfInstanceAndValue") | ||
inline fun <reified T> Assertion.Builder<*>.isSuccess(expected: T) = | ||
isA<Success<T>>().and { get { value }.isEqualTo(expected) } | ||
|
||
@JvmName("isFailureOfInstanceAndValue") | ||
inline fun <reified E> Assertion.Builder<*>.isFailure(expected: E) = | ||
isA<Failure<E>>().and { get { reason }.isEqualTo(expected) } | ||
|
84 changes: 84 additions & 0 deletions
84
result4k/strikt/src/test/kotlin/dev/forkhandles/result4k/strikt/MatchersTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package dev.forkhandles.result4k.strikt | ||
|
||
import dev.forkhandles.result4k.Failure | ||
import dev.forkhandles.result4k.Success | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import strikt.api.expectThat | ||
import strikt.api.expectThrows | ||
import strikt.assertions.isEqualTo | ||
|
||
class MatchersTest { | ||
|
||
@Test | ||
fun `should correctly assert when Success`() { | ||
val actualValue = "Successful" | ||
val actualResult = Success(actualValue) | ||
|
||
assertDoesNotThrow { expectThat(actualResult).isSuccess() } | ||
assertDoesNotThrow { expectThat(actualResult).isSuccess<String>() } | ||
assertDoesNotThrow { expectThat(actualResult).isSuccess(actualValue) } | ||
} | ||
|
||
@Test | ||
fun `should correctly assert when Failure`() { | ||
val actualValue = "Failed" | ||
val actualResult = Failure(actualValue) | ||
|
||
assertDoesNotThrow { expectThat(actualResult).isFailure() } | ||
assertDoesNotThrow { expectThat(actualResult).isFailure<String>() } | ||
assertDoesNotThrow { expectThat(actualResult).isFailure(actualValue) } | ||
} | ||
|
||
@Test | ||
fun `should correctly assert when Success but expecting Failure`() { | ||
val actualValue = "Test successful" | ||
val actualResult = Success(actualValue) | ||
val expectedMessage = | ||
"Expect that Success(value=Test successful) is an instance of dev.forkhandles.resultk.Failure found dev.forkhandles.resultk.Success" | ||
|
||
expectAssertionError(expectedMessage) { | ||
expectThat(actualResult).isFailure() | ||
} | ||
|
||
expectAssertionError(expectedMessage) { | ||
expectThat(actualResult).isFailure<String>() | ||
} | ||
|
||
expectAssertionError(expectedMessage) { | ||
expectThat(actualResult).isFailure(actualValue) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should correctly assert when Failure but expecting Success`() { | ||
val actualValue = "Test failed" | ||
val actualResult = Failure(actualValue) | ||
val expectedMessage = | ||
"Expect that Failure(reason=Test failed) is an instance of dev.forkhandles.resultk.Success found dev.forkhandles.resultk.Failure" | ||
|
||
expectAssertionError(expectedMessage) { | ||
expectThat(actualResult).isSuccess() | ||
} | ||
|
||
expectAssertionError(expectedMessage) { | ||
expectThat(actualResult).isSuccess<String>() | ||
} | ||
|
||
expectAssertionError(expectedMessage) { | ||
expectThat(actualResult).isSuccess(actualValue) | ||
} | ||
} | ||
|
||
private fun expectAssertionError(message: String, block: () -> Unit) = | ||
expectThrows<AssertionError> { block() } | ||
.and { get { subject.formatterMessage }.isEqualTo(message) } | ||
|
||
private val AssertionError.formatterMessage | ||
get() = | ||
message | ||
?.replace("[^\\p{L}.()\\[\\] =]+".toRegex(), "") | ||
?.replace(" +".toRegex(), " ") | ||
?.trim() | ||
.also { println(it) } | ||
} |
40 changes: 40 additions & 0 deletions
40
result4k/strikt/src/test/kotlin/dev/forkhandles/result4k/strikt/WeatherExampleKotest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package dev.forkhandles.result4k.strikt | ||
|
||
import dev.forkhandles.result4k.Weather | ||
import dev.forkhandles.result4k.WeatherError | ||
import dev.forkhandles.result4k.getWeather | ||
import org.junit.jupiter.api.Test | ||
import strikt.api.expectThat | ||
import java.math.BigDecimal | ||
|
||
class WeatherExampleKotest { | ||
@Test | ||
fun `assert any success`() { | ||
expectThat(getWeather(20)).isSuccess() | ||
} | ||
|
||
@Test | ||
fun `assert type success`() { | ||
expectThat(getWeather(20)).isSuccess<Weather>() | ||
} | ||
|
||
@Test | ||
fun `assert exact success`() { | ||
expectThat(getWeather(20)).isSuccess(Weather(BigDecimal("295.15"), 101_390)) | ||
} | ||
|
||
@Test | ||
fun `assert any failure`() { | ||
expectThat(getWeather(9001)).isFailure() | ||
} | ||
|
||
@Test | ||
fun `assert type failure`() { | ||
expectThat(getWeather(9001)).isFailure<WeatherError>() | ||
} | ||
|
||
@Test | ||
fun `assert exact failure`() { | ||
expectThat(getWeather(9001)).isFailure(WeatherError(404, "unsupported location")) | ||
} | ||
} |