Skip to content

Commit

Permalink
Added floating point mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier committed Jul 28, 2024
1 parent a5bccdc commit 0e2e789
Show file tree
Hide file tree
Showing 15 changed files with 613 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,24 @@ class BuiltinMappieDefinitionsCollector {
private val MAPPERS = listOf(
"LocalDateTimeToLocalTimeMapper",
"LocalDateTimeToLocalDateMapper",
"CharToStringMapper",
"LongToBigIntegerMapper",
"LongToBigDecimalMapper",
"IntToLongMapper",
"IntToBigIntegerMapper",
"IntToBigDecimalMapper",
"ShortToIntMapper",
"ShortToLongMapper",
"ShortToBigIntegerMapper",
"ShortToBigDecimalMapper",
"ByteToShortMapper",
"ByteToIntMapper",
"ByteToLongMapper",
"ByteToBigIntegerMapper",
"CharToStringMapper",
"ByteToBigDecimalMapper",
"FloatToDoubleMapper",
"FloatToBigDecimalMapper",
"DoubleToBigDecimalMapper",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tech.mappie.testing.compilation.KotlinCompilation.ExitCode
import tech.mappie.testing.compilation.SourceFile.Companion.kotlin
import tech.mappie.testing.loadObjectMappieClass
import java.io.File
import java.math.BigDecimal
import java.math.BigInteger

class ByteMappersTest {
Expand All @@ -25,6 +26,8 @@ class ByteMappersTest {

data class BigIntegerOutput(val value: BigInteger)

data class BigDecimalOutput(val value: BigDecimal)

@Test
fun `map Byte to Short implicit should succeed`() {
KotlinCompilation(directory).apply {
Expand Down Expand Up @@ -300,4 +303,73 @@ class ByteMappersTest {
.isEqualTo(BigIntegerOutput(BigInteger.valueOf(input.toLong())))
}
}

@Test
fun `map Byte to BigDecimal implicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.builtin.ByteMappersTest.*
class Mapper : ObjectMappie<ByteInput, BigDecimalOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input: Byte = 2

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

assertThat(mapper.map(ByteInput(input)))
.isEqualTo(BigDecimalOutput(BigDecimal.valueOf(input.toLong())))
}
}

@Test
fun `map Byte to BigDecimal explicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.api.builtin.*
import tech.mappie.testing.builtin.ByteMappersTest.*
class Mapper : ObjectMappie<ByteInput, BigDecimalOutput>() {
override fun map(from: ByteInput) = mapping {
to::value fromProperty from::value via ByteToBigDecimalMapper()
}
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input: Byte = 5

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

assertThat(mapper.map(ByteInput(input)))
.isEqualTo(BigDecimalOutput(BigDecimal.valueOf(input.toLong())))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package tech.mappie.testing.builtin

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
import java.math.BigDecimal

class DoubleMappersTest {

@TempDir
lateinit var directory: File

data class DoubleInput(val value: Double)

data class BigDecimalOutput(val value: BigDecimal)

@Test
fun `map Double to BigDecimal implicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.builtin.DoubleMappersTest.*
class Mapper : ObjectMappie<DoubleInput, BigDecimalOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = 2.0

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

assertThat(mapper.map(DoubleInput(input)))
.isEqualTo(BigDecimalOutput(input.toBigDecimal()))
}
}

@Test
fun `map Double to BigDecimal explicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.api.builtin.*
import tech.mappie.testing.builtin.DoubleMappersTest.*
class Mapper : ObjectMappie<DoubleInput, BigDecimalOutput>() {
override fun map(from: DoubleInput) = mapping {
to::value fromProperty from::value via DoubleToBigDecimalMapper()
}
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = 5.0

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

assertThat(mapper.map(DoubleInput(input)))
.isEqualTo(BigDecimalOutput(input.toBigDecimal()))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package tech.mappie.testing.builtin

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
import java.math.BigDecimal
import java.math.BigInteger

class FloatMappersTest {

@TempDir
lateinit var directory: File

data class FloatInput(val value: Float)

data class DoubleOutput(val value: Double)

data class BigDecimalOutput(val value: BigDecimal)

@Test
fun `map Float to Double implicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.builtin.FloatMappersTest.*
class Mapper : ObjectMappie<FloatInput, DoubleOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = 2.0f

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

assertThat(mapper.map(FloatInput(input)))
.isEqualTo(DoubleOutput(input.toDouble()))
}
}

@Test
fun `map Float to Double explicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.api.builtin.*
import tech.mappie.testing.builtin.FloatMappersTest.*
class Mapper : ObjectMappie<FloatInput, DoubleOutput>() {
override fun map(from: FloatInput) = mapping {
to::value fromProperty from::value via FloatToDoubleMapper()
}
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = 5.0f

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

assertThat(mapper.map(FloatInput(input)))
.isEqualTo(DoubleOutput(input.toDouble()))
}
}

@Test
fun `map Float to BigDecimal implicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.builtin.FloatMappersTest.*
class Mapper : ObjectMappie<FloatInput, BigDecimalOutput>()
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = 2.0f

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

assertThat(mapper.map(FloatInput(input)))
.isEqualTo(BigDecimalOutput(input.toBigDecimal()))
}
}

@Test
fun `map Float to BigDecimal explicit should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.api.builtin.*
import tech.mappie.testing.builtin.FloatMappersTest.*
class Mapper : ObjectMappie<FloatInput, BigDecimalOutput>() {
override fun map(from: FloatInput) = mapping {
to::value fromProperty from::value via FloatToBigDecimalMapper()
}
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

val input = 5.0f

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

assertThat(mapper.map(FloatInput(input)))
.isEqualTo(BigDecimalOutput(input.toBigDecimal()))
}
}
}
Loading

0 comments on commit 0e2e789

Please sign in to comment.