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

commonMain.dev.mokkery.internal.verify.render.MatchersStatusRenderer.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.verify.render

import dev.mokkery.internal.render.Renderer
import dev.mokkery.internal.render.ToStringRenderer
import dev.mokkery.internal.render.ValueDescriptionRenderer
import dev.mokkery.internal.templating.CallTemplate
import dev.mokkery.internal.tracing.CallArg
import dev.mokkery.internal.tracing.CallTrace
import dev.mokkery.matcher.ArgMatcher

internal class MatchersStatusRenderer(
    private val matcherRenderer: Renderer> = ToStringRenderer,
    private val valueRenderer: Renderer = ValueDescriptionRenderer
) : Renderer> {

    override fun render(value: Pair): String {
        val (template, trace) = value
        return buildString {
            trace.args.forEach {
                append(it.describeMatchingAgainst(template.matchers[it.name]))
            }
        }
    }

    private fun CallArg.describeMatchingAgainst(matcher: ArgMatcher?): String = buildString {
        val matches = matcher?.matches(value) == true
        val status = if (matches) "[+]" else "[-]"
        val statusLine = "$status $name:"
        val matcherRendered = matcher?.let(matcherRenderer::render) ?: "null"
        append(statusLine)
        if (matches) {
            appendLine(" $matcherRendered ~ ${valueRenderer.render(value)}")
        } else {
            appendLine()
            appendLine("   expect: $matcherRendered")
            appendLine("   actual: ${valueRenderer.render(value)}")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy