Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Jul 3, 2024
1 parent 3235a87 commit cd16032
Show file tree
Hide file tree
Showing 36 changed files with 241 additions and 111 deletions.
4 changes: 2 additions & 2 deletions _itest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-root</artifactId>
<artifactId>kotlin-code-generation-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../_mvn/parent/pom.xml</relativePath>
</parent>

<groupId>io.toolisticon.kotlin.generation.itest</groupId>
<artifactId>kotlin-code-generation-itest</artifactId>
<name>test: ${project.artifactId}</name>
Expand Down
2 changes: 1 addition & 1 deletion _itest/src/main/kotlin/KotlinCodeGenerationITest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.toolisticon.kotlin.generation.itest

object KotlinCodeGenerationITest {
// empty
const val ROOT_PACKAGE = "io.toolisticon.kotlin.generation.itest.created"
}
21 changes: 21 additions & 0 deletions _itest/src/main/kotlin/MyCustomAnnotationSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotationClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.itest.KotlinCodeGenerationITest.ROOT_PACKAGE

object MyCustomAnnotationSpec {
val name = ClassName(ROOT_PACKAGE, "MyCustomAnnotation")

val spec = buildAnnotationClass(name) {
mustBeDocumented()
retention(AnnotationRetention.RUNTIME)
target(AnnotationTarget.CLASS)
addConstructorProperty("value", String::class)
}

val file = buildFile(name) {
addType(spec)
}
}
44 changes: 19 additions & 25 deletions _itest/src/test/kotlin/HelloWorldExampleTest.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
@file:OptIn(ExperimentalCompilerApi::class)

package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.TypeSpec
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.builder.KotlinFileSpecBuilder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFun
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

internal class HelloWorldExampleTest {
Expand All @@ -20,30 +16,28 @@ internal class HelloWorldExampleTest {
fun `hello world from kotlin poet talk`() {
val name = ClassName("foo.bar", "HelloWorld")

val type = TypeSpec.classBuilder(name)
.addKdoc("%L", "Simple hello world class")
.addFunction(
FunSpec.builder("helloWorld")
.returns(String::class)
.addCode("return %S", "Hello World!")
.build()
)
.build()

val file = KotlinFileSpecBuilder.builder(name).invoke {
val type = buildClass(name) {
addFunction(
buildFun("helloWorld") {
builder {
returns(String::class)
addCode("return %S", "Hello World!")
}
})
addKdoc("%L", "Simple hello world class")
}

val file = buildFile(name) {
addType(type)
}.build()

}
println(file.code)

Assertions.assertThat(file.packageName).isEqualTo("foo.bar")
assertThat(file.packageName).isEqualTo("foo.bar")

val result = KotlinCodeGenerationTest.compile(cmd = KotlinCompilationCommand(fileSpec = file))

println(result)

assertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)


KotlinCodeGenerationTest.assertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)
}
}
14 changes: 5 additions & 9 deletions _itest/src/test/kotlin/KotlinDataClassSpecTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ package io.toolisticon.kotlin.generation.itest
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.asTypeName
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildDataClass
import io.toolisticon.kotlin.generation.builder.KotlinConstructorPropertyBuilder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.constructorPropertyBuilder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.dataClassBuilder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.dataClassBuilder
import io.toolisticon.kotlin.generation.builder.KotlinDataClassSpecBuilder
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test

