From 045fcbdef2149700da7c17e58cce017054f858ee Mon Sep 17 00:00:00 2001 From: stefankoppier Date: Fri, 26 Jul 2024 13:09:32 +0200 Subject: [PATCH] Added nullable list tests --- .../lists/NonNullListToNullListTest.kt | 54 -------- .../NonNullableListToNullableListTest.kt | 126 ++++++++++++++++++ .../{lists => objects}/NullToNonNullTest.kt | 6 +- 3 files changed, 129 insertions(+), 57 deletions(-) delete mode 100644 compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NonNullListToNullListTest.kt create mode 100644 compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NonNullableListToNullableListTest.kt rename compiler-plugin/src/test/kotlin/tech/mappie/testing/{lists => objects}/NullToNonNullTest.kt (92%) diff --git a/compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NonNullListToNullListTest.kt b/compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NonNullListToNullListTest.kt deleted file mode 100644 index 287c5afe..00000000 --- a/compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NonNullListToNullListTest.kt +++ /dev/null @@ -1,54 +0,0 @@ -package tech.mappie.testing.lists - -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.io.TempDir -import tech.mappie.testing.compilation.KotlinCompilation -import tech.mappie.testing.compilation.KotlinCompilation.ExitCode -import tech.mappie.testing.compilation.SourceFile.Companion.kotlin -import tech.mappie.testing.loadObjectMappieClass -import java.io.File - -class NonNullListToNullListTest { - data class Input(val text: List, val int: Int) - data class InnerInput(val value: String) - data class Output(val text: List?, val int: Int) - data class InnerOutput(val value: String) - - @TempDir - lateinit var directory: File - - @Test - fun `map data classes with nested null list to non-null list using object InnerMapper without declaring mapping should succeed`() { - KotlinCompilation(directory).apply { - sources = buildList { - add( - kotlin("Test.kt", - """ - import tech.mappie.api.ObjectMappie - import tech.mappie.testing.lists.NonNullListToNullListTest.* - - class Mapper : ObjectMappie() - - object InnerMapper : ObjectMappie() - - object SecondInnerMapper : ObjectMappie() - """ - ) - ) - } - }.compile { - assertThat(exitCode).isEqualTo(ExitCode.OK) - assertThat(messages).isEmpty() - - val mapper = classLoader - .loadObjectMappieClass("Mapper") - .constructors - .first() - .call() - - assertThat(mapper.map(Input(listOf(InnerInput("first"), InnerInput("second")), 20))) - .isEqualTo(Output(listOf(InnerOutput("first"), InnerOutput("second")), 20)) - } - } -} \ No newline at end of file diff --git a/compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NonNullableListToNullableListTest.kt b/compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NonNullableListToNullableListTest.kt new file mode 100644 index 00000000..4e84fb64 --- /dev/null +++ b/compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NonNullableListToNullableListTest.kt @@ -0,0 +1,126 @@ +package tech.mappie.testing.lists + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import tech.mappie.testing.compilation.KotlinCompilation +import tech.mappie.testing.compilation.KotlinCompilation.ExitCode +import tech.mappie.testing.compilation.SourceFile.Companion.kotlin +import tech.mappie.testing.loadObjectMappieClass +import java.io.File + +class NonNullableListToNullableListTest { + data class Input(val text: List, val int: Int) + data class InnerInput(val value: String) + data class Output(val text: List?, val int: Int) + data class InnerOutput(val value: String) + + @TempDir + lateinit var directory: File + + @Test + fun `map nested non-nullable list to nullable list explicit should succeed`() { + KotlinCompilation(directory).apply { + sources = buildList { + add( + kotlin("Test.kt", + """ + import tech.mappie.api.ObjectMappie + import tech.mappie.testing.lists.NonNullableListToNullableListTest.* + + class Mapper : ObjectMappie() { + override fun map(from: Input) = mapping { + to::text fromProperty from::text via InnerMapper.forList + } + } + + object InnerMapper : ObjectMappie() + """ + ) + ) + } + }.compile { + assertThat(exitCode).isEqualTo(ExitCode.OK) + assertThat(messages).isEmpty() + + val mapper = classLoader + .loadObjectMappieClass("Mapper") + .constructors + .first() + .call() + + assertThat(mapper.map(Input(listOf(InnerInput("first"), InnerInput("second")), 20))) + .isEqualTo(Output(listOf(InnerOutput("first"), InnerOutput("second")), 20)) + } + } + + @Test + fun `map nested non-nullable list to nullable list explicit without via should succeed`() { + KotlinCompilation(directory).apply { + sources = buildList { + add( + kotlin("Test.kt", + """ + import tech.mappie.api.ObjectMappie + import tech.mappie.testing.lists.NonNullableListToNullableListTest.* + + class Mapper : ObjectMappie() { + override fun map(from: Input) = mapping { + to::text fromProperty from::text + } + } + + object InnerMapper : ObjectMappie() + """ + ) + ) + } + }.compile { + assertThat(exitCode).isEqualTo(ExitCode.OK) + assertThat(messages).isEmpty() + + val mapper = classLoader + .loadObjectMappieClass("Mapper") + .constructors + .first() + .call() + + assertThat(mapper.map(Input(listOf(InnerInput("first"), InnerInput("second")), 20))) + .isEqualTo(Output(listOf(InnerOutput("first"), InnerOutput("second")), 20)) + } + } + + @Test + fun `map nested non-nullable list to nullable list implicit should succeed`() { + KotlinCompilation(directory).apply { + sources = buildList { + add( + kotlin("Test.kt", + """ + import tech.mappie.api.ObjectMappie + import tech.mappie.testing.lists.NonNullableListToNullableListTest.* + + class Mapper : ObjectMappie() + + object InnerMapper : ObjectMappie() + + object SecondInnerMapper : ObjectMappie() + """ + ) + ) + } + }.compile { + assertThat(exitCode).isEqualTo(ExitCode.OK) + assertThat(messages).isEmpty() + + val mapper = classLoader + .loadObjectMappieClass("Mapper") + .constructors + .first() + .call() + + assertThat(mapper.map(Input(listOf(InnerInput("first"), InnerInput("second")), 20))) + .isEqualTo(Output(listOf(InnerOutput("first"), InnerOutput("second")), 20)) + } + } +} \ No newline at end of file diff --git a/compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NullToNonNullTest.kt b/compiler-plugin/src/test/kotlin/tech/mappie/testing/objects/NullToNonNullTest.kt similarity index 92% rename from compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NullToNonNullTest.kt rename to compiler-plugin/src/test/kotlin/tech/mappie/testing/objects/NullToNonNullTest.kt index ef439177..6a8ed7e4 100644 --- a/compiler-plugin/src/test/kotlin/tech/mappie/testing/lists/NullToNonNullTest.kt +++ b/compiler-plugin/src/test/kotlin/tech/mappie/testing/objects/NullToNonNullTest.kt @@ -1,4 +1,4 @@ -package tech.mappie.testing.lists +package tech.mappie.testing.objects import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test @@ -25,7 +25,7 @@ class NullToNonNullTest { kotlin("Test.kt", """ import tech.mappie.api.ObjectMappie - import tech.mappie.testing.lists.NullToNonNullTest.* + import tech.mappie.testing.objects.NullToNonNullTest.* class Mapper : ObjectMappie() """ @@ -47,7 +47,7 @@ class NullToNonNullTest { kotlin("Test.kt", """ import tech.mappie.api.ObjectMappie - import tech.mappie.testing.lists.NullToNonNullTest.* + import tech.mappie.testing.objects.NullToNonNullTest.* class Mapper : ObjectMappie() { override fun map(from: Input) = mapping {