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

commonMain.dev.mokkery.internal.templating.TemplatingScopeDataBinder.kt Maven / Gradle / Ivy

Go to download

Mokkery is a mocking library for Kotlin Multiplatform, easy to use, boilerplate-free and compiler plugin driven.

The newest version!
package dev.mokkery.internal.templating

import dev.mokkery.internal.MokkeryMockScope
import dev.mokkery.internal.dynamic.MokkeryScopeLookup

internal interface TemplatingScopeDataBinder {

    fun getDataFor(token: Int): RawTemplateData?

    fun bind(token: Int, obj: Any?): MokkeryMockScope?

    fun firstProperlyBoundedData(): RawTemplateData

    fun reset()
}


internal fun TemplatingScopeDataBinder(
    lookup: MokkeryScopeLookup = MokkeryScopeLookup.current,
): TemplatingScopeDataBinder = TemplatingScopeDataBinderImpl(lookup)

private class TemplatingScopeDataBinderImpl(
    private val lookup: MokkeryScopeLookup
) : TemplatingScopeDataBinder {

    private val data = mutableMapOf()
    private val bindings = mutableMapOf()

    override fun getDataFor(token: Int): RawTemplateData? {
        val binding = bindings[token]
        if (binding != null && lookup.resolve(binding) == null) return null
        return data.getOrPut(token) { RawTemplateData() }
    }

    override fun bind(token: Int, obj: Any?): MokkeryMockScope? {
        obj ?: return null
        bindings[token] = obj
        val scope = lookup.resolve(obj) as? MokkeryMockScope
        if (scope != null) getDataFor(token)
        return scope
    }

    override fun firstProperlyBoundedData(): RawTemplateData = bindings.keys.firstNotNullOf(::getDataFor)

    override fun reset() {
        data.clear()
        bindings.clear()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy