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

commonMain.dk.cachet.carp.test.Mock.kt Maven / Gradle / Ivy

Go to download

Helper classes relied upon by test projects of all subsystems. E.g., to disable tests specified in common part of projects for the JavaScript runtime only.

There is a newer version: 1.2.1
Show newest version
@file:Suppress( "TooManyFunctions", "ParameterListWrapping", "LongParameterList" )

package dk.cachet.carp.test

import kotlin.reflect.*


/**
 * Base class which can be used to implement a simple mock for the interface [TMock].
 * For now, this is very basic and solely supports tracking the last function call.
 */
open class Mock
{
    private val functionCalls: MutableMap> = mutableMapOf()


    fun  trackCall( function: KFunction1 ) =
        trackCallable( function )
    fun  trackSuspendCall( function: KSuspendFunction1 ) =
        trackCallable( function )
    fun  trackCall( function: KFunction2, arg1: T1 ) =
        trackCallable( function, arg1 as Any )
    fun  trackSuspendCall( function: KSuspendFunction2, arg1: T1 ) =
        trackCallable( function, arg1 as Any )
    fun  trackCall( function: KFunction3, arg1: T1, arg2: T2 ) =
        trackCallable( function, arg1 as Any, arg2 as Any )
    fun  trackSuspendCall( function: KSuspendFunction3, arg1: T1, arg2: T2 ) =
        trackCallable( function, arg1 as Any, arg2 as Any )
    fun  trackCall( function: KFunction4, arg1: T1, arg2: T2, arg3: T3 ) =
        trackCallable( function, arg1 as Any, arg2 as Any, arg3 as Any )
    fun  trackSuspendCall( function: KSuspendFunction4, arg1: T1, arg2: T2, arg3: T3 ) =
        trackCallable( function, arg1 as Any, arg2 as Any, arg3 as Any )
    fun  trackCall( function: KFunction5, arg1: T1, arg2: T2, arg3: T3, arg4: T4 ) =
        trackCallable( function, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any )
    fun  trackSuspendCall( function: KSuspendFunction5, arg1: T1, arg2: T2, arg3: T3, arg4: T4 ) =
        trackCallable( function, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any )
    fun  trackCall( function: KFunction6, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5 ) =
        trackCallable( function, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any, arg5 as Any )
    fun  trackSuspendCall( function: KSuspendFunction6, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5 ) =
        trackCallable( function, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any, arg5 as Any )

