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

com.navercorp.fixturemonkey.kotlin.FixtureMonkeyExtensions.kt Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
/*
 * Fixture Monkey
 *
 * Copyright (c) 2021-present NAVER Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.navercorp.fixturemonkey.kotlin

import com.navercorp.fixturemonkey.ArbitraryBuilder
import com.navercorp.fixturemonkey.FixtureMonkey
import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary
import com.navercorp.fixturemonkey.api.expression.TypedPropertySelector
import com.navercorp.fixturemonkey.api.instantiator.Instantiator
import com.navercorp.fixturemonkey.api.property.PropertySelector
import com.navercorp.fixturemonkey.api.type.TypeReference
import com.navercorp.fixturemonkey.customizer.InnerSpec
import com.navercorp.fixturemonkey.experimental.ExperimentalArbitraryBuilder
import net.jqwik.api.Arbitrary
import net.jqwik.api.Combinators
import java.util.function.BiConsumer
import java.util.function.BiFunction
import java.util.function.Consumer
import java.util.function.Function
import java.util.function.Predicate
import java.util.function.Supplier
import java.util.stream.Stream
import kotlin.reflect.KFunction1
import kotlin.reflect.KProperty1
import kotlin.streams.asSequence

inline fun  FixtureMonkey.giveMe(): Sequence =
    this.giveMe(object : TypeReference() {}).asSequence()

inline fun  FixtureMonkey.giveMe(size: Int): List =
    this.giveMe(object : TypeReference() {}, size)

inline fun  FixtureMonkey.giveMeOne(): T = this.giveMeOne(object : TypeReference() {})

inline fun  FixtureMonkey.giveMeArbitrary(): Arbitrary =
    this.giveMeArbitrary(object : TypeReference() {})

inline fun  FixtureMonkey.giveMeBuilder(): ArbitraryBuilder =
    this.giveMeBuilder(object : TypeReference() {})

inline fun  FixtureMonkey.giveMeKotlinBuilder(): KotlinTypeDefaultArbitraryBuilder =
    KotlinTypeDefaultArbitraryBuilder(this.giveMeBuilder(object : TypeReference() {}))

inline fun  FixtureMonkey.giveMeKotlinBuilder(value: T): KotlinTypeDefaultArbitraryBuilder =
    KotlinTypeDefaultArbitraryBuilder(this.giveMeBuilder(value))

inline fun  FixtureMonkey.giveMeExperimentalBuilder(): ExperimentalArbitraryBuilder =
    this.giveMeExperimentalBuilder(object : TypeReference() {})

class KotlinTypeDefaultArbitraryBuilder(
    val delegate: ArbitraryBuilder,
) : ArbitraryBuilder {
    override fun set(expression: String, value: Any?): KotlinTypeDefaultArbitraryBuilder = this.apply {
        delegate.set(expression, value)
    }

    override fun set(expression: String, value: Any?, limit: Int): KotlinTypeDefaultArbitraryBuilder = this.apply {
        delegate.set(expression, value, limit)
    }

    override fun set(propertySelector: PropertySelector, value: Any?): KotlinTypeDefaultArbitraryBuilder =
        this.apply {
            delegate.set(propertySelector, value)
        }

    override fun set(
        propertySelector: PropertySelector,
        value: Any?,
        limit: Int,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.set(propertySelector, value, limit) }

    override fun set(value: Any?): KotlinTypeDefaultArbitraryBuilder = this.apply {
        delegate.set(value)
    }

    override fun setInner(innerSpec: InnerSpec): KotlinTypeDefaultArbitraryBuilder = this.apply {
        delegate.setInner(innerSpec)
    }

    override fun setLazy(expression: String, supplier: Supplier<*>?): KotlinTypeDefaultArbitraryBuilder =
        this.apply {
            delegate.setLazy(expression, supplier)
        }

    override fun setLazy(expression: String, supplier: Supplier<*>?, limit: Int): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setLazy(expression, supplier, limit) }

    override fun setLazy(
        propertySelector: PropertySelector,
        supplier: Supplier<*>?,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setLazy(propertySelector, supplier) }

    override fun setLazy(
        propertySelector: PropertySelector,
        supplier: Supplier<*>?,
        limit: Int,
    ): KotlinTypeDefaultArbitraryBuilder = this.apply { delegate.setLazy(propertySelector, supplier, limit) }

    override fun setNull(expression: String): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setNull(expression) }

    override fun setNull(propertySelector: PropertySelector): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setNull(propertySelector) }

    override fun setNotNull(expression: String): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setNotNull(expression) }

    override fun setNotNull(propertySelector: PropertySelector): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setNotNull(propertySelector) }

    override fun  setPostCondition(
        expression: String,
        type: Class,
        predicate: Predicate,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setPostCondition(expression, type, predicate) }

    override fun  setPostCondition(
        propertySelector: PropertySelector,
        type: Class,
        predicate: Predicate,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setPostCondition(propertySelector, type, predicate) }

    override fun  setPostCondition(
        expression: String,
        type: Class,
        predicate: Predicate,
        limit: Int,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setPostCondition(expression, type, predicate, limit) }

    override fun  setPostCondition(
        propertySelector: PropertySelector,
        type: Class,
        predicate: Predicate,
        limit: Int,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setPostCondition(propertySelector, type, predicate, limit) }

    override fun setPostCondition(predicate: Predicate): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setPostCondition(predicate) }

    override fun size(expression: String, size: Int): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.size(expression, size) }

    override fun size(propertySelector: PropertySelector, size: Int): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.size(propertySelector, size) }

    override fun size(expression: String, minSize: Int, maxSize: Int): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.size(expression, minSize, maxSize) }

    override fun size(
        propertySelector: PropertySelector,
        minSize: Int,
        maxSize: Int,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.size(propertySelector, minSize, maxSize) }

    override fun minSize(expression: String, minSize: Int): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.minSize(expression, minSize) }

    override fun minSize(propertySelector: PropertySelector, minSize: Int): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.minSize(propertySelector, minSize) }

    override fun maxSize(expression: String, maxSize: Int): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.maxSize(expression, maxSize) }

    override fun maxSize(propertySelector: PropertySelector, maxSize: Int): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.maxSize(propertySelector, maxSize) }

    override fun fixed(): KotlinTypeDefaultArbitraryBuilder = this.apply { delegate.fixed() }

    override fun  zipWith(
        others: List>,
        combinator: Function, R>?,
    ): KotlinTypeDefaultArbitraryBuilder = KotlinTypeDefaultArbitraryBuilder(delegate.zipWith(others, combinator))

    override fun  zipWith(
        other: ArbitraryBuilder,
        combinator: BiFunction,
    ): KotlinTypeDefaultArbitraryBuilder = KotlinTypeDefaultArbitraryBuilder(delegate.zipWith(other, combinator))

    override fun  zipWith(
        other: ArbitraryBuilder,
        another: ArbitraryBuilder,
        combinator: Combinators.F3,
    ): KotlinTypeDefaultArbitraryBuilder =
        KotlinTypeDefaultArbitraryBuilder(delegate.zipWith(other, another, combinator))

    override fun  zipWith(
        other: ArbitraryBuilder,
        another: ArbitraryBuilder,
        theOther: ArbitraryBuilder,
        combinator: Combinators.F4,
    ): KotlinTypeDefaultArbitraryBuilder =
        KotlinTypeDefaultArbitraryBuilder(delegate.zipWith(other, another, theOther, combinator))

    override fun sampleList(size: Int): List = delegate.sampleList(size)

    override fun validOnly(validOnly: Boolean): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.validOnly(validOnly) }

    override fun instantiate(instantiator: Instantiator): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.instantiate(instantiator) }

    override fun instantiate(type: Class<*>, instantiator: Instantiator): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.instantiate(type, instantiator) }

    override fun instantiate(
        type: TypeReference<*>,
        instantiator: Instantiator?,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.instantiate(type, instantiator) }

    override fun  customizeProperty(
        propertySelector: TypedPropertySelector,
        combinableArbitraryCustomizer: Function, CombinableArbitrary>
    ): ArbitraryBuilder = this.apply { delegate.customizeProperty(propertySelector, combinableArbitraryCustomizer) }

    override fun build(): Arbitrary = delegate.build()

    override fun sample(): T = delegate.sample()

    override fun sampleStream(): Stream = delegate.sampleStream()

    override fun copy(): KotlinTypeDefaultArbitraryBuilder = KotlinTypeDefaultArbitraryBuilder(delegate.copy())

    override fun thenApply(biConsumer: BiConsumer>): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.thenApply(biConsumer) }

    override fun acceptIf(
        predicate: Predicate,
        consumer: Consumer>,
    ): KotlinTypeDefaultArbitraryBuilder = this.apply { delegate.acceptIf(predicate, consumer) }

    override fun  map(mapper: Function): KotlinTypeDefaultArbitraryBuilder =
        KotlinTypeDefaultArbitraryBuilder(delegate.map(mapper))

    fun setInner(innerSpecConfigurer: (InnerSpec.() -> InnerSpec)): KotlinTypeDefaultArbitraryBuilder =
        this.apply { delegate.setInner(innerSpecConfigurer(InnerSpec())) }

    /**
     * The following are the APIs use the [PropertySelector].
     */
    fun set(property: KProperty1, value: Any?, limit: Long): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertyExpressionGenerator(property), value, limit.toInt())

    fun set(property: KProperty1, value: Any?): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertyExpressionGenerator(property), value)

    fun setExp(property: KProperty1, value: Any?, limit: Long): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertyExpressionGenerator(property), value, limit.toInt())

    fun setExp(property: KProperty1, value: Any?): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertyExpressionGenerator(property), value)

    fun setExp(
        propertySelector: PropertySelector,
        value: Any?,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertySelector, value, limit.toInt())

    fun setExp(propertySelector: PropertySelector, value: Any?): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertySelector, value)

    fun setExpGetter(property: KFunction1, value: Any?, limit: Long): KotlinTypeDefaultArbitraryBuilder =
        this.set(
            propertyExpressionGenerator(property),
            value,
            limit.toInt(),
        )

    fun setExpGetter(property: KFunction1, value: Any?): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertyExpressionGenerator(property), value)

    fun setExpGetter(
        propertySelector: PropertySelector,
        value: Any?,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertySelector, value, limit.toInt())

    fun setExpGetter(propertySelector: PropertySelector, value: Any?): KotlinTypeDefaultArbitraryBuilder =
        this.set(propertySelector, value)

    inline fun  setPostCondition(
        expression: String,
        noinline filter: (U) -> Boolean,
        limit: Int,
    ) = this.apply { delegate.setPostCondition(expression, U::class.java, filter, limit) }

    inline fun  setPostCondition(
        expression: String,
        noinline filter: (U) -> Boolean,
    ) = this.apply { delegate.setPostCondition(expression, U::class.java, filter) }

    inline fun  setPostCondition(
        property: KProperty1,
        noinline filter: (U) -> Boolean,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertyExpressionGenerator(property),
            U::class.java,
            filter,
            limit.toInt(),
        )

    inline fun  setPostCondition(
        property: KProperty1,
        noinline filter: (U) -> Boolean,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertyExpressionGenerator(property),
            U::class.java,
            filter,
        )

    inline fun  setPostCondition(
        propertySelector: PropertySelector,
        noinline predicate: (U) -> Boolean,
        limit: Int,
    ) = this.apply { delegate.setPostCondition(propertySelector, U::class.java, predicate, limit) }

    inline fun  setPostCondition(
        propertySelector: PropertySelector,
        noinline predicate: (U) -> Boolean,
    ) = this.apply { delegate.setPostCondition(propertySelector, U::class.java, predicate) }

    inline fun  setPostConditionExp(
        property: KProperty1,
        noinline filter: (U) -> Boolean,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertyExpressionGenerator(property),
            U::class.java,
            filter,
            limit.toInt(),
        )

    inline fun  setPostConditionExp(
        property: KProperty1,
        noinline filter: (U) -> Boolean,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertyExpressionGenerator(property),
            U::class.java,
            filter,
        )

    inline fun  setPostConditionExp(
        propertySelector: PropertySelector,
        noinline filter: (U) -> Boolean,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertySelector,
            U::class.java,
            filter,
            limit.toInt(),
        )

    inline fun  setPostConditionExp(
        propertySelector: PropertySelector,
        noinline filter: (U) -> Boolean,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertySelector,
            U::class.java,
            filter,
        )

    inline fun  setPostConditionExpGetter(
        propertySelector: PropertySelector,
        noinline filter: (U) -> Boolean,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertySelector,
            U::class.java,
            filter,
            limit.toInt(),
        )

    inline fun  setPostConditionExpGetter(
        propertySelector: PropertySelector,
        noinline filter: (U) -> Boolean,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertySelector,
            U::class.java,
            filter,
        )

    inline fun  setPostConditionExpGetter(
        property: KFunction1,
        noinline filter: (U) -> Boolean,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertyExpressionGenerator(property),
            U::class.java,
            filter,
            limit.toInt(),
        )

    inline fun  setPostConditionExpGetter(
        property: KFunction1,
        noinline filter: (U) -> Boolean,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setPostCondition(
            propertyExpressionGenerator(property),
            U::class.java,
            filter,
        )

    fun setNull(property: KProperty1): KotlinTypeDefaultArbitraryBuilder =
        this.setNull(propertyExpressionGenerator(property))

    fun setNullExp(property: KProperty1): KotlinTypeDefaultArbitraryBuilder =
        this.setNull(propertyExpressionGenerator(property))

    fun setNullExp(propertySelector: PropertySelector): KotlinTypeDefaultArbitraryBuilder =
        this.setNull(propertySelector)

    fun setNullExpGetter(property: KFunction1): KotlinTypeDefaultArbitraryBuilder =
        this.setNull(propertyExpressionGenerator(property))

    fun setNullExpGetter(propertySelector: PropertySelector): KotlinTypeDefaultArbitraryBuilder =
        this.setNull(propertySelector)

    fun setNotNull(property: KProperty1): KotlinTypeDefaultArbitraryBuilder =
        this.setNotNull(propertyExpressionGenerator(property))

    fun setNotNullExp(property: KProperty1): KotlinTypeDefaultArbitraryBuilder =
        this.setNotNull(propertyExpressionGenerator(property))

    fun setNotNullExp(propertySelector: PropertySelector): KotlinTypeDefaultArbitraryBuilder =
        this.setNotNull(propertySelector)

    fun setNotNullExpGetter(property: KFunction1): KotlinTypeDefaultArbitraryBuilder =
        this.setNotNull(propertyExpressionGenerator(property))

    fun setNotNullExpGetter(propertySelector: PropertySelector): KotlinTypeDefaultArbitraryBuilder =
        this.setNotNull(propertySelector)

    fun size(property: KProperty1, size: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertyExpressionGenerator(property), size)

    fun size(property: KProperty1, min: Int, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertyExpressionGenerator(property), min, max)

    fun sizeExp(property: KProperty1, size: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertyExpressionGenerator(property), size)

    fun sizeExp(propertySelector: PropertySelector, size: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertySelector, size)

    fun sizeExp(propertySelector: PropertySelector, min: Int, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertySelector, min, max)

    fun sizeExp(property: KProperty1, min: Int, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertyExpressionGenerator(property), min, max)

    fun sizeExpGetter(property: KFunction1, size: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertyExpressionGenerator(property), size)

    fun sizeExpGetter(property: KFunction1, min: Int, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertyExpressionGenerator(property), min, max)

    fun sizeExpGetter(propertySelector: PropertySelector, size: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertySelector, size)

    fun sizeExpGetter(propertySelector: PropertySelector, min: Int, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.size(propertySelector, min, max)

    fun minSize(property: KProperty1, min: Int): KotlinTypeDefaultArbitraryBuilder =
        this.minSize(propertyExpressionGenerator(property), min)

    fun minSizeExp(property: KProperty1, min: Int): KotlinTypeDefaultArbitraryBuilder =
        this.minSize(propertyExpressionGenerator(property), min)

    fun minSizeExp(propertySelector: PropertySelector, min: Int): KotlinTypeDefaultArbitraryBuilder =
        this.minSize(propertySelector, min)

    fun minSizeExpGetter(property: KFunction1, min: Int): KotlinTypeDefaultArbitraryBuilder =
        this.minSize(propertyExpressionGenerator(property), min)

    fun minSizeExpGetter(propertySelector: PropertySelector, min: Int): KotlinTypeDefaultArbitraryBuilder =
        this.minSize(propertySelector, min)

    fun maxSize(property: KProperty1, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.maxSize(propertyExpressionGenerator(property), max)

    fun maxSizeExp(property: KProperty1, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.maxSize(propertyExpressionGenerator(property), max)

    fun maxSizeExp(propertySelector: PropertySelector, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.maxSize(propertySelector, max)

    fun maxSizeExpGetter(property: KFunction1, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.maxSize(propertyExpressionGenerator(property), max)

    fun maxSizeExpGetter(propertySelector: PropertySelector, max: Int): KotlinTypeDefaultArbitraryBuilder =
        this.maxSize(propertySelector, max)

    fun setLazy(
        property: KProperty1,
        supplier: Supplier,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setLazy(propertyExpressionGenerator(property), supplier, limit.toInt())

    fun setLazy(
        property: KProperty1,
        supplier: Supplier,
    ): KotlinTypeDefaultArbitraryBuilder = this.setLazy(propertyExpressionGenerator(property), supplier)

    fun setLazyExp(
        property: KProperty1,
        supplier: Supplier,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setLazy(propertyExpressionGenerator(property), supplier, limit.toInt())

    fun setLazyExp(
        property: KProperty1,
        supplier: Supplier,
    ): KotlinTypeDefaultArbitraryBuilder = this.setLazy(propertyExpressionGenerator(property), supplier)

    fun setLazyExp(
        propertySelector: PropertySelector,
        supplier: Supplier,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setLazy(propertySelector, supplier, limit.toInt())

    fun setLazyExp(
        propertySelector: PropertySelector,
        supplier: Supplier,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setLazy(propertySelector, supplier)

    fun setLazyExpGetter(
        property: KFunction1,
        supplier: Supplier,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setLazy(propertyExpressionGenerator(property), supplier, limit.toInt())

    fun setLazyExpGetter(
        property: KFunction1,
        supplier: Supplier,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setLazy(propertyExpressionGenerator(property), supplier)

    fun setLazyExpGetter(
        propertySelector: PropertySelector,
        supplier: Supplier,
        limit: Long,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setLazy(propertySelector, supplier, limit.toInt())

    fun setLazyExpGetter(
        propertySelector: PropertySelector,
        supplier: Supplier,
    ): KotlinTypeDefaultArbitraryBuilder =
        this.setLazy(propertySelector, supplier)
}