Skip to content

Commit d3a6d77

Browse files
EgorkaKulikovsofurihafe
authored andcommitted
Some suggestions to Spring variables creation
1 parent d46153e commit d3a6d77

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
6666

6767
fields += CgFieldDeclaration(mockClassId, declaration, annotationInjectMock)
6868
val value = vc.getOrCreateVariable(listOfUtModels.first(), name)
69-
CgSpringVariableConstructor.fromListToVariableList += Pair(listOfUtModels, value)
69+
CgSpringVariableConstructor.injectingMocksModelsVariables += listOfUtModels to value
7070
}
7171

7272
// endregion
@@ -89,7 +89,7 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
8989
)
9090
fields += CgFieldDeclaration(mockClassId, declaration, annotationMock)
9191
val value = vc.getOrCreateVariable(listOfUtModels.first(), name)
92-
CgSpringVariableConstructor.fromListToVariableList += Pair(listOfUtModels, value)
92+
CgSpringVariableConstructor.mockedModelsVariables += listOfUtModels to value
9393
}
9494

9595
// endregion
Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
11
package org.utbot.framework.codegen.tree
22

3+
import com.jetbrains.rd.util.firstOrNull
34
import org.utbot.framework.codegen.domain.context.CgContext
45
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
58
import org.utbot.framework.plugin.api.UtModel
9+
import org.utbot.framework.plugin.api.isMockModel
610

711
class CgSpringVariableConstructor(context: CgContext) : CgVariableConstructor(context) {
12+
13+
private val mockFrameworkManager = CgComponents.getMockFrameworkManagerBy(context)
14+
815
override fun getOrCreateVariable(model: UtModel, name: String?): CgValue {
9-
// val existingValue = fromListToVariableList[model]
10-
val existingValue = fromListToVariableList.find { it.first.contains(model) }?.second
16+
val alreadyCreatedMock = mockedModelsVariables
17+
.filter { it.key.contains(model) }
18+
.firstOrNull()
19+
?.value
20+
21+
val alreadyCreatedInjectMocks = injectingMocksModelsVariables
22+
.filter { it.key.contains(model) }
23+
.firstOrNull()
24+
?.value
25+
26+
if (alreadyCreatedInjectMocks != null) {
27+
val compModel = model as UtCompositeModel
28+
for (mock in compModel.fields.values) {
29+
getOrCreateVariable(mock)
30+
}
31+
}
32+
33+
if (alreadyCreatedMock != null) {
34+
if (model.isMockModel()) {
35+
mockFrameworkManager.createMockForVariable(
36+
model as UtCompositeModel,
37+
alreadyCreatedMock as CgVariable,
38+
)
39+
}
40+
41+
return alreadyCreatedMock
42+
}
1143

12-
return existingValue ?: super.getOrCreateVariable(model, name)
44+
return super.getOrCreateVariable(model, name)
1345
}
1446

1547
companion object {
16-
// val fromListToVariableList: MutableMap<List<UtModel>, CgValue> = mutableMapOf()
17-
val fromListToVariableList: MutableList<Pair<List<UtModel>, CgValue>> = mutableListOf()
48+
val mockedModelsVariables: MutableMap<List<UtModel>, CgValue> = mutableMapOf()
49+
val injectingMocksModelsVariables: MutableMap<List<UtModel>, CgValue> = mutableMapOf()
1850
}
1951
}

0 commit comments

Comments
 (0)