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

main.kotlin.ch.tutteli.atrium.logic.utils.mapArguments.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package ch.tutteli.atrium.logic.utils

import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.creating.PleaseUseReplacementException
import kotlin.js.JsName

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 *
 * For instance (there are more options as second step to choose from):
 * ```
 * fun  pairs(pair: Pair, vararg otherPairs: Pair) {
 *   mapArguments(pair, otherPairs).toExpect> { isKeyValue(it) }
 * }
 * ```
 */
fun  mapArguments(first: T, others: Array): ArgumentMapperBuilder = ArgumentMapperBuilder(first, others)

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(first: T, others: Array, mapper: (T) -> R): Pair> =
    mapArguments(first, others).to(mapper)

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 */
fun mapArguments(first: Byte, others: ByteArray): ArgumentMapperBuilder =
    mapArguments(first, others.toTypedArray())

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(first: Byte, others: ByteArray, mapper: (Byte) -> R): Pair> =
    mapArguments(first, others).to(mapper)

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 */
fun mapArguments(first: Char, others: CharArray): ArgumentMapperBuilder =
    mapArguments(first, others.toTypedArray())

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(first: Char, others: CharArray, mapper: (Char) -> R): Pair> =
    mapArguments(first, others).to(mapper)

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 */
fun mapArguments(first: Short, others: ShortArray): ArgumentMapperBuilder =
    mapArguments(first, others.toTypedArray())

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(first: Short, others: ShortArray, mapper: (Short) -> R): Pair> =
    mapArguments(first, others).to(mapper)

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 */
fun mapArguments(first: Int, others: IntArray): ArgumentMapperBuilder = mapArguments(first, others.toTypedArray())

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(first: Int, others: IntArray, mapper: (Int) -> R): Pair> =
    mapArguments(first, others).to(mapper)

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 */
fun mapArguments(first: Long, others: LongArray): ArgumentMapperBuilder =
    mapArguments(first, others.toTypedArray())

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(first: Long, others: LongArray, mapper: (Long) -> R): Pair> =
    mapArguments(first, others).to(mapper)

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 */
fun mapArguments(first: Float, others: FloatArray): ArgumentMapperBuilder =
    mapArguments(first, others.toTypedArray())

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(first: Float, others: FloatArray, mapper: (Float) -> R): Pair> =
    mapArguments(first, others).to(mapper)

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 */
fun mapArguments(first: Double, others: DoubleArray): ArgumentMapperBuilder =
    mapArguments(first, others.toTypedArray())

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(first: Double, others: DoubleArray, mapper: (Double) -> R): Pair> =
    mapArguments(first, others).to(mapper)

/**
 * Creates a [ArgumentMapperBuilder] with the given [first] and [others] as arguments;
 * specify the mapping as such using a subsequent step in the building process.
 */
fun mapArguments(first: Boolean, others: BooleanArray): ArgumentMapperBuilder =
    mapArguments(first, others.toTypedArray())

/**
 * Maps the given [first] and [others] to the given type [R] with the help of the given [mapper].
 *
 * Use the overload without `mapper` in case you want to map to an lambda with an [Expect] receiver
 * and use one of the available options as second step. For instance, `mapArguments(a, aX).toExpect { ... }`
 */
inline fun  mapArguments(
    first: Boolean,
    others: BooleanArray,
    mapper: (Boolean) -> R
): Pair> = mapArguments(first, others).to(mapper)

/**
 * Builder to map variable length arguments formulated as ([first]: [T], `vararg` [others] : [T]) to something else.
 */
class ArgumentMapperBuilder internal constructor(
    val first: T,
    val others: Array
) {

    /**
     * Maps each argument to an [Expect]<[R]> lambda with receiver
     * using the given [assertionCreator] (passing in the argument).
     *
     * @returns The mapped [first] and [others].
     */
    inline fun  toExpect(
        crossinline assertionCreator: Expect.(T) -> Unit
    ): Pair.() -> Unit, Array.() -> Unit>> = to { t -> expectLambda { assertionCreator(t) } }

    /**
     * Maps each argument to the given type [R] by using the given [mapper]
     *
     * @returns The mapped [first] and [others].
     */
    inline fun  to(mapper: (T) -> R): Pair> =
        mapper(first) to others.map { mapper(it) }.toTypedArray()
}

/**
 * Maps each argument to `null` if it is already `null` and if not, then ...
 *
 * ... what happens otherwise needs to be specified in the next step.
 *
 * Notice, a future version might constrain [T] with a lower bound `Nothing?`.
 *
 * @returns The mapped [ArgumentMapperBuilder.first] and [ArgumentMapperBuilder.others].
 */
fun  ArgumentMapperBuilder.toNullOr(): ArgumentToNullOrMapperBuilder =
    ArgumentToNullOrMapperBuilder(this)

@JsName("deprecatedToNullOr")
@Suppress("unused")
@Deprecated(
    "Since your arguments are not nullable it does not make sense to call this function - use the replacement.",
    ReplaceWith("toExpect(assertionCreator)")
)
fun  ArgumentMapperBuilder.toNullOr(): Nothing =
    throw PleaseUseReplacementException("Since your arguments are not nullable it does not make sense to call this function.")

/**
 * Builder to map variable length arguments formulated as `(first: T, vararg others : T)` to something else.
 *
 * Its usage is intended to turn the nullable argument type of the given [argumentMapperBuilder] into its non-nullable
 * type if the argument is not null in order that the non-nullable type can be used/passed to function which expect
 * a non-nullable type.
 */
class ArgumentToNullOrMapperBuilder(
    val argumentMapperBuilder: ArgumentMapperBuilder
) {
    /**
     * Maps each argument to `null` if it is already `null` and if not, then to an [Expect]<[R]>
     * lambda with receiver by using the given [assertionCreator] (passing in the argument).
     *
     * @returns The mapped arguments.
     */
    inline fun  toExpect(
        crossinline assertionCreator: Expect.(T) -> Unit
    ): Pair<(Expect.() -> Unit)?, Array.() -> Unit)?>> =
        argumentMapperBuilder.to { nullableT -> nullableT?.let { t -> expectLambda { assertionCreator(t) } } }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy