Skip to content

Commit

Permalink
Merge pull request #72 from bitPogo/feature/fix-warnings
Browse files Browse the repository at this point in the history
Fix warnings
  • Loading branch information
bitPogo authored Mar 26, 2022
2 parents 4b55097 + a4d4cf3 commit 67fe620
Show file tree
Hide file tree
Showing 32 changed files with 322 additions and 268 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ toc::[]

=== Fixed

* Warnings for unused expression and unused parameter in MockFactory

=== Security

=== Bumped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package tech.antibytes.kmock.processor.factory

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
Expand All @@ -15,13 +16,20 @@ import tech.antibytes.kmock.processor.ProcessorContract
import tech.antibytes.kmock.processor.ProcessorContract.Companion.KMOCK_CONTRACT
import tech.antibytes.kmock.processor.ProcessorContract.Companion.KMOCK_FACTORY_TYPE_NAME
import tech.antibytes.kmock.processor.ProcessorContract.Companion.KSPY_FACTORY_TYPE_NAME
import tech.antibytes.kmock.processor.ProcessorContract.Companion.NOOP_COLLECTOR_NAME
import tech.antibytes.kmock.processor.ProcessorContract.TemplateSource

internal class KMockFactoryEntryPointGenerator(
private val utils: ProcessorContract.MockFactoryGeneratorUtil,
private val genericResolver: ProcessorContract.GenericResolver,
private val codeGenerator: ProcessorContract.KmpCodeGenerator,
) : ProcessorContract.MockFactoryEntryPointGenerator {
private val unused = AnnotationSpec.builder(Suppress::class).addMember(
"%S, %S",
"UNUSED_PARAMETER",
"UNUSED_EXPRESSION"
).build()

private fun buildMockFactory(): FunSpec {
val type = TypeVariableName(KMOCK_FACTORY_TYPE_NAME)

Expand Down Expand Up @@ -119,7 +127,9 @@ internal class KMockFactoryEntryPointGenerator(
)
val (_, generics) = utils.splitInterfacesIntoRegularAndGenerics(templateSources)

file.addAnnotation(unused)
file.addImport(KMOCK_CONTRACT.packageName, KMOCK_CONTRACT.simpleName)
file.addImport(NOOP_COLLECTOR_NAME.packageName, NOOP_COLLECTOR_NAME.simpleName)

file.addFunction(buildMockFactory())
file.addFunction(buildSpyFactory())
Expand Down Expand Up @@ -149,8 +159,10 @@ internal class KMockFactoryEntryPointGenerator(
options.rootPackage,
"MockFactory"
)
file.addAnnotation(unused)

file.addImport(KMOCK_CONTRACT.packageName, KMOCK_CONTRACT.simpleName)
file.addImport(NOOP_COLLECTOR_NAME.packageName, NOOP_COLLECTOR_NAME.simpleName)

generateGenericEntryPoints(
file,
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.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSFile
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
Expand All @@ -19,6 +20,7 @@ import tech.antibytes.kmock.processor.ProcessorContract
import tech.antibytes.kmock.processor.ProcessorContract.Companion.KMOCK_CONTRACT
import tech.antibytes.kmock.processor.ProcessorContract.Companion.KMOCK_FACTORY_TYPE_NAME
import tech.antibytes.kmock.processor.ProcessorContract.Companion.KSPY_FACTORY_TYPE_NAME
import tech.antibytes.kmock.processor.ProcessorContract.Companion.NOOP_COLLECTOR_NAME
import tech.antibytes.kmock.processor.ProcessorContract.Relaxer
import tech.antibytes.kmock.processor.ProcessorContract.TemplateSource

Expand All @@ -31,6 +33,12 @@ internal class KMockFactoryGenerator(
private val genericResolver: ProcessorContract.GenericResolver,
private val codeGenerator: CodeGenerator,
) : ProcessorContract.MockFactoryGenerator {
private val unused = AnnotationSpec.builder(Suppress::class).addMember(
"%S, %S",
"UNUSED_PARAMETER",
"UNUSED_EXPRESSION"
).build()

private fun buildGenericsInfo(
generics: List<TypeName>
): String {
Expand Down Expand Up @@ -326,8 +334,13 @@ internal class KMockFactoryGenerator(
options.rootPackage,
"MockFactory"
)
file.addAnnotation(unused)
file.addImport(KMOCK_CONTRACT.packageName, KMOCK_CONTRACT.simpleName)

if (!options.isKmp) {
file.addImport(NOOP_COLLECTOR_NAME.packageName, NOOP_COLLECTOR_NAME.simpleName)
}

val (regular, generics) = utils.splitInterfacesIntoRegularAndGenerics(templateSources)

val genericFactories = buildGenericFactories(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

package tech.antibytes.kmock.processor.factory

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterSpec
Expand All @@ -18,8 +17,6 @@ import tech.antibytes.kmock.processor.ProcessorContract.TemplateSource
internal class KMockFactoryGeneratorUtil(
private val genericResolver: ProcessorContract.GenericResolver
) : ProcessorContract.MockFactoryGeneratorUtil {
private val unused = AnnotationSpec.builder(Suppress::class).addMember("%S", "UNUSED_PARAMETER").build()

private fun buildGenericFactoryArgument(
identifier: TypeVariableName,
generics: List<TypeVariableName>
Expand Down Expand Up @@ -48,7 +45,7 @@ internal class KMockFactoryGeneratorUtil(
ParameterSpec.builder(
name = "templateType",
type = buildGenericFactoryArgument(identifier, generics)
).addAnnotation(unused).build()
).build()
)
}

Expand All @@ -63,7 +60,7 @@ internal class KMockFactoryGeneratorUtil(
parameter.defaultValue("false")
}

return parameter.addAnnotation(unused).build()
return parameter.build()
}

private fun buildUnitRelaxedParameter(
Expand All @@ -74,22 +71,22 @@ internal class KMockFactoryGeneratorUtil(
parameter.defaultValue("false")
}

return parameter.addAnnotation(unused).build()
return parameter.build()
}

private fun buildVerifierParameter(
hasDefault: Boolean
): ParameterSpec {
val parameter = ParameterSpec.builder("verifier", ProcessorContract.COLLECTOR_NAME)
if (hasDefault) {
parameter.defaultValue("Collector { _, _ -> Unit }")
parameter.defaultValue("NoopCollector")
}

return parameter.build()
}

private fun buildFreezeParameter(
hasDefault: Boolean
hasDefault: Boolean,
): ParameterSpec {
val parameter = ParameterSpec.builder("freeze", Boolean::class)
if (hasDefault) {
Expand Down Expand Up @@ -122,7 +119,6 @@ internal class KMockFactoryGeneratorUtil(

private fun buildSpyParameter(): ParameterSpec {
return ParameterSpec.builder("spyOn", TypeVariableName("SpyOn"))
.addAnnotation(unused)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("UNUSED_PARAMETER", "UNUSED_EXPRESSION")

package generatorTest

import kotlin.Boolean
Expand All @@ -7,8 +9,8 @@ import tech.antibytes.kmock.KMockContract.Collector

internal actual inline fun <reified Mock> kmock(
verifier: KMockContract.Collector,
@Suppress("UNUSED_PARAMETER") relaxed: Boolean,
@Suppress("UNUSED_PARAMETER") relaxUnitFun: Boolean,
relaxed: Boolean,
relaxUnitFun: Boolean,
freeze: Boolean
): Mock = when (Mock::class) {
factory.template.alias.AliasCommonMock::class -> factory.template.alias.AliasCommonMock(verifier =
Expand All @@ -17,7 +19,7 @@ internal actual inline fun <reified Mock> kmock(
}

internal actual inline fun <reified Mock : SpyOn, reified SpyOn> kspy(
@Suppress("UNUSED_PARAMETER") spyOn: SpyOn,
spyOn: SpyOn,
verifier: KMockContract.Collector,
freeze: Boolean
): Mock = when (Mock::class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
@file:Suppress("UNUSED_PARAMETER", "UNUSED_EXPRESSION")

package generatorTest

import kotlin.Boolean
import kotlin.Suppress
import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.Collector
import tech.antibytes.kmock.proxy.NoopCollector

internal expect inline fun <reified Mock> kmock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
@Suppress("UNUSED_PARAMETER") relaxed: Boolean = false,
@Suppress("UNUSED_PARAMETER") relaxUnitFun: Boolean = false,
verifier: KMockContract.Collector = NoopCollector,
relaxed: Boolean = false,
relaxUnitFun: Boolean = false,
freeze: Boolean = true
): Mock

internal expect inline fun <reified Mock : SpyOn, reified SpyOn> kspy(
@Suppress("UNUSED_PARAMETER") spyOn: SpyOn,
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: SpyOn,
verifier: KMockContract.Collector = NoopCollector,
freeze: Boolean = true
): Mock
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("UNUSED_PARAMETER", "UNUSED_EXPRESSION")

package generatorTest

import factory.template.alias.Generic
Expand All @@ -7,30 +9,30 @@ import kotlin.Comparable
import kotlin.Suppress
import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.Collector
import tech.antibytes.kmock.proxy.NoopCollector

internal inline fun <reified Mock> kmock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
@Suppress("UNUSED_PARAMETER") relaxed: Boolean = false,
@Suppress("UNUSED_PARAMETER") relaxUnitFun: Boolean = false,
verifier: KMockContract.Collector = NoopCollector,
relaxed: Boolean = false,
relaxUnitFun: Boolean = false,
freeze: Boolean = true
): Mock = when (Mock::class) {
else -> throw RuntimeException("Unknown Interface ${Mock::class.simpleName}.")
}

internal inline fun <reified Mock : SpyOn, reified SpyOn> kspy(
@Suppress("UNUSED_PARAMETER") spyOn: SpyOn,
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: SpyOn,
verifier: KMockContract.Collector = NoopCollector,
freeze: Boolean = true
): Mock = when (Mock::class) {
else -> throw RuntimeException("Unknown Interface ${Mock::class.simpleName}.")
}

internal inline fun <reified Mock : Generic<K, L>, K : Any, L> kmock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
@Suppress("UNUSED_PARAMETER") relaxed: Boolean = false,
@Suppress("UNUSED_PARAMETER") relaxUnitFun: Boolean = false,
verifier: KMockContract.Collector = NoopCollector,
relaxed: Boolean = false,
relaxUnitFun: Boolean = false,
freeze: Boolean = true,
@Suppress("UNUSED_PARAMETER")
templateType: kotlin.reflect.KClass<factory.template.alias.Generic<*, *>>
): Mock where L : Any, L : Comparable<L> = when (Mock::class) {
factory.template.alias.AliasGenericMock::class -> factory.template.alias.AliasGenericMock<K,
Expand All @@ -39,10 +41,9 @@ internal inline fun <reified Mock : Generic<K, L>, K : Any, L> kmock(
}

internal inline fun <reified Mock : SpyOn, reified SpyOn : Generic<K, L>, K : Any, L> kspy(
@Suppress("UNUSED_PARAMETER") spyOn: SpyOn,
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: SpyOn,
verifier: KMockContract.Collector = NoopCollector,
freeze: Boolean = true,
@Suppress("UNUSED_PARAMETER")
templateType: kotlin.reflect.KClass<factory.template.alias.Generic<*, *>>
): Mock where L : Any, L : Comparable<L> = when (Mock::class) {
else -> throw RuntimeException("Unknown Interface ${Mock::class.simpleName}.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
@file:Suppress("UNUSED_PARAMETER", "UNUSED_EXPRESSION")

package generatorTest

import kotlin.Boolean
import kotlin.Suppress
import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.Collector
import tech.antibytes.kmock.proxy.NoopCollector

internal inline fun <reified Mock> kmock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
@Suppress("UNUSED_PARAMETER") relaxed: Boolean = false,
@Suppress("UNUSED_PARAMETER") relaxUnitFun: Boolean = false,
verifier: KMockContract.Collector = NoopCollector,
relaxed: Boolean = false,
relaxUnitFun: Boolean = false,
freeze: Boolean = true
): Mock = when (Mock::class) {
factory.template.alias.AliasPlatformMock::class ->
Expand All @@ -18,8 +21,8 @@ internal inline fun <reified Mock> kmock(
}

internal inline fun <reified Mock : SpyOn, reified SpyOn> kspy(
@Suppress("UNUSED_PARAMETER") spyOn: SpyOn,
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: SpyOn,
verifier: KMockContract.Collector = NoopCollector,
freeze: Boolean = true
): Mock = when (Mock::class) {
else -> throw RuntimeException("Unknown Interface ${Mock::class.simpleName}.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("UNUSED_PARAMETER", "UNUSED_EXPRESSION")

package generatorTest

import factory.template.generic.Common
Expand All @@ -10,15 +12,15 @@ import tech.antibytes.kmock.KMockContract.Collector

internal actual inline fun <reified Mock> kmock(
verifier: KMockContract.Collector,
@Suppress("UNUSED_PARAMETER") relaxed: Boolean,
@Suppress("UNUSED_PARAMETER") relaxUnitFun: Boolean,
relaxed: Boolean,
relaxUnitFun: Boolean,
freeze: Boolean
): Mock = when (Mock::class) {
else -> throw RuntimeException("Unknown Interface ${Mock::class.simpleName}.")
}

internal actual inline fun <reified Mock : SpyOn, reified SpyOn> kspy(
@Suppress("UNUSED_PARAMETER") spyOn: SpyOn,
spyOn: SpyOn,
verifier: KMockContract.Collector,
freeze: Boolean
): Mock = when (Mock::class) {
Expand All @@ -27,10 +29,9 @@ internal actual inline fun <reified Mock : SpyOn, reified SpyOn> kspy(

internal actual inline fun <reified Mock : Common<K, L>, K : Any, L> kmock(
verifier: KMockContract.Collector,
@Suppress("UNUSED_PARAMETER") relaxed: Boolean,
@Suppress("UNUSED_PARAMETER") relaxUnitFun: Boolean,
relaxed: Boolean,
relaxUnitFun: Boolean,
freeze: Boolean,
@Suppress("UNUSED_PARAMETER")
templateType: kotlin.reflect.KClass<factory.template.generic.Common<*, *>>
): Mock where L : Any, L : Comparable<L> = when (Mock::class) {
factory.template.generic.CommonMock::class -> factory.template.generic.CommonMock<K, L>(verifier =
Expand All @@ -39,11 +40,10 @@ internal actual inline fun <reified Mock : Common<K, L>, K : Any, L> kmock(
}

internal actual inline fun <reified Mock : SpyOn, reified SpyOn : Common<K, L>, K : Any, L> kspy(
@Suppress("UNUSED_PARAMETER") spyOn: SpyOn,
spyOn: SpyOn,
verifier: KMockContract.Collector,
freeze: Boolean,
@Suppress("UNUSED_PARAMETER")
templateType: kotlin.reflect.KClass<factory.template.generic.Common<*, *>>
templateType: kotlin.reflect.KClass<factory.template.generic.Common<*, *>>
): Mock where L : Any, L : Comparable<L> = when (Mock::class) {
else -> throw RuntimeException("Unknown Interface ${Mock::class.simpleName}.")
}
Loading

0 comments on commit 67fe620

Please sign in to comment.