@@ -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, [currentOuterMostTestClass ] 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
@@ -79,19 +80,15 @@ internal interface CgContextOwner {
79
80
val classUnderTest: ClassId
80
81
81
82
// test class currently being generated
82
- val currentTestClass : ClassId
83
+ val currentOuterMostTestClass : 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
+ // TODO: add comments (especially about the case when outerMostTestClassInfo === currentTestClassInfo )
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
+ var currentTestClassInfo: TestClassInfo
95
92
96
93
// exceptions that can be thrown inside of current method being built
97
94
val collectedExceptions: MutableSet <ClassId >
@@ -262,7 +259,7 @@ internal interface CgContextOwner {
262
259
}
263
260
}
264
261
265
- fun <R > withClassScope (block : () -> R ): R {
262
+ fun <R > withTestClassFileScope (block : () -> R ): R {
266
263
clearClassScope()
267
264
return try {
268
265
block()
@@ -271,6 +268,16 @@ internal interface CgContextOwner {
271
268
}
272
269
}
273
270
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
+
274
281
/* *
275
282
* Set [mockFrameworkUsed] flag to true if the block is successfully executed
276
283
*/
@@ -307,6 +314,8 @@ internal interface CgContextOwner {
307
314
}
308
315
309
316
private fun clearClassScope () {
317
+ outerMostTestClassInfo = TestClassInfo ()
318
+ currentTestClassInfo = outerMostTestClassInfo
310
319
collectedImports.clear()
311
320
importedStaticMethods.clear()
312
321
importedClasses.clear()
@@ -321,7 +330,7 @@ internal interface CgContextOwner {
321
330
* Check whether a method is an util method of the current class
322
331
*/
323
332
val MethodId .isUtil: Boolean
324
- get() = this in currentTestClass .possibleUtilMethodIds
333
+ get() = this in currentOuterMostTestClass .possibleUtilMethodIds
325
334
326
335
/* *
327
336
* Checks is it our util reflection field getter method.
@@ -335,49 +344,49 @@ internal interface CgContextOwner {
335
344
// util methods of current test class
336
345
337
346
val getUnsafeInstance: MethodId
338
- get() = currentTestClass .getUnsafeInstanceMethodId
347
+ get() = currentOuterMostTestClass .getUnsafeInstanceMethodId
339
348
340
349
val createInstance: MethodId
341
- get() = currentTestClass .createInstanceMethodId
350
+ get() = currentOuterMostTestClass .createInstanceMethodId
342
351
343
352
val createArray: MethodId
344
- get() = currentTestClass .createArrayMethodId
353
+ get() = currentOuterMostTestClass .createArrayMethodId
345
354
346
355
val setField: MethodId
347
- get() = currentTestClass .setFieldMethodId
356
+ get() = currentOuterMostTestClass .setFieldMethodId
348
357
349
358
val setStaticField: MethodId
350
- get() = currentTestClass .setStaticFieldMethodId
359
+ get() = currentOuterMostTestClass .setStaticFieldMethodId
351
360
352
361
val getFieldValue: MethodId
353
- get() = currentTestClass .getFieldValueMethodId
362
+ get() = currentOuterMostTestClass .getFieldValueMethodId
354
363
355
364
val getStaticFieldValue: MethodId
356
- get() = currentTestClass .getStaticFieldValueMethodId
365
+ get() = currentOuterMostTestClass .getStaticFieldValueMethodId
357
366
358
367
val getEnumConstantByName: MethodId
359
- get() = currentTestClass .getEnumConstantByNameMethodId
368
+ get() = currentOuterMostTestClass .getEnumConstantByNameMethodId
360
369
361
370
val deepEquals: MethodId
362
- get() = currentTestClass .deepEqualsMethodId
371
+ get() = currentOuterMostTestClass .deepEqualsMethodId
363
372
364
373
val arraysDeepEquals: MethodId
365
- get() = currentTestClass .arraysDeepEqualsMethodId
374
+ get() = currentOuterMostTestClass .arraysDeepEqualsMethodId
366
375
367
376
val iterablesDeepEquals: MethodId
368
- get() = currentTestClass .iterablesDeepEqualsMethodId
377
+ get() = currentOuterMostTestClass .iterablesDeepEqualsMethodId
369
378
370
379
val streamsDeepEquals: MethodId
371
- get() = currentTestClass .streamsDeepEqualsMethodId
380
+ get() = currentOuterMostTestClass .streamsDeepEqualsMethodId
372
381
373
382
val mapsDeepEquals: MethodId
374
- get() = currentTestClass .mapsDeepEqualsMethodId
383
+ get() = currentOuterMostTestClass .mapsDeepEqualsMethodId
375
384
376
385
val hasCustomEquals: MethodId
377
- get() = currentTestClass .hasCustomEqualsMethodId
386
+ get() = currentOuterMostTestClass .hasCustomEqualsMethodId
378
387
379
388
val getArrayLength: MethodId
380
- get() = currentTestClass .getArrayLengthMethodId
389
+ get() = currentOuterMostTestClass .getArrayLengthMethodId
381
390
}
382
391
383
392
/* *
@@ -386,8 +395,8 @@ internal interface CgContextOwner {
386
395
internal data class CgContext (
387
396
override val classUnderTest : ClassId ,
388
397
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 ,
391
400
override val collectedExceptions : MutableSet <ClassId > = mutableSetOf(),
392
401
override val collectedMethodAnnotations : MutableSet <CgAnnotation > = mutableSetOf(),
393
402
override val collectedImports : MutableSet <Import > = mutableSetOf(),
@@ -425,7 +434,7 @@ internal data class CgContext(
425
434
override lateinit var statesCache: EnvironmentFieldStateCache
426
435
override lateinit var actual: CgVariable
427
436
428
- override val currentTestClass : ClassId by lazy {
437
+ override val currentOuterMostTestClass : ClassId by lazy {
429
438
val packagePrefix = if (testClassPackageName.isNotEmpty()) " $testClassPackageName ." else " "
430
439
val simpleName = testClassCustomName ? : " ${createTestClassName(classUnderTest.name)} Test"
431
440
val name = " $packagePrefix$simpleName "
@@ -436,20 +445,11 @@ internal data class CgContext(
436
445
)
437
446
}
438
447
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
448
override var valueByModel: IdentityHashMap <UtModel , CgValue > = IdentityHashMap ()
449
449
450
450
override var valueByModelId: MutableMap <Int ?, CgValue > = mutableMapOf ()
451
451
452
452
override val currentMethodParameters: MutableMap <CgParameterKind , CgVariable > = mutableMapOf ()
453
453
454
- override val testClassThisInstance: CgThisInstance = CgThisInstance (currentTestClass )
454
+ override val testClassThisInstance: CgThisInstance = CgThisInstance (currentOuterMostTestClass )
455
455
}
0 commit comments