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

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

The newest version!
package tech.skot.core.test

import kotlinx.coroutines.flow.Flow
import tech.skot.core.SKLog
import tech.skot.model.DatedData
import tech.skot.model.SKData
import tech.skot.model.SKManualData

class SKDataMock(val name: String) : SKData {

    private var internalManual: SKManualData? = null

    var error: Exception? = null

    fun setValue(newVal: D) {
        val currentInternal = internalManual
        if (currentInternal == null) {
            internalManual = SKManualData(newVal)
        } else {
            currentInternal.value = newVal
        }
    }

    private val errorNotSetMessage: Exception by lazy {
        Exception("You have to set a value for SKData $name before it is watched")
    }

    override val flow: Flow?>
        get() = internalManual?.flow ?: throw errorNotSetMessage
    override val defaultValidity: Long
        get() = internalManual?.defaultValidity ?: throw errorNotSetMessage
    override val _current: DatedData?
        get() {
            val currentInternalManual = internalManual
            return error?.let { throw it } ?: if (currentInternalManual == null) {
                throw errorNotSetMessage
            } else {
                currentInternalManual._current
            }
        }

    override suspend fun update(): D {
        val currentInternalManual = internalManual
        return error?.let { throw it } ?: if (currentInternalManual == null) {
            throw errorNotSetMessage
        } else {
            currentInternalManual.update()
        }
    }

    override suspend fun fallBackValue(): D? {
        val currentInternalManual = internalManual
        return error?.let { throw it } ?: if (currentInternalManual == null) {
            throw errorNotSetMessage
        } else {
            currentInternalManual.fallBackValue()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy