@@ -52,6 +52,7 @@ import kotlinx.collections.immutable.persistentListOf
52
52
import kotlinx.collections.immutable.persistentMapOf
53
53
import kotlinx.collections.immutable.persistentSetOf
54
54
import org.utbot.framework.codegen.model.constructor.CgMethodTestSet
55
+ import org.utbot.framework.codegen.model.constructor.TestClassInfo
55
56
import org.utbot.framework.codegen.model.constructor.builtin.streamsDeepEqualsMethodId
56
57
import org.utbot.framework.codegen.model.tree.CgParameterKind
57
58
import org.utbot.framework.plugin.api.util.id
@@ -66,7 +67,7 @@ import org.utbot.framework.plugin.api.util.jClass
66
67
* Although, some of the properties are declared as 'var' so that
67
68
* they can be reassigned as well as modified
68
69
*
69
- * For example, [currentTestClass ] and [currentExecutable] can be reassigned
70
+ * For example, [outerMostTestClass ] and [currentExecutable] can be reassigned
70
71
* when we start generating another method or test class
71
72
*
72
73
* [existingVariableNames] is a 'var' property
@@ -78,20 +79,18 @@ internal interface CgContextOwner {
78
79
// current class under test
79
80
val classUnderTest: ClassId
80
81
81
- // test class currently being generated
82
- val currentTestClass : ClassId
82
+ // test class currently being generated (if series of nested classes is generated, it is the outermost one)
83
+ val outerMostTestClass : ClassId
83
84
84
85
// current executable under test
85
86
var currentExecutable: ExecutableId ?
86
87
87
- // test class superclass (if needed)
88
- var testClassSuperclass : ClassId ?
88
+ // ClassInfo for the outermost class currently being generated
89
+ var outerMostTestClassInfo : TestClassInfo
89
90
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
+ // If generating series of nested classes, it is ClassInfo for the innermost one,
92
+ // otherwise it should be equal to outerMostTestClassInfo
93
+ var currentTestClassInfo: TestClassInfo
95
94
96
95
// exceptions that can be thrown inside of current method being built
97
96
val collectedExceptions: MutableSet <ClassId >
@@ -262,7 +261,7 @@ internal interface CgContextOwner {
262
261
}
263
262
}
264
263
265
- fun <R > withClassScope (block : () -> R ): R {
264
+ fun <R > withTestClassFileScope (block : () -> R ): R {
266
265
clearClassScope()
267
266
return try {
268
267
block()
@@ -271,6 +270,16 @@ internal interface CgContextOwner {
271
270
}
272
271
}
273
272
273
+ fun <R > withTestClassScope (block : () -> R ): R {
274
+ val savedCurrentTestClassInfo = currentTestClassInfo
275
+ currentTestClassInfo = TestClassInfo ()
276
+ return try {
277
+ block()
278
+ } finally {
279
+ currentTestClassInfo = savedCurrentTestClassInfo
280
+ }
281
+ }
282
+
274
283
/* *
275
284
* Set [mockFrameworkUsed] flag to true if the block is successfully executed
276
285
*/
@@ -307,6 +316,8 @@ internal interface CgContextOwner {
307
316
}
308
317
309
318
private fun clearClassScope () {
319
+ outerMostTestClassInfo = TestClassInfo ()
320
+ currentTestClassInfo = outerMostTestClassInfo
310
321
collectedImports.clear()
311
322
importedStaticMethods.clear()
312
323
importedClasses.clear()
@@ -321,7 +332,7 @@ internal interface CgContextOwner {
321
332
* Check whether a method is an util method of the current class
322
333
*/
323
334
val MethodId .isUtil: Boolean
324
- get() = this in currentTestClass .possibleUtilMethodIds
335
+ get() = this in outerMostTestClass .possibleUtilMethodIds
325
336
326
337
/* *
327
338
* Checks is it our util reflection field getter method.
@@ -335,49 +346,49 @@ internal interface CgContextOwner {
335
346
// util methods of current test class
336
347
337
348
val getUnsafeInstance: MethodId
338
- get() = currentTestClass .getUnsafeInstanceMethodId
349
+ get() = outerMostTestClass .getUnsafeInstanceMethodId
339
350
340
351
val createInstance: MethodId
341
- get() = currentTestClass .createInstanceMethodId
352
+ get() = outerMostTestClass .createInstanceMethodId
342
353
343
354
val createArray: MethodId
344
- get() = currentTestClass .createArrayMethodId
355
+ get() = outerMostTestClass .createArrayMethodId
345
356
346
357
val setField: MethodId
347
- get() = currentTestClass .setFieldMethodId
358
+ get() = outerMostTestClass .setFieldMethodId
348
359
349
360
val setStaticField: MethodId
350
- get() = currentTestClass .setStaticFieldMethodId
361
+ get() = outerMostTestClass .setStaticFieldMethodId
351
362
352
363
val getFieldValue: MethodId
353
- get() = currentTestClass .getFieldValueMethodId
364
+ get() = outerMostTestClass .getFieldValueMethodId
354
365
355
366
val getStaticFieldValue: MethodId
356
- get() = currentTestClass .getStaticFieldValueMethodId
367
+ get() = outerMostTestClass .getStaticFieldValueMethodId
357
368
358
369
val getEnumConstantByName: MethodId
359
- get() = currentTestClass .getEnumConstantByNameMethodId
370
+ get() = outerMostTestClass .getEnumConstantByNameMethodId
360
371
361
372
val deepEquals: MethodId
362
- get() = currentTestClass .deepEqualsMethodId
373
+ get() = outerMostTestClass .deepEqualsMethodId
363
374
364
375
val arraysDeepEquals: MethodId
365
- get() = currentTestClass .arraysDeepEqualsMethodId
376
+ get() = outerMostTestClass .arraysDeepEqualsMethodId
366
377
367
378
val iterablesDeepEquals: MethodId
368
- get() = currentTestClass .iterablesDeepEqualsMethodId
379
+ get() = outerMostTestClass .iterablesDeepEqualsMethodId
369
380
370
381
val streamsDeepEquals: MethodId
371
- get() = currentTestClass .streamsDeepEqualsMethodId
382
+ get() = outerMostTestClass .streamsDeepEqualsMethodId
372
383
373
384
val mapsDeepEquals: MethodId
374
- get() = currentTestClass .mapsDeepEqualsMethodId
385
+ get() = outerMostTestClass .mapsDeepEqualsMethodId
375
386
376
387
val hasCustomEquals: MethodId
377
- get() = currentTestClass .hasCustomEqualsMethodId
388
+ get() = outerMostTestClass .hasCustomEqualsMethodId
378
389
379
390
val getArrayLength: MethodId
380
- get() = currentTestClass .getArrayLengthMethodId
391
+ get() = outerMostTestClass .getArrayLengthMethodId
381
392
}
382
393
383
394
/* *
@@ -386,8 +397,8 @@ internal interface CgContextOwner {
386
397
internal data class CgContext (
387
398
override val classUnderTest : ClassId ,
388
399
override var currentExecutable : ExecutableId ? = null ,
389
- override val collectedTestClassInterfaces : MutableSet < ClassId > = mutableSetOf (),
390
- override val collectedTestClassAnnotations : MutableSet < CgAnnotation > = mutableSetOf() ,
400
+ override var outerMostTestClassInfo : TestClassInfo = TestClassInfo (),
401
+ override var currentTestClassInfo : TestClassInfo = outerMostTestClassInfo ,
391
402
override val collectedExceptions : MutableSet <ClassId > = mutableSetOf(),
392
403
override val collectedMethodAnnotations : MutableSet <CgAnnotation > = mutableSetOf(),
393
404
override val collectedImports : MutableSet <Import > = mutableSetOf(),
@@ -425,7 +436,7 @@ internal data class CgContext(
425
436
override lateinit var statesCache: EnvironmentFieldStateCache
426
437
override lateinit var actual: CgVariable
427
438
428
- override val currentTestClass : ClassId by lazy {
439
+ override val outerMostTestClass : ClassId by lazy {
429
440
val packagePrefix = if (testClassPackageName.isNotEmpty()) " $testClassPackageName ." else " "
430
441
val simpleName = testClassCustomName ? : " ${createTestClassName(classUnderTest.name)} Test"
431
442
val name = " $packagePrefix$simpleName "
@@ -436,20 +447,11 @@ internal data class CgContext(
436
447
)
437
448
}
438
449
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
-
448
450
override var valueByModel: IdentityHashMap <UtModel , CgValue > = IdentityHashMap ()
449
451
450
452
override var valueByModelId: MutableMap <Int ?, CgValue > = mutableMapOf ()
451
453
452
454
override val currentMethodParameters: MutableMap <CgParameterKind , CgVariable > = mutableMapOf ()
453
455
454
- override val testClassThisInstance: CgThisInstance = CgThisInstance (currentTestClass )
456
+ override val testClassThisInstance: CgThisInstance = CgThisInstance (outerMostTestClass )
455
457
}
0 commit comments