@@ -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
@@ -305,15 +301,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
305
301
assertEquality(expected, actual)
306
302
}
307
303
}
308
- .onFailure { exception ->
309
- processExecutionFailure(currentExecution, exception)
310
- }
304
+ .onFailure { exception -> processExecutionFailure(exception) }
311
305
}
312
306
else -> {} // TODO: check this specific case
313
307
}
314
308
}
315
309
316
- private fun processExecutionFailure (execution : UtExecution , exception : Throwable ) {
310
+ private fun processExecutionFailure (exception : Throwable ) {
317
311
val methodInvocationBlock = {
318
312
with (currentExecutable) {
319
313
when (this ) {
@@ -324,42 +318,36 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
324
318
}
325
319
}
326
320
327
- if (shouldTestPassWithException(execution, exception)) {
328
- testFrameworkManager.expectException(exception::class .id) {
329
- methodInvocationBlock()
330
- }
331
- methodType = SUCCESSFUL
332
-
333
- return
334
- }
335
-
336
- if (shouldTestPassWithTimeoutException(execution, exception)) {
337
- writeWarningAboutTimeoutExceeding()
338
- testFrameworkManager.expectTimeout(hangingTestsTimeout.timeoutMs) {
339
- methodInvocationBlock()
321
+ when (methodType) {
322
+ SUCCESSFUL -> error(" Unexpected successful without exception method type for execution with exception $exception " )
323
+ PASSED_EXCEPTION -> {
324
+ testFrameworkManager.expectException(exception::class .id) {
325
+ methodInvocationBlock()
326
+ }
340
327
}
341
- methodType = TIMEOUT
342
-
343
- return
344
- }
345
-
346
- when (exception) {
347
- is ConcreteExecutionFailureException -> {
348
- methodType = CRASH
349
- writeWarningAboutCrash()
328
+ TIMEOUT -> {
329
+ writeWarningAboutTimeoutExceeding()
330
+ testFrameworkManager.expectTimeout(hangingTestsTimeout.timeoutMs) {
331
+ methodInvocationBlock()
332
+ }
350
333
}
351
- is AccessControlException -> {
352
- methodType = CRASH
353
- writeWarningAboutFailureTest(exception)
354
- return
334
+ CRASH -> when (exception) {
335
+ is ConcreteExecutionFailureException -> {
336
+ writeWarningAboutCrash()
337
+ methodInvocationBlock()
338
+ }
339
+ is AccessControlException -> {
340
+ // exception from sandbox
341
+ writeWarningAboutFailureTest(exception)
342
+ }
343
+ else -> error(" Unexpected crash suite for failing execution with $exception exception" )
355
344
}
356
- else -> {
357
- methodType = FAILING
345
+ FAILING -> {
358
346
writeWarningAboutFailureTest(exception)
347
+ methodInvocationBlock()
359
348
}
349
+ PARAMETRIZED -> error(" Unexpected $PARAMETRIZED method type for failing execution with $exception exception" )
360
350
}
361
-
362
- methodInvocationBlock()
363
351
}
364
352
365
353
private fun shouldTestPassWithException (execution : UtExecution , exception : Throwable ): Boolean {
@@ -1187,9 +1175,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1187
1175
constructorCall(* methodArguments.toTypedArray())
1188
1176
}
1189
1177
}
1190
- .onFailure { exception ->
1191
- processExecutionFailure(currentExecution, exception)
1192
- }
1178
+ .onFailure { exception -> processExecutionFailure(exception) }
1193
1179
}
1194
1180
1195
1181
/* *
@@ -1288,11 +1274,22 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1288
1274
val name = paramNames[executableId]?.get(index)
1289
1275
methodArguments + = variableConstructor.getOrCreateVariable(param, name)
1290
1276
}
1291
- fieldStateManager.rememberInitialEnvironmentState(modificationInfo)
1277
+
1278
+ if (requiresFieldStateAssertions()) {
1279
+ // we should generate field assertions only for successful tests
1280
+ // that does not break the current test execution after invocation of method under test
1281
+ fieldStateManager.rememberInitialEnvironmentState(modificationInfo)
1282
+ }
1283
+
1292
1284
recordActualResult()
1293
1285
generateResultAssertions()
1294
- fieldStateManager.rememberFinalEnvironmentState(modificationInfo)
1295
- generateFieldStateAssertions()
1286
+
1287
+ if (requiresFieldStateAssertions()) {
1288
+ // we should generate field assertions only for successful tests
1289
+ // that does not break the current test execution after invocation of method under test
1290
+ fieldStateManager.rememberFinalEnvironmentState(modificationInfo)
1291
+ generateFieldStateAssertions()
1292
+ }
1296
1293
}
1297
1294
1298
1295
if (statics.isNotEmpty()) {
@@ -1340,6 +1337,10 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1340
1337
}
1341
1338
}
1342
1339
1340
+ private fun requiresFieldStateAssertions (): Boolean =
1341
+ ! methodType.isThrowing ||
1342
+ (methodType == PASSED_EXCEPTION && ! testFrameworkManager.isExpectedExceptionExecutionBreaking)
1343
+
1343
1344
private val expectedResultVarName = " expectedResult"
1344
1345
private val expectedErrorVarName = " expectedError"
1345
1346
@@ -1367,7 +1368,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1367
1368
substituteStaticFields(statics, isParametrized = true )
1368
1369
1369
1370
// build this instance
1370
- thisInstance = genericExecution.stateBefore.thisInstance?.let {
1371
+ thisInstance =
1372
+ genericExecution.stateBefore.thisInstance?.let {
1371
1373
variableConstructor.getOrCreateVariable(it)
1372
1374
}
1373
1375
@@ -1583,6 +1585,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1583
1585
private fun <R > withTestMethodScope (execution : UtExecution , block : () -> R ): R {
1584
1586
clearTestMethodScope()
1585
1587
currentExecution = execution
1588
+ determineExecutionType()
1586
1589
statesCache = EnvironmentFieldStateCache .emptyCacheFor(execution)
1587
1590
return try {
1588
1591
block()
@@ -1649,6 +1652,27 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1649
1652
testSet.executions.any { it.result is UtExecutionFailure }
1650
1653
1651
1654
1655
+ /* *
1656
+ * Determines [CgTestMethodType] for current execution according to its success or failure.
1657
+ */
1658
+ private fun determineExecutionType () {
1659
+ val currentExecution = currentExecution!!
1660
+
1661
+ currentExecution.result
1662
+ .onSuccess { methodType = SUCCESSFUL }
1663
+ .onFailure { exception ->
1664
+ methodType = when {
1665
+ shouldTestPassWithException(currentExecution, exception) -> PASSED_EXCEPTION
1666
+ shouldTestPassWithTimeoutException(currentExecution, exception) -> TIMEOUT
1667
+ else -> when (exception) {
1668
+ is ConcreteExecutionFailureException -> CRASH
1669
+ is AccessControlException -> CRASH // exception from sandbox
1670
+ else -> FAILING
1671
+ }
1672
+ }
1673
+ }
1674
+ }
1675
+
1652
1676
private fun testMethod (
1653
1677
methodName : String ,
1654
1678
displayName : String? ,
0 commit comments