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 relaxUnitFun #20

Merged
merged 3 commits into from
Feb 27, 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 @@ -11,6 +11,7 @@ import tech.antibytes.kmock.KMockContract
internal expect inline fun <reified T> kmock(
verifier: KMockContract.Collector = KMockContract.Collector { _, _ -> Unit },
relaxed: Boolean = false,
relaxUnitFun: Boolean = false,
freeze: Boolean = true
): T

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ internal object FactoryGenerator : KMockPluginContract.FactoryGenerator {
.build()
}

private fun buildUnitRelaxedParameter(): ParameterSpec {
return ParameterSpec.builder("relaxUnitFun", Boolean::class)
.defaultValue("false")
.build()
}

private fun buildVerifierParameter(): ParameterSpec {
return ParameterSpec.builder("verifier", COLLECTOR_NAME)
.defaultValue("Collector { _, _ -> Unit }")
Expand All @@ -51,6 +57,7 @@ internal object FactoryGenerator : KMockPluginContract.FactoryGenerator {
.returns(type)
.addParameter(buildVerifierParameter())
.addParameter(buildRelaxedParameter())
.addParameter(buildUnitRelaxedParameter())
.addParameter(buildFreezeParameter())
.addModifiers(KModifier.EXPECT)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class FactoryGeneratorSpec {
}
}

println(actual)

actual!!.normalizeSource() mustBe expected.normalizeSource()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tech.antibytes.kmock.KMockContract.Collector
internal expect inline fun <reified T> kmock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
relaxed: Boolean = false,
relaxUnitFun: Boolean = false,
freeze: Boolean = true
): T

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ internal class KMockFactoryGenerator(
return parameter.build()
}

private fun buildUnitRelaxedParameter(
isKmp: Boolean
): ParameterSpec {
val parameter = ParameterSpec.builder("relaxUnitFun", Boolean::class)
if (!isKmp) {
parameter.defaultValue("false")
}

return parameter.build()
}

private fun buildVerifierParameter(
isKmp: Boolean
): ParameterSpec {
Expand Down Expand Up @@ -67,24 +78,24 @@ internal class KMockFactoryGenerator(

if (relaxer == null) {
function.addStatement(
"%L::class -> %LMock(verifier = verifier, freeze = freeze) as T",
"%L::class -> %LMock(verifier = verifier, relaxUnitFun = relaxUnitFun, freeze = freeze) as T",
qualifiedName,
interfaceName,
)

function.addStatement(
"%LMock::class -> %LMock(verifier = verifier, freeze = freeze) as T",
"%LMock::class -> %LMock(verifier = verifier, relaxUnitFun = relaxUnitFun, freeze = freeze) as T",
interfaceName,
interfaceName,
)
} else {
function.addStatement(
"%L::class -> %LMock(verifier = verifier, relaxed = relaxed, freeze = freeze) as T",
"%L::class -> %LMock(verifier = verifier, relaxed = relaxed, relaxUnitFun = relaxUnitFun, freeze = freeze) as T",
qualifiedName,
interfaceName,
)
function.addStatement(
"%LMock::class -> %LMock(verifier = verifier, relaxed = relaxed, freeze = freeze) as T",
"%LMock::class -> %LMock(verifier = verifier, relaxed = relaxed, relaxUnitFun = relaxUnitFun, freeze = freeze) as T",
interfaceName,
interfaceName,
)
Expand All @@ -109,6 +120,7 @@ internal class KMockFactoryGenerator(
.returns(type)
.addParameter(buildVerifierParameter(isKmp))
.addParameter(buildRelaxedParameter(isKmp))
.addParameter(buildUnitRelaxedParameter(isKmp))
.addParameter(buildFreezeParameter(isKmp))

if (isKmp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import tech.antibytes.kmock.processor.ProcessorContract.Companion.COLLECTOR_NAME
import tech.antibytes.kmock.processor.ProcessorContract.Companion.KMOCK_CONTRACT
import tech.antibytes.kmock.processor.ProcessorContract.Companion.PROP_NAME
import tech.antibytes.kmock.processor.ProcessorContract.Companion.SYNC_FUN_NAME
import tech.antibytes.kmock.processor.ProcessorContract.Companion.UNIT_RELAXER
import tech.antibytes.kmock.processor.ProcessorContract.Relaxer
import java.util.Locale

Expand Down Expand Up @@ -91,11 +92,22 @@ internal class KMockGenerator(
}
}

private fun buildRelaxer(relaxer: Relaxer?): String {
return if (relaxer == null) {
private fun buildRelaxers(relaxer: Relaxer?, useUnitFunRelaxer: Boolean): String {
val unitFunRelaxerStr = if (useUnitFunRelaxer) {
"unitFunRelaxer = if (relaxUnitFun) { { ${UNIT_RELAXER.simpleName}() } } else { null }"
} else {
""
}
val relaxerStr = if (relaxer != null) {
"relaxer = if (relaxed) { { mockId -> ${relaxer.functionName}(mockId) } } else { null }"
} else {
"relaxer = null"
}

return if (unitFunRelaxerStr.isEmpty()) {
relaxerStr
} else {
"relaxer = if(relaxed) { { mockId -> ${relaxer.functionName}(mockId) } } else { null }"
"$unitFunRelaxerStr, $relaxerStr"
}
}

Expand All @@ -114,9 +126,13 @@ internal class KMockGenerator(
val freeze = ParameterSpec.builder("freeze", Boolean::class)
freeze.defaultValue("true")

val relaxUnit = ParameterSpec.builder("relaxUnitFun", Boolean::class)
relaxUnit.defaultValue("false")

constructor.addParameter(collector.build())
constructor.addParameter(spy.build())
constructor.addParameter(freeze.build())
constructor.addParameter(relaxUnit.build())

if (relaxer != null) {
val relaxed = ParameterSpec.builder("relaxed", Boolean::class)
Expand Down Expand Up @@ -188,10 +204,10 @@ internal class KMockGenerator(
""".trimMargin(),
name,
"null",
buildRelaxer(relaxer),
buildRelaxers(relaxer, false),
name,
"{ spyOn.$propertyName }",
buildRelaxer(relaxer)
buildRelaxers(relaxer, false)
)
} else {
propertyMock.initializer(
Expand All @@ -206,11 +222,11 @@ internal class KMockGenerator(
name,
"null",
"null",
buildRelaxer(relaxer),
buildRelaxers(relaxer, false),
name,
"{ spyOn.$propertyName }",
"{ spyOn.$propertyName = it; Unit }",
buildRelaxer(relaxer)
buildRelaxers(relaxer, false)
)
}
}
Expand Down Expand Up @@ -317,7 +333,7 @@ internal class KMockGenerator(
mockery,
"$qualifier#$mockeryName",
"if (spyOn != null) { ${buildFunctionSpyInvocation(functionName, parameterNames)} } else { null }",
buildRelaxer(relaxer)
buildRelaxers(relaxer, true)
)
}

Expand Down Expand Up @@ -567,7 +583,7 @@ internal class KMockGenerator(
}

implementation.primaryConstructor(
buildConstructor(superType, relaxer,)
buildConstructor(superType, relaxer)
)

template.getAllProperties().forEach { ksProperty ->
Expand Down Expand Up @@ -626,6 +642,7 @@ internal class KMockGenerator(
file.addImport(PROP_NAME.packageName, PROP_NAME.simpleName)
file.addImport(SYNC_FUN_NAME.packageName, SYNC_FUN_NAME.simpleName)
file.addImport(ASYNC_FUN_NAME.packageName, ASYNC_FUN_NAME.simpleName)
file.addImport(UNIT_RELAXER.packageName, UNIT_RELAXER.simpleName)

if (relaxer != null) {
file.addImport(relaxer.packageName, relaxer.functionName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ internal interface ProcessorContract {
KMockContract::class.java.simpleName
)

val UNIT_RELAXER = ClassName(
KMockContract::class.java.packageName,
"relaxVoidFunction"
)

val SYNC_FUN_NAME = ClassName(
SyncFunMockery::class.java.packageName,
SyncFunMockery::class.java.simpleName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.Collector
import tech.antibytes.kmock.PropertyMockery
import tech.antibytes.kmock.SyncFunMockery
import tech.antibytes.kmock.relaxVoidFunction

internal class AsyncFunctionCommonMock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: AsyncFunctionCommon? = null,
freeze: Boolean = true
freeze: Boolean = true,
relaxUnitFun: Boolean = false
) : AsyncFunctionCommon {
public val _foo: KMockContract.AsyncFunMockery<Any, suspend (kotlin.Int, kotlin.Any) ->
kotlin.Any> = AsyncFunMockery("generatorTest.AsyncFunctionCommon#_foo", spyOn = if (spyOn !=
null) { { fuzz, ozz ->
foo(fuzz, ozz) } } else { null }, collector = verifier, freeze = freeze, )
foo(fuzz, ozz) } } else { null }, collector = verifier, freeze = freeze, unitFunRelaxer = if
(relaxUnitFun) { { relaxVoidFunction() } } else { null }, relaxer = null)

public override suspend fun foo(fuzz: Int, ozz: Any): Any = _foo.invoke(fuzz, ozz)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.Collector
import tech.antibytes.kmock.PropertyMockery
import tech.antibytes.kmock.SyncFunMockery
import tech.antibytes.kmock.relaxVoidFunction

internal class AsyncFunctionPlatformMock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: AsyncFunctionPlatform? = null,
freeze: Boolean = true
freeze: Boolean = true,
relaxUnitFun: Boolean = false
) : AsyncFunctionPlatform {
public val _foo: KMockContract.AsyncFunMockery<Any, suspend (kotlin.Int, kotlin.Any) ->
kotlin.Any> = AsyncFunMockery("generatorTest.AsyncFunctionPlatform#_foo", spyOn = if (spyOn !=
null) { { fuzz, ozz ->
foo(fuzz, ozz) } } else { null }, collector = verifier, freeze = freeze, )
foo(fuzz, ozz) } } else { null }, collector = verifier, freeze = freeze, unitFunRelaxer = if
(relaxUnitFun) { { relaxVoidFunction() } } else { null }, relaxer = null)

public override suspend fun foo(fuzz: Int, ozz: Any): Any = _foo.invoke(fuzz, ozz)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import tech.antibytes.kmock.KMockContract.Collector
internal actual inline fun <reified T> kmock(
verifier: KMockContract.Collector,
relaxed: Boolean,
relaxUnitFun: Boolean,
freeze: Boolean
): T = when (T::class) {
generatorTest.PropertyCommon::class -> generatorTest.PropertyCommonMock(verifier = verifier,
freeze = freeze) as T
relaxUnitFun = relaxUnitFun, freeze = freeze) as T
generatorTest.PropertyCommonMock::class -> generatorTest.PropertyCommonMock(verifier = verifier,
freeze = freeze) as T
relaxUnitFun = relaxUnitFun, freeze = freeze) as T
else -> throw RuntimeException("Unknown Interface ${T::class.simpleName}.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import tech.antibytes.kmock.KMockContract.Collector
internal inline fun <reified T> kmock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
relaxed: Boolean = false,
relaxUnitFun: Boolean = false,
freeze: Boolean = true
): T = when (T::class) {
generatorTest.PropertyPlatform::class -> generatorTest.PropertyPlatformMock(verifier = verifier,
freeze = freeze) as T
relaxUnitFun = relaxUnitFun, freeze = freeze) as T
generatorTest.PropertyPlatformMock::class -> generatorTest.PropertyPlatformMock(verifier =
verifier, freeze = freeze) as T
verifier, relaxUnitFun = relaxUnitFun, freeze = freeze) as T
else -> throw RuntimeException("Unknown Interface ${T::class.simpleName}.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import tech.antibytes.kmock.KMockContract.Collector
internal inline fun <reified T> kmock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
relaxed: Boolean = false,
relaxUnitFun: Boolean = false,
freeze: Boolean = true
): T = when (T::class) {
generatorTest.Relaxed::class -> generatorTest.RelaxedMock(verifier = verifier, relaxed = relaxed,
freeze = freeze) as T
relaxUnitFun = relaxUnitFun, freeze = freeze) as T
generatorTest.RelaxedMock::class -> generatorTest.RelaxedMock(verifier = verifier, relaxed =
relaxed, freeze = freeze) as T
relaxed, relaxUnitFun = relaxUnitFun, freeze = freeze) as T
else -> throw RuntimeException("Unknown Interface ${T::class.simpleName}.")
}

Expand Down
Loading