@@ -58,11 +58,7 @@ import org.utbot.framework.codegen.model.tree.CgStatement
5858import org.utbot.framework.codegen.model.tree.CgStaticFieldAccess
5959import org.utbot.framework.codegen.model.tree.CgTestMethod
6060import 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.*
6662import org.utbot.framework.codegen.model.tree.CgTryCatch
6763import org.utbot.framework.codegen.model.tree.CgTypeCast
6864import org.utbot.framework.codegen.model.tree.CgValue
@@ -305,15 +301,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
305301 assertEquality(expected, actual)
306302 }
307303 }
308- .onFailure { exception ->
309- processExecutionFailure(currentExecution, exception)
310- }
304+ .onFailure { exception -> processExecutionFailure(exception) }
311305 }
312306 else -> {} // TODO: check this specific case
313307 }
314308 }
315309
316- private fun processExecutionFailure (execution : UtExecution , exception : Throwable ) {
310+ private fun processExecutionFailure (exception : Throwable ) {
317311 val methodInvocationBlock = {
318312 with (currentExecutable) {
319313 when (this ) {
@@ -324,42 +318,36 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
324318 }
325319 }
326320
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+ }
340327 }
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+ }
350333 }
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" )
355344 }
356- else -> {
357- methodType = FAILING
345+ FAILING -> {
358346 writeWarningAboutFailureTest(exception)
347+ methodInvocationBlock()
359348 }
349+ PARAMETRIZED -> error(" Unexpected $PARAMETRIZED method type for failing execution with $exception exception" )
360350 }
361-
362- methodInvocationBlock()
363351 }
364352
365353 private fun shouldTestPassWithException (execution : UtExecution , exception : Throwable ): Boolean {
@@ -1187,9 +1175,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
11871175 constructorCall(* methodArguments.toTypedArray())
11881176 }
11891177 }
1190- .onFailure { exception ->
1191- processExecutionFailure(currentExecution, exception)
1192- }
1178+ .onFailure { exception -> processExecutionFailure(exception) }
11931179 }
11941180
11951181 /* *
@@ -1288,11 +1274,22 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12881274 val name = paramNames[executableId]?.get(index)
12891275 methodArguments + = variableConstructor.getOrCreateVariable(param, name)
12901276 }
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+
12921284 recordActualResult()
12931285 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+ }
12961293 }
12971294
12981295 if (statics.isNotEmpty()) {
@@ -1340,6 +1337,10 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13401337 }
13411338 }
13421339
1340+ private fun requiresFieldStateAssertions (): Boolean =
1341+ ! methodType.isThrowing ||
1342+ (methodType == PASSED_EXCEPTION && ! testFrameworkManager.isExpectedExceptionExecutionBreaking)
1343+
13431344 private val expectedResultVarName = " expectedResult"
13441345 private val expectedErrorVarName = " expectedError"
13451346
@@ -1367,7 +1368,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13671368 substituteStaticFields(statics, isParametrized = true )
13681369
13691370 // build this instance
1370- thisInstance = genericExecution.stateBefore.thisInstance?.let {
1371+ thisInstance =
1372+ genericExecution.stateBefore.thisInstance?.let {
13711373 variableConstructor.getOrCreateVariable(it)
13721374 }
13731375
@@ -1583,6 +1585,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
15831585 private fun <R > withTestMethodScope (execution : UtExecution , block : () -> R ): R {
15841586 clearTestMethodScope()
15851587 currentExecution = execution
1588+ determineExecutionType()
15861589 statesCache = EnvironmentFieldStateCache .emptyCacheFor(execution)
15871590 return try {
15881591 block()
@@ -1649,6 +1652,27 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
16491652 testSet.executions.any { it.result is UtExecutionFailure }
16501653
16511654
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+
16521676 private fun testMethod (
16531677 methodName : String ,
16541678 displayName : String? ,
0 commit comments