autoparams.kotlin.ValueAutoKotlinSource.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of autoparams-kotlin Show documentation
Show all versions of autoparams-kotlin Show documentation
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.
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
}
}
}