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

Commit d24d369

Browse files
committed
テストを移植
1 parent 35f02af commit d24d369

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.mapk.core
2+
3+
import com.mapk.annotations.KConstructor
4+
import kotlin.reflect.KFunction
5+
import kotlin.reflect.full.memberProperties
6+
import kotlin.reflect.full.primaryConstructor
7+
import kotlin.reflect.jvm.isAccessible
8+
import org.junit.jupiter.api.Assertions
9+
import org.junit.jupiter.api.DisplayName
10+
import org.junit.jupiter.api.Test
11+
import org.junit.jupiter.api.assertThrows
12+
13+
@Suppress("UNCHECKED_CAST", "unused")
14+
@DisplayName("クラスからのコンストラクタ抽出関連テスト")
15+
class ToKConstructorTest {
16+
private class SecondaryConstructorDst(val argument: Int) {
17+
@KConstructor
18+
constructor(argument: Number) : this(argument.toInt())
19+
}
20+
21+
class CompanionFactoryDst(val argument: IntArray) {
22+
companion object {
23+
@KConstructor
24+
fun factory(csv: String): CompanionFactoryDst {
25+
return csv.split(",").map { it.toInt() }.toIntArray().let { CompanionFactoryDst(it) }
26+
}
27+
}
28+
}
29+
private class ConstructorDst(val argument: String)
30+
class MultipleConstructorDst @KConstructor constructor(val argument: Int) {
31+
@KConstructor
32+
constructor(argument: String) : this(argument.toInt())
33+
}
34+
35+
private fun <T : Any> KFunctionForCall<T>.getTargetFunction(): KFunction<T> {
36+
return this::class.memberProperties.first { it.name == "function" }.getter.let {
37+
it.isAccessible = true
38+
it.call(this) as KFunction<T>
39+
}
40+
}
41+
42+
@Test
43+
@DisplayName("セカンダリコンストラクタからの取得テスト")
44+
fun testGetFromSecondaryConstructor() {
45+
val function = SecondaryConstructorDst::class.toKConstructor().function
46+
Assertions.assertTrue(function.annotations.any { it is KConstructor })
47+
}
48+
49+
@Test
50+
@DisplayName("ファクトリーメソッドからの取得テスト")
51+
fun testGetFromFactoryMethod() {
52+
val function = CompanionFactoryDst::class.toKConstructor().function
53+
Assertions.assertTrue(function.annotations.any { it is KConstructor })
54+
}
55+
56+
@Test
57+
@DisplayName("無指定でプライマリコンストラクタからの取得テスト")
58+
fun testGetFromPrimaryConstructor() {
59+
val function = ConstructorDst::class.toKConstructor().function
60+
Assertions.assertEquals(ConstructorDst::class.primaryConstructor, function)
61+
}
62+
63+
@Test
64+
@DisplayName("対象を複数指定した場合のテスト")
65+
fun testMultipleDeclareConstructor() {
66+
assertThrows<IllegalArgumentException> { MultipleConstructorDst::class.toKConstructor() }
67+
}
68+
}

0 commit comments

Comments
 (0)