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

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

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

import ch.tutteli.atrium.core.ExperimentalNewExpectTypes
import ch.tutteli.atrium.creating.ComponentFactory
import ch.tutteli.atrium.creating.ComponentFactoryContainer
import ch.tutteli.atrium.creating.ExperimentalComponentFactoryContainer
import ch.tutteli.atrium.creating.RootExpectOptions
import ch.tutteli.atrium.logic.creating.RootExpectBuilder
import ch.tutteli.atrium.reporting.translating.Translatable
import kotlin.reflect.KClass

@ExperimentalComponentFactoryContainer
@ExperimentalNewExpectTypes
class RootExpectOptionsChooserImpl : RootExpectBuilder.OptionsChooser {
    private var description: Translatable? = null
    private var representationInsteadOfSubject: ((T) -> Any)? = null
    private var components = mutableMapOf, ComponentFactory>()
    private var chainedComponents = mutableMapOf, Sequence>()

    override fun withVerb(verb: Translatable) {
        this.description = verb
    }

    override fun withRepresentation(representationProvider: (T) -> Any) {
        this.representationInsteadOfSubject = representationProvider
    }

    @ExperimentalComponentFactoryContainer
    override fun  withComponent(kClass: KClass, factory: (ComponentFactoryContainer) -> I) {
        components[kClass] = ComponentFactory(factory, producesSingleton = false)
    }

    @ExperimentalComponentFactoryContainer
    override fun  withSingletonComponent(kClass: KClass, factory: (ComponentFactoryContainer) -> I) {
        components[kClass] = ComponentFactory(factory, producesSingleton = true)
    }

    @ExperimentalComponentFactoryContainer
    override fun  prependChainedComponents(kClass: KClass, factories: Sequence) {
        chainedComponents[kClass] = factories
    }

    fun build(): RootExpectOptions =
        RootExpectOptions(
            description,
            representationInsteadOfSubject,
            ComponentFactoryContainer.createIfNotEmpty(components, chainedComponents)
        )


}