Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/51 delegate list and map #53

Merged
merged 11 commits into from
Sep 11, 2024
41 changes: 41 additions & 0 deletions _itest/builder-itest/src/test/kotlin/DelegateStringListITest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.itest.KotlinCodeGenerationITestConfig.ROOT_PACKAGE
import io.toolisticon.kotlin.generation.spec.toFileSpec
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat as compileAssertThat

@Suppress("UNCHECKED_CAST")
@OptIn(ExperimentalKotlinPoetApi::class, ExperimentalCompilerApi::class)
internal class DelegateStringListITest {

@Test
fun `create and use string list`() {
val list = KotlinCodeGeneration.buildDelegateListValueClass(ROOT_PACKAGE, "StringList", String::class) {
propertyName("list")
}.toFileSpec()

val result = KotlinCodeGenerationTest.compile(KotlinCompilationCommand(list))


compileAssertThat(result).errorMessages().isEmpty()
compileAssertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)

val klass: KClass<out Any> = result.loadClass(list.className)

val values = listOf("a", "b", "c")

val instance: List<String> = klass.primaryConstructor!!.call(values) as List<String>

assertThat(instance).hasToString("StringList(list=[a, b, c])")
}
}
44 changes: 44 additions & 0 deletions _itest/builder-itest/src/test/kotlin/DelegateStringLongMapITest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.asTypeName
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.itest.KotlinCodeGenerationITestConfig.ROOT_PACKAGE
import io.toolisticon.kotlin.generation.spec.toFileSpec
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat as compileAssertThat

@Suppress("UNCHECKED_CAST")
@OptIn(ExperimentalKotlinPoetApi::class, ExperimentalCompilerApi::class)
internal class DelegateStringLongMapITest {

@Test
fun `create and use string long map`() {
val map = KotlinCodeGeneration.buildDelegateMapValueClass(
packageName = ROOT_PACKAGE,
simpleName = "StringLongMap",
valueType = Long::class.asTypeName()
) {
propertyName("map")
}.toFileSpec()

val result = KotlinCodeGenerationTest.compile(KotlinCompilationCommand(map))
compileAssertThat(result).errorMessages().isEmpty()
compileAssertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)

val klass: KClass<out Any> = result.loadClass(map.className)

val values = mapOf("a" to 1, "b" to 2, "c" to 3)

val instance: Map<String, Long> = klass.primaryConstructor!!.call(values) as Map<String, Long>

assertThat(instance).hasToString("StringLongMap(map={a=1, b=2, c=3})")
}
}
52 changes: 52 additions & 0 deletions _itest/builder-itest/src/test/kotlin/DummyExceptionITest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildRuntimeExceptionClass
import io.toolisticon.kotlin.generation.itest.KotlinCodeGenerationITestConfig.ROOT_PACKAGE
import io.toolisticon.kotlin.generation.spec.toFileSpec
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat as compileAssertThat

@OptIn(ExperimentalKotlinPoetApi::class, ExperimentalCompilerApi::class)
internal class DummyExceptionITest {

@Test
fun `generate and create dummy exception`() {
val exceptionFile = buildRuntimeExceptionClass(ROOT_PACKAGE, "DummyException") {
messageTemplate("Dummy exception: expected: \$expected, actual: '\$actual'.")
addConstructorProperty("expected", Boolean::class)
addParameter("actual", String::class)
includeCause()
}.toFileSpec()


val result = KotlinCodeGenerationTest.compile(KotlinCompilationCommand(exceptionFile))

compileAssertThat(result).errorMessages().isEmpty()
compileAssertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)

val c: KClass<out Any> = result.loadClass(exceptionFile.className)

val cause = IllegalStateException("foo")
val e: RuntimeException = c.primaryConstructor!!.call(true, "false", cause) as RuntimeException

assertThat(e.localizedMessage).isEqualTo("Dummy exception: expected: true, actual: 'false'.")

// TODO try to get value via pure kotlin without falling back to java
val expectedProperty: KProperty1<out Any, *> = c.memberProperties.single { it.name == "expected" }
val field = c.java.getDeclaredField("expected").apply { isAccessible = true }

val expectedValue = field.get(e) as Boolean

assertThat(expectedValue).isTrue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ internal class HelloWorldExampleITest {
val file = buildFile(name) {
addType(type)
}
println(file.code)

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

Expand Down
Loading