|
| 1 | +@file:Suppress("unused") |
| 2 | + |
| 3 | +package mapk.core |
| 4 | + |
| 5 | +import com.wrongwrong.mapk.annotations.KConverter |
| 6 | +import com.wrongwrong.mapk.core.KMapper |
| 7 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 8 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 9 | +import org.junit.jupiter.api.DisplayName |
| 10 | +import org.junit.jupiter.api.Test |
| 11 | + |
| 12 | +private data class ConstructorConverterDst(val argument: ConstructorConverter) |
| 13 | +private data class ConstructorConverter @KConverter constructor(val arg: Number) |
| 14 | + |
| 15 | +private data class CompanionConverterDst(val argument: CompanionConverter) |
| 16 | +// NOTE: privateクラスのcompanion objectにアクセスする方法を見つけられなかった |
| 17 | +class CompanionConverter private constructor(val arg: String) { |
| 18 | + private companion object { |
| 19 | + @KConverter |
| 20 | + private fun converter(arg: String): CompanionConverter { |
| 21 | + return CompanionConverter(arg) |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +private data class StaticMethodConverterDst(val argument: StaticMethodConverter) |
| 27 | + |
| 28 | +@DisplayName("コンバータ有りでのマッピングテスト") |
| 29 | +class ConverterKMapperTest { |
| 30 | + @Test |
| 31 | + @DisplayName("コンストラクターでのコンバートテスト") |
| 32 | + fun constructorConverterTest() { |
| 33 | + val mapper = KMapper(ConstructorConverterDst::class) |
| 34 | + val result = mapper.map(mapOf("argument" to 1)) |
| 35 | + |
| 36 | + assertEquals(ConstructorConverter(1), result.argument) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + @DisplayName("コンパニオンオブジェクトに定義したコンバータでのコンバートテスト") |
| 41 | + fun companionConverterTest() { |
| 42 | + val mapper = KMapper(CompanionConverterDst::class) |
| 43 | + val result = mapper.map(mapOf("argument" to "arg")) |
| 44 | + |
| 45 | + assertEquals("arg", result.argument.arg) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @DisplayName("スタティックメソッドに定義したコンバータでのコンバートテスト") |
| 50 | + fun staticMethodConverterTest() { |
| 51 | + val mapper = KMapper(StaticMethodConverterDst::class) |
| 52 | + val result = mapper.map(mapOf("argument" to "1,2,3")) |
| 53 | + |
| 54 | + assertTrue(intArrayOf(1, 2, 3) contentEquals result.argument.arg) |
| 55 | + } |
| 56 | +} |
0 commit comments