@@ -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
@@ -269,15 +265,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
269265 assertEquality(expected, actual)
270266 }
271267 }
272- .onFailure { exception ->
273- processExecutionFailure(currentExecution, exception)
274- }
268+ .onFailure { exception -> processExecutionFailure(exception) }
275269 }
276270 else -> {} // TODO: check this specific case
277271 }
278272 }
279273
280- private fun processExecutionFailure (execution : UtExecution , exception : Throwable ) {
274+ private fun processExecutionFailure (exception : Throwable ) {
281275 val methodInvocationBlock = {
282276 with (currentExecutable) {
283277 when (this ) {
@@ -288,42 +282,36 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
288282 }
289283 }
290284
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+ }
304291 }
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+ }
314297 }
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" )
319308 }
320- else -> {
321- methodType = FAILING
309+ FAILING -> {
322310 writeWarningAboutFailureTest(exception)
311+ methodInvocationBlock()
323312 }
313+ PARAMETRIZED -> error(" Unexpected $PARAMETRIZED method type for failing execution with $exception exception" )
324314 }
325-
326- methodInvocationBlock()
327315 }
328316
329317 private fun shouldTestPassWithException (execution : UtExecution , exception : Throwable ): Boolean {
@@ -1157,9 +1145,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
11571145 constructorCall(* methodArguments.toTypedArray())
11581146 }
11591147 }
1160- .onFailure { exception ->
1161- processExecutionFailure(currentExecution, exception)
1162- }
1148+ .onFailure { exception -> processExecutionFailure(exception) }
11631149 }
11641150
11651151 /* *
@@ -1258,11 +1244,22 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12581244 val name = paramNames[executableId]?.get(index)
12591245 methodArguments + = variableConstructor.getOrCreateVariable(param, name)
12601246 }
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+
12621254 recordActualResult()
12631255 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+ }
12661263 }
12671264
12681265 if (statics.isNotEmpty()) {
@@ -1310,6 +1307,10 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13101307 }
13111308 }
13121309
1310+ private fun requiresFieldStateAssertions (): Boolean =
1311+ ! methodType.isThrowing ||
1312+ (methodType == PASSED_EXCEPTION && ! testFrameworkManager.isExpectedExceptionExecutionBreaking)
1313+
13131314 private val expectedResultVarName = " expectedResult"
13141315 private val expectedErrorVarName = " expectedError"
13151316
@@ -1337,7 +1338,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13371338 substituteStaticFields(statics, isParametrized = true )
13381339
13391340 // build this instance
1340- thisInstance = genericExecution.stateBefore.thisInstance?.let {
1341+ thisInstance =
1342+ genericExecution.stateBefore.thisInstance?.let {
13411343 variableConstructor.getOrCreateVariable(it)
13421344 }
13431345
@@ -1553,6 +1555,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
15531555 private fun <R > withTestMethodScope (execution : UtExecution , block : () -> R ): R {
15541556 clearTestMethodScope()
15551557 currentExecution = execution
1558+ determineExecutionType()
15561559 statesCache = EnvironmentFieldStateCache .emptyCacheFor(execution)
15571560 return try {
15581561 block()
@@ -1619,6 +1622,27 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
16191622 testSet.executions.any { it.result is UtExecutionFailure }
16201623
16211624
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+
16221646 private fun testMethod (
16231647 methodName : String ,
16241648 displayName : String? ,
0 commit comments