Skip to content

Commit d46153e

Browse files
committed
wip
1 parent 4884a91 commit d46153e

File tree

2 files changed

+77
-50
lines changed

2 files changed

+77
-50
lines changed

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

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
package org.utbot.framework.codegen.tree
22

33
import org.utbot.framework.codegen.domain.builtin.TestClassUtilMethodProvider
4-
import org.utbot.framework.codegen.domain.builtin.injectMocksClassId
5-
import org.utbot.framework.codegen.domain.builtin.mockClassId
4+
import org.utbot.framework.codegen.domain.builtin.closeMethodId
5+
import org.utbot.framework.codegen.domain.builtin.openMocksMethodId
66
import org.utbot.framework.codegen.domain.context.CgContext
7+
import org.utbot.framework.codegen.domain.models.CgAssignment
78
import org.utbot.framework.codegen.domain.models.CgClassBody
89
import org.utbot.framework.codegen.domain.models.CgDeclaration
9-
import org.utbot.framework.codegen.domain.models.CgFieldDeclaration
10+
import org.utbot.framework.codegen.domain.models.CgFrameworkUtilMethod
1011
import org.utbot.framework.codegen.domain.models.CgMethod
12+
import org.utbot.framework.codegen.domain.models.CgMethodCall
1113
import org.utbot.framework.codegen.domain.models.CgMethodTestSet
1214
import org.utbot.framework.codegen.domain.models.CgMethodsCluster
1315
import org.utbot.framework.codegen.domain.models.CgRegion
16+
import org.utbot.framework.codegen.domain.models.CgSimpleRegion
17+
import org.utbot.framework.codegen.domain.models.CgStatementExecutableCall
1418
import org.utbot.framework.codegen.domain.models.CgStaticsRegion
19+
import org.utbot.framework.codegen.domain.models.CgVariable
1520
import org.utbot.framework.codegen.domain.models.SpringTestClassModel
16-
import org.utbot.framework.codegen.domain.models.builders.SpringTestClassModelBuilder
21+
import org.utbot.framework.plugin.api.UtCompositeModel
22+
import org.utbot.framework.plugin.api.util.id
23+
import org.utbot.framework.plugin.api.util.objectClassId
1724

1825
class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConstructor<SpringTestClassModel>(context) {
1926

27+
private val variableConstructor: CgVariableConstructor = CgComponents.getVariableConstructorBy(context)
28+
private val statementConstructor: CgStatementConstructor = CgComponents.getStatementConstructorBy(context)
29+
2030
override fun constructTestClassBody(testClassModel: SpringTestClassModel): CgClassBody {
2131
return buildClassBody(currentTestClass) {
2232

@@ -56,7 +66,7 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
5666

5767
fields += CgFieldDeclaration(mockClassId, declaration, annotationInjectMock)
5868
val value = vc.getOrCreateVariable(listOfUtModels.first(), name)
59-
CgSpringVariableConstructor.injectingMocksModelsVariables += listOfUtModels to value
69+
CgSpringVariableConstructor.fromListToVariableList += Pair(listOfUtModels, value)
6070
}
6171

6272
// endregion
@@ -79,20 +89,16 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
7989
)
8090
fields += CgFieldDeclaration(mockClassId, declaration, annotationMock)
8191
val value = vc.getOrCreateVariable(listOfUtModels.first(), name)
82-
CgSpringVariableConstructor.mockedModelsVariables += listOfUtModels to value
92+
CgSpringVariableConstructor.fromListToVariableList += Pair(listOfUtModels, value)
8393
}
8494

8595
// endregion
8696

8797

8898

89-
// TODO: create beforeEach/ afterEach methods
90-
// Requires new implementation of CgMethod for test framework builtins
91-
92-
// Render the following things here?
93-
// private AutoCloseable mockitoCloseable;
94-
// @BeforeEach ...
95-
// @AfterEach ...
99+
val (closeableField, closeableMethods) = constructMockitoCloseables()
100+
fields += closeableField
101+
methodRegions += closeableMethods
96102