    fun  trackCallOverloaded( function: KFunction1, overloadIdentifier: String ) =
        trackCallableOverloaded( function, overloadIdentifier )
    fun  trackSuspendCallOverloaded( function: KSuspendFunction1, overloadIdentifier: String ) =
        trackCallableOverloaded( function, overloadIdentifier )
    fun  trackCallOverloaded( function: KFunction2, overloadIdentifier: String, arg1: T1 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any )
    fun  trackSuspendCallOverloaded( function: KSuspendFunction2, overloadIdentifier: String, arg1: T1 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any )
    fun  trackCallOverloaded( function: KFunction3, overloadIdentifier: String, arg1: T1, arg2: T2 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any )
    fun  trackSuspendCallOverloaded( function: KSuspendFunction3, overloadIdentifier: String, arg1: T1, arg2: T2 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any )
    fun  trackCallOverloaded( function: KFunction4, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any )
    fun  trackSuspendCallOverloaded( function: KSuspendFunction4, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any )
    fun  trackCallOverloaded( function: KFunction5, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3, arg4: T4 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any )
    fun  trackSuspendCallOverloaded( function: KSuspendFunction5, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3, arg4: T4 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any )
    fun  trackCallOverloaded( function: KFunction6, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any, arg5 as Any )
    fun  trackSuspendCallOverloaded( function: KSuspendFunction6, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5 ) =
        trackCallableOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any, arg5 as Any )

    private fun trackCallable( function: KCallable<*>, vararg arguments: Any )
    {
        functionCalls[ function.name ] = arguments
    }
    private fun trackCallableOverloaded( function: KCallable<*>, overloadIdentifier: String, vararg arguments: Any )
    {
        functionCalls[ "${function.name}-$overloadIdentifier" ] = arguments
    }


    fun  wasCalled( function: KFunction1 ): Boolean =
        wasCallableCalled( function )
    fun  wasSuspendCalled( function: KSuspendFunction1 ): Boolean =
        wasCallableCalled( function )
    fun  wasCalled( function: KFunction2, arg1: T1 ): Boolean =
        wasCallableCalled( function, arg1 as Any )
    fun  wasSuspendCalled( function: KSuspendFunction2, arg1: T1 ): Boolean =
        wasCallableCalled( function, arg1 as Any )
    fun  wasCalled( function: KFunction3, arg1: T1, arg2: T2 ): Boolean =
        wasCallableCalled( function, arg1 as Any, arg2 as Any )
    fun  wasSuspendCalled( function: KSuspendFunction3, arg1: T1, arg2: T2 ): Boolean =
        wasCallableCalled( function, arg1 as Any, arg2 as Any )
    fun  wasCalled( function: KFunction4, arg1: T1, arg2: T2, arg3: T3 ): Boolean =
        wasCallableCalled( function, arg1 as Any, arg2 as Any, arg3 as Any )
    fun  wasSuspendCalled( function: KSuspendFunction4, arg1: T1, arg2: T2, arg3: T3 ): Boolean =
        wasCallableCalled( function, arg1 as Any, arg2 as Any, arg3 as Any )
    fun  wasCalled( function: KFunction5, arg1: T1, arg2: T2, arg3: T3, arg4: T4 ): Boolean =
        wasCallableCalled( function, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any )
    fun  wasSuspendCalled( function: KSuspendFunction5, arg1: T1, arg2: T2, arg3: T3, arg4: T4 ): Boolean =
        wasCallableCalled( function, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any )
    fun  wasCalled( function: KFunction6, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5 ): Boolean =
        wasCallableCalled( function, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any, arg5 as Any )
    fun  wasSuspendCalled( function: KSuspendFunction6, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5 ): Boolean =
        wasCallableCalled( function, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any, arg5 as Any )

    fun  wasCalledOverloaded( function: KFunction1, overloadIdentifier: String ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier )
    fun  wasSuspendCalledOverloaded( function: KSuspendFunction1, overloadIdentifier: String ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier )
    fun  wasCalledOverloaded( function: KFunction2, overloadIdentifier: String, arg1: T1 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any )
    fun  wasSuspendCalledOverloaded( function: KSuspendFunction2, overloadIdentifier: String, arg1: T1 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any )
    fun  wasCalledOverloaded( function: KFunction3, overloadIdentifier: String, arg1: T1, arg2: T2 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any )
    fun  wasSuspendCalledOverloaded( function: KSuspendFunction3, overloadIdentifier: String, arg1: T1, arg2: T2 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any )
    fun  wasCalledOverloaded( function: KFunction4, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any )
    fun  wasSuspendCalledOverloaded( function: KSuspendFunction4, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any )
    fun  wasCalledOverloaded( function: KFunction5, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3, arg4: T4 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any )
    fun  wasSuspendCalledOverloaded( function: KSuspendFunction5, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3, arg4: T4 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any )
    fun  wasCalledOverloaded( function: KFunction6, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any, arg5 as Any )
    fun  wasSuspendCalledOverloaded( function: KSuspendFunction6, overloadIdentifier: String, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5 ): Boolean =
        wasCallableCalledOverloaded( function, overloadIdentifier, arg1 as Any, arg2 as Any, arg3 as Any, arg4 as Any, arg5 as Any )

    private fun wasCallableCalled( function: KCallable<*>, vararg expectedArguments: Any ): Boolean
    {
        val calledArguments = functionCalls[ function.name ] ?: return false

        return calledArguments.contentEquals( expectedArguments )
    }
    private fun wasCallableCalledOverloaded( function: KCallable<*>, overloadIdentifier: String, vararg expectedArguments: Any ): Boolean
    {
        val calledArguments = functionCalls[ function.name + "-" + overloadIdentifier ] ?: return false

        return calledArguments.contentEquals( expectedArguments )
    }

    fun  wasCalled( function: KCallable, overloadIdentifier: String? = null ): Boolean =
        if ( overloadIdentifier == null ) functionCalls.containsKey( function.name )
        else functionCalls.containsKey( "${function.name}-$overloadIdentifier" )
    fun  wasNotCalled( function: KCallable, overloadIdentifier: String? = null ): Boolean =
        !wasCalled( function, overloadIdentifier )

    fun reset()
    {
        functionCalls.clear()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy