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

jvmMain.com.bkahlert.kommons.test.junit.testEach.kt Maven / Gradle / Ivy

There is a newer version: 2.8.0
Show newest version
package com.bkahlert.kommons.test.junit

import com.bkahlert.kommons.test.SLF4J
import com.bkahlert.kommons.test.junit.DynamicTestDisplayNameGenerator.displayNameFor
import com.bkahlert.kommons.test.test
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.function.Executable
import java.util.stream.Stream

/**
 * Builds a single test
 * that [softly] checks the specified [assertions]
 * for each of the specified [subjects].
 *
 * The name for each container is heuristically derived but can also be explicitly specified using [testNamePattern]
 * which supports curly placeholders `{}` like [SLF4J] does.
 */
public fun  testEach(
    vararg subjects: T,
    testNamePattern: String? = null,
    softly: Boolean = true,
    assertions: Assertions,
): Stream = subjects.asList().testEach(testNamePattern, softly, assertions)

/**
 * Builds a single test
 * that [softly] checks the specified [assertions]
 * for each subject of this [Collection].
 *
 * The name for each container is heuristically derived but can also be explicitly specified using [testNamePattern]
 * which supports curly placeholders `{}` like [SLF4J] does.
 */
public fun  Iterable.testEach(
    testNamePattern: String? = null,
    softly: Boolean = true,
    assertions: Assertions,
): Stream {
    val testSourceUri = PathSource.currentUri
    return toList()
        .also { require(it.isNotEmpty()) { "At least one subject must be provided for testing." } }
        .map { subject ->
            val executable = if (softly) Executable { test { assertions(subject) } } else Executable { assertions(subject) }
            DynamicTest.dynamicTest(
                displayNameFor(subject, testNamePattern),
                testSourceUri,
                executable,
            )
        }.stream()
}

/**
 * Builds a single test
 * that [softly] checks the specified [assertions]
 * for each subject of this [Sequence].
 *
 * The name for each container is heuristically derived but can also be explicitly specified using [testNamePattern]
 * which supports curly placeholders `{}` like [SLF4J] does.
 */
public fun  Sequence.testEach(
    testNamePattern: String? = null,
    softly: Boolean = true,
    assertions: Assertions,
): Stream = toList().testEach(testNamePattern, softly, assertions)

/**
 * Builds a single test
 * that [softly] checks the specified [assertions]
 * for each entry of this [Map].
 *
 * The name for each container is heuristically derived but can also be explicitly specified using [testNamePattern]
 * which supports curly placeholders `{}` like [SLF4J] does.
 */
public fun  Map.testEach(
    testNamePattern: String? = null,
    softly: Boolean = true,
    assertions: Assertions>,
): Stream = entries.testEach(testNamePattern, softly, assertions)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy