Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi interface support for kspy for common #136

Merged
merged 3 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ internal class KMockProcessor(

entryPointGenerator.generateCommon(
templateSources = commonAggregated.extractedTemplates,
totalTemplates = totalAggregated.extractedTemplates
templateMultiSources = commonMultiAggregated.extractedTemplates,
totalTemplates = totalAggregated.extractedTemplates,
totalMultiSources = commonMultiAggregated.extractedTemplates,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import tech.antibytes.kmock.processor.aggregation.KMockSingleSourceAggregator
import tech.antibytes.kmock.processor.factory.KMockFactoryEntryPointGenerator
import tech.antibytes.kmock.processor.factory.KMockFactoryGenerator
import tech.antibytes.kmock.processor.factory.KMockFactoryGeneratorUtil
import tech.antibytes.kmock.processor.factory.KMockFactoryMultiInterfaceGenerator
import tech.antibytes.kmock.processor.factory.KMockFactoryWithGenerics
import tech.antibytes.kmock.processor.factory.KMockFactoryWithoutGenerics
import tech.antibytes.kmock.processor.factory.NoopFactoryGenerator
Expand Down Expand Up @@ -49,6 +50,7 @@ class KMockProcessorProvider : SymbolProcessorProvider {
Pair(NoopFactoryGenerator, NoopFactoryGenerator)
} else {
val factoryUtils = KMockFactoryGeneratorUtil(
isKmp = options.isKmp,
freezeOnDefault = options.freezeOnDefault,
genericResolver = KMockGenerics,
)
Expand All @@ -70,6 +72,10 @@ class KMockProcessorProvider : SymbolProcessorProvider {
utils = factoryUtils,
genericResolver = KMockGenerics,
),
multiInterfaceGenerator = KMockFactoryMultiInterfaceGenerator(
spyContainer = spyContainer,
utils = factoryUtils,
),
spyContainer = spyContainer,
spiesOnly = options.spiesOnly,
utils = factoryUtils,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ internal interface ProcessorContract {
packageName: String,
templateName: String
): Boolean
fun hasSpies(): Boolean

fun hasSpies(filter: List<Source> = emptyList()): Boolean
}

sealed interface Source {
Expand Down Expand Up @@ -367,7 +368,7 @@ internal interface ProcessorContract {
}

interface MockFactoryGeneratorUtil {
fun generateMockFactorySignature(
fun generateSharedMockFactorySignature(
mockType: TypeVariableName,
spyType: TypeVariableName,
generics: List<TypeVariableName>,
Expand All @@ -393,6 +394,10 @@ internal interface ProcessorContract {
): Pair<List<TemplateSource>, List<TemplateSource>>

fun resolveGenerics(templateSource: TemplateSource): List<TypeVariableName>

fun resolveModifier(): KModifier?

fun toTypeNames(types: List<KSClassDeclaration>): List<TypeName>
}

interface MockFactoryWithoutGenerics {
Expand All @@ -419,6 +424,13 @@ internal interface ProcessorContract {
): List<FactoryBundle>
}

interface MockFactoryMultiInterface {
fun buildSpyFactory(
templateMultiSources: List<TemplateMultiSource>,
relaxer: Relaxer?
): List<FunSpec>
}

interface MockFactoryGenerator {
fun writeFactories(
templateSources: List<TemplateSource>,
Expand All @@ -431,6 +443,8 @@ internal interface ProcessorContract {
interface MockFactoryEntryPointGenerator {
fun generateCommon(
templateSources: List<TemplateSource>,
templateMultiSources: List<TemplateMultiSource>,
totalMultiSources: List<TemplateMultiSource>,
totalTemplates: List<TemplateSource>,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package tech.antibytes.kmock.processor.factory
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.ksp.writeTo
import tech.antibytes.kmock.processor.ProcessorContract.Companion.COMMON_INDICATOR
Expand All @@ -23,6 +24,7 @@ import tech.antibytes.kmock.processor.ProcessorContract.KmpCodeGenerator
import tech.antibytes.kmock.processor.ProcessorContract.MockFactoryEntryPointGenerator
import tech.antibytes.kmock.processor.ProcessorContract.MockFactoryGeneratorUtil
import tech.antibytes.kmock.processor.ProcessorContract.SpyContainer
import tech.antibytes.kmock.processor.ProcessorContract.TemplateMultiSource
import tech.antibytes.kmock.processor.ProcessorContract.TemplateSource

internal class KMockFactoryEntryPointGenerator(
Expand Down Expand Up @@ -116,30 +118,66 @@ internal class KMockFactoryEntryPointGenerator(
}
}

private fun generateGenericEntryPoints(
file: FileSpec.Builder,
generics: List<TemplateSource>,
) {
private fun FileSpec.Builder.generateGenericEntryPoints(generics: List<TemplateSource>,) {
val genericFactories = buildGenericFactories(generics)

genericFactories.forEach { factories ->
val (mockFactory, spyFactory) = factories

if (!spiesOnly) {
file.addFunction(mockFactory)
this.addFunction(mockFactory)
}

if (spyFactory != null) {
file.addFunction(spyFactory)
this.addFunction(spyFactory)
}
}
}

private fun buildMultiInterfaceSpyFactory(
boundaries: List<TypeName>
): FunSpec {
val spyType = TypeVariableName(KSPY_FACTORY_TYPE_NAME, bounds = boundaries)
val mockType = TypeVariableName(KMOCK_FACTORY_TYPE_NAME).copy(bounds = listOf(spyType))

return utils.generateKspySignature(
spyType = spyType,
mockType = mockType,
generics = emptyList(),
hasDefault = true,
modifier = KModifier.EXPECT
).build()
}

private fun buildMultiInterfaceSpyFactory(
templateSource: TemplateMultiSource
): FunSpec? {
return if (spyContainer.isSpyable(null, templateSource.packageName, templateSource.templateName)) {
buildMultiInterfaceSpyFactory(utils.toTypeNames(templateSource.templates))
} else {
null
}
}

private fun FileSpec.Builder.generateMultiInterfaceEntryPoints(
templateSources: List<TemplateMultiSource>
) {
templateSources.forEach { source ->
val factory = buildMultiInterfaceSpyFactory(source)

if (factory != null) {
this.addFunction(factory)
}
}
}

override fun generateCommon(
templateSources: List<TemplateSource>,
templateMultiSources: List<TemplateMultiSource>,
totalMultiSources: List<TemplateMultiSource>,
totalTemplates: List<TemplateSource>,
) {
if (isKmp && totalTemplates.isNotEmpty()) { // TODO: Solve multi Rounds in a better way
if (isKmp && (totalTemplates.isNotEmpty() || totalMultiSources.isNotEmpty())) { // TODO: Solve multi Rounds in a better way
val file = FileSpec.builder(
rootPackage,
FACTORY_FILE_NAME
Expand All @@ -154,14 +192,12 @@ internal class KMockFactoryEntryPointGenerator(
file.addFunction(buildMockFactory())
}

if (spyContainer.hasSpies()) {
if (spyContainer.hasSpies(totalMultiSources)) {
file.addFunction(buildSpyFactory())
}

generateGenericEntryPoints(
file,
generics
)
file.generateGenericEntryPoints(generics)
file.generateMultiInterfaceEntryPoints(templateMultiSources)

codeGenerator.setOneTimeSourceSet(COMMON_INDICATOR)
file.build().writeTo(
Expand All @@ -187,10 +223,7 @@ internal class KMockFactoryEntryPointGenerator(
file.addImport(KMOCK_CONTRACT.packageName, KMOCK_CONTRACT.simpleName)
file.addImport(NOOP_COLLECTOR_NAME.packageName, NOOP_COLLECTOR_NAME.simpleName)

generateGenericEntryPoints(
file,
generics
)
file.generateGenericEntryPoints(generics)

codeGenerator.setOneTimeSourceSet(indicator)
file.build().writeTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import tech.antibytes.kmock.processor.ProcessorContract.Companion.NOOP_COLLECTOR
import tech.antibytes.kmock.processor.ProcessorContract.Companion.UNUSED
import tech.antibytes.kmock.processor.ProcessorContract.MockFactoryGenerator
import tech.antibytes.kmock.processor.ProcessorContract.MockFactoryGeneratorUtil
import tech.antibytes.kmock.processor.ProcessorContract.MockFactoryMultiInterface
import tech.antibytes.kmock.processor.ProcessorContract.MockFactoryWithGenerics
import tech.antibytes.kmock.processor.ProcessorContract.MockFactoryWithoutGenerics
import tech.antibytes.kmock.processor.ProcessorContract.Relaxer
Expand All @@ -32,6 +33,7 @@ internal class KMockFactoryGenerator(
private val spiesOnly: Boolean,
private val nonGenericGenerator: MockFactoryWithoutGenerics,
private val genericGenerator: MockFactoryWithGenerics,
private val multiInterfaceGenerator: MockFactoryMultiInterface,
private val utils: MockFactoryGeneratorUtil,
private val codeGenerator: CodeGenerator,
) : MockFactoryGenerator {
Expand Down Expand Up @@ -73,13 +75,12 @@ internal class KMockFactoryGenerator(
)
}

if (spyContainer.hasSpies()) {
if (spyContainer.hasSpies(templateMultiSources)) {
file.addFunction(
nonGenericGenerator.buildSpyFactory()
)
}

// Shell
genericFactories.forEach { factories ->
file.addFunction(factories.shared)

Expand All @@ -92,6 +93,15 @@ internal class KMockFactoryGenerator(
}
}

val multiInterfaceMocks = multiInterfaceGenerator.buildSpyFactory(
templateMultiSources = templateMultiSources,
relaxer = relaxer
)

multiInterfaceMocks.forEach { factory ->
file.addFunction(factory)
}

file.build().writeTo(
codeGenerator = codeGenerator,
aggregating = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@

package tech.antibytes.kmock.processor.factory

import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeParameterResolver
import tech.antibytes.kmock.processor.ProcessorContract
import tech.antibytes.kmock.processor.ProcessorContract.Companion.SHARED_MOCK_FACTORY
import tech.antibytes.kmock.processor.ProcessorContract.TemplateSource

internal class KMockFactoryGeneratorUtil(
freezeOnDefault: Boolean,
isKmp: Boolean,
private val genericResolver: ProcessorContract.GenericResolver
) : ProcessorContract.MockFactoryGeneratorUtil {
private val modifier: KModifier? = if (isKmp) {
KModifier.ACTUAL
} else {
null
}

private val spyOn = ParameterSpec.builder(
"spyOn",
TypeVariableName("SpyOn").copy(nullable = false)
Expand Down Expand Up @@ -48,25 +58,42 @@ internal class KMockFactoryGeneratorUtil(
.defaultValue(freezeOnDefault.toString())
.build()

private fun resolveArgumentType(identifier: TypeName): String {
return if (identifier is TypeVariableName) {
identifier
.bounds
.first()
.toString()
.substringBeforeLast('<')
} else {
identifier.toString()
}
}

private fun resolveGenericParameter(
generics: List<TypeVariableName>
): String {
return if (generics.isEmpty()) {
""
} else {
val genericTypes = List(generics.size) { "*" }

"<${genericTypes.joinToString(", ")}>"
}
}

private fun buildGenericFactoryArgument(
identifier: TypeVariableName,
identifier: TypeName,
generics: List<TypeVariableName>
): TypeVariableName {
val mockType = identifier
.bounds
.first()
.toString()
.substringBeforeLast('<')

val genericTypes = List(generics.size) { "*" }
val mockType = resolveArgumentType(identifier)
val parameter = resolveGenericParameter(generics)

return TypeVariableName(
"kotlin.reflect.KClass<$mockType<${genericTypes.joinToString(", ")}>>"
)
return TypeVariableName("kotlin.reflect.KClass<$mockType$parameter>")
}

private fun FunSpec.Builder.amendGenericValues(
identifier: TypeVariableName,
identifier: TypeName,
generics: List<TypeVariableName>
): FunSpec.Builder {
this.addTypeVariables(generics)
Expand All @@ -83,6 +110,21 @@ internal class KMockFactoryGeneratorUtil(
return this
}

private fun FunSpec.Builder.amendMultiBounded(
identifier: TypeVariableName,
): FunSpec.Builder {
identifier.bounds.forEachIndexed { idx, boundary ->
this.addParameter(
ParameterSpec.builder(
name = "templateType$idx",
type = buildGenericFactoryArgument(boundary, emptyList())
).build()
)
}

return this
}

private fun buildRelaxedParameter(
hasDefault: Boolean
): ParameterSpec {
Expand Down Expand Up @@ -174,10 +216,14 @@ internal class KMockFactoryGeneratorUtil(
kspy.addModifiers(modifier)
}

return kspy.amendGenericValues(spyType, generics)
return if (spyType.bounds.size > 1) {
kspy.amendMultiBounded(spyType)
} else {
kspy.amendGenericValues(spyType, generics)
}
}

override fun generateMockFactorySignature(
override fun generateSharedMockFactorySignature(
mockType: TypeVariableName,
spyType: TypeVariableName,
generics: List<TypeVariableName>,
Expand Down Expand Up @@ -214,4 +260,10 @@ internal class KMockFactoryGeneratorUtil(
typeResolver = typeResolver
)
}

override fun resolveModifier(): KModifier? = modifier

override fun toTypeNames(
types: List<KSClassDeclaration>
): List<TypeName> = types.map { source -> source.toClassName() }
}
Loading