-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from AntonButov/bridge-core
bridge-core
- Loading branch information
Showing
15 changed files
with
248 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import com.code.factory.Bridge | ||
import com.code.factory.BridgeImpl | ||
import com.code.factory.OpenAiService | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
|
||
class BridgeCoreTest: StringSpec({ | ||
|
||
lateinit var bridge: Bridge | ||
lateinit var openAi: OpenAiService | ||
|
||
beforeTest { | ||
openAi = mockk(relaxed = true) | ||
coEvery { openAi.getCode(any(), any()) } returns "AiCode" | ||
bridge = BridgeImpl( | ||
openAi = openAi | ||
) | ||
} | ||
|
||
"when InterfaceFinder find interfaces bridge should to request code resolver for resolve id" { | ||
val code = bridge.getCode("some code", "interface") | ||
code shouldBe "AiCode" | ||
} | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
ksp-processor/src/main/kotlin/com/code/factory/AllDeclarationFinder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.code.factory | ||
|
||
import com.code.factory.usescases.visitors.Visitor | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.symbol.KSDeclaration | ||
import kotlin.sequences.forEach | ||
|
||
interface AllDeclarationFinder { | ||
fun getAllDeclaration(resolver: Resolver) : List<KSDeclaration> | ||
} | ||
|
||
internal class AllDeclarationFinderImpl : AllDeclarationFinder { | ||
override fun getAllDeclaration(resolver: Resolver): List<KSDeclaration> { | ||
return buildSet<KSDeclaration> { | ||
resolver.getAllFiles().forEach { | ||
it.accept( | ||
visitor = Visitor { | ||
add(it) | ||
}, | ||
data = "" | ||
) | ||
} | ||
}.filter { | ||
it.qualifiedName?.asString() != "kotlin.Any" | ||
}.filter { | ||
it.qualifiedName?.asString() != "kotlin.Unit" | ||
} | ||
} | ||
} | ||
|
||
fun allDeclarationFinder(): AllDeclarationFinder = AllDeclarationFinderImpl() |
17 changes: 17 additions & 0 deletions
17
ksp-processor/src/main/kotlin/com/code/factory/CodeResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.code.factory | ||
|
||
import com.google.devtools.ksp.symbol.KSDeclaration | ||
|
||
interface CodeResolver { | ||
fun getCodeString(vararg declaration: KSDeclaration): String | ||
} | ||
|
||
internal class CodeResolverImpl: CodeResolver { | ||
override fun getCodeString(vararg declaration: KSDeclaration): String { | ||
return "" | ||
} | ||
|
||
} | ||
|
||
fun codeResolver(): CodeResolver = | ||
CodeResolverImpl() |
28 changes: 28 additions & 0 deletions
28
ksp-processor/src/main/kotlin/com/code/factory/InterfaceFinder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.code.factory | ||
|
||
import com.code.factory.usescases.getClassKind | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.symbol.ClassKind | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
|
||
interface InterfaceFinder { | ||
fun getInterfacesWithOutImplementation(resolver: Resolver): Sequence<KSClassDeclaration> | ||
} | ||
|
||
internal class InterfaceFinderImpl() : InterfaceFinder { | ||
override fun getInterfacesWithOutImplementation(resolver: Resolver): Sequence<KSClassDeclaration> { | ||
val interfaces = resolver.getClassKind(ClassKind.INTERFACE) | ||
val classes = resolver.getClassKind(ClassKind.CLASS) | ||
|
||
return interfaces.filter { interfaceItem -> | ||
classes.none { classItem -> | ||
classItem.superTypes.any { superType -> | ||
superType.resolve().declaration.qualifiedName?.asString() == | ||
interfaceItem.qualifiedName?.asString() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun interfaceFinder(): InterfaceFinder = InterfaceFinderImpl() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,67 @@ | ||
import com.code.factory.AllDeclarationFinder | ||
import com.code.factory.Bridge | ||
import com.code.factory.CodeResolver | ||
import com.code.factory.InterfaceFinder | ||
import com.code.factory.ksp.KspProcessor | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.google.devtools.ksp.symbol.KSDeclaration | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.mockk.coVerify | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
|
||
class KspProcessorTest : StringSpec({ | ||
|
||
"should call bridge get code" { | ||
val bridge: Bridge = mockk(relaxed = true) | ||
KspProcessor( | ||
codeGenerator = mockk(relaxed = true), | ||
lateinit var bridge: Bridge | ||
lateinit var kspProcessor: KspProcessor | ||
lateinit var codeResolver: CodeResolver | ||
lateinit var allDeclarationFinder: AllDeclarationFinder | ||
lateinit var interfaceFinder : InterfaceFinder | ||
lateinit var resolver: Resolver | ||
lateinit var types: Sequence<KSClassDeclaration> | ||
lateinit var declarations: List<KSDeclaration> | ||
|
||
beforeTest { | ||
bridge = mockk(relaxed = true) | ||
allDeclarationFinder = mockk() | ||
resolver = mockk(relaxed = true) | ||
declarations = listOf() | ||
every { | ||
allDeclarationFinder.getAllDeclaration(resolver) | ||
} returns declarations | ||
codeResolver = mockk(relaxed = true) | ||
every { | ||
codeResolver.getCodeString(*declarations.toTypedArray()) | ||
} returns "code context" | ||
interfaceFinder = mockk(relaxed = true) | ||
kspProcessor = KspProcessor( | ||
logger = mockk(relaxed = true), | ||
bridge = bridge | ||
).process(mockk(relaxed = true)) | ||
writer = mockk(relaxed = true), | ||
allDeclarationFinder = mockk(relaxed = true), | ||
interfaceFinder = interfaceFinder, | ||
codeResolver = codeResolver, | ||
bridge = bridge, | ||
) | ||
types = sequenceOf(mockk(relaxed = true)) | ||
every { | ||
interfaceFinder.getInterfacesWithOutImplementation(resolver) | ||
} returns types | ||
} | ||
|
||
"should call InterfaceFinder for getting a types." { | ||
kspProcessor.process(resolver) | ||
coVerify { interfaceFinder.getInterfacesWithOutImplementation(resolver) } | ||
} | ||
|
||
"should call CodeResolver for getting a code." { | ||
kspProcessor.process(resolver) | ||
coVerify { codeResolver.getCodeString(*declarations.toTypedArray()) } | ||
} | ||
|
||
coVerify { bridge.getCode(any(), any()) } | ||
"should call bridge get code" { | ||
kspProcessor.process(resolver) | ||
coVerify { bridge.getCode("code context", any()) } | ||
} | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ include("bridge") | |
include("gradle-plugin") | ||
include("ksp-processor") | ||
include("utils") | ||
include("writer") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
group = "com.code.factory" | ||
version = "unspecified" | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(libs.kotlin.kspApi) | ||
|
||
testImplementation(kotlin("test")) | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} | ||
kotlin { | ||
jvmToolchain(21) | ||
} |
Oops, something went wrong.