@@ -2,11 +2,14 @@ package org.utbot.framework.codegen.tree
2
2
3
3
import org.utbot.framework.codegen.domain.builtin.TestClassUtilMethodProvider
4
4
import org.utbot.framework.codegen.domain.builtin.closeMethodId
5
+ import org.utbot.framework.codegen.domain.builtin.injectMocksClassId
6
+ import org.utbot.framework.codegen.domain.builtin.mockClassId
5
7
import org.utbot.framework.codegen.domain.builtin.openMocksMethodId
6
8
import org.utbot.framework.codegen.domain.context.CgContext
7
9
import org.utbot.framework.codegen.domain.models.CgAssignment
8
10
import org.utbot.framework.codegen.domain.models.CgClassBody
9
11
import org.utbot.framework.codegen.domain.models.CgDeclaration
12
+ import org.utbot.framework.codegen.domain.models.CgFieldDeclaration
10
13
import org.utbot.framework.codegen.domain.models.CgFrameworkUtilMethod
11
14
import org.utbot.framework.codegen.domain.models.CgMethod
12
15
import org.utbot.framework.codegen.domain.models.CgMethodCall
@@ -18,7 +21,9 @@ import org.utbot.framework.codegen.domain.models.CgStatementExecutableCall
18
21
import org.utbot.framework.codegen.domain.models.CgStaticsRegion
19
22
import org.utbot.framework.codegen.domain.models.CgVariable
20
23
import org.utbot.framework.codegen.domain.models.SpringTestClassModel
24
+ import org.utbot.framework.plugin.api.ClassId
21
25
import org.utbot.framework.plugin.api.UtCompositeModel
26
+ import org.utbot.framework.plugin.api.UtModel
22
27
import org.utbot.framework.plugin.api.util.id
23
28
import org.utbot.framework.plugin.api.util.objectClassId
24
29
@@ -32,69 +37,11 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
32
37
33
38
// TODO: support inner classes here
34
39
35
- // TODO: create class variables with Mock/InjectMock annotations using testClassModel
36
-
37
- val vc = CgComponents .getVariableConstructorBy(context)
38
- val ng = CgComponents .getNameGeneratorBy(context)
39
-
40
- // outer things
41
- val listOfInjectMocksUtModels =
42
- testClassModel
43
- .methodTestSets
44
- .flatMap { testSet ->
45
- testSet.executions.map { execution -> execution.stateBefore.thisInstance!! }
46
- }
47
-
48
-
49
- val mockModels = SpringTestClassModelBuilder .listOfUtModels
50
- //
51
-
52
- // inject mock field region
53
-
54
- val groupedUtModelsByClassIdInjectMock = listOfInjectMocksUtModels.groupBy { it.classId }
55
-
56
- val annotationInjectMock = CgComponents .getStatementConstructorBy(context).annotation(injectMocksClassId)
57
-
58
- groupedUtModelsByClassIdInjectMock.forEach { (classId, listOfUtModels) ->
59
- val name = ng.nameFrom(classId)
60
- val declaration =
61
- CgDeclaration (
62
- classId,
63
- name, // is not a mock...
64
- null
65
- )
66
-
67
- fields + = CgFieldDeclaration (mockClassId, declaration, annotationInjectMock)
68
- val value = vc.getOrCreateVariable(listOfUtModels.first(), name)
69
- CgSpringVariableConstructor .injectingMocksModelsVariables + = listOfUtModels to value
70
- }
71
-
72
- // endregion
73
-
74
-
75
-
76
- // mock fields region
77
-
78
- val groupedUtModelsByClassIdMock = mockModels.groupBy { it.classId }
79
-
80
- val annotationMock = CgComponents .getStatementConstructorBy(context).annotation(mockClassId)
81
-
82
- groupedUtModelsByClassIdMock.forEach { (classId, listOfUtModels) ->
83
- val name = ng.nameFrom(classId)
84
- val declaration =
85
- CgDeclaration (
86
- classId,
87
- name + " Mock" ,
88
- null
89
- )
90
- fields + = CgFieldDeclaration (mockClassId, declaration, annotationMock)
91
- val value = vc.getOrCreateVariable(listOfUtModels.first(), name)
92
- CgSpringVariableConstructor .mockedModelsVariables + = listOfUtModels to value
93
- }
94
-
95
- // endregion
96
-
40
+ val groupedInjectedModelsByClassId = testClassModel.injectedMockModels.groupBy { it.classId }
41
+ fields + = constructClassFields(groupedInjectedModelsByClassId, injectMocksClassId, isMock = false )
97
42
43
+ val groupedMockedModelsByClassId = testClassModel.mockedModels.groupBy { it.classId }
44
+ fields + = constructClassFields(groupedMockedModelsByClassId, mockClassId, isMock = true )
98
45
99
46
val (closeableField, closeableMethods) = constructMockitoCloseables()
100
47
fields + = closeableField
@@ -139,7 +86,37 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
139
86
return if (regions.any()) regions else null
140
87
}
141
88
142
- private fun constructMockitoCloseables (): Pair <CgDeclaration , CgMethodsCluster > {
89
+ private fun constructClassFields (
90
+ groupedModelsByClassId : Map <ClassId , List <UtModel >>,
91
+ annotationClassId : ClassId ,
92
+ isMock : Boolean
93
+ ): MutableList <CgFieldDeclaration > {
94
+ val constructedDeclarations = mutableListOf<CgFieldDeclaration >()
95
+
96
+ val annotation = statementConstructor.annotation(annotationClassId)
97
+ groupedModelsByClassId.forEach { (classId, listOfUtModels) ->
98
+ val name = nameGenerator.nameFrom(classId) + if (isMock) " Mock" else " "
99
+ val declaration =
100
+ CgDeclaration (
101
+ classId,
102
+ name,
103
+ null
104
+ )
105
+
106
+ constructedDeclarations + = CgFieldDeclaration (declaration, annotation)
107
+
108
+ val createdValue = variableConstructor.getOrCreateVariable(listOfUtModels.first(), name)
109
+ when (annotationClassId) {
110
+ injectMocksClassId -> CgSpringVariableConstructor .injectedMocksModelsVariables + = listOfUtModels to createdValue
111
+ mockClassId -> CgSpringVariableConstructor .mockedModelsVariables + = listOfUtModels to createdValue
112
+ else -> error(" Unexpected annotation ClassId" )
113
+ }
114
+ }
115
+
116
+ return constructedDeclarations
117
+ }
118
+
119
+ private fun constructMockitoCloseables (): Pair <CgFieldDeclaration , CgMethodsCluster > {
143
120
val mockitoCloseableVarName = " mockitoCloseable"
144
121
val mockitoCloseableVarType = java.lang.AutoCloseable ::class .id
145
122
@@ -151,7 +128,8 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
151
128
152
129
val mockitoCloseableVariable =
153
130
variableConstructor.getOrCreateVariable(mockitoCloseableModel, mockitoCloseableVarName)
154
- val mockitoCloseableField = CgDeclaration (mockitoCloseableVarType, mockitoCloseableVarName, initializer = null )
131
+ val mockitoCloseableDeclaration = CgDeclaration (mockitoCloseableVarType, mockitoCloseableVarName, initializer = null )
132
+ val mockitoCloseableFieldDeclaration = CgFieldDeclaration (mockitoCloseableDeclaration)
155
133
156
134
importIfNeeded(openMocksMethodId)
157
135
@@ -189,6 +167,6 @@ class CgSpringTestClassConstructor(context: CgContext): CgAbstractTestClassConst
189
167
listOf (CgSimpleRegion (" Mocking utils" , listOf (beforeMethod, afterMethod)))
190
168
)
191
169
192
- return mockitoCloseableField to methodCluster
170
+ return mockitoCloseableFieldDeclaration to methodCluster
193
171
}
194
172
}
0 commit comments