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

jvmMain.tech.skot.core.test.SKFunMock.kt Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
package tech.skot.core.test

import kotlin.test.assertTrue

open class SKFunMock(val name: String) {

    var calls: MutableList = mutableListOf()

    var returns: List = emptyList()

    fun willReturn(r: R) {
        returns = listOf(r)
    }

    fun willReturnList(rs: List) {
        returns = rs
    }

    var nextCallException: Exception? = null
    fun willFail(ex: Exception) {
        nextCallException = ex
    }

    operator fun invoke(c: C): R {
        calls.add(c)
        nextCallException?.let {
            nextCallException = null
            throw it
        }
        return getReturn()
    }

    fun assertCalled(rule:String = "") {
        assertTrue("$rule -> $name : la méthode doit avoir été appelé") {
            calls.isNotEmpty()
        }
    }

    open fun getReturn(): R {
        return when (returns.size) {
            0 -> throw Exception("You have to set returns of $name before function be called")
            1 -> returns.first()
            else -> {
                returns.first().also {
                    returns = returns.drop(1)
                }
            }
        }
    }
}

class SKFunUnitMock(name: String) : SKFunMock(name) {
    override fun getReturn() {
        return Unit
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy