Skip to content

Commit 85795ac

Browse files
committed
Added tests for Kotlin top-level functions
1 parent c2fb676 commit 85795ac

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

Lines changed: 1 addition & 0 deletions
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")
Lines changed: 38 additions & 0 deletions
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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,16 @@ class EngineProcess(parent: Lifetime, val project: Project) {
236236
}
237237

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

241250
fun generate(
242251
mockInstalled: Boolean,
Lines changed: 6 additions & 0 deletions
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+
}
Lines changed: 23 additions & 0 deletions
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)