Skip to content

Commit 8534e67

Browse files
committed
Fixed non-matching with filename test name for nested classes in contest estimator
1 parent 963bca1 commit 8534e67

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -865,11 +865,11 @@ open class ClassId @JvmOverloads constructor(
865865
* It is needed because [simpleName] for inner classes does not
866866
* take into account enclosing classes' names.
867867
*/
868-
open val simpleNameWithEnclosings: String
868+
open val simpleNameWithEnclosingClasses: String
869869
get() {
870870
val clazz = jClass
871871
return if (clazz.isMemberClass) {
872-
"${clazz.enclosingClass.id.simpleNameWithEnclosings}.$simpleName"
872+
"${clazz.enclosingClass.id.simpleNameWithEnclosingClasses}.$simpleName"
873873
} else {
874874
simpleName
875875
}
@@ -910,7 +910,7 @@ class BuiltinClassId(
910910
// set name manually only if it differs from canonical (e.g. for nested classes)
911911
name: String = canonicalName,
912912
// by default, we assume that the class is not a member class
913-
override val simpleNameWithEnclosings: String = simpleName,
913+
override val simpleNameWithEnclosingClasses: String = simpleName,
914914
override val isNullable: Boolean = false,
915915
isPublic: Boolean = true,
916916
isProtected: Boolean = false,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ val ClassId.isKotlinFile: Boolean
206206
KotlinClassHeader.Kind.getById(it.kind) == KotlinClassHeader.Kind.FILE_FACADE
207207
} ?: false
208208

209+
/**
210+
* Returns [ClassId.simpleNameWithEnclosingClasses] with '.' replaced with '_' - to use it as a class name.
211+
*/
212+
val ClassId.nameWithEnclosingClassesAsContigousString: String
213+
get() = simpleNameWithEnclosingClasses.replace('.', '_')
214+
209215
val voidClassId = ClassId("void")
210216
val booleanClassId = ClassId("boolean")
211217
val byteClassId = ClassId("byte")

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgAbstractRenderer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ abstract class CgAbstractRenderer(
848848
if (!context.shouldOptimizeImports) return canonicalName
849849

850850
// use simpleNameWithEnclosings instead of simpleName to consider nested classes case
851-
return if (this.isAccessibleBySimpleName()) simpleNameWithEnclosings else canonicalName
851+
return if (this.isAccessibleBySimpleName()) simpleNameWithEnclosingClasses else canonicalName
852852
}
853853

854854
private fun renderClassPackage(element: CgClass) {

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/ConstructorUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ internal fun ClassId.utilMethodId(
406406
): MethodId =
407407
BuiltinMethodId(this, name, returnType, arguments.toList(), isStatic = isStatic)
408408

409-
fun ClassId.toImport(): RegularImport = RegularImport(packageName, simpleNameWithEnclosings)
409+
fun ClassId.toImport(): RegularImport = RegularImport(packageName, simpleNameWithEnclosingClasses)
410410

411411
// Immutable collections utils
412412

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.utbot.framework.plugin.api.utils
22

33
import org.utbot.framework.plugin.api.ClassId
4+
import org.utbot.framework.plugin.api.util.nameWithEnclosingClassesAsContigousString
45

56
fun testClassNameGenerator(
67
testClassCustomName: String?,
78
testClassPackageName: String,
89
classUnderTest: ClassId
910
): Pair<String, String> {
1011
val packagePrefix = if (testClassPackageName.isNotEmpty()) "$testClassPackageName." else ""
11-
val simpleName = testClassCustomName ?: "${classUnderTest.simpleName}Test"
12+
val simpleName = testClassCustomName ?: "${classUnderTest.nameWithEnclosingClassesAsContigousString}Test"
1213
val name = "$packagePrefix$simpleName"
1314
return Pair(name, simpleName)
1415
}

utbot-junit-contest/src/main/kotlin/org/utbot/contest/ClassUnderTest.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.utbot.contest
22

33
import org.utbot.common.FileUtil
44
import org.utbot.framework.plugin.api.ClassId
5+
import org.utbot.framework.plugin.api.util.nameWithEnclosingClassesAsContigousString
56
import org.utbot.framework.plugin.api.util.utContext
67
import java.io.File
78
import java.nio.file.Paths
@@ -32,11 +33,12 @@ class ClassUnderTest(
3233
// val classpathDir : File get() = FileUtil.locateClassPath(kotlinClass)?.absoluteFile !!
3334

3435

35-
val packageName: String get() = fqn.substringBeforeLast('.', "")
36-
val simpleName: String get() = createTestClassName(fqn)
37-
36+
/**
37+
* These properties should be obtained only with utContext set
38+
*/
39+
private val packageName: String get() = classId.packageName
40+
val simpleName: String get() = classId.nameWithEnclosingClassesAsContigousString
3841
val testClassSimpleName: String get() = simpleName + "Test"
39-
4042
val generatedTestFile: File
4143
get() = Paths.get(
4244
generatedTestsSourcesDir.canonicalPath,
@@ -51,13 +53,4 @@ class ClassUnderTest(
5153
"\n generatedTestsSourcesDir: $generatedTestsSourcesDir" +
5254
"\n]"
5355
}
54-
55-
/**
56-
* Creates a name of test class.
57-
* We need the name in code and the name of test class file be similar.
58-
* On this way we need to avoid symbols like '$'.
59-
*/
60-
private fun createTestClassName(name: String): String = name
61-
.substringAfterLast('.')
62-
.replace('\$', '_')
6356
}

utbot-junit-contest/src/main/kotlin/org/utbot/contest/ContestEstimator.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.contest
22

3+
import kotlinx.coroutines.ObsoleteCoroutinesApi
34
import java.io.File
45
import java.io.FileInputStream
56
import java.net.URLClassLoader
@@ -126,6 +127,7 @@ object Paths {
126127
@Suppress("unused")
127128
enum class Tool {
128129
UtBot {
130+
@OptIn(ObsoleteCoroutinesApi::class)
129131
@Suppress("EXPERIMENTAL_API_USAGE")
130132
override fun run(
131133
project: ProjectToEstimate,
@@ -136,19 +138,17 @@ enum class Tool {
136138
statsForProject: StatsForProject,
137139
compiledTestDir: File,
138140
classFqn: String
139-
) {
141+
) = withUtContext(ContextManager.createNewContext(project.classloader)) {
140142
val classStats: StatsForClass = try {
141-
withUtContext(ContextManager.createNewContext(project.classloader)) {
142-
runGeneration(
143-
project.name,
144-
cut,
145-
timeLimit,
146-
fuzzingRatio,
147-
project.sootClasspathString,
148-
runFromEstimator = true,
149-
methodNameFilter
150-
)
151-
}
143+
runGeneration(
144+
project.name,
145+
cut,
146+
timeLimit,
147+
fuzzingRatio,
148+
project.sootClasspathString,
149+
runFromEstimator = true,
150+
methodNameFilter
151+
)
152152
} catch (e: CancellationException) {
153153
logger.info { "[$classFqn] finished with CancellationException" }
154154
return

0 commit comments

Comments
 (0)