Skip to content

Commit

Permalink
Add relaxUnit into Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
bitPogo committed Feb 27, 2022
1 parent 6665345 commit 91316d4
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 10 deletions.
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 @@ -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

0 comments on commit 91316d4

Please sign in to comment.