97103
for (testSet in testClassModel.methodTestSets) {
98104
updateCurrentExecutable(testSet.executableId)
@@ -132,4 +138,57 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
132138

133139
return if (regions.any()) regions else null
134140
}
141+
142+
private fun constructMockitoCloseables(): Pair<CgDeclaration, CgMethodsCluster> {
143+
val mockitoCloseableVarName = "mockitoCloseable"
144+
val mockitoCloseableVarType = java.lang.AutoCloseable::class.id
145+
146+
val mockitoCloseableModel = UtCompositeModel(
147+
id = null,
148+
classId = mockitoCloseableVarType,
149+
isMock = false,
150+
)
151+
152+
val mockitoCloseableVariable =
153+
variableConstructor.getOrCreateVariable(mockitoCloseableModel, mockitoCloseableVarName)
154+
val mockitoCloseableField = CgDeclaration(mockitoCloseableVarType, mockitoCloseableVarName, initializer = null)
155+
156+
importIfNeeded(openMocksMethodId)
157+
158+
val openMocksCall = CgMethodCall(
159+
caller = null,
160+
executableId = openMocksMethodId,
161+
//TODO: this is a hack of this
162+
arguments = listOf(CgVariable("this", objectClassId))
163+
)
164+
165+
val closeCall = CgMethodCall(
166+
caller = mockitoCloseableVariable,
167+
executableId = closeMethodId,
168+
arguments = emptyList(),
169+
)
170+
171+
val openMocksStatement = CgAssignment(mockitoCloseableVariable, openMocksCall)
172+
val beforeMethod = CgFrameworkUtilMethod(
173+
name = "setUp",
174+
statements = listOf(openMocksStatement),
175+
exceptions = emptySet(),
176+
annotations = listOf(statementConstructor.annotation(context.testFramework.beforeMethodId)),
177+
)
178+
179+
val closeStatement = CgStatementExecutableCall(closeCall)
180+
val afterMethod = CgFrameworkUtilMethod(
181+
name = "tearDown",
182+
statements = listOf(closeStatement),
183+
exceptions = setOf(java.lang.Exception::class.id),
184+
annotations = listOf(statementConstructor.annotation(context.testFramework.afterMethodId)),
185+
)
186+
187+
val methodCluster = CgMethodsCluster(
188+
"Test run utility methods",
189+
listOf(CgSimpleRegion("Mocking utils", listOf(beforeMethod, afterMethod)))
190+
)
191+
192+
return mockitoCloseableField to methodCluster
193+
}
135194
}
Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,19 @@
11
package org.utbot.framework.codegen.tree
22

3-
import com.jetbrains.rd.util.firstOrNull
43
import org.utbot.framework.codegen.domain.context.CgContext
54
import org.utbot.framework.codegen.domain.models.CgValue
6-
import org.utbot.framework.codegen.domain.models.CgVariable
7-
import org.utbot.framework.plugin.api.UtCompositeModel
85
import org.utbot.framework.plugin.api.UtModel
9-
import org.utbot.framework.plugin.api.isMockModel
106

117
class CgSpringVariableConstructor(context: CgContext) : CgVariableConstructor(context) {
12-
13-
private val mockFrameworkManager = CgComponents.getMockFrameworkManagerBy(context)
14-
158
override fun getOrCreateVariable(model: UtModel, name: String?): CgValue {
16-
val alreadyCreatedMock = findCgValueByModel(model, mockedModelsVariables)
17-
val alreadyCreatedInjectMocks = findCgValueByModel(model, injectingMocksModelsVariables)
18-
19-
if (alreadyCreatedInjectMocks != null) {
20-
val compModel = model as UtCompositeModel
21-
for (mock in compModel.fields.values) {
22-
getOrCreateVariable(mock)
23-
}
24-
}
25-
26-
if (alreadyCreatedMock != null) {
27-
if (model.isMockModel()) {
28-
mockFrameworkManager.createMockForVariable(
29-
model as UtCompositeModel,
30-
alreadyCreatedMock as CgVariable,
31-
)
32-
}
9+
// val existingValue = fromListToVariableList[model]
10+
val existingValue = fromListToVariableList.find { it.first.contains(model) }?.second
3311

34-
return alreadyCreatedMock
35-
}
36-
37-
return super.getOrCreateVariable(model, name)
12+
return existingValue ?: super.getOrCreateVariable(model, name)
3813
}
3914

40-
41-
private fun findCgValueByModel(model: UtModel, collection: Map<List<UtModel>, CgValue>): CgValue? =
42-
collection
43-
.filter { it.key.contains(model) }
44-
.firstOrNull()
45-
?.value
46-
4715
companion object {
48-
val mockedModelsVariables: MutableMap<List<UtModel>, CgValue> = mutableMapOf()
49-
val injectingMocksModelsVariables: MutableMap<List<UtModel>, CgValue> = mutableMapOf()
16+
// val fromListToVariableList: MutableMap<List<UtModel>, CgValue> = mutableMapOf()
17+
val fromListToVariableList: MutableList<Pair<List<UtModel>, CgValue>> = mutableListOf()
5018
}
5119
}

0 commit comments

Comments
 (0)