Skip to content

Commit 00372ae

Browse files
committed
Added tests for Kotlin top-level functions
1 parent 81880d6 commit 00372ae

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/IdUtil.kt

+1
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ val Method.displayName: String
498498

499499
val KCallable<*>.declaringClazz: Class<*>
500500
get() = when (this) {
501+
is KFunction<*> -> javaMethod?.declaringClass?.kotlin
501502
is CallableReference -> owner as? KClass<*>
502503
else -> instanceParameter?.type?.classifier as? KClass<*>
503504
}?.java ?: tryConstructor(this) ?: error("Can't get parent class for $this")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.utbot.examples.codegen
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.testcheckers.eq
5+
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
6+
7+
internal class FileWithTopLevelFunctionsTest : UtValueTestCaseChecker(testClass = FileWithTopLevelFunctionsReflectHelper.clazz.kotlin) {
8+
@Test
9+
fun topLevelSumTest() {
10+
check(
11+
::topLevelSum,
12+
eq(1),
13+
)
14+
}
15+
16+
@Test
17+
fun extensionOnBasicTypeTest() {
18+
check(
19+
Int::extensionOnBasicType,
20+
eq(1),
21+
)
22+
}
23+
24+
@Test
25+
fun extensionOnCustomClassTest() {
26+
check(
27+
CustomClass::extensionOnCustomClass,
28+
eq(3),
29+
additionalDependencies = dependenciesForClassExtensions
30+
)
31+
}
32+
33+
companion object {
34+
// Compilation of extension methods for ref objects produces call to
35+
// `kotlin.jvm.internal.Intrinsics::checkNotNullParameter`, so we need to add it to dependencies
36+
val dependenciesForClassExtensions = arrayOf<Class<*>>(kotlin.jvm.internal.Intrinsics::class.java)
37+
}
38+
}

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/process/EngineProcess.kt

+10-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,16 @@ class EngineProcess(parent: Lifetime, val project: Project) {
238238
}
239239

240240
private fun MemberInfo.paramNames(): List<String> =
241-
(this.member as PsiMethod).parameterList.parameters.map { it.name }
241+
(this.member as PsiMethod).parameterList.parameters.map {
242+
if (it.name.startsWith("\$this"))
243+
// If member is Kotlin extension function, name of first argument isn't good for further usage,
244+
// so we better choose name based on type of receiver.
245+
//
246+
// There seems no API to check whether parameter is an extension receiver by PSI
247+
it.type.presentableText
248+
else
249+
it.name
250+
}
242251

243252
fun generate(
244253
mockInstalled: Boolean,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.utbot.examples.codegen;
2+
3+
// We can't access FileWithTopLevelFunctionsKt::class from Kotlin, so we use this class to get reflection from Java
4+
public class FileWithTopLevelFunctionsReflectHelper {
5+
static Class<FileWithTopLevelFunctionsKt> clazz = FileWithTopLevelFunctionsKt.class;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.utbot.examples.codegen
2+
3+
// TODO: currently we can't properly handle properties in constructors, change CustomClass to data class after that is fixed
4+
class CustomClass {
5+
var x: Int = 0
6+
var y: Int = 0
7+
8+
fun f(): Int {
9+
return 0
10+
}
11+
}
12+
13+
fun topLevelSum(a: Int, b: Int): Int {
14+
return a + b
15+
}
16+
17+
fun Int.extensionOnBasicType(other: Int): Int {
18+
return this + other
19+
}
20+
21+
fun CustomClass.extensionOnCustomClass(other: CustomClass): Boolean {
22+
return x >= other.x && y >= other.y
23+
}

0 commit comments

Comments
 (0)