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

run.qontract.core.value.UseExampleDeclarations.kt Maven / Gradle / Ivy

Go to download

A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner

There is a newer version: 0.23.1
Show newest version
package run.qontract.core.value

import run.qontract.core.ExampleDeclarations
import run.qontract.core.pattern.isPatternToken

data class UseExampleDeclarations(override val examples: Map = emptyMap(), override val messages: List = emptyList()) : ExampleDeclarations {
    override fun plus(more: ExampleDeclarations): ExampleDeclarations {
        val duplicateMessage = messageWhenDuplicateKeysExist(more, examples)
        for(message in duplicateMessage)
            println(duplicateMessage)

        return this.copy(examples = examples.plus(more.examples.filterNot { isPatternToken(it.value) }), messages = messages.plus(more.messages).plus(duplicateMessage))
    }

    override fun plus(more: Pair): ExampleDeclarations = when {
        !isPatternToken(more.second) || more.second == "(null)"-> this.copy(examples = examples.plus(more))
        else -> this
    }

    override fun getNewName(typeName: String, keys: Collection): String =
            generateSequence(typeName) { "${it}_" }.first { it !in keys }
}

internal fun messageWhenDuplicateKeysExist(newExampleDeclarations: ExampleDeclarations, examples: Map): List {
    val duplicateKeys = newExampleDeclarations.examples.keys.filter { it in examples }.filter { key ->
        val oldValue = examples.getValue(key)
        val newValue = newExampleDeclarations.examples.getValue(key)

        oldValue != newValue
    }

    return when {
        duplicateKeys.isNotEmpty() -> {
            val keysCsv = duplicateKeys.joinToString(", ")
            listOf("Duplicate keys with different values found: $keysCsv")
        }
        else -> emptyList()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy