Skip to content

Commit

Permalink
Added nullable list tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier committed Jul 26, 2024
1 parent e9d8dea commit 045fcbd
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 57 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<InnerInput>, val int: Int)
data class InnerInput(val value: String)
data class Output(val text: List<InnerOutput>?, 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<Input, Output>() {
override fun map(from: Input) = mapping {
to::text fromProperty from::text via InnerMapper.forList
}
}
object InnerMapper : ObjectMappie<InnerInput, InnerOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val mapper = classLoader
.loadObjectMappieClass<Input, Output>("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<Input, Output>() {
override fun map(from: Input) = mapping {
to::text fromProperty from::text
}
}
object InnerMapper : ObjectMappie<InnerInput, InnerOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val mapper = classLoader
.loadObjectMappieClass<Input, Output>("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<Input, Output>()
object InnerMapper : ObjectMappie<InnerInput, InnerOutput>()
object SecondInnerMapper : ObjectMappie<InnerOutput, InnerInput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val mapper = classLoader
.loadObjectMappieClass<Input, Output>("Mapper")
.constructors
.first()
.call()

assertThat(mapper.map(Input(listOf(InnerInput("first"), InnerInput("second")), 20)))
.isEqualTo(Output(listOf(InnerOutput("first"), InnerOutput("second")), 20))
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Input, Output>()
"""
Expand All @@ -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<Input, Output>() {
override fun map(from: Input) = mapping {
Expand Down

0 comments on commit 045fcbd

Please sign in to comment.