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

Commit 30634c1

Browse files
committed
SingleArgSingleArgFastKFunctionのテスト追加
1 parent 7fe0309 commit 30634c1

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.mapk.fastkfunction.singleargfastkfunction
2+
3+
import com.mapk.fastkfunction.SingleArgFastKFunction
4+
import org.junit.jupiter.api.Assertions
5+
import org.junit.jupiter.api.TestInstance
6+
import org.junit.jupiter.api.assertDoesNotThrow
7+
import org.junit.jupiter.params.ParameterizedTest
8+
import org.junit.jupiter.params.provider.Arguments
9+
import org.junit.jupiter.params.provider.MethodSource
10+
import java.util.stream.Stream
11+
import kotlin.reflect.KFunction
12+
import kotlin.reflect.full.companionObject
13+
import kotlin.reflect.full.companionObjectInstance
14+
import kotlin.reflect.full.functions
15+
16+
private fun topLevelFunc(arg: Int): SingleArgSingleArgFastKFunctionTest.Dst = SingleArgSingleArgFastKFunctionTest.Dst(arg)
17+
private fun SingleArgSingleArgFastKFunctionTest.Class.topLevelExtensionFunc(arg: Int): SingleArgSingleArgFastKFunctionTest.Dst =
18+
SingleArgSingleArgFastKFunctionTest.Dst(this.arg + arg)
19+
20+
/**
21+
* 網羅しているパターン
22+
* - コンストラクタ
23+
* - インスタンスメソッド
24+
* - インスタンスメソッド + インスタンス
25+
* - コンパニオンオブジェクトに定義したメソッド
26+
* - コンパニオンオブジェクトに定義したメソッド + コンパニオンオブジェクトインスタンス
27+
* - リフレクションで取得したコンパニオンオブジェクトに定義したメソッド
28+
* - リフレクションで取得したコンパニオンオブジェクトに定義したメソッド + コンパニオンオブジェクトインスタンス
29+
* - トップレベル関数
30+
* - クラスから取得したトップレベル拡張関数 + レシーバインスタンス
31+
* - インスタンスから取得したトップレベル拡張関数
32+
* - インスタンスから取得したトップレベル拡張関数 + インスタンス
33+
*/
34+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
35+
private class SingleArgSingleArgFastKFunctionTest {
36+
class Class(val arg: Int)
37+
38+
data class Dst(val arg: Int) {
39+
companion object {
40+
fun of(arg: Int) = Dst(arg)
41+
}
42+
}
43+
44+
private fun instanceFunction(arg: Int) = Dst(arg)
45+
46+
@ParameterizedTest
47+
@MethodSource("argumentsProvider")
48+
fun call(target: KFunction<Dst>, instance: Any?, message: String) {
49+
val sut = SingleArgFastKFunction.of(target, instance)
50+
assertDoesNotThrow("Fail $message") {
51+
Assertions.assertEquals(Dst(100), sut.call(100), message)
52+
}
53+
}
54+
55+
fun argumentsProvider(): Stream<Arguments> {
56+
val companionRawFunc = Dst::class.companionObject!!.functions.first { it.name == "of" }
57+
58+
return listOf(
59+
Arguments.of(::Dst, null, "constructor"),
60+
Arguments.of(::instanceFunction, null, "instance func"),
61+
Arguments.of(::instanceFunction, this, "instance func with instance"),
62+
Arguments.of((Dst)::of, null, "companion object func"),
63+
Arguments.of((Dst)::of, Dst::class.companionObjectInstance, "companion object func with instance"),
64+
Arguments.of(companionRawFunc, null, "companion object func from reflection"),
65+
Arguments.of(
66+
companionRawFunc,
67+
Dst::class.companionObjectInstance,
68+
"companion object func from reflection with instance"
69+
),
70+
Arguments.of(::topLevelFunc, null, "top level func"),
71+
Arguments.of(Class::topLevelExtensionFunc, Class(0), "top level extension func from class"),
72+
Class(0).let {
73+
Arguments.of(it::topLevelExtensionFunc, null, "top level extension func from instance")
74+
},
75+
Class(0).let {
76+
Arguments.of(it::topLevelExtensionFunc, it, "top level extension func from instance with instance")
77+
}
78+
).stream()
79+
}
80+
}

0 commit comments

Comments
 (0)