@@ -21,7 +21,6 @@ import org.utbot.framework.codegen.model.constructor.util.CgStatementConstructor
21
21
import org.utbot.framework.codegen.model.tree.CgAuxiliaryClass
22
22
import org.utbot.framework.codegen.model.tree.CgExecutableUnderTestCluster
23
23
import org.utbot.framework.codegen.model.tree.CgMethod
24
- import org.utbot.framework.codegen.model.tree.CgParameterDeclaration
25
24
import org.utbot.framework.codegen.model.tree.CgRegion
26
25
import org.utbot.framework.codegen.model.tree.CgSimpleRegion
27
26
import org.utbot.framework.codegen.model.tree.CgStaticsRegion
@@ -37,12 +36,12 @@ import org.utbot.framework.codegen.model.tree.buildTestClassBody
37
36
import org.utbot.framework.codegen.model.tree.buildTestClassFile
38
37
import org.utbot.framework.codegen.model.visitor.importUtilMethodDependencies
39
38
import org.utbot.framework.plugin.api.ClassId
40
- import org.utbot.framework.plugin.api.ExecutableId
41
39
import org.utbot.framework.plugin.api.MethodId
42
40
import org.utbot.framework.plugin.api.UtExecutionSuccess
43
41
import org.utbot.framework.plugin.api.UtMethodTestSet
44
42
import org.utbot.framework.plugin.api.util.description
45
43
import org.utbot.framework.plugin.api.util.humanReadableName
44
+ import org.utbot.fuzzer.UtFuzzedExecution
46
45
47
46
internal class CgTestClassConstructor (val context : CgContext ) :
48
47
CgContextOwner by context,
@@ -140,45 +139,18 @@ internal class CgTestClassConstructor(val context: CgContext) :
140
139
.filter { it.result is UtExecutionSuccess }
141
140
.map { (it.result as UtExecutionSuccess ).model }
142
141
143
- val (methodUnderTest, _, _, clustersInfo) = testSet
144
142
val regions = mutableListOf<CgRegion <CgMethod >>()
145
- val requiredFields = mutableListOf<CgParameterDeclaration >()
146
-
147
- when (context.parametrizedTestSource) {
148
- ParametrizedTestSource .DO_NOT_PARAMETRIZE -> {
149
- for ((clusterSummary, executionIndices) in clustersInfo) {
150
- val currentTestCaseTestMethods = mutableListOf<CgTestMethod >()
151
- emptyLineIfNeeded()
152
- for (i in executionIndices) {
153
- runCatching {
154
- currentTestCaseTestMethods + = methodConstructor.createTestMethod(methodUnderTest, testSet.executions[i])
155
- }.onFailure { e -> processFailure(testSet, e) }
156
- }
157
- val clusterHeader = clusterSummary?.header
158
- val clusterContent = clusterSummary?.content
159
- ?.split(' \n ' )
160
- ?.let { CgTripleSlashMultilineComment (it) }
161
- regions + = CgTestMethodCluster (clusterHeader, clusterContent, currentTestCaseTestMethods)
162
143
163
- testsGenerationReport.addTestsByType(testSet, currentTestCaseTestMethods)
164
- }
165
- }
166
- ParametrizedTestSource .PARAMETRIZE -> {
167
- // Mocks are not supported in parametrized tests, we should exclude them
168
- val testSetWithoutMocking = testSet.excludeExecutionsWithMocking()
169
-
170
- for (splitByExecutionTestSet in testSetWithoutMocking.splitExecutionsByResult()) {
171
- for (splitByChangedStaticsTestSet in splitByExecutionTestSet.splitExecutionsByChangedStatics()) {
172
- createParametrizedTestAndDataProvider(
173
- splitByChangedStaticsTestSet,
174
- requiredFields,
175
- regions,
176
- methodUnderTest
177
- )
178
- }
179
- }
144
+ runCatching {
145
+ when (context.parametrizedTestSource) {
146
+ ParametrizedTestSource .DO_NOT_PARAMETRIZE -> createTest(testSet, regions)
147
+ ParametrizedTestSource .PARAMETRIZE ->
148
+ createParametrizedTestAndDataProvider(
149
+ testSet,
150
+ regions
151
+ )
180
152
}
181
- }
153
+ }.onFailure { e -> processFailure(testSet, e) }
182
154
183
155
val errors = testSet.allErrors
184
156
if (errors.isNotEmpty()) {
@@ -195,29 +167,62 @@ internal class CgTestClassConstructor(val context: CgContext) :
195
167
.merge(failure.description, 1 , Int ::plus)
196
168
}
197
169
170
+ private fun createTest (
171
+ testSet : CgMethodTestSet ,
172
+ regions : MutableList <CgRegion <CgMethod >>
173
+ ) {
174
+ val (methodUnderTest, _, _, clustersInfo) = testSet
175
+
176
+ for ((clusterSummary, executionIndices) in clustersInfo) {
177
+ val currentTestCaseTestMethods = mutableListOf<CgTestMethod >()
178
+ emptyLineIfNeeded()
179
+ for (i in executionIndices) {
180
+ currentTestCaseTestMethods + = methodConstructor.createTestMethod(methodUnderTest, testSet.executions[i])
181
+ }
182
+ val clusterHeader = clusterSummary?.header
183
+ val clusterContent = clusterSummary?.content
184
+ ?.split(' \n ' )
185
+ ?.let { CgTripleSlashMultilineComment (it) }
186
+ regions + = CgTestMethodCluster (clusterHeader, clusterContent, currentTestCaseTestMethods)
187
+
188
+ testsGenerationReport.addTestsByType(testSet, currentTestCaseTestMethods)
189
+ }
190
+ }
191
+
198
192
private fun createParametrizedTestAndDataProvider (
199
193
testSet : CgMethodTestSet ,
200
- requiredFields : MutableList <CgParameterDeclaration >,
201
- regions : MutableList <CgRegion <CgMethod >>,
202
- methodUnderTest : ExecutableId ,
194
+ regions : MutableList <CgRegion <CgMethod >>
203
195
) {
204
- runCatching {
205
- val dataProviderMethodName = nameGenerator.dataProviderMethodNameFor(testSet.executableId)
196
+ val (methodUnderTest, _, _, _) = testSet
206
197
207
- val parameterizedTestMethod =
208
- methodConstructor.createParameterizedTestMethod(testSet, dataProviderMethodName )
198
+ for (preparedTestSet in testSet.prepareTestSetsForParameterizedTestGeneration()) {
199
+ val dataProviderMethodName = nameGenerator.dataProviderMethodNameFor(preparedTestSet.executableId )
209
200
210
- requiredFields + = parameterizedTestMethod.requiredFields
201
+ val parameterizedTestMethod =
202
+ methodConstructor.createParameterizedTestMethod(preparedTestSet, dataProviderMethodName)
211
203
212
204
testFrameworkManager.addDataProvider(
213
- methodConstructor.createParameterizedTestDataProvider(testSet , dataProviderMethodName)
205
+ methodConstructor.createParameterizedTestDataProvider(preparedTestSet , dataProviderMethodName)
214
206
)
215
207
216
208
regions + = CgSimpleRegion (
217
209
" Parameterized test for method ${methodUnderTest.humanReadableName} " ,
218
210
listOf (parameterizedTestMethod),
219
211
)
220
- }.onFailure { error -> processFailure(testSet, error) }
212
+ }
213
+
214
+ // We cannot track mocking in fuzzed executions,
215
+ // so we generate standard tests for each [UtFuzzedExecution].
216
+ // [https://github.com/UnitTestBot/UTBotJava/issues/1137]
217
+ val testCaseTestMethods = mutableListOf<CgTestMethod >()
218
+ for (execution in testSet.executions.filterIsInstance<UtFuzzedExecution >()) {
219
+ testCaseTestMethods + = methodConstructor.createTestMethod(methodUnderTest, execution)
220
+ }
221
+
222
+ regions + = CgSimpleRegion (
223
+ " FUZZER: EXECUTIONS for method ${methodUnderTest.humanReadableName} " ,
224
+ testCaseTestMethods,
225
+ )
221
226
}
222
227
223
228
/* *
0 commit comments