in.specmatic.core.value.UseExampleDeclarations.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-core Show documentation
Show all versions of specmatic-core Show documentation
Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
Deprecation Notice for group ID "in.specmatic"
******************************************************************************************************
Updates for "specmatic-core" will no longer be available under the deprecated group ID "in.specmatic".
Please update your dependencies to use the new group ID "io.specmatic".
******************************************************************************************************
package `in`.specmatic.core.value
import `in`.specmatic.core.ExampleDeclarations
import `in`.specmatic.core.pattern.isPatternToken
data class UseExampleDeclarations(override val examples: Map = emptyMap(), override val messages: List = emptyList(), override val comment: String? = null) : 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 }
override fun withComment(comment: String?): ExampleDeclarations {
return this.copy(comment = comment)
}
}
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()
}
}