@@ -4,6 +4,7 @@ import com.mapk.fastkfunction.SingleArgFastKFunction.Companion.checkParameters
4
4
import io.mockk.every
5
5
import io.mockk.mockk
6
6
import org.junit.jupiter.api.Assertions.assertEquals
7
+ import org.junit.jupiter.api.Assertions.assertTrue
7
8
import org.junit.jupiter.api.Nested
8
9
import org.junit.jupiter.api.Test
9
10
import org.junit.jupiter.api.TestInstance
@@ -13,7 +14,12 @@ import org.junit.jupiter.params.ParameterizedTest
13
14
import org.junit.jupiter.params.provider.Arguments
14
15
import org.junit.jupiter.params.provider.MethodSource
15
16
import java.util.stream.Stream
17
+ import kotlin.reflect.KFunction
16
18
import kotlin.reflect.KParameter
19
+ import kotlin.reflect.jvm.javaMethod
20
+
21
+ private fun topLevelFunc (arg : String ) = println (arg)
22
+ private fun String.topLevelExtensionFunc (arg : String ) = println (this + arg)
17
23
18
24
private class SingleArgFastKFunctionTest {
19
25
@Nested
@@ -69,4 +75,64 @@ private class SingleArgFastKFunctionTest {
69
75
Arguments .of(listOf (instanceParameter, valueParameter))
70
76
)
71
77
}
78
+
79
+ @Nested
80
+ inner class TopLevelFunctionOfTest {
81
+ @Nested
82
+ inner class KindIsExtensionFunction {
83
+ val function: KFunction <Unit > = String ::topLevelExtensionFunc
84
+ val parameters = function.parameters
85
+ val javaMethod = function.javaMethod!!
86
+
87
+ @Test
88
+ fun nullInstanceTest () {
89
+ assertThrows<IllegalArgumentException > {
90
+ SingleArgFastKFunction .topLevelFunctionOf(function, null , parameters, javaMethod)
91
+ }
92
+ }
93
+
94
+ @Test
95
+ fun isCorrect () {
96
+ val result = assertDoesNotThrow {
97
+ SingleArgFastKFunction .topLevelFunctionOf(function, " " , parameters, javaMethod)
98
+ }
99
+ assertTrue(result is SingleArgFastKFunction .TopLevelExtensionFunction )
100
+ }
101
+ }
102
+
103
+ @Nested
104
+ inner class ExtensionFunction {
105
+ val instance = " "
106
+ val function: KFunction <Unit > = instance::topLevelExtensionFunc
107
+ val parameters = function.parameters
108
+ val javaMethod = function.javaMethod!!
109
+
110
+ @Test
111
+ fun withInstanceTest () {
112
+ val result = assertDoesNotThrow {
113
+ SingleArgFastKFunction .topLevelFunctionOf(function, " " , parameters, javaMethod)
114
+ }
115
+ assertTrue(result is SingleArgFastKFunction .TopLevelExtensionFunction )
116
+ }
117
+
118
+ @Test
119
+ fun withoutInstanceTest () {
120
+ val result = assertDoesNotThrow {
121
+ SingleArgFastKFunction .topLevelFunctionOf(function, null , parameters, javaMethod)
122
+ }
123
+ assertTrue(result is SingleArgFastKFunction .Function )
124
+ }
125
+ }
126
+
127
+ @Test
128
+ fun topLevelFunctionTest () {
129
+ val function: KFunction <Unit > = ::topLevelFunc
130
+ val parameters = function.parameters
131
+
132
+ val result = assertDoesNotThrow {
133
+ SingleArgFastKFunction .topLevelFunctionOf(function, null , parameters, function.javaMethod!! )
134
+ }
135
+ assertTrue(result is SingleArgFastKFunction .TopLevelFunction )
136
+ }
137
+ }
72
138
}
0 commit comments