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

autoparams.kotlin.ValueAutoKotlinSource.kt Maven / Gradle / Ivy

Go to download

A Kotlin-specific addition to the AutoParams library, allowing for the automated generation of random parameters for unit testing in Kotlin. This extension is designed to leverage Kotlin's unique features for more powerful and flexible test data generation.

There is a newer version: 8.3.0
Show newest version
package autoparams.kotlin

import java.lang.reflect.Proxy
import autoparams.customization.Customization
import org.junit.jupiter.params.provider.ArgumentsSource
import kotlin.reflect.KClass

@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@ArgumentsSource(ValueAutoKotlinArgumentsProvider::class)
@Customization(KotlinCustomizer::class)
annotation class ValueAutoKotlinSource(
    val shorts: ShortArray = [],
    val bytes: ByteArray = [],
    val ints: IntArray = [],
    val longs: LongArray = [],
    val floats: FloatArray = [],
    val doubles: DoubleArray = [],
    val chars: CharArray = [],
    val booleans: BooleanArray = [],
    val strings: Array = [],
    val classes: Array> = [],
) {

    companion object ProxyFactory {

        fun create(
            shorts: ShortArray,
            bytes: ByteArray,
            ints: IntArray,
            longs: LongArray,
            floats: FloatArray,
            doubles: DoubleArray,
            chars: CharArray,
            booleans: BooleanArray,
            strings: Array,
            classes: Array>
        ): ValueAutoKotlinSource {
            return Proxy.newProxyInstance(
                ValueAutoKotlinSource::class.java.classLoader,
                arrayOf(ValueAutoKotlinSource::class.java)
            ) { _, method, _ ->
                when (method.name) {
                    "annotationType" -> ValueAutoKotlinSource::class.java
                    "shorts" -> shorts
                    "bytes" -> bytes
                    "ints" -> ints
                    "longs" -> longs
                    "floats" -> floats
                    "doubles" -> doubles
                    "chars" -> chars
                    "booleans" -> booleans
                    "strings" -> strings
                    "classes" -> classes.map { it.java }.toTypedArray()
                    else -> method.defaultValue
                }
            } as ValueAutoKotlinSource
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy