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

Commit

Permalink
Merge pull request #41 from k163377/more_coveradge
Browse files Browse the repository at this point in the history
Add test case and refactor.
  • Loading branch information
k163377 authored Jul 23, 2020
2 parents 66ad13f + ceb75cc commit 08b5947
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
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

0 comments on commit 08b5947

Please sign in to comment.