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

main.kotlin.ch.tutteli.atrium.logic.impl.DefaultMapLikeAssertions.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.core.Option
import ch.tutteli.atrium.creating.AssertionContainer
import ch.tutteli.atrium.logic.MapLikeAssertions
import ch.tutteli.atrium.logic.createDescriptiveAssertion
import ch.tutteli.atrium.logic.creating.maplike.contains.MapLikeContains
import ch.tutteli.atrium.logic.creating.maplike.contains.searchbehaviours.NoOpSearchBehaviour
import ch.tutteli.atrium.logic.creating.maplike.contains.searchbehaviours.impl.NoOpSearchBehaviourImpl
import ch.tutteli.atrium.logic.creating.maplike.contains.steps.impl.EntryPointStepImpl
import ch.tutteli.atrium.logic.creating.transformers.FeatureExtractorBuilder
import ch.tutteli.atrium.logic.creating.typeutils.MapLike
import ch.tutteli.atrium.logic.extractFeature
import ch.tutteli.atrium.translations.DescriptionMapLikeExpectation.*

class DefaultMapLikeAssertions : MapLikeAssertions {
    override fun  builderContainsInMapLike(
        container: AssertionContainer,
        converter: (T) -> Map
    ): MapLikeContains.EntryPointStep =
        EntryPointStepImpl(container, converter, NoOpSearchBehaviourImpl())


    override fun  containsKey(
        container: AssertionContainer,
        converter: (T) -> Map,
        key: K
    ): Assertion =
        container.createDescriptiveAssertion(TO_CONTAIN_KEY, key) {
            converter(it).containsKey(key)
        }

    override fun  containsNotKey(
        container: AssertionContainer,
        converter: (T) -> Map,
        key: K
    ): Assertion =
        container.createDescriptiveAssertion(NOT_TO_CONTAIN_KEY, key) {
            converter(it).containsKey(key).not()
        }

    override fun  getExisting(
        container: AssertionContainer,
        converter: (T) -> Map,
        key: K
    ): FeatureExtractorBuilder.ExecutionStep =
        container.extractFeature
            .methodCall("get", key)
            .withRepresentationForFailure(KEY_DOES_NOT_EXIST)
            .withFeatureExtraction { extractKey(converter(it), key) }
            .withoutOptions()
            .build()

    private fun , V> extractKey(it: T, key: K): Option {
        return Option.someIf(it.containsKey(key)) {
            @Suppress(
                "UNCHECKED_CAST"
                /*
                UNCHECKED_CAST is OK as this function will only be called if the key exists, so the value should be V
                One note though, if one deals with a Map returned by Java code and forgot that the Map actually contains
                `null` as values as well, then we ignore it here (due to the UNCHECKED_CAST). However, usually this
                should not matter as the assertion about the value will cover it. In the worst case, a null-check included
                by the Kotlin compiler will throw -> in such a case it might be hard for the user to grasp what is going on.
                In this sense it might be better if we catch that already here and report accordingly. Yet, in the end we
                end up introducing null-checks everywhere only because of Java => keep it like this for now.
                */
            )
            it[key] as V
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy