Skip to content

Commit

Permalink
Merge pull request #1490 from znsio/assertion_support_for_other_libs
Browse files Browse the repository at this point in the history
Assertion support for other libs
  • Loading branch information
joelrosario authored Dec 19, 2024
2 parents e2ccf88 + db8f343 commit 399d044
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions core/src/main/kotlin/io/specmatic/test/ExamplePreProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ object ExampleProcessor {
runningEntity = emptyMap()
}

private fun defaultIfNotExits(lookupKey: String, type: SubstitutionType = SubstitutionType.SIMPLE): Value {
fun defaultIfNotExits(lookupKey: String, type: SubstitutionType = SubstitutionType.SIMPLE): Value {
throw ContractException(breadCrumb = lookupKey, errorMessage = "Could not resolve ${lookupKey.quote()}, key does not exist in fact store")
}

private fun ifNotExitsToLookupPattern(lookupKey: String, type: SubstitutionType = SubstitutionType.SIMPLE): Value {
fun ifNotExitsToLookupPattern(lookupKey: String, type: SubstitutionType = SubstitutionType.SIMPLE): Value {
return when (type) {
SubstitutionType.SIMPLE -> StringValue("$($lookupKey)")
SubstitutionType.DELAYED_RANDOM -> StringValue("$delayedRandomSubstitutionKey($lookupKey)")
Expand Down Expand Up @@ -121,15 +121,7 @@ object ExampleProcessor {
return value.mapValues { (_, value) -> resolve(value, ifNotExists) }
}

private fun resolve(value: String, ifNotExists: (lookupKey: String, type: SubstitutionType) -> Value): String {
return value.ifSubstitutionToken { token, type ->
if (type == SubstitutionType.DELAYED_RANDOM && ifNotExists == ::ifNotExitsToLookupPattern ) {
return@ifSubstitutionToken ifNotExitsToLookupPattern(token, type).toStringLiteral()
} else getValue(token, type)?.toStringLiteral() ?: ifNotExists(token, type).toStringLiteral()
} ?: value
}

private fun resolve(value: Value, ifNotExists: (lookupKey: String, type: SubstitutionType) -> Value): Value {
fun resolve(value: Value, ifNotExists: (lookupKey: String, type: SubstitutionType) -> Value): Value {
return when (value) {
is StringValue -> resolve(value, ifNotExists)
is JSONObjectValue -> resolve(value, ifNotExists)
Expand All @@ -138,6 +130,14 @@ object ExampleProcessor {
}
}

private fun resolve(value: String, ifNotExists: (lookupKey: String, type: SubstitutionType) -> Value): String {
return value.ifSubstitutionToken { token, type ->
if (type == SubstitutionType.DELAYED_RANDOM && ifNotExists == ::ifNotExitsToLookupPattern ) {
return@ifSubstitutionToken ifNotExitsToLookupPattern(token, type).toStringLiteral()
} else getValue(token, type)?.toStringLiteral() ?: ifNotExists(token, type).toStringLiteral()
} ?: value
}

private fun resolve(value: StringValue, ifNotExists: (lookupKey: String, type: SubstitutionType) -> Value): Value {
return value.ifSubstitutionToken { token, type ->
if (type == SubstitutionType.DELAYED_RANDOM && ifNotExists == ::ifNotExitsToLookupPattern) {
Expand Down Expand Up @@ -170,6 +170,10 @@ object ExampleProcessor {
}
}

fun store(exampleValue: Value) {
runningEntity = exampleValue.toFactStore(prefix = "ENTITY")
}

private fun Value.ifContainsStoreToken(block: (storeType: StoreType) -> Unit) {
if (this !is JSONObjectValue) return

Expand All @@ -182,7 +186,7 @@ object ExampleProcessor {
}

/* PARSER HELPERS */
private fun Value.toFactStore(prefix: String = ""): Map<String, Value> {
fun Value.toFactStore(prefix: String = ""): Map<String, Value> {
return this.traverse(
prefix = prefix,
onScalar = { scalar, key -> mapOf(key to scalar) },
Expand Down
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.0.48
version=2.0.49

0 comments on commit 399d044

Please sign in to comment.