Skip to content

Commit

Permalink
Ensure Arrays of primitives are correctly generated. (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmook committed May 3, 2023
1 parent 3e579d1 commit 6749aa8
Show file tree
Hide file tree
Showing 4 changed files with 416 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Appmattus Limited
* Copyright 2020-2023 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,14 +19,28 @@ package com.appmattus.kotlinfixture.resolver
import com.appmattus.kotlinfixture.Context
import com.appmattus.kotlinfixture.Unresolved
import com.appmattus.kotlinfixture.Unresolved.Companion.createUnresolved
import com.appmattus.kotlinfixture.typeOf
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.starProjectedType

internal class ArrayKTypeResolver : Resolver {

@Suppress("ReturnCount")
override fun resolve(context: Context, obj: Any): Any? {
override fun resolve(context: Context, obj: Any): Any {
// Special handling for primitive arrays as Kotlin's KType seems to see Array<Int> as IntArray etc when looking at the classifier
// See https://youtrack.jetbrains.com/issue/KT-52170/Reflection-typeOfArrayLong-gives-classifier-LongArray
when (obj) {
BooleanArrayKType -> return (context.resolve(BooleanArray::class) as BooleanArray).toTypedArray()
ByteArrayKType -> return (context.resolve(ByteArray::class) as ByteArray).toTypedArray()
DoubleArrayKType -> return (context.resolve(DoubleArray::class) as DoubleArray).toTypedArray()
FloatArrayKType -> return (context.resolve(FloatArray::class) as FloatArray).toTypedArray()
IntArrayKType -> return (context.resolve(IntArray::class) as IntArray).toTypedArray()
LongArrayKType -> return (context.resolve(LongArray::class) as LongArray).toTypedArray()
ShortArrayKType -> return (context.resolve(ShortArray::class) as ShortArray).toTypedArray()
CharArrayKType -> return (context.resolve(CharArray::class) as CharArray).toTypedArray()
}

if (obj is KType && obj.classifier?.starProjectedType == Array<Any?>::class.starProjectedType) {
val size = context.configuration.repeatCount()

Expand All @@ -48,4 +62,15 @@ internal class ArrayKTypeResolver : Resolver {

return Unresolved.Unhandled
}

companion object {
private val BooleanArrayKType = typeOf<Array<Boolean>>()
private val ByteArrayKType = typeOf<Array<Byte>>()
private val DoubleArrayKType = typeOf<Array<Double>>()
private val FloatArrayKType = typeOf<Array<Float>>()
private val IntArrayKType = typeOf<Array<Int>>()
private val LongArrayKType = typeOf<Array<Long>>()
private val ShortArrayKType = typeOf<Array<Short>>()
private val CharArrayKType = typeOf<Array<Char>>()
}
}
Loading

0 comments on commit 6749aa8

Please sign in to comment.