@OptIn(ExperimentalCompilerApi::class)
internal class KotlinDataClassSpecTest {

@Test
fun `create simple data class`() {
val className = ClassName("foo.bar", "Bar")
val builder: KotlinDataClassSpecBuilder = dataClassBuilder(className)
.addConstructorProperty(constructorPropertyBuilder("name", String::class))
.addConstructorProperty(constructorPropertyBuilder("age", Int::class))
.addConstructorProperty("name", String::class)
.addConstructorProperty("age", Int::class)

val spec = buildDataClass(className) {
addConstructorProperty(KotlinConstructorPropertyBuilder.builder("name", String::class.asTypeName()))
addConstructorProperty(KotlinConstructorPropertyBuilder.builder("age", Int::class.asTypeName()))
addConstructorProperty("name", String::class.asTypeName())
addConstructorProperty("age", Int::class.asTypeName())
}

// val file = spec.toFileSpec()
Expand Down
5 changes: 2 additions & 3 deletions _itest/src/test/kotlin/KotlinFileSpecTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ internal class KotlinFileSpecTest {

@Test
fun `build filespec`() {
val builder: KotlinFileSpecBuilder = KotlinFileSpecBuilder.builder(ClassName("foo", "Bar")).invoke {
val builder: KotlinFileSpecBuilder = KotlinFileSpecBuilder.builder(ClassName("foo", "Bar")).builder {
addFileComment("%L", "this is a comment.")
}

val b = builder {
val b = builder.builder {
addAnnotation(Foo::class)
}

val spec = b.build()

println(spec.code)
println(spec)
}
}
46 changes: 46 additions & 0 deletions _itest/src/test/kotlin/MyCustomAnnotationSpecTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat as assertThatCompilation


internal class MyCustomAnnotationSpecTest {

@Test
fun `generate class with custom annotation`() {
val name = ClassName(KotlinCodeGenerationITest.ROOT_PACKAGE, "MyClass")

val customAnnotation = buildAnnotation(MyCustomAnnotationSpec.name) {
addStringMember("value", "hello")
}

val myClass = KotlinCodeGeneration.buildClass(name) {
addAnnotation(customAnnotation)
}

val file = buildFile(name) {
addType(myClass)
}

println(myClass.code)

val result = KotlinCodeGenerationTest.compile(KotlinCompilationCommand(MyCustomAnnotationSpec.file).plus(file))

assertThatCompilation(result).hasExitCode(KotlinCompilation.ExitCode.OK);

val klass: KClass<out Any> = result.loadClass(name)
assertThat(klass.annotations).hasSize(1)
val annotation: Annotation = klass.annotations[0]

//assertThat(annotation::class.)
}
}
8 changes: 6 additions & 2 deletions _mvn/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<properties>
<kotlin-poet.version>1.17.0</kotlin-poet.version>
<kotlin-compile-testing.version>0.5.0-alpha08</kotlin-compile-testing.version>

<!-- TEST -->
<assertj.version>3.26.0</assertj.version>
<logback.version>1.5.6</logback.version>
</properties>

<dependencyManagement>
Expand All @@ -36,13 +40,13 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.0</version>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.6</version>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
13 changes: 0 additions & 13 deletions kotlin-code-generation-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup</groupId>
<artifactId>kotlinpoet-jvm</artifactId>
<version>${kotlin-poet.version}</version>
</dependency>

<dependency>
<groupId>dev.zacsweers.kctfork</groupId>
<artifactId>core</artifactId>
<scope>test</scope>
<version>${kotlin-compile-testing.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
15 changes: 2 additions & 13 deletions kotlin-code-generation-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,15 @@

<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-root</artifactId>
<artifactId>kotlin-code-generation-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../_mvn/parent/pom.xml</relativePath>
</parent>

<groupId>io.toolisticon.kotlin.generation</groupId>
<artifactId>kotlin-code-generation-test</artifactId>
<description>Tools for testing generated classes. This lib exposes testing libraries and is supposed to be included with `scope=test`</description>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.toolisticon.kotlin.generation</groupId>
<artifactId>kotlin-code-generation-bom</artifactId>
<version>${project.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- KOTLIN -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package io.toolisticon.kotlin.generation.test

import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import io.toolisticon.kotlin.generation.spec.KotlinFileSpec
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationResult
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import java.io.ByteArrayOutputStream

/**
Expand All @@ -28,7 +29,6 @@ import java.io.ByteArrayOutputStream
* }.compile()
* ```
*/
@OptIn(ExperimentalCompilerApi::class)
object KotlinCodeGenerationTest {

@JvmStatic
Expand All @@ -38,7 +38,7 @@ object KotlinCodeGenerationTest {
fun compile(cmd: KotlinCompilationCommand): KotlinCompilationResult {

val result: JvmCompilationResult = KotlinCompilation().apply {
sources = listOf(cmd.sourceFile)
sources = cmd.sourceFiles
inheritClassPath = true

this.
Expand All @@ -50,5 +50,5 @@ object KotlinCodeGenerationTest {
return KotlinCompilationResult(cmd = cmd, result = result)
}


fun KotlinFileSpec.sourceFile() = SourceFile.kotlin(name = this.fileName, contents = this.code)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationResult
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.Assertions
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi

@ExperimentalCompilerApi
class KotlinCompilationAssert(
actual: KotlinCompilationResult,
) : AbstractAssert<KotlinCompilationAssert, KotlinCompilationResult>(actual, KotlinCompilationAssert::class.java) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package io.toolisticon.kotlin.generation.test.model

import com.tschuchort.compiletesting.SourceFile
import io.toolisticon.kotlin.generation._BAK.KotlinFileSpec
import io.toolisticon.kotlin.generation.spec.KotlinFileSpec
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.sourceFile

data class KotlinCompilationCommand(
val fileSpec: KotlinFileSpec
val fileSpecs: List<KotlinFileSpec>
) {

val sourceFile = SourceFile.kotlin(name = fileSpec.fileName, contents = fileSpec.code)
constructor(fileSpec: KotlinFileSpec) : this(listOf(fileSpec))

operator fun plus(fileSpec: KotlinFileSpec) = copy(fileSpecs = fileSpecs + fileSpec)


val sourceFiles: List<SourceFile> by lazy { fileSpecs.map { it.sourceFile() } }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.toolisticon.kotlin.generation.test.model

import com.squareup.kotlinpoet.ClassName
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import kotlin.reflect.KClass
import kotlin.reflect.full.declaredMemberExtensionFunctions

@OptIn(ExperimentalCompilerApi::class)
data class KotlinCompilationResult(
val cmd: KotlinCompilationCommand,
val result: JvmCompilationResult
Expand All @@ -26,7 +24,7 @@ data class KotlinCompilationResult(
}
}

fun loadClass(): KClass<out Any> = result.classLoader.loadClass(cmd.fileSpec.fqn).kotlin
fun loadClass(className: ClassName): KClass<out Any> = result.classLoader.loadClass(className.canonicalName).kotlin

fun shouldBeOk() {
assertThat(exitCode)
Expand Down
Loading

0 comments on commit cd16032

Please sign in to comment.