CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-kotest--kotest-assertions-core-jvm

Core assertion and matcher library for Kotest testing framework

Pending
Overview
Eval results
Files

result.mddocs/

Result Matchers

Result matchers provide comprehensive testing support for Kotlin's Result<T> type, enabling precise validation of both successful values and failure exceptions with type-safe assertions.

Capabilities

Success Validation

Assert that a Result contains a successful value.

/**
 * Assert that this result is a success and return the wrapped value
 * @return The success value for further assertions
 */
fun <T> Result<T>.shouldBeSuccess(): T

/**
 * Assert that this result is a success containing the expected value
 * @param expected The expected success value to validate against
 * @return The success value for chaining
 */
infix fun <T> Result<T>.shouldBeSuccess(expected: T): T

/**
 * Assert that this result is a success and execute block with the value
 * @param block Function to execute with the success value
 * @return The success value for chaining
 */
infix fun <T> Result<T>.shouldBeSuccess(block: ((T) -> Unit)): T

/**
 * Assert that this result is not a success
 * @return The original Result for chaining
 */
fun <T> Result<T>.shouldNotBeSuccess(): Result<T>

Usage Examples:

import io.kotest.matchers.result.shouldBeSuccess
import io.kotest.matchers.result.shouldNotBeSuccess
import io.kotest.matchers.string.shouldStartWith

// Basic success validation
val successResult = Result.success("Hello World")
successResult.shouldBeSuccess() // Returns "Hello World"

// Success with expected value
successResult shouldBeSuccess "Hello World"

// Success with validation block
successResult shouldBeSuccess { value ->
    value shouldStartWith "Hello"
    value.length shouldBe 11
}

// Failure should not be success
val failureResult = Result.failure<String>(Exception("Error"))
failureResult.shouldNotBeSuccess()

Failure Validation

Assert that a Result contains a failure exception.

/**
 * Assert that this result is a failure and return the exception
 * @return The failure exception for further assertions
 */
fun Result<*>.shouldBeFailure(): Throwable

/**
 * Assert that this result is a failure with the expected exception
 * @param expected The expected exception to validate against
 * @return The failure exception for chaining
 */
infix fun Result<*>.shouldBeFailure(expected: Throwable): Throwable

/**
 * Assert that this result is a failure and execute block with the exception
 * @param block Function to execute with the failure exception
 * @return The failure exception for chaining
 */
infix fun Result<*>.shouldBeFailure(block: ((Throwable) -> Unit)): Throwable

/**
 * Assert that this result is a failure of specific exception type
 * @return The typed failure exception for chaining
 */
inline fun <reified T : Throwable> Result<*>.shouldBeFailure(): T

/**
 * Assert that this result is not a failure
 * @return The original Result for chaining
 */
fun Result<*>.shouldNotBeFailure(): Result<*>

Usage Examples:

import io.kotest.matchers.result.shouldBeFailure
import io.kotest.matchers.result.shouldNotBeFailure
import io.kotest.matchers.throwable.shouldHaveMessage

// Basic failure validation
val failureResult = Result.failure<String>(IllegalArgumentException("Invalid input"))
val exception = failureResult.shouldBeFailure()

// Failure with expected exception
val specificException = IllegalArgumentException("Invalid input")
failureResult shouldBeFailure specificException

// Failure with validation block
failureResult shouldBeFailure { error ->
    error shouldHaveMessage "Invalid input"
    error shouldBeInstanceOf<IllegalArgumentException>()
}

// Typed failure validation
val typedException = failureResult.shouldBeFailure<IllegalArgumentException>()

// Success should not be failure
val successResult = Result.success("Hello")
successResult.shouldNotBeFailure()

Common Patterns

import io.kotest.matchers.result.shouldBeSuccess
import io.kotest.matchers.result.shouldBeFailure
import io.kotest.matchers.string.shouldContain

// Testing function that returns Result<String>
fun parseData(input: String): Result<String> {
    return if (input.isNotBlank()) {
        Result.success(input.uppercase())
    } else {
        Result.failure(IllegalArgumentException("Input cannot be blank"))
    }
}

// Validate successful parsing
val result = parseData("hello")
result shouldBeSuccess { value ->
    value shouldContain "HELLO"
}

// Validate failure cases
val emptyResult = parseData("")
emptyResult.shouldBeFailure<IllegalArgumentException>()

// Chain further assertions on unwrapped values
val unwrapped = parseData("test").shouldBeSuccess()
unwrapped shouldBe "TEST"

Type Definitions

class SuccessMatcher<T>(val expected: T?) : Matcher<Result<T?>> {
    override fun test(value: Result<T?>): MatcherResult
}

class FailureMatcher<T : Throwable>(val expected: T) : Matcher<Result<*>> {
    override fun test(value: Result<*>): MatcherResult
}

class FailureTypeMatcher<T : Throwable>(val clazz: KClass<T>) : Matcher<Result<*>> {
    override fun test(value: Result<*>): MatcherResult
}

Install with Tessl CLI

npx tessl i tessl/maven-io-kotest--kotest-assertions-core-jvm

docs

collections.md

concurrency.md

core-dsl.md

datetime.md

filesystem.md

index.md

nondeterministic.md

primitives.md

reflection.md

result.md

strings.md

throwable.md

tuples.md

types.md

tile.json