All Downloads are FREE. Search and download functionalities are using the official Maven repository.

in.rcard.assertj.arrowcore.RaiseAssert.kt Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
@file:OptIn(ExperimentalTypeInference::class)

package `in`.rcard.assertj.arrowcore

import arrow.core.raise.Raise
import arrow.core.raise.fold
import `in`.rcard.assertj.arrowcore.errors.RaiseShouldThrowAnException.Companion.shouldThrowAnException
import org.assertj.core.api.AbstractThrowableAssert
import org.assertj.core.api.Assertions
import org.assertj.core.internal.Failures
import kotlin.experimental.ExperimentalTypeInference

/**
 * Assertions for functions within a [Raise] context.
 *
 * @param VALUE type of the value returned by the function.
 * @param ERROR type of the logical error raised by the function.
 * @author Riccardo Cardin
 *
 * @since 0.2.0
 */
class RaiseAssert private constructor(lambda: Raise.() -> VALUE) :
    AbstractRaiseAssert, ERROR, VALUE>(lambda) {
    companion object {
        fun  assertThat(
            @BuilderInference lambda: Raise.() -> VALUE
        ): RaiseAssert =
            RaiseAssert(lambda)

        /**
         * Verifies that the function in the [Raise] context throws an exception.
         * @param shouldRaiseThrowable the function to be executed in the [Raise] context.
         * @return the [AbstractThrowableAssert] to be used to verify the exception.
         */
        fun  assertThatThrownBy(
            @BuilderInference shouldRaiseThrowable: Raise.() -> VALUE
        ): AbstractThrowableAssert<*, out Throwable> {
            val throwable: Throwable? = fold(block = shouldRaiseThrowable,
                recover = { null },
                transform = { null },
                catch = { exception -> exception })

            @Suppress("KotlinConstantConditions")
            return throwable?.let { return Assertions.assertThat(throwable) } ?: throw Failures.instance()
                .failure(Assertions.assertThat(throwable).writableAssertionInfo, shouldThrowAnException())
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy