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

commonMain.dev.mokkery.internal.names.GroupMockReceiverShortener.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.names

import dev.mokkery.internal.MockUniqueReceiversGenerator
import dev.mokkery.internal.templating.CallTemplate
import dev.mokkery.internal.tracing.CallTrace
import kotlin.collections.associateByTo
import kotlin.collections.getValue
import kotlin.collections.map
import kotlin.collections.mapTo
import kotlin.collections.toList
import kotlin.text.removePrefix

internal class GroupMockReceiverShortener(
    private val namesShortener: NameShortener = NameShortener.default,
    private val receiversGenerator: MockUniqueReceiversGenerator = MockUniqueReceiversGenerator
) {

    private lateinit var names: Map
    private lateinit var callsMapping: Map

    fun prepare(calls: List, templates: List) {
        val names = mutableSetOf()
        calls.mapTo(names) { receiversGenerator.extractType(it.receiver) }
        templates.mapTo(names) { receiversGenerator.extractType(it.receiver) }
        this.names = namesShortener.shorten(names)
    }

    fun shortenTemplates(templates: List): List = templates.map {
        val noIdReceiver = receiversGenerator.extractType(it.receiver)
        val id = it.receiver.removePrefix(noIdReceiver)
        it.copy(receiver = "${names.getValue(noIdReceiver)}$id")
    }

    fun shortenTraces(calls: List): List {
        val callsMapping = linkedMapOf()
        calls.associateByTo(callsMapping) {
            val noIdReceiver = receiversGenerator.extractType(it.receiver)
            val id = it.receiver.removePrefix(noIdReceiver)
            it.copy(receiver = "${names.getValue(noIdReceiver)}$id")
        }
        this.callsMapping = callsMapping
        return callsMapping.keys.toList()
    }

    fun getOriginalTrace(trace: CallTrace): CallTrace = callsMapping.getValue(trace)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy