From ebb0cbccf88fdb581123b824f6bcbfb67c240260 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Thu, 23 Jul 2020 17:32:03 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B1?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mapk/kmapper/BoundKMapperInitTest.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/test/kotlin/com/mapk/kmapper/BoundKMapperInitTest.kt diff --git a/src/test/kotlin/com/mapk/kmapper/BoundKMapperInitTest.kt b/src/test/kotlin/com/mapk/kmapper/BoundKMapperInitTest.kt new file mode 100644 index 0000000..94ed395 --- /dev/null +++ b/src/test/kotlin/com/mapk/kmapper/BoundKMapperInitTest.kt @@ -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 { + val mapper: BoundKMapper = BoundKMapper() + } + } + + @Test + @DisplayName("足りないのがオプショナルな引数の場合エラーにならないテスト") + fun isCollect() { + assertDoesNotThrow { val mapper: BoundKMapper = BoundKMapper(::Dst2) } + } +} From e8a6f7c5779b73f0c690ff7ffdfd2318a8596420 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Thu, 23 Jul 2020 17:35:42 +0900 Subject: [PATCH 2/3] =?UTF-8?q?BoundKMapper=E3=81=AE=E3=83=80=E3=83=9F?= =?UTF-8?q?=E3=83=BC=E3=82=B3=E3=83=B3=E3=82=B9=E3=83=88=E3=83=A9=E3=82=AF?= =?UTF-8?q?=E3=82=BF=E5=88=A9=E7=94=A8=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/com/mapk/kmapper/ConversionTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt index 817bb1a..bd717be 100644 --- a/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt +++ b/src/test/kotlin/com/mapk/kmapper/ConversionTest.kt @@ -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().map(NumberSrc(it)) assertEquals(0, BigDecimal.valueOf(it.toDouble()).compareTo(actual.number)) } } @@ -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().map(StringSrc(str)) assertEquals(0, BigDecimal(str).compareTo(actual.number)) } } From ceb75cc02a7e483e61d982dd688f0697a989e201 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Thu, 23 Jul 2020 17:37:53 +0900 Subject: [PATCH 3/3] =?UTF-8?q?apply=E3=81=A7=E7=B0=A1=E7=95=A5=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/kmapper/BoundKMapper.kt | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/mapk/kmapper/BoundKMapper.kt b/src/main/kotlin/com/mapk/kmapper/BoundKMapper.kt index 54f7890..fac4864 100644 --- a/src/main/kotlin/com/mapk/kmapper/BoundKMapper.kt +++ b/src/main/kotlin/com/mapk/kmapper/BoundKMapper.kt @@ -38,16 +38,13 @@ class BoundKMapper 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 } }