Skip to content

Commit

Permalink
Wire UnitFunRelaxer into Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
bitPogo committed Feb 27, 2022
1 parent 191d365 commit 6665345
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 57 deletions.
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 @@ -11,71 +11,82 @@ 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 SyncFunctionOverloadMock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: SyncFunctionOverload? = null,
freeze: Boolean = true
freeze: Boolean = true,
relaxUnitFun: Boolean = false
) : SyncFunctionOverload {
public override val foo: Any
get() = _foo.onGet()

public val _foo: KMockContract.PropertyMockery<Any> = if (spyOn == null) {
PropertyMockery("generatorTest.SyncFunctionOverload#_foo", spyOnGet = null, collector =
verifier, freeze = freeze, )
verifier, freeze = freeze, relaxer = null)
} else {
PropertyMockery("generatorTest.SyncFunctionOverload#_foo", spyOnGet = { spyOn.foo },
collector = verifier, freeze = freeze, )
collector = verifier, freeze = freeze, relaxer = null)
}


public val _fooWithIntAny: KMockContract.SyncFunMockery<Any, (kotlin.Int, kotlin.Any) ->
kotlin.Any> = SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithIntAny", 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 val _fooWithAnyInt: KMockContract.SyncFunMockery<Any, (kotlin.Any, kotlin.Int) ->
kotlin.Any> = SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithAnyInt", 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 val _fooWithAnyString: KMockContract.SyncFunMockery<Any, (kotlin.Any, kotlin.String) ->
kotlin.Any> = SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithAnyString", 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 val _fooWithStringAny: KMockContract.SyncFunMockery<Any, (kotlin.String, kotlin.Any) ->
kotlin.Any> = SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithStringAny", 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 val _fooWithStringGeneratorTestAbc: KMockContract.SyncFunMockery<Any, (kotlin.String,
generatorTest.Abc) -> kotlin.Any> =
SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithStringGeneratorTestAbc", 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 val _fooWithFunction1: KMockContract.SyncFunMockery<Any, (kotlin.Function1<kotlin.Any,
kotlin.Unit>) -> kotlin.Any> =
SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithFunction1", spyOn = if (spyOn !=
null) { { fuzz ->
foo(fuzz) } } else { null }, collector = verifier, freeze = freeze, )
foo(fuzz) } } else { null }, collector = verifier, freeze = freeze, unitFunRelaxer = if
(relaxUnitFun) { { relaxVoidFunction() } } else { null }, relaxer = null)

public val _fooWithAny: KMockContract.SyncFunMockery<Unit, (Any?) -> kotlin.Unit> =
SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithAny", spyOn = if (spyOn != null) {
{ fuzz ->
foo(fuzz) } } else { null }, collector = verifier, freeze = freeze, )
foo(fuzz) } } else { null }, collector = verifier, freeze = freeze, unitFunRelaxer = if
(relaxUnitFun) { { relaxVoidFunction() } } else { null }, relaxer = null)

public val _fooWithSyncFunctionOverload:
KMockContract.SyncFunMockery<Unit, (SyncFunctionOverload) -> kotlin.Unit> =
SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithSyncFunctionOverload", spyOn = if
(spyOn != null) { { fuzz ->
foo(fuzz) } } else { null }, collector = verifier, freeze = freeze, )
foo(fuzz) } } else { null }, collector = verifier, freeze = freeze, unitFunRelaxer = if
(relaxUnitFun) { { relaxVoidFunction() } } else { null }, relaxer = null)

public val _fooWithLPG: KMockContract.SyncFunMockery<Unit, (LPG) -> kotlin.Unit> =
SyncFunMockery("generatorTest.SyncFunctionOverload#_fooWithLPG", spyOn = if (spyOn != null) {
{ fuzz ->
foo(fuzz) } } else { null }, collector = verifier, freeze = freeze, )
foo(fuzz) } } else { null }, collector = verifier, freeze = freeze, unitFunRelaxer = if
(relaxUnitFun) { { relaxVoidFunction() } } else { null }, relaxer = null)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@ 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 GenericsMock<K : Any, L>(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: Generics<K, L>? = null,
freeze: Boolean = true
freeze: Boolean = true,
relaxUnitFun: Boolean = false
) : Generics<K, L> {
public override var template: K
get() = _template.onGet()
set(`value`) = _template.onSet(value)

public val _template: KMockContract.PropertyMockery<K> = if (spyOn == null) {
PropertyMockery("generatorTest.Generics#_template", spyOnGet = null, spyOnSet = null,
collector = verifier, freeze = freeze, )
collector = verifier, freeze = freeze, relaxer = null)
} else {
PropertyMockery("generatorTest.Generics#_template", spyOnGet = { spyOn.template }, spyOnSet
= { spyOn.template = it; Unit }, collector = verifier, freeze = freeze, )
= { spyOn.template = it; Unit }, collector = verifier, freeze = freeze, relaxer = null)
}


