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

Commit 5aacd45

Browse files
committed
topLevelFunctionOfのテスト追加
1 parent 99b2f69 commit 5aacd45

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/test/kotlin/com/mapk/fastkfunction/FastKFunctionTest.kt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import io.mockk.every
55
import io.mockk.mockk
66
import org.junit.jupiter.api.*
77
import org.junit.jupiter.api.Assertions.assertEquals
8+
import org.junit.jupiter.api.Assertions.assertTrue
89
import org.junit.jupiter.params.ParameterizedTest
910
import org.junit.jupiter.params.provider.Arguments
1011
import org.junit.jupiter.params.provider.MethodSource
1112
import java.util.stream.Stream
13+
import kotlin.reflect.KFunction
1214
import kotlin.reflect.KParameter
15+
import kotlin.reflect.jvm.javaMethod
16+
17+
private fun topLevelFunc(arg: String) = println(arg)
18+
private fun String.topLevelExtensionFunc(arg: String) = println(this + arg)
1319

1420
private class FastKFunctionTest {
1521
val valueParameter = mockk<KParameter>() {
@@ -55,4 +61,64 @@ private class FastKFunctionTest {
5561
Arguments.of(listOf(instanceParameter, valueParameter))
5662
)
5763
}
64+
65+
@Nested
66+
inner class TopLevelFunctionOfTest {
67+
@Nested
68+
inner class KindIsExtensionFunction {
69+
val function: KFunction<Unit> = String::topLevelExtensionFunc
70+
val parameters = function.parameters
71+
val javaMethod = function.javaMethod!!
72+
73+
@Test
74+
fun nullInstanceTest() {
75+
assertThrows<IllegalArgumentException> {
76+
FastKFunction.topLevelFunctionOf(function, null, parameters, javaMethod)
77+
}
78+
}
79+
80+
@Test
81+
fun isCorrect() {
82+
val result = assertDoesNotThrow {
83+
FastKFunction.topLevelFunctionOf(function, "", parameters, javaMethod)
84+
}
85+
assertTrue(result is FastKFunction.TopLevelExtensionFunction)
86+
}
87+
}
88+
89+
@Nested
90+
inner class ExtensionFunction {
91+
val instance = ""
92+
val function: KFunction<Unit> = instance::topLevelExtensionFunc
93+
val parameters = function.parameters
94+
val javaMethod = function.javaMethod!!
95+
96+
@Test
97+
fun withInstanceTest() {
98+
val result = assertDoesNotThrow {
99+
FastKFunction.topLevelFunctionOf(function, "", parameters, javaMethod)
100+
}
101+
assertTrue(result is FastKFunction.TopLevelExtensionFunction)
102+
}
103+
104+
@Test
105+
fun withoutInstanceTest() {
106+
val result = assertDoesNotThrow {
107+
FastKFunction.topLevelFunctionOf(function, null, parameters, javaMethod)
108+
}
109+
assertTrue(result is FastKFunction.Function)
110+
}
111+
}
112+
113+
@Test
114+
fun topLevelFunctionTest() {
115+
val function: KFunction<Unit> = ::topLevelFunc
116+
val parameters = function.parameters
117+
118+
val result = assertDoesNotThrow {
119+
FastKFunction.topLevelFunctionOf(function, null, parameters, function.javaMethod!!)
120+
}
121+
assertTrue(result is FastKFunction.TopLevelFunction)
122+
}
123+
}
58124
}

0 commit comments

Comments
 (0)