Skip to content

Commit

Permalink
Investigate
Browse files Browse the repository at this point in the history
  • Loading branch information
bitPogo committed Jun 29, 2022
1 parent 864d480 commit 47942e4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 45 deletions.
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 @@ -46,20 +46,24 @@ internal class KMockRelaxationAggregator(
private fun fetchRelaxerAnnotated(resolver: Resolver): Sequence<KSAnnotated> {
return resolver.getSymbolsWithAnnotation(
ProcessorContract.RELAXATION_NAME,
false
true
)
}

override fun extractRelaxer(resolver: Resolver): Relaxer? {
val annotatedSymbol = fetchRelaxerAnnotated(resolver).firstOrNull()

return if (annotatedSymbol is KSFunctionDeclaration) {
logger.warn("relaxer found")
validateRelaxer(annotatedSymbol)

Relaxer(
annotatedSymbol.packageName.asString(),
annotatedSymbol.simpleName.asString()
annotatedSymbol.simpleName.asString(),
annotatedSymbol.containingFile!!
)
} else {
logger.warn("relaxer not found")
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 @@ -65,13 +66,15 @@ class KMockRelaxationAggregatorSpec {
resolver.getSymbolsWithAnnotation(any(), any())
} returns annotated

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

// When
val relaxer = KMockRelaxationAggregator(logger).extractRelaxer(resolver)

// Then
relaxer mustBe null
verify(exactly = 1) {
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, false)
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, true)
}
}

Expand Down Expand Up @@ -105,9 +108,12 @@ 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

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

// When
KMockRelaxationAggregator(logger).extractRelaxer(resolver)

Expand All @@ -116,7 +122,7 @@ class KMockRelaxationAggregatorSpec {
logger.error("Invalid Relaxer!")
}
verify(exactly = 1) {
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, false)
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, true)
}
}

Expand Down Expand Up @@ -151,9 +157,12 @@ 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

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

// When
KMockRelaxationAggregator(logger).extractRelaxer(resolver)

Expand All @@ -162,7 +171,7 @@ class KMockRelaxationAggregatorSpec {
logger.error("Invalid Relaxer!")
}
verify(exactly = 1) {
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, false)
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, true)
}
}

Expand Down Expand Up @@ -197,9 +206,12 @@ 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

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

// When
KMockRelaxationAggregator(logger).extractRelaxer(resolver)

Expand All @@ -208,7 +220,7 @@ class KMockRelaxationAggregatorSpec {
logger.error("Invalid Relaxer!")
}
verify(exactly = 1) {
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, false)
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, true)
}
}

Expand Down Expand Up @@ -247,9 +259,12 @@ 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

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

// When
KMockRelaxationAggregator(logger).extractRelaxer(resolver)

Expand All @@ -258,7 +273,7 @@ class KMockRelaxationAggregatorSpec {
logger.error("Invalid Relaxer!")
}
verify(exactly = 1) {
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, false)
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, true)
}
}

Expand Down Expand Up @@ -293,9 +308,12 @@ 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

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

// When
KMockRelaxationAggregator(logger).extractRelaxer(resolver)

Expand All @@ -304,7 +322,7 @@ class KMockRelaxationAggregatorSpec {
logger.error("Invalid Relaxer!")
}
verify(exactly = 1) {
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, false)
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, true)
}
}

Expand All @@ -322,6 +340,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,18 +358,21 @@ 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

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

// When
val actual = KMockRelaxationAggregator(logger).extractRelaxer(resolver)

// 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)
resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, true)
}
}
}

0 comments on commit 47942e4

Please sign in to comment.