@@ -52,6 +52,7 @@ import kotlinx.collections.immutable.persistentListOf
5252import kotlinx.collections.immutable.persistentMapOf
5353import kotlinx.collections.immutable.persistentSetOf
5454import org.utbot.framework.codegen.model.constructor.CgMethodTestSet
55+ import org.utbot.framework.codegen.model.constructor.TestClassInfo
5556import org.utbot.framework.codegen.model.constructor.builtin.streamsDeepEqualsMethodId
5657import org.utbot.framework.codegen.model.tree.CgParameterKind
5758import org.utbot.framework.plugin.api.util.id
@@ -66,7 +67,7 @@ import org.utbot.framework.plugin.api.util.jClass
6667 * Although, some of the properties are declared as 'var' so that
6768 * they can be reassigned as well as modified
6869 *
69- * For example, [currentTestClass ] and [currentExecutable] can be reassigned
70+ * For example, [currentOuterMostTestClass ] and [currentExecutable] can be reassigned
7071 * when we start generating another method or test class
7172 *
7273 * [existingVariableNames] is a 'var' property
@@ -79,19 +80,15 @@ internal interface CgContextOwner {
7980 val classUnderTest: ClassId
8081
8182 // test class currently being generated
82- val currentTestClass : ClassId
83+ val currentOuterMostTestClass : ClassId
8384
8485 // current executable under test
8586 var currentExecutable: ExecutableId ?
8687
87- // test class superclass (if needed )
88- var testClassSuperclass : ClassId ?
88+ // TODO: add comments (especially about the case when outerMostTestClassInfo === currentTestClassInfo )
89+ var outerMostTestClassInfo : TestClassInfo
8990
90- // list of interfaces that the test class must inherit
91- val collectedTestClassInterfaces: MutableSet <ClassId >
92-
93- // list of annotations of the test class
94- val collectedTestClassAnnotations: MutableSet <CgAnnotation >
91+ var currentTestClassInfo: TestClassInfo
9592
9693 // exceptions that can be thrown inside of current method being built
9794 val collectedExceptions: MutableSet <ClassId >
@@ -262,7 +259,7 @@ internal interface CgContextOwner {
262259 }
263260 }
264261
265- fun <R > withClassScope (block : () -> R ): R {
262+ fun <R > withTestClassFileScope (block : () -> R ): R {
266263 clearClassScope()
267264 return try {
268265 block()
@@ -271,6 +268,16 @@ internal interface CgContextOwner {
271268 }
272269 }
273270
271+ fun <R > withTestClassScope (block : () -> R ): R {
272+ val savedCurrentTestClassInfo = currentTestClassInfo
273+ currentTestClassInfo = TestClassInfo ()
274+ return try {
275+ block()
276+ } finally {
277+ currentTestClassInfo = savedCurrentTestClassInfo
278+ }
279+ }
280+
274281 /* *
275282 * Set [mockFrameworkUsed] flag to true if the block is successfully executed
276283 */
@@ -307,6 +314,8 @@ internal interface CgContextOwner {
307314 }
308315
309316 private fun clearClassScope () {
317+ outerMostTestClassInfo = TestClassInfo ()
318+ currentTestClassInfo = outerMostTestClassInfo
310319 collectedImports.clear()
311320 importedStaticMethods.clear()
312321 importedClasses.clear()
@@ -321,7 +330,7 @@ internal interface CgContextOwner {
321330 * Check whether a method is an util method of the current class
322331 */
323332 val MethodId .isUtil: Boolean
324- get() = this in currentTestClass .possibleUtilMethodIds
333+ get() = this in currentOuterMostTestClass .possibleUtilMethodIds
325334
326335 /* *
327336 * Checks is it our util reflection field getter method.
@@ -335,49 +344,49 @@ internal interface CgContextOwner {
335344 // util methods of current test class
336345
337346 val getUnsafeInstance: MethodId
338- get() = currentTestClass .getUnsafeInstanceMethodId
347+ get() = currentOuterMostTestClass .getUnsafeInstanceMethodId
339348
340349 val createInstance: MethodId
341- get() = currentTestClass .createInstanceMethodId
350+ get() = currentOuterMostTestClass .createInstanceMethodId
342351
343352 val createArray: MethodId
344- get() = currentTestClass .createArrayMethodId
353+ get() = currentOuterMostTestClass .createArrayMethodId
345354
346355 val setField: MethodId
347- get() = currentTestClass .setFieldMethodId
356+ get() = currentOuterMostTestClass .setFieldMethodId
348357
349358 val setStaticField: MethodId
350- get() = currentTestClass .setStaticFieldMethodId
359+ get() = currentOuterMostTestClass .setStaticFieldMethodId
351360
352361 val getFieldValue: MethodId
353- get() = currentTestClass .getFieldValueMethodId
362+ get() = currentOuterMostTestClass .getFieldValueMethodId
354363
355364 val getStaticFieldValue: MethodId
356- get() = currentTestClass .getStaticFieldValueMethodId
365+ get() = currentOuterMostTestClass .getStaticFieldValueMethodId
357366
358367 val getEnumConstantByName: MethodId
359- get() = currentTestClass .getEnumConstantByNameMethodId
368+ get() = currentOuterMostTestClass .getEnumConstantByNameMethodId
360369
361370 val deepEquals: MethodId
362- get() = currentTestClass .deepEqualsMethodId
371+ get() = currentOuterMostTestClass .deepEqualsMethodId
363372
364373 val arraysDeepEquals: MethodId
365- get() = currentTestClass .arraysDeepEqualsMethodId
374+ get() = currentOuterMostTestClass .arraysDeepEqualsMethodId
366375
367376 val iterablesDeepEquals: MethodId
368- get() = currentTestClass .iterablesDeepEqualsMethodId
377+ get() = currentOuterMostTestClass .iterablesDeepEqualsMethodId
369378
370379 val streamsDeepEquals: MethodId
371- get() = currentTestClass .streamsDeepEqualsMethodId
380+ get() = currentOuterMostTestClass .streamsDeepEqualsMethodId
372381
373382 val mapsDeepEquals: MethodId
374- get() = currentTestClass .mapsDeepEqualsMethodId
383+ get() = currentOuterMostTestClass .mapsDeepEqualsMethodId
375384
376385 val hasCustomEquals: MethodId
377- get() = currentTestClass .hasCustomEqualsMethodId
386+ get() = currentOuterMostTestClass .hasCustomEqualsMethodId
378387
379388 val getArrayLength: MethodId
380- get() = currentTestClass .getArrayLengthMethodId
389+ get() = currentOuterMostTestClass .getArrayLengthMethodId
381390}
382391
383392/* *
@@ -386,8 +395,8 @@ internal interface CgContextOwner {
386395internal data class CgContext (
387396 override val classUnderTest : ClassId ,
388397 override var currentExecutable : ExecutableId ? = null ,
389- override val collectedTestClassInterfaces : MutableSet < ClassId > = mutableSetOf (),
390- override val collectedTestClassAnnotations : MutableSet < CgAnnotation > = mutableSetOf() ,
398+ override var outerMostTestClassInfo : TestClassInfo = TestClassInfo (),
399+ override var currentTestClassInfo : TestClassInfo = outerMostTestClassInfo ,
391400 override val collectedExceptions : MutableSet <ClassId > = mutableSetOf(),
392401 override val collectedMethodAnnotations : MutableSet <CgAnnotation > = mutableSetOf(),
393402 override val collectedImports : MutableSet <Import > = mutableSetOf(),
@@ -425,7 +434,7 @@ internal data class CgContext(
425434 override lateinit var statesCache: EnvironmentFieldStateCache
426435 override lateinit var actual: CgVariable
427436
428- override val currentTestClass : ClassId by lazy {
437+ override val currentOuterMostTestClass : ClassId by lazy {
429438 val packagePrefix = if (testClassPackageName.isNotEmpty()) " $testClassPackageName ." else " "
430439 val simpleName = testClassCustomName ? : " ${createTestClassName(classUnderTest.name)} Test"
431440 val name = " $packagePrefix$simpleName "
@@ -436,20 +445,11 @@ internal data class CgContext(
436445 )
437446 }
438447
439- override var testClassSuperclass: ClassId ? = null
440- set(value) {
441- // Assigning a value to the testClassSuperclass when it is already non-null
442- // means that we need the test class to have more than one superclass
443- // which is impossible in Java and Kotlin.
444- require(field == null ) { " It is impossible for the test class to have more than one superclass" }
445- field = value
446- }
447-
448448 override var valueByModel: IdentityHashMap <UtModel , CgValue > = IdentityHashMap ()
449449
450450 override var valueByModelId: MutableMap <Int ?, CgValue > = mutableMapOf ()
451451
452452 override val currentMethodParameters: MutableMap <CgParameterKind , CgVariable > = mutableMapOf ()
453453
454- override val testClassThisInstance: CgThisInstance = CgThisInstance (currentTestClass )
454+ override val testClassThisInstance: CgThisInstance = CgThisInstance (currentOuterMostTestClass )
455455}
0 commit comments