Skip to content

Commit

Permalink
Added standard collections to builtins (fix #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salomon BRYS committed Sep 15, 2022
1 parent f9e7fa0 commit 5f238a2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ public class MocKMPProcessor(
"kotlin.Double" to ("%L" to "0.0"),
"kotlin.String" to ("%L" to "\"\""),
"kotlin.collections.List" to ("%M()" to MemberName("kotlin.collections", "emptyList")),
"kotlin.collections.ArrayList" to ("%M()" to MemberName("kotlin.collections", "ArrayList")),
"java.util.ArrayList" to ("%M()" to MemberName("kotlin.collections", "ArrayList")),
"kotlin.collections.ArrayDeque" to ("%M()" to MemberName("kotlin.collections", "ArrayDeque")),
"kotlin.collections.Set" to ("%M()" to MemberName("kotlin.collections", "emptySet")),
"kotlin.collections.HashSet" to ("%M()" to MemberName("kotlin.collections", "HashSet")),
"java.util.HashSet" to ("%M()" to MemberName("kotlin.collections", "HashSet")),
"kotlin.collections.LinkedHashSet" to ("%M()" to MemberName("kotlin.collections", "LinkedHashSet")),
"java.util.LinkedHashSet" to ("%M()" to MemberName("kotlin.collections", "LinkedHashSet")),
"kotlin.collections.Map" to ("%M()" to MemberName("kotlin.collections", "emptyMap")),
"kotlin.collections.HashMap" to ("%M()" to MemberName("kotlin.collections", "HashMap")),
"java.util.HashMap" to ("%M()" to MemberName("kotlin.collections", "HashMap")),
"kotlin.collections.LinkedHashMap" to ("%M()" to MemberName("kotlin.collections", "LinkedHashMap")),
"java.util.LinkedHashMap" to ("%M()" to MemberName("kotlin.collections", "LinkedHashMap")),
"kotlin.Array" to ("%M()" to MemberName("kotlin", "emptyArray"))
)
}

Expand Down Expand Up @@ -56,7 +68,6 @@ public class MocKMPProcessor(
else -> (parent?.let { it.asString() + "." } ?: "") + toString()
}

@Suppress("NOTHING_TO_INLINE")
private fun error(node: KSNode, message: String): Nothing {
val prefix = when (val loc = node.location) {
is FileLocation -> "$node (${loc.filePath}:${loc.lineNumber})"
Expand Down Expand Up @@ -256,7 +267,10 @@ public class MocKMPProcessor(
val vCls = vType.realDeclaration() as KSClassDeclaration
val filesDeps = HashSet(process.files)
val mockFunName = "fake${vType.toFunName()}"
val gFile = FileSpec.builder(vCls.packageName.asString(), mockFunName)
val mockPkg =
if (vCls.packageName.isKotlinStdlib()) "fake." + vCls.packageName.asString()
else vCls.packageName.asString()
val gFile = FileSpec.builder(mockPkg, mockFunName)
val gFun = FunSpec.builder(mockFunName)
.addModifiers(KModifier.INTERNAL)
.returns(vType.toRealTypeName(vCls.typeParameters.toTypeParameterResolver()))
Expand Down Expand Up @@ -289,7 +303,12 @@ public class MocKMPProcessor(
f.containingFile?.let { filesDeps += it }
"%M()" to MemberName(f.packageName.asString(), f.simpleName.asString())
}
else -> "%M()" to MemberName(vParamTypeToFakeDecl.packageName.asString(), "fake${vParamTypeToFake.toFunName()}")
else -> {
val pkg =
if (vParamTypeToFakeDecl.packageName.isKotlinStdlib()) "fake." + vParamTypeToFakeDecl.packageName.asString()
else vParamTypeToFakeDecl.packageName.asString()
"%M()" to MemberName(pkg, "fake${vParamTypeToFake.toFunName()}")
}
}
if (vParamType.isAnyFunctionType) {
args.add("${vParam.name!!.asString()} = { ${"_, ".repeat(vParamType.arguments.size - 1)}-> $template }" to value)
Expand Down
10 changes: 8 additions & 2 deletions mockmp-processor/src/main/kotlin/org/kodein/mock/ksp/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import com.squareup.kotlinpoet.ksp.toTypeParameterResolver

internal fun String.withNonEmptyPrefix(p: String) = if (isEmpty()) "" else "$p$this"

internal fun KSClassDeclaration.firstPublicConstructor() = (sequenceOf(primaryConstructor) + getConstructors()).firstOrNull { it?.isPublic() ?: false }
internal fun KSClassDeclaration.firstPublicConstructor() = (sequenceOf(primaryConstructor) + getConstructors())
.filterNotNull()
.filter { it.isPublic() }
.sortedBy { it.parameters.size }
.firstOrNull()


internal fun KSTypeReference.toRealTypeName(typeParamResolver: TypeParameterResolver = TypeParameterResolver.EMPTY): TypeName {
Expand Down Expand Up @@ -50,4 +54,6 @@ internal fun KSType.realDeclaration(): KSDeclaration {
is KSTypeAlias -> decl.type.resolve().realDeclaration()
else -> decl
}
}
}

internal fun KSName.isKotlinStdlib() = asString().let { it == "kotlin" || it.startsWith("kotlin.") }
14 changes: 13 additions & 1 deletion tests/tests-junit4/src/commonMain/kotlin/data/Data.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ data class Data(
val dir2: SomeDirection,
val special: Instant,
val list: List<String>,
val map: NamesMap<Int>
val arrayList: ArrayList<String>,
val arrayDeque: ArrayDeque<String>,
val set: Set<String>,
val hashSet: HashSet<String>,
val linkedHashSet: LinkedHashSet<String>,
val map: NamesMap<Int>,
val hashMap: HashMap<String, Long>,
val linkedHashMap: LinkedHashMap<String, Long>,
)

class Arrays(
val bytes: ByteArray,
val strings: Array<String>
)

class Funs(
Expand Down
20 changes: 18 additions & 2 deletions tests/tests-junit4/src/commonTest/kotlin/tests/InjectionTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class InjectionTests : TestsWithMocks() {
@Fake
lateinit var data: Data

@Fake
lateinit var arrays: Arrays

@Fake
lateinit var funs: Funs

Expand All @@ -35,7 +38,7 @@ class InjectionTests : TestsWithMocks() {
}

@Test
fun testFake() {
fun testFakeData() {
assertEquals(
Data(
SubData("", 0),
Expand All @@ -46,12 +49,25 @@ class InjectionTests : TestsWithMocks() {
SomeDirection(Direction.LEFT),
Instant.fromEpochSeconds(0),
emptyList(),
emptyMap()
ArrayList(),
ArrayDeque(),
emptySet(),
HashSet(),
LinkedHashSet(),
emptyMap(),
HashMap(),
LinkedHashMap(),
),
data
)
}

@Test
fun testFakeArray() {
assertEquals(0, arrays.bytes.size)
assertEquals(0, arrays.strings.size)
}

@Test
fun testDeferred() {
every { bar.doData(isAny()) } returns Unit
Expand Down

0 comments on commit 5f238a2

Please sign in to comment.