-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/51 delegate list and map (#53)
Introduce opinionated builders for Exception, Delegate-List and Delegate-Map. refactor and simplify
- Loading branch information
1 parent
6d71610
commit 4b5ee13
Showing
34 changed files
with
973 additions
and
261 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
_itest/builder-itest/src/test/kotlin/DelegateStringListITest.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,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
44
_itest/builder-itest/src/test/kotlin/DelegateStringLongMapITest.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,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
52
_itest/builder-itest/src/test/kotlin/DummyExceptionITest.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,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() | ||
} | ||
} |
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
Oops, something went wrong.