commonMain.org.kodein.mock.ArgConstraintsBuilder.kt Maven / Gradle / Ivy
package org.kodein.mock
import kotlin.reflect.KClass
public class ArgConstraintsBuilder internal constructor() {
private val constraints: MutableList> = ArrayList()
internal fun getConstraints(args: Array<*>): List> {
val list = when {
constraints.size == args.size -> constraints.toList()
constraints.isEmpty() -> args.map { if (it == null) ArgConstraint.isNull() else ArgConstraint.isEqual(it) }
else -> throw Mocker.MockingException("You cannot mix literal values and constraints. Please replace all literal values by their constraint counterpart (isEqual(value) or isNull()).")
}
constraints.clear()
return list
}
@PublishedApi
internal fun toReturn(constraint: ArgConstraint, cls: KClass<*>): T {
constraints.add(constraint)
@Suppress("UNCHECKED_CAST", "RemoveRedundantCallsOfConversionMethods", "IMPLICIT_CAST_TO_ANY")
return when (cls) {
Boolean::class -> false
UByte::class -> 0.toUByte()
Byte::class -> 0.toByte()
UShort::class -> 0.toUShort()
Short::class -> 0.toShort()
Char::class -> 0.toChar()
UInt::class -> 0.toUInt()
Int::class -> 0.toInt()
Float::class -> 0.toFloat()
ULong::class -> 0.toULong()
Long::class -> 0.toLong()
Double::class -> 0.toDouble()
else -> cls.unsafeValue()
} as T
}
public inline fun isAny(capture: MutableList? = null): T = toReturn(ArgConstraint.isAny(capture), T::class)
public inline fun isEqual(expected: T, capture: MutableList? = null): T = toReturn(ArgConstraint.isEqual(expected, capture), T::class)
public inline fun isNotEqual(expected: T, capture: MutableList? = null): T = toReturn(ArgConstraint.isNotEqual(expected, capture), T::class)
public inline fun isSame(expected: T, capture: MutableList? = null): T = toReturn(ArgConstraint.isSame(expected, capture), T::class)
public inline fun isNotSame(expected: T, capture: MutableList? = null): T = toReturn(ArgConstraint.isNotSame(expected, capture), T::class)
public inline fun isNull(capture: MutableList? = null): T = toReturn(ArgConstraint.isNull(capture), T::class)
public inline fun isNotNull(capture: MutableList? = null): T = toReturn(ArgConstraint.isNotNull(capture), T::class)
public inline fun isValid(constraint: ArgConstraint): T = toReturn(constraint, T::class)
@Suppress("UNCHECKED_CAST")
public inline fun isValid(capture: MutableList? = null, description: String = "isValid", noinline test: (T) -> ArgConstraint.Result): T = isValid(ArgConstraint(capture, description, test as (Any?) -> ArgConstraint.Result))
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy