Skip to content

Commit 7b9bca0

Browse files
committed
Assert nullable executions in parametrized tests with special approach
1 parent d202023 commit 7b9bca0

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ package org.utbot.framework.codegen.model.constructor.tree
33
import org.utbot.common.PathUtil
44
import org.utbot.common.packageName
55
import org.utbot.engine.isStatic
6-
import org.utbot.framework.codegen.ForceStaticMocking
7-
import org.utbot.framework.codegen.JUNIT5_PARAMETERIZED_PACKAGE
8-
import org.utbot.framework.codegen.Junit4
9-
import org.utbot.framework.codegen.Junit5
6+
import org.utbot.framework.codegen.*
107
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour.PASS
11-
import org.utbot.framework.codegen.TestNg
128
import org.utbot.framework.codegen.model.constructor.builtin.closeMethodIdOrNull
139
import org.utbot.framework.codegen.model.constructor.builtin.forName
1410
import org.utbot.framework.codegen.model.constructor.builtin.getClass
@@ -996,7 +992,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
996992
"but `${resultModel::class}` found"
997993
}
998994

999-
generateDeepEqualsAssertion(expected, actual)
995+
generateDeepEqualsOrNullAssertion(expected, actual)
1000996
}
1001997
}
1002998
}
@@ -1028,16 +1024,40 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
10281024
return
10291025
}
10301026

1031-
generateDeepEqualsAssertion(expected, actual)
1027+
generateDeepEqualsOrNullAssertion(expected, actual)
10321028
}
10331029
}
10341030
}
10351031
}
10361032

1037-
private fun generateDeepEqualsAssertion(
1033+
/**
1034+
* We can't use standard deepEquals method in parametrized tests
1035+
* because nullable objects require different asserts.
1036+
* See https://github.com/UnitTestBot/UTBotJava/issues/252 for more details.
1037+
*/
1038+
private fun generateDeepEqualsOrNullAssertion(
10381039
expected: CgValue,
1039-
actual: CgVariable
1040+
actual: CgVariable,
10401041
) {
1042+
when (parameterizedTestSource) {
1043+
ParametrizedTestSource.DO_NOT_PARAMETRIZE ->
1044+
currentBlock = currentBlock.addAll(generateDeepEqualsAssertion(expected, actual))
1045+
ParametrizedTestSource.PARAMETRIZE -> {
1046+
val trueStmts = listOf<CgStatement>(
1047+
testFrameworkManager.assertions[testFramework.assertNull](actual).toStatement()
1048+
)
1049+
val falseStmts = generateDeepEqualsAssertion(expected, actual)
1050+
currentBlock = currentBlock.add(
1051+
CgIfStatement(CgEqualTo(expected, nullLiteral()), trueStmts, falseStmts)
1052+
)
1053+
}
1054+
}
1055+
}
1056+
1057+
private fun generateDeepEqualsAssertion(
1058+
expected: CgValue,
1059+
actual: CgVariable,
1060+
): List<CgStatement> {
10411061
require(expected is CgVariable) {
10421062
"Expected value have to be Literal or Variable but `${expected::class}` found"
10431063
}
@@ -1051,7 +1071,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
10511071
depth = 0,
10521072
visitedModels = hashSetOf()
10531073
)
1054-
currentBlock = currentBlock.addAll(statements.dropLastWhile { it is CgEmptyLine })
1074+
1075+
return statements.dropLastWhile { it is CgEmptyLine }
10551076
}
10561077

10571078
private fun recordActualResult() {
@@ -1166,7 +1187,9 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
11661187
}
11671188

11681189
//TODO: orientation on arbitrary execution may be misleading, but what is the alternative?
1169-
val arbitraryExecution = utTestCase.executions.firstOrNull { it.result is UtExecutionSuccess }
1190+
//may be a heuristic to select a model with minimal number of internal nulls should be used
1191+
val arbitraryExecution = utTestCase.executions
1192+
.firstOrNull { it.result is UtExecutionSuccess && (it.result as UtExecutionSuccess).model !is UtNullModel }
11701193
?: utTestCase.executions.first()
11711194

11721195
return withTestMethodScope(arbitraryExecution) {
@@ -1217,7 +1240,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12171240
//record result and generate result assertions
12181241
recordActualResult()
12191242
generateAssertionsForParameterizedTest()
1220-
12211243
}
12221244

12231245
methodType = PARAMETRIZED

0 commit comments

Comments
 (0)