Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/main/kotlin/com/mapk/kmapper/BoundKMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,13 @@ class BoundKMapper<S : Any, D : Any> private constructor(

parameters = function.requiredParameters
.mapNotNull {
val temp = srcPropertiesMap[it.name]?.let { property ->
srcPropertiesMap[it.name]?.let { property ->
BoundParameterForMap.newInstance(it, property, parameterNameConverter)
}.apply {
// 必須引数に対応するプロパティがsrcに定義されていない場合エラー
if (this == null && !it.isOptional)
throw IllegalArgumentException("Property ${it.name} is not declared in ${src.jvmName}.")
}

// 必須引数に対応するプロパティがsrcに定義されていない場合エラー
if (temp == null && !it.isOptional) {
throw IllegalArgumentException("Property ${it.name} is not declared in ${src.jvmName}.")
}

temp
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/kotlin/com/mapk/kmapper/BoundKMapperInitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mapk.kmapper

import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows

@Suppress("UNUSED_VARIABLE")
@DisplayName("BoundKMapperの初期化時にエラーを吐かせるテスト")
internal class BoundKMapperInitTest {
data class Dst1(val foo: Int, val bar: Short, val baz: Long)
data class Dst2(val foo: Int, val bar: Short, val baz: Long = 0)

data class Src(val foo: Int, val bar: Short)

@Test
@DisplayName("引数が足りない場合のエラーテスト")
fun isError() {
assertThrows<IllegalArgumentException> {
val mapper: BoundKMapper<Src, Dst1> = BoundKMapper()
}
}

@Test
@DisplayName("足りないのがオプショナルな引数の場合エラーにならないテスト")
fun isCollect() {
assertDoesNotThrow { val mapper: BoundKMapper<Src, Dst2> = BoundKMapper(::Dst2) }
}
}
4 changes: 2 additions & 2 deletions src/test/kotlin/com/mapk/kmapper/ConversionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ConversionTest {
@DisplayName("Numberソース")
fun fromNumber(numbers: NumberSource) {
numbers.values.forEach {
val actual = BoundKMapper(::Dst, NumberSrc::class).map(NumberSrc(it))
val actual = BoundKMapper<NumberSrc, Dst>().map(NumberSrc(it))
assertEquals(0, BigDecimal.valueOf(it.toDouble()).compareTo(actual.number))
}
}
Expand All @@ -130,7 +130,7 @@ class ConversionTest {
@ValueSource(strings = ["100", "2.0", "-500"])
@DisplayName("Stringソース")
fun fromString(str: String) {
val actual = BoundKMapper(::Dst, StringSrc::class).map(StringSrc(str))
val actual = BoundKMapper<StringSrc, Dst>().map(StringSrc(str))
assertEquals(0, BigDecimal(str).compareTo(actual.number))
}
}
Expand Down