@@ -58,11 +58,7 @@ import org.utbot.framework.codegen.model.tree.CgStatement
58
58
import org.utbot.framework.codegen.model.tree.CgStaticFieldAccess
59
59
import org.utbot.framework.codegen.model.tree.CgTestMethod
60
60
import org.utbot.framework.codegen.model.tree.CgTestMethodType
61
- import org.utbot.framework.codegen.model.tree.CgTestMethodType.CRASH
62
- import org.utbot.framework.codegen.model.tree.CgTestMethodType.FAILING
63
- import org.utbot.framework.codegen.model.tree.CgTestMethodType.PARAMETRIZED
64
- import org.utbot.framework.codegen.model.tree.CgTestMethodType.SUCCESSFUL
65
- import org.utbot.framework.codegen.model.tree.CgTestMethodType.TIMEOUT
61
+ import org.utbot.framework.codegen.model.tree.CgTestMethodType.*
66
62
import org.utbot.framework.codegen.model.tree.CgTryCatch
67
63
import org.utbot.framework.codegen.model.tree.CgTypeCast
68
64
import org.utbot.framework.codegen.model.tree.CgValue
@@ -269,15 +265,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
269
265
assertEquality(expected, actual)
270
266
}
271
267
}
272
- .onFailure { exception ->
273
- processExecutionFailure(currentExecution, exception)
274
- }
268
+ .onFailure { exception -> processExecutionFailure(exception) }
275
269
}
276
270
else -> {} // TODO: check this specific case
277
271
}
278
272
}
279
273
280
- private fun processExecutionFailure (execution : UtExecution , exception : Throwable ) {
274
+ private fun processExecutionFailure (exception : Throwable ) {
281
275
val methodInvocationBlock = {
282
276
with (currentExecutable) {
283
277
when (this ) {
@@ -288,42 +282,36 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
288
282
}
289
283
}
290
284
291
- if (shouldTestPassWithException(execution, exception)) {
292
- testFrameworkManager.expectException(exception::class .id) {
293
- methodInvocationBlock()
294
- }
295
- methodType = SUCCESSFUL
296
-
297
- return
298
- }
299
-
300
- if (shouldTestPassWithTimeoutException(execution, exception)) {
301
- writeWarningAboutTimeoutExceeding()
302
- testFrameworkManager.expectTimeout(hangingTestsTimeout.timeoutMs) {
303
- methodInvocationBlock()
285
+ when (methodType) {
286
+ SUCCESSFUL -> error(" Unexpected successful without exception method type for execution with exception $exception " )
287
+ PASSED_EXCEPTION -> {
288
+ testFrameworkManager.expectException(exception::class .id) {
289
+ methodInvocationBlock()
290
+ }
304
291
}
305
- methodType = TIMEOUT
306
-
307
- return
308
- }
309
-
310
- when (exception) {
311
- is ConcreteExecutionFailureException -> {
312
- methodType = CRASH
313
- writeWarningAboutCrash()
292
+ TIMEOUT -> {
293
+ writeWarningAboutTimeoutExceeding()
294
+ testFrameworkManager.expectTimeout(hangingTestsTimeout.timeoutMs) {
295
+ methodInvocationBlock()
296
+ }
314
297
}
315
- is AccessControlException -> {
316
- methodType = CRASH
317
- writeWarningAboutFailureTest(exception)
318
- return
298
+ CRASH -> when (exception) {
299
+ is ConcreteExecutionFailureException -> {
300
+ writeWarningAboutCrash()
301
+ methodInvocationBlock()
302
+ }
303
+ is AccessControlException -> {
304
+ // exception from sandbox
305
+ writeWarningAboutFailureTest(exception)
306
+ }
307
+ else -> error(" Unexpected crash suite for failing execution with $exception exception" )
319
308
}
320
- else -> {
321
- methodType = FAILING
309
+ FAILING -> {
322
310
writeWarningAboutFailureTest(exception)
311
+ methodInvocationBlock()
323
312
}
313
+ PARAMETRIZED -> error(" Unexpected $PARAMETRIZED method type for failing execution with $exception exception" )
324
314
}
325
-
326
- methodInvocationBlock()
327
315
}
328
316
329
317
private fun shouldTestPassWithException (execution : UtExecution , exception : Throwable ): Boolean {
@@ -1157,9 +1145,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1157
1145
constructorCall(* methodArguments.toTypedArray())
1158
1146
}
1159
1147
}
1160
- .onFailure { exception ->
1161
- processExecutionFailure(currentExecution, exception)
1162
- }
1148
+ .onFailure { exception -> processExecutionFailure(exception) }
1163
1149
}
1164
1150
1165
1151
/* *
@@ -1258,11 +1244,22 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1258
1244
val name = paramNames[executableId]?.get(index)
1259
1245
methodArguments + = variableConstructor.getOrCreateVariable(param, name)
1260
1246
}
1261
- fieldStateManager.rememberInitialEnvironmentState(modificationInfo)
1247
+
1248
+ if (requiresFieldStateAssertions()) {
1249
+ // we should generate field assertions only for successful tests
1250
+ // that does not break the current test execution after invocation of method under test
1251
+ fieldStateManager.rememberInitialEnvironmentState(modificationInfo)
1252
+ }
1253
+
1262
1254
recordActualResult()
1263
1255
generateResultAssertions()
1264
- fieldStateManager.rememberFinalEnvironmentState(modificationInfo)
1265
- generateFieldStateAssertions()
1256
+
1257
+ if (requiresFieldStateAssertions()) {
1258
+ // we should generate field assertions only for successful tests
1259
+ // that does not break the current test execution after invocation of method under test
1260
+ fieldStateManager.rememberFinalEnvironmentState(modificationInfo)
1261
+ generateFieldStateAssertions()
1262
+ }
1266
1263
}
1267
1264
1268
1265
if (statics.isNotEmpty()) {
@@ -1310,6 +1307,10 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1310
1307
}
1311
1308
}
1312
1309
1310
+ private fun requiresFieldStateAssertions (): Boolean =
1311
+ ! methodType.isThrowing ||
1312
+ (methodType == PASSED_EXCEPTION && ! testFrameworkManager.isExpectedExceptionExecutionBreaking)
1313
+
1313
1314
private val expectedResultVarName = " expectedResult"
1314
1315
private val expectedErrorVarName = " expectedError"
1315
1316
@@ -1337,7 +1338,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1337
1338
substituteStaticFields(statics, isParametrized = true )
1338
1339
1339
1340
// build this instance
1340
- thisInstance = genericExecution.stateBefore.thisInstance?.let {
1341
+ thisInstance =
1342
+ genericExecution.stateBefore.thisInstance?.let {
1341
1343
variableConstructor.getOrCreateVariable(it)
1342
1344
}
1343
1345
@@ -1553,6 +1555,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1553
1555
private fun <R > withTestMethodScope (execution : UtExecution , block : () -> R ): R {
1554
1556
clearTestMethodScope()
1555
1557
currentExecution = execution
1558
+ determineExecutionType()
1556
1559
statesCache = EnvironmentFieldStateCache .emptyCacheFor(execution)
1557
1560
return try {
1558
1561
block()
@@ -1619,6 +1622,27 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1619
1622
testSet.executions.any { it.result is UtExecutionFailure }
1620
1623
1621
1624
1625
+ /* *
1626
+ * Determines [CgTestMethodType] for current execution according to its success or failure.
1627
+ */
1628
+ private fun determineExecutionType () {
1629
+ val currentExecution = currentExecution!!
1630
+
1631
+ currentExecution.result
1632
+ .onSuccess { methodType = SUCCESSFUL }
1633
+ .onFailure { exception ->
1634
+ methodType = when {
1635
+ shouldTestPassWithException(currentExecution, exception) -> PASSED_EXCEPTION
1636
+ shouldTestPassWithTimeoutException(currentExecution, exception) -> TIMEOUT
1637
+ else -> when (exception) {
1638
+ is ConcreteExecutionFailureException -> CRASH
1639
+ is AccessControlException -> CRASH // exception from sandbox
1640
+ else -> FAILING
1641
+ }
1642
+ }
1643
+ }
1644
+ }
1645
+
1622
1646
private fun testMethod (
1623
1647
methodName : String ,
1624
1648
displayName : String? ,
0 commit comments