Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 08b5947

Browse files
authoredJul 23, 2020
Merge pull request #41 from k163377/more_coveradge
Add test case and refactor.
2 parents 66ad13f + ceb75cc commit 08b5947

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed
 

‎src/main/kotlin/com/mapk/kmapper/BoundKMapper.kt

+5-8
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@ class BoundKMapper<S : Any, D : Any> private constructor(
3838

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mapk.kmapper
2+
3+
import org.junit.jupiter.api.DisplayName
4+
import org.junit.jupiter.api.Test
5+
import org.junit.jupiter.api.assertDoesNotThrow
6+
import org.junit.jupiter.api.assertThrows
7+
8+
@Suppress("UNUSED_VARIABLE")
9+
@DisplayName("BoundKMapperの初期化時にエラーを吐かせるテスト")
10+
internal class BoundKMapperInitTest {
11+
data class Dst1(val foo: Int, val bar: Short, val baz: Long)
12+
data class Dst2(val foo: Int, val bar: Short, val baz: Long = 0)
13+
14+
data class Src(val foo: Int, val bar: Short)
15+
16+
@Test
17+
@DisplayName("引数が足りない場合のエラーテスト")
18+
fun isError() {
19+
assertThrows<IllegalArgumentException> {
20+
val mapper: BoundKMapper<Src, Dst1> = BoundKMapper()
21+
}
22+
}
23+
24+
@Test
25+
@DisplayName("足りないのがオプショナルな引数の場合エラーにならないテスト")
26+
fun isCollect() {
27+
assertDoesNotThrow { val mapper: BoundKMapper<Src, Dst2> = BoundKMapper(::Dst2) }
28+
}
29+
}

‎src/test/kotlin/com/mapk/kmapper/ConversionTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class ConversionTest {
121121
@DisplayName("Numberソース")
122122
fun fromNumber(numbers: NumberSource) {
123123
numbers.values.forEach {
124-
val actual = BoundKMapper(::Dst, NumberSrc::class).map(NumberSrc(it))
124+
val actual = BoundKMapper<NumberSrc, Dst>().map(NumberSrc(it))
125125
assertEquals(0, BigDecimal.valueOf(it.toDouble()).compareTo(actual.number))
126126
}
127127
}
@@ -130,7 +130,7 @@ class ConversionTest {
130130
@ValueSource(strings = ["100", "2.0", "-500"])
131131
@DisplayName("Stringソース")
132132
fun fromString(str: String) {
133-
val actual = BoundKMapper(::Dst, StringSrc::class).map(StringSrc(str))
133+
val actual = BoundKMapper<StringSrc, Dst>().map(StringSrc(str))
134134
assertEquals(0, BigDecimal(str).compareTo(actual.number))
135135
}
136136
}

0 commit comments

Comments
 (0)
This repository has been archived.