commonMain.test.ValidationIssueAsserts.kt Maven / Gradle / Ivy
@file:JvmName("ValidationIssueAsserts")
package io.kform.test
import io.kform.ValidationIssue
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.toList
/** Whether an [expectedIssue] matches the [actualIssue]. */
private fun issuesMatch(expectedIssue: ValidationIssue, actualIssue: ValidationIssue): Boolean =
actualIssue.contains(expectedIssue)
/**
* Asserts that the [expectedIssues] match the [actualIssues]. The order in which the issues appear
* is not relevant.
*
* Matching issues are those where the actual issue [contains][ValidationIssue.contains] the
* expected issue.
*/
@JvmOverloads
public fun assertMatchingIssues(
expectedIssues: Iterable,
actualIssues: Iterable,
message: String? = null
) {
val expectedIssuesMap = expectedIssues.groupBy { it.code }
val actualIssuesMap = actualIssues.groupBy { it.code }
for (expectedIssue in expectedIssues) {
val similarActualIssues = actualIssuesMap[expectedIssue.code]
if (
similarActualIssues == null ||
!similarActualIssues.any { actualIssue -> issuesMatch(expectedIssue, actualIssue) }
) {
throw AssertionError(
messagePrefix(message) +
"Expected issue <$expectedIssue> does not match any actual issue.\n" +
"Expected issues: <$expectedIssues>, actual issues: <$actualIssues>."
)
}
}
for (actualIssue in actualIssues) {
val similarExpectedIssues = expectedIssuesMap[actualIssue.code]
if (
similarExpectedIssues == null ||
!similarExpectedIssues.any { expectedIssue ->
issuesMatch(expectedIssue, actualIssue)
}
) {
throw AssertionError(
messagePrefix(message) +
"Actual issue <$actualIssue> does not match any expected issue.\n" +
"Expected issues: <$expectedIssues>, actual issues: <$actualIssues>."
)
}
}
}
/**
* Asserts that the [expectedIssues] match the [actualIssues]. The order in which the issues appear
* is not relevant.
*
* Matching issues are those where the actual issue [contains][ValidationIssue.contains] the
* expected issue.
*/
@JvmOverloads
public suspend fun assertMatchingIssues(
expectedIssues: Iterable,
actualIssues: Flow,
message: String? = null
): Unit = assertMatchingIssues(expectedIssues, actualIssues.toList(), message)
/**
* Asserts that the [actualIssues] contains all matching [expectedIssues]. The order in which the
* issues appear is not relevant.
*
* Matching issues are those where the actual issue [contains][ValidationIssue.contains] the
* expected issue.
*/
@JvmOverloads
public fun assertContainsMatchingIssues(
expectedIssues: Iterable,
actualIssues: Iterable,
message: String? = null
) {
val actualIssuesMap = actualIssues.groupBy { it.code }
for (expectedIssue in expectedIssues) {
val similarActualIssues = actualIssuesMap[expectedIssue.code]
if (
similarActualIssues == null ||
!similarActualIssues.any { actualIssue -> issuesMatch(expectedIssue, actualIssue) }
) {
throw AssertionError(
messagePrefix(message) +
"Expected actual issues to contain expected issues.\n" +
"Expected issue <$expectedIssue> does not match any actual issue.\n" +
"Expected issues: <$expectedIssues>, actual issues: <$actualIssues>."
)
}
}
}
/**
* Asserts that the [actualIssues] contains all matching [expectedIssues]. The order in which the
* issues appear is not relevant.
*
* Matching issues are those where the actual issue [contains][ValidationIssue.contains] the
* expected issue.
*/
@JvmOverloads
public suspend fun assertContainsMatchingIssues(
expectedIssues: Iterable,
actualIssues: Flow,
message: String? = null
): Unit = assertContainsMatchingIssues(expectedIssues, actualIssues.toList(), message)
/**
* Asserts that [actualIssues] contains a matching [expectedIssue].
*
* Matching issues are those where the actual issue [contains][ValidationIssue.contains] the
* expected issue.
*/
@JvmOverloads
public fun assertContainsMatchingIssue(
expectedIssue: ValidationIssue,
actualIssues: Iterable,
message: String? = null
) {
for (actualIssue in actualIssues) {
if (issuesMatch(expectedIssue, actualIssue)) {
return
}
}
throw AssertionError(
messagePrefix(message) +
"Expected actual issues to contain expected issue.\n" +
"Expected issue: <$expectedIssue>, actual issues: <$actualIssues>."
)
}
/**
* Asserts that [actualIssues] contains a matching [expectedIssue].
*
* Matching issues are those where the actual issue [contains][ValidationIssue.contains] the
* expected issue.
*/
@JvmOverloads
public suspend fun assertContainsMatchingIssue(
expectedIssue: ValidationIssue,
actualIssues: Flow,
message: String? = null
): Unit = assertContainsMatchingIssue(expectedIssue, actualIssues.toList(), message)