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

commonMain.org.kodein.mock.ArgConstraint.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package org.kodein.mock


public class ArgConstraint(internal val capture: MutableList? = null, internal val description: String = "?", internal val test: (T) -> Result) {

    public sealed class Result {
        public object Success : Result()
        public class Failure(public val error: () -> String) : Result()
    }

    public companion object {
        private fun result(success: Boolean, error: () -> String) = if (success) Result.Success else Result.Failure(error)

        public fun  isAny(capture: MutableList? = null): ArgConstraint = ArgConstraint(capture, "isAny") { Result.Success }
        public fun  isEqual(expected: T, capture: MutableList? = null): ArgConstraint = ArgConstraint(capture, "isEqual($expected)") { actual -> result(actual == expected) { "Expected <$expected>, actual <$actual>" } }
        public fun  isNotEqual(expected: T, capture: MutableList? = null): ArgConstraint = ArgConstraint(capture, "isNotEqual($expected)") { actual -> result(actual != expected) { "Illegal value: <$actual>" } }
        public fun  isSame(expected: T, capture: MutableList? = null): ArgConstraint = ArgConstraint(capture, "isSame($expected)") { actual -> result(actual === expected) { "Expected <$expected>, actual <$actual> is not same" } }
        public fun  isNotSame(expected: T, capture: MutableList? = null): ArgConstraint = ArgConstraint(capture, "isNotSame($expected)") { actual -> result(actual !== expected) { "Expected not same as <$actual>" } }
        public fun  isNull(capture: MutableList? = null): ArgConstraint = ArgConstraint(capture, "isNull") { actual -> result(actual == null) { "Expected value to be null, but was: <$actual>" } }
        public fun  isNotNull(capture: MutableList? = null): ArgConstraint = ArgConstraint(capture, "isNotNull") { actual -> result(actual != null) { "Expected value to be not null" } }
    }
}

internal fun  ArgConstraint.isValid(arg: T): Boolean = test(arg) is ArgConstraint.Result.Success

internal fun  ArgConstraint.assert(name: String, arg: T) { (test(arg) as? ArgConstraint.Result.Failure)?.let { throw AssertionError("$name: ${it.error()}") } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy