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

main.kotlin.ch.tutteli.atrium.logic.impl.DefaultAnyAssertions.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package ch.tutteli.atrium.logic.impl

import ch.tutteli.atrium.assertions.Assertion
import ch.tutteli.atrium.assertions.builders.assertionBuilder
import ch.tutteli.atrium.assertions.builders.invisibleGroup
import ch.tutteli.atrium.core.None
import ch.tutteli.atrium.core.Some
import ch.tutteli.atrium.core.falseProvider
import ch.tutteli.atrium.creating.AssertionContainer
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.logic.*
import ch.tutteli.atrium.logic.creating.transformers.SubjectChangerBuilder
import ch.tutteli.atrium.reporting.Text
import ch.tutteli.atrium.translations.DescriptionAnyExpectation.*
import kotlin.reflect.KClass

class DefaultAnyAssertions : AnyAssertions {
    override fun  toBe(container: AssertionContainer, expected: T): Assertion =
        container.createDescriptiveAssertion(TO_EQUAL, expected) { it == expected }

    override fun  notToBe(container: AssertionContainer, expected: T): Assertion =
        container.createDescriptiveAssertion(NOT_TO_EQUAL, expected) { it != expected }

    override fun  isSameAs(container: AssertionContainer, expected: T): Assertion =
        container.createDescriptiveAssertion(TO_BE_THE_INSTANCE, expected) { it === expected }

    override fun  isNotSameAs(container: AssertionContainer, expected: T): Assertion =
        container.createDescriptiveAssertion(NOT_TO_BE_THE_INSTANCE, expected) { it !== expected }

    override fun  toBeNullIfNullGivenElse(
        container: AssertionContainer,
        assertionCreatorOrNull: (Expect.() -> Unit)?
    ): Assertion =
        if (assertionCreatorOrNull == null) {
            container.toBe(null)
        } else {
            val collectSubject = container.maybeSubject.flatMap { if (it != null) Some(it) else None }
            val assertion = container.collectBasedOnSubject(collectSubject) {
                _logic.appendAsGroup(assertionCreatorOrNull)
            }
            //TODO 0.19.0 this is a pattern which occurs over and over again, maybe incorporate into collect?
            container.maybeSubject.fold(
                {
                    // already in an explanatory assertion context, no need to wrap it again
                    assertion
                },
                {
                    if (it != null) {
                        assertion
                    } else {
                        assertionBuilder.explanatoryGroup
                            .withDefaultType
                            .withAssertion(assertion)
                            .failing
                            .build()
                    }
                }
            )
        }

    override fun  notToBeNullButOfType(
        container: AssertionContainer,
        subType: KClass
    ): SubjectChangerBuilder.ExecutionStep = container.isA(subType)

    override fun  isA(
        container: AssertionContainer,
        subType: KClass
    ): SubjectChangerBuilder.ExecutionStep =
        container.changeSubject.reportBuilder()
            .downCastTo(subType)
            .build()

    override fun  isNotIn(container: AssertionContainer, expected: Iterable): Assertion {
        val assertions = expected.map { value ->
            assertionBuilder.representationOnly
                .withTest(container.toExpect()) { it != value }
                .withRepresentation(value)
                .build()
        }
        return assertionBuilder.list
            .withDescriptionAndEmptyRepresentation(NOT_TO_EQUAL_ONE_IN)
            .withAssertions(assertions)
            .build()
    }

    override fun  because(
        container: AssertionContainer,
        reason: String,
        assertionCreator: Expect.() -> Unit
    ): Assertion {
        val assertion = container.collect(assertionCreator)
        return assertionBuilder.invisibleGroup.withAssertions(
            assertion,
            assertionBuilder.explanatoryGroup
                .withInformationType(withIndent = false)
                .withAssertion(assertionBuilder.createDescriptive(BECAUSE, Text(reason), falseProvider))
                .build()
        ).build()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy