Skip to content

Commit

Permalink
Add the possibility to generate public mocks & fakes #40.
Browse files Browse the repository at this point in the history
  • Loading branch information
Salomon BRYS committed Dec 5, 2022
1 parent 2669fa2 commit d74b132
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
21 changes: 21 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,24 @@ tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().all {
<3> Use KSP generated JVM sources on all targets.
<4> Apply the processor only on the JVM target.
<5> Make compilation of all targets dependant on the JVM KSP processor.


=== Generated classes & functions visibility

By default, every generated class or function is *`internal`*.

If you wish to have it *`public`* (because you need to share it accross modules), then you can configure the processor to generate public classes & functions:

[source,kotlin,subs="verbatim,attributes"]
.build.gradle.kts
----
// When using the MocKMP plugin:
mockmp {
public = true
}
// When using KSP directly:
ksp {
arg("org.kodein.mock.visibility", "public")
}
----
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
// Common Google, having KSP work in multiplatform correctly must be very important!
class MocKMPGradlePlugin : Plugin<Project> {

class Extension(var usesHelper: Boolean = false, val throwErrors: Boolean = false)
class Extension(
var usesHelper: Boolean = false,
var throwErrors: Boolean = false,
var public: Boolean = false
)

override fun apply(target: Project) {
target.plugins.apply("com.google.devtools.ksp")
Expand Down Expand Up @@ -58,8 +62,12 @@ class MocKMPGradlePlugin : Plugin<Project> {
if (ext.usesHelper) {
implementation("org.kodein.mock:mockmp-test-helper:${BuildConfig.VERSION}")
}
val ksp = target.extensions.getByName<com.google.devtools.ksp.gradle.KspExtension>("ksp")
if (ext.throwErrors) {
target.extensions.getByName<com.google.devtools.ksp.gradle.KspExtension>("ksp").arg("org.kodein.mock.errors", "throw")
ksp.arg("org.kodein.mock.errors", "throw")
}
if (ext.public) {
ksp.arg("org.kodein.mock.visibility", "public")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import com.squareup.kotlinpoet.ksp.*
public class MocKMPProcessor(
private val codeGenerator: CodeGenerator,
private val logger: KSPLogger,
private val throwErrors: Boolean
private val throwErrors: Boolean,
public: Boolean
) : SymbolProcessor {

private companion object {
Expand Down Expand Up @@ -46,6 +47,8 @@ public class MocKMPProcessor(

private val KSType.isAnyFunctionType get() = isFunctionType || isSuspendFunctionType

private val visibilityModifier = if (public) KModifier.PUBLIC else KModifier.INTERNAL

private class Error(message: String, val node: KSNode) : Exception(message)

override fun process(resolver: Resolver): List<KSAnnotated> {
Expand Down Expand Up @@ -187,7 +190,7 @@ public class MocKMPProcessor(
if (vItf.typeParameters.isEmpty()) vItf.toClassName()
else vItf.toClassName().parameterizedBy(vItf.typeParameters.map { it.toTypeVariableName() })
)
.addModifiers(KModifier.INTERNAL)
.addModifiers(visibilityModifier)
vItf.typeParameters.forEach { vParam ->
gCls.addTypeVariable(vParam.toTypeVariableName())
}
Expand Down Expand Up @@ -272,7 +275,7 @@ public class MocKMPProcessor(
else vCls.packageName.asString()
val gFile = FileSpec.builder(mockPkg, mockFunName)
val gFun = FunSpec.builder(mockFunName)
.addModifiers(KModifier.INTERNAL)
.addModifiers(visibilityModifier)
.returns(vType.toRealTypeName(vCls.typeParameters.toTypeParameterResolver()))
when (vCls.classKind) {
ClassKind.CLASS -> {
Expand Down Expand Up @@ -336,7 +339,7 @@ public class MocKMPProcessor(

val gFile = FileSpec.builder(vCls.packageName.asString(), "${vCls.simpleName.asString()}_injectMocks")
val gFun = FunSpec.builder("injectMocks")
.addModifiers(KModifier.INTERNAL)
.addModifiers(visibilityModifier)
.receiver(vCls.toClassName())
.addParameter("mocker", mockerTypeName)
vProps.forEach { (anno, vProp) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class MocKMPProcessorProvider : SymbolProcessorProvider {
MocKMPProcessor(
environment.codeGenerator,
environment.logger,
environment.options["org.kodein.mock.errors"] == "throw"
environment.options["org.kodein.mock.errors"] == "throw",
environment.options["org.kodein.mock.visibility"] == "public"
)
}

0 comments on commit d74b132

Please sign in to comment.