Skip to content

Commit

Permalink
Added tests for Kotlin top-level functions
Browse files Browse the repository at this point in the history
  • Loading branch information
volivan239 committed Oct 25, 2022
1 parent 81880d6 commit 00372ae
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ val Method.displayName: String

val KCallable<*>.declaringClazz: Class<*>
get() = when (this) {
is KFunction<*> -> javaMethod?.declaringClass?.kotlin
is CallableReference -> owner as? KClass<*>
else -> instanceParameter?.type?.classifier as? KClass<*>
}?.java ?: tryConstructor(this) ?: error("Can't get parent class for $this")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.utbot.examples.codegen

import org.junit.jupiter.api.Test
import org.utbot.testcheckers.eq
import org.utbot.tests.infrastructure.UtValueTestCaseChecker

internal class FileWithTopLevelFunctionsTest : UtValueTestCaseChecker(testClass = FileWithTopLevelFunctionsReflectHelper.clazz.kotlin) {
@Test
fun topLevelSumTest() {
check(
::topLevelSum,
eq(1),
)
}

@Test
fun extensionOnBasicTypeTest() {
check(
Int::extensionOnBasicType,
eq(1),
)
}

@Test
fun extensionOnCustomClassTest() {
check(
CustomClass::extensionOnCustomClass,
eq(3),
additionalDependencies = dependenciesForClassExtensions
)
}

companion object {
// Compilation of extension methods for ref objects produces call to
// `kotlin.jvm.internal.Intrinsics::checkNotNullParameter`, so we need to add it to dependencies
val dependenciesForClassExtensions = arrayOf<Class<*>>(kotlin.jvm.internal.Intrinsics::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,16 @@ class EngineProcess(parent: Lifetime, val project: Project) {
}

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

fun generate(
mockInstalled: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.utbot.examples.codegen;

// We can't access FileWithTopLevelFunctionsKt::class from Kotlin, so we use this class to get reflection from Java
public class FileWithTopLevelFunctionsReflectHelper {
static Class<FileWithTopLevelFunctionsKt> clazz = FileWithTopLevelFunctionsKt.class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.utbot.examples.codegen

// TODO: currently we can't properly handle properties in constructors, change CustomClass to data class after that is fixed
class CustomClass {
var x: Int = 0
var y: Int = 0

fun f(): Int {
return 0
}
}

fun topLevelSum(a: Int, b: Int): Int {
return a + b
}

fun Int.extensionOnBasicType(other: Int): Int {
return this + other
}

fun CustomClass.extensionOnCustomClass(other: CustomClass): Boolean {
return x >= other.x && y >= other.y
}

0 comments on commit 00372ae

Please sign in to comment.