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

commonMain.ch.tutteli.atrium.api.verbs.internal.atriumVerbs.kt Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package ch.tutteli.atrium.api.verbs.internal

import ch.tutteli.atrium._core
import ch.tutteli.atrium.api.verbs.internal.factory.InternalExpectationVerbs
import ch.tutteli.atrium.core.ExperimentalNewExpectTypes
import ch.tutteli.atrium.creating.*
import ch.tutteli.atrium.logic._logic
import ch.tutteli.atrium.logic.creating.RootExpectBuilder
import ch.tutteli.atrium.logic.manualFeature
import ch.tutteli.atrium.logic.toAssertionCreator
import ch.tutteli.atrium.logic.toExpectGrouping
import ch.tutteli.atrium.reporting.AtriumErrorAdjuster
import ch.tutteli.atrium.reporting.Text
import ch.tutteli.atrium.reporting.erroradjusters.MultiAtriumErrorAdjuster
import ch.tutteli.atrium.reporting.erroradjusters.RemoveAtriumFromAtriumError
import ch.tutteli.atrium.reporting.erroradjusters.RemoveRunnerFromAtriumError
import ch.tutteli.atrium.reporting.theming.text.TextIconStyler
import ch.tutteli.atrium.reporting.theming.text.impl.MarkdownTextIconStyler
import ch.tutteli.atrium.testfactories.TestFactoryBuilder
import ch.tutteli.atrium.testfactories.testFactoryTemplate


@OptIn(ExperimentalNewExpectTypes::class, ExperimentalComponentFactoryContainer::class)
fun  expect(subject: T): RootExpect =
    RootExpectBuilder.forSubject(subject)
        .withVerb("I expected subject")
        .withOptions {
            withComponent(AtriumErrorAdjuster::class) { c ->
                MultiAtriumErrorAdjuster(
                    c.build(),
                    RemoveAtriumButNotAtriumSpecsFromAtriumErrorImpl(),
                    otherAdjusters = emptyList()
                )
            }
            withComponent(TextIconStyler::class) {
                MarkdownTextIconStyler()
            }
        }
        .build()

fun  expect(subject: T, assertionCreator: Expect.() -> Unit): Expect =
    expect(subject)._core.appendAsGroupIndicateIfOneCollected(
        ExpectationCreatorWithUsageHints(
            // we don't have an alternative, we always expect expectations and hence we don't provide a failure hint
            // (proposing `expect(subject).` as alternative would be wrong as we also expect further expectations)
            usageHintsOverloadWithoutExpectationCreator = emptyList(),
            expectationCreator = assertionCreator
        )
    ).first


@OptIn(ExperimentalNewExpectTypes::class)
fun expectGrouped(
    description: String = "my expectations",
    configuration: RootExpectBuilder.OptionsChooser<*>.() -> Unit = {},
    groupingActions: ExpectGrouping.() -> Unit,
): ExpectGrouping = RootExpectBuilder.forSubject(Text.EMPTY)
    .withVerb(description)
    .withOptions {
        configuration()
    }
    .build()
    ._core.appendAsGroupIndicateIfOneCollected(
        ExpectationCreatorWithUsageHints(
            // we don't have an alternative, we always expect sub-expectations and hence we don't provide a failure hint
            usageHintsOverloadWithoutExpectationCreator = emptyList(),
            expectationCreator = groupingActions.toAssertionCreator()
        )
    ).first
    .toExpectGrouping()


fun  ExpectGrouping.expect(subject: R): Expect =
    expectWithinExpectGroup(subject).transform()

fun  ExpectGrouping.expect(subject: R, assertionCreator: Expect.() -> Unit): Expect =
    expectWithinExpectGroup(subject).transformAndAppend(assertionCreator)

@Suppress("DEPRECATION")
private fun  ExpectGrouping.expectWithinExpectGroup(subject: R) =
    //TODO 1.3.0 change to _core
    _logic.manualFeature("I expected subject") { subject }

expect class RemoveAtriumButNotAtriumSpecsFromAtriumErrorImpl() : RemoveAtriumFromAtriumError {
    override fun adjust(throwable: Throwable)
    override fun adjustOtherThanStacks(throwable: Throwable)
}

fun testFactory(setup: TestFactoryBuilder.() -> Unit) = testFactoryTemplate(setup, InternalExpectationVerbs)
fun testFactory(
    setup: TestFactoryBuilder.() -> Unit,
    vararg otherSetups: TestFactoryBuilder.() -> Unit
) = testFactoryTemplate(setup, otherSetups, InternalExpectationVerbs)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy