Skip to content

Commit

Permalink
assertions for io.strikt:strikt-core (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
potfur authored Aug 8, 2024
1 parent 876b407 commit 33ddef2
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
8 changes: 8 additions & 0 deletions result4k/strikt/build.gradle.kts
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"))
}
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) }

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) }
}
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"))
}
}

0 comments on commit 33ddef2

Please sign in to comment.