Skip to content

Commit

Permalink
Merge pull request #225 from bitPogo/feature/fix-relaxer
Browse files Browse the repository at this point in the history
Fix Relaxation
  • Loading branch information
bitPogo authored Jun 29, 2022
2 parents 864d480 + cadb31f commit 965a9fc
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

* Build Cache enabled, Mocks are not created
* Relaxer not picked correctly up on multiple test runs

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ open class KMockPublishingConfiguration {
gitWorkDirectory = "dev",
url = "https://$gitHubOwnerPath/maven-dev",
username = username,
password = passwordGitHubRepos
password = passwordGitHubRepos,
),
GitRepositoryConfiguration(
name = "Snapshot",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2022 Matthias Geisler (bitPogo) / All rights reserved.
*
* Use of this source code is governed by Apache v2.0
*/

package tech.antibytes.kmock.example

import tech.antibytes.kfixture.PublicApi
import tech.antibytes.kfixture.fixture
import tech.antibytes.kfixture.kotlinFixture
import tech.antibytes.kmock.Relaxer
import kotlin.native.concurrent.ThreadLocal
import kotlin.reflect.KClass

@ThreadLocal
object Fixture {
var fixture: PublicApi.Fixture? = null
}

@Relaxer
@Suppress("UNUSED_PARAMETER")
internal inline fun <reified T> relax(id: String): T {
if (Fixture.fixture == null) {
Fixture.fixture = kotlinFixture()
}

return Fixture.fixture!!.fixture()
}

@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER")
internal fun <T> relax(id: String, type0: KClass<CharSequence>, type1: KClass<Comparable<*>>): T {
return Fixture.fixture!!.fixture<String>() as T
}

@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER")
internal fun <T> relax(id: String, type0: KClass<Any>): T {
return Fixture.fixture!!.fixture<Int>() as T
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import tech.antibytes.kfixture.PublicApi
import tech.antibytes.kfixture.fixture
import tech.antibytes.kfixture.kotlinFixture
import tech.antibytes.kfixture.listFixture
import tech.antibytes.kmock.MockCommon
import tech.antibytes.kmock.Relaxer
import tech.antibytes.kmock.example.contract.ExampleContract
import tech.antibytes.kmock.example.contract.ExampleContract.SampleDomainObject
import tech.antibytes.kmock.example.contract.ExampleContract.SampleLocalRepository
Expand All @@ -36,36 +34,9 @@ import tech.antibytes.util.test.coroutine.runBlockingTestWithTimeoutInScope
import tech.antibytes.util.test.fulfils
import tech.antibytes.util.test.mustBe
import kotlin.js.JsName
import kotlin.native.concurrent.ThreadLocal
import kotlin.reflect.KClass
import kotlin.test.BeforeTest
import kotlin.test.Test

@ThreadLocal
object Fixture {
var fixture: PublicApi.Fixture? = null
}

@Relaxer
@Suppress("UNUSED_PARAMETER")
internal inline fun <reified T> relax(id: String): T {
if (Fixture.fixture == null) {
Fixture.fixture = kotlinFixture()
}

return Fixture.fixture!!.fixture()
}

@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER")
internal fun <T> relax(id: String, type0: KClass<CharSequence>, type1: KClass<Comparable<*>>): T {
return Fixture.fixture!!.fixture<String>() as T
}

@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER")
internal fun <T> relax(id: String, type0: KClass<Any>): T {
return Fixture.fixture!!.fixture<Int>() as T
}

@MockCommon(
SampleRemoteRepository::class,
SampleLocalRepository::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ import tech.antibytes.kmock.Relaxer as RelaxationAnnotation
internal interface ProcessorContract {
data class Relaxer(
val packageName: String,
val functionName: String
val functionName: String,
val source: KSFile,
)

data class Options(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ internal class KMockRelaxationAggregator(

return if (annotatedSymbol is KSFunctionDeclaration) {
validateRelaxer(annotatedSymbol)

Relaxer(
annotatedSymbol.packageName.asString(),
annotatedSymbol.simpleName.asString()
annotatedSymbol.simpleName.asString(),
annotatedSymbol.containingFile!!
)
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,14 @@ internal class KMockGenerator(
return mock.build()
}

private fun List<KSFile>.amendRelaxer(relaxer: Relaxer?): List<KSFile> {
return if (relaxer == null) {
this
} else {
this.toMutableList().also { it.add(relaxer.source) }
}
}

private fun writeMock(
template: KSClassDeclaration,
parents: TemplateMultiSource?,
Expand Down Expand Up @@ -520,7 +528,7 @@ internal class KMockGenerator(
file.build().writeTo(
codeGenerator = codeGenerator,
aggregating = true,
originatingKSFiles = dependencies
originatingKSFiles = dependencies.amendRelaxer(relaxer)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class KMockProcessorSpec {
val entryPointGenerator: MockFactoryEntryPointGenerator = mockk()

val filter: ProcessorContract.SourceFilter = mockk()
val relaxer = Relaxer(fixture.fixture(), fixture.fixture())
val relaxer = Relaxer(fixture.fixture(), fixture.fixture(), mockk())

val illegal: List<KSAnnotated> = listOf(mockk())

Expand Down Expand Up @@ -487,7 +487,7 @@ class KMockProcessorSpec {
val entryPointGenerator: MockFactoryEntryPointGenerator = mockk()

val filter: ProcessorContract.SourceFilter = mockk()
val relaxer = Relaxer(fixture.fixture(), fixture.fixture())
val relaxer = Relaxer(fixture.fixture(), fixture.fixture(), mockk())

val illegal: List<KSAnnotated> = listOf(mockk())

Expand Down Expand Up @@ -853,7 +853,7 @@ class KMockProcessorSpec {
val entryPointGenerator: MockFactoryEntryPointGenerator = mockk()

val filter: ProcessorContract.SourceFilter = mockk()
val relaxer = Relaxer(fixture.fixture(), fixture.fixture())
val relaxer = Relaxer(fixture.fixture(), fixture.fixture(), mockk())

val illegal: List<KSAnnotated> = listOf(mockk())

Expand Down Expand Up @@ -1011,7 +1011,7 @@ class KMockProcessorSpec {
val entryPointGenerator: MockFactoryEntryPointGenerator = mockk()

val filter: ProcessorContract.SourceFilter = mockk()
val relaxer = Relaxer(fixture.fixture(), fixture.fixture())
val relaxer = Relaxer(fixture.fixture(), fixture.fixture(), mockk())

val illegal: List<KSAnnotated> = listOf(mockk())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package tech.antibytes.kmock.processor.aggregation
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSFile
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSTypeParameter
import com.google.devtools.ksp.symbol.KSTypeReference
Expand Down Expand Up @@ -105,6 +106,7 @@ class KMockRelaxationAggregatorSpec {

every { source.packageName.asString() } returns packageName
every { source.simpleName.asString() } returns functionName
every { source.containingFile } returns mockk()

every { logger.error(any()) } just Runs

Expand Down Expand Up @@ -151,6 +153,7 @@ class KMockRelaxationAggregatorSpec {

every { source.packageName.asString() } returns packageName
every { source.simpleName.asString() } returns functionName
every { source.containingFile } returns mockk()

every { logger.error(any()) } just Runs

Expand Down Expand Up @@ -197,6 +200,7 @@ class KMockRelaxationAggregatorSpec {

every { source.packageName.asString() } returns packageName
every { source.simpleName.asString() } returns functionName
every { source.containingFile } returns mockk()

every { logger.error(any()) } just Runs

Expand Down Expand Up @@ -247,6 +251,7 @@ class KMockRelaxationAggregatorSpec {

every { source.packageName.asString() } returns packageName
every { source.simpleName.asString() } returns functionName
every { source.containingFile } returns mockk()

every { logger.error(any()) } just Runs

Expand Down Expand Up @@ -293,6 +298,7 @@ class KMockRelaxationAggregatorSpec {

every { source.packageName.asString() } returns packageName
every { source.simpleName.asString() } returns functionName
every { source.containingFile } returns mockk()

every { logger.error(any()) } just Runs

Expand Down Expand Up @@ -322,6 +328,7 @@ class KMockRelaxationAggregatorSpec {
val packageName: String = fixture.fixture()
val parameter: KSValueParameter = mockk()
val typeParameter: KSTypeParameter = mockk()
val dependency: KSFile = mockk()

every {
resolver.getSymbolsWithAnnotation(any(), any())
Expand All @@ -339,6 +346,7 @@ class KMockRelaxationAggregatorSpec {

every { source.packageName.asString() } returns packageName
every { source.simpleName.asString() } returns functionName
every { source.containingFile } returns dependency

every { logger.error(any()) } just Runs

Expand All @@ -348,7 +356,7 @@ class KMockRelaxationAggregatorSpec {
// Then
verify(exactly = 0) { logger.error(any()) }

actual mustBe ProcessorContract.Relaxer(packageName, functionName)
actual mustBe ProcessorContract.Relaxer(packageName, functionName, dependency)
verify(exactly = 1) {
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, false)
}
Expand Down

0 comments on commit 965a9fc

Please sign in to comment.