public val _foo: KMockContract.SyncFunMockery<Unit, (Any?) -> kotlin.Unit> =
SyncFunMockery("generatorTest.Generics#_foo", spyOn = if (spyOn != null) { { payload ->
foo(payload) } } else { null }, collector = verifier, freeze = freeze, )
foo(payload) } } else { null }, collector = verifier, freeze = freeze, unitFunRelaxer = if
(relaxUnitFun) { { relaxVoidFunction() } } else { null }, relaxer = null)

public val _fooWithInt: KMockContract.SyncFunMockery<Unit, (Int) -> kotlin.Unit> =
SyncFunMockery("generatorTest.Generics#_fooWithInt", spyOn = if (spyOn != null) { { payload ->
foo(payload) } } else { null }, collector = verifier, freeze = freeze, )
foo(payload) } } else { null }, collector = verifier, freeze = freeze, unitFunRelaxer = if
(relaxUnitFun) { { relaxVoidFunction() } } else { null }, relaxer = null)

public override fun <T> foo(payload: T): Unit = _foo.invoke(payload)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ 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 PropertyCommonMock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: PropertyCommon? = null,
freeze: Boolean = true
freeze: Boolean = true,
relaxUnitFun: Boolean = false
) : PropertyCommon {
public override val foo: String
get() = _foo.onGet()

public val _foo: KMockContract.PropertyMockery<String> = if (spyOn == null) {
PropertyMockery("generatorTest.PropertyCommon#_foo", spyOnGet = null, collector = verifier,
freeze = freeze, )
freeze = freeze, relaxer = null)
} else {
PropertyMockery("generatorTest.PropertyCommon#_foo", spyOnGet = { spyOn.foo }, collector =
verifier, freeze = freeze, )
verifier, freeze = freeze, relaxer = null)
}


Expand All @@ -34,10 +36,10 @@ internal class PropertyCommonMock(

public val _buzz: KMockContract.PropertyMockery<Any> = if (spyOn == null) {
PropertyMockery("generatorTest.PropertyCommon#_buzz", spyOnGet = null, spyOnSet = null,
collector = verifier, freeze = freeze, )
collector = verifier, freeze = freeze, relaxer = null)
} else {
PropertyMockery("generatorTest.PropertyCommon#_buzz", spyOnGet = { spyOn.buzz }, spyOnSet =
{ spyOn.buzz = it; Unit }, collector = verifier, freeze = freeze, )
{ spyOn.buzz = it; Unit }, collector = verifier, freeze = freeze, relaxer = null)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,36 @@ 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 PropertyPlatformMock(
verifier: KMockContract.Collector = Collector { _, _ -> Unit },
spyOn: PropertyPlatform? = null,
freeze: Boolean = true
freeze: Boolean = true,
relaxUnitFun: Boolean = false
) : PropertyPlatform {
public override val foo: String
get() = _foo.onGet()

public val _foo: KMockContract.PropertyMockery<String> = if(spyOn == null) {
public val _foo: KMockContract.PropertyMockery<String> = if (spyOn == null) {
PropertyMockery("generatorTest.PropertyPlatform#_foo", spyOnGet = null, collector =
verifier, freeze = freeze, )
verifier, freeze = freeze, relaxer = null)
} else {
PropertyMockery("generatorTest.PropertyPlatform#_foo", spyOnGet = { spyOn.foo }, collector
= verifier, freeze = freeze, )
= verifier, freeze = freeze, relaxer = null)
}


public override var buzz: Any
get() = _buzz.onGet()
set(`value`) = _buzz.onSet(value)

public val _buzz: KMockContract.PropertyMockery<Any> = if(spyOn == null) {
public val _buzz: KMockContract.PropertyMockery<Any> = if (spyOn == null) {
PropertyMockery("generatorTest.PropertyPlatform#_buzz", spyOnGet = null, spyOnSet = null,
collector = verifier, freeze = freeze, )
collector = verifier, freeze = freeze, relaxer = null)
} else {
PropertyMockery("generatorTest.PropertyPlatform#_buzz", spyOnGet = { spyOn.buzz }, spyOnSet
= { spyOn.buzz = it; Unit }, collector = verifier, freeze = freeze, )
= { spyOn.buzz = it; Unit }, collector = verifier, freeze = freeze, relaxer = null)
}


Expand Down
Loading

0 comments on commit 6665345

Please sign in to comment.