Skip to content

Commit f62acae

Browse files
Assert nullable executions in parametrized tests with special approach (#290)
1 parent 9f6d83f commit f62acae

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.utbot.framework.codegen.ForceStaticMocking
77
import org.utbot.framework.codegen.JUNIT5_PARAMETERIZED_PACKAGE
88
import org.utbot.framework.codegen.Junit4
99
import org.utbot.framework.codegen.Junit5
10+
import org.utbot.framework.codegen.ParametrizedTestSource
1011
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour.PASS
1112
import org.utbot.framework.codegen.TestNg
1213
import org.utbot.framework.codegen.model.constructor.builtin.closeMethodIdOrNull
@@ -996,7 +997,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
996997
"but `${resultModel::class}` found"
997998
}
998999

999-
generateDeepEqualsAssertion(expected, actual)
1000+
generateDeepEqualsOrNullAssertion(expected, actual)
10001001
}
10011002
}
10021003
}
@@ -1028,16 +1029,41 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
10281029
return
10291030
}
10301031

1031-
generateDeepEqualsAssertion(expected, actual)
1032+
generateDeepEqualsOrNullAssertion(expected, actual)
10321033
}
10331034
}
10341035
}
10351036
}
10361037

1037-
private fun generateDeepEqualsAssertion(
1038+
/**
1039+
* We can't use standard deepEquals method in parametrized tests
1040+
* because nullable objects require different asserts.
1041+
* See https://github.com/UnitTestBot/UTBotJava/issues/252 for more details.
1042+
*/
1043+
private fun generateDeepEqualsOrNullAssertion(
10381044
expected: CgValue,
1039-
actual: CgVariable
1045+
actual: CgVariable,
10401046
) {
1047+
when (parameterizedTestSource) {
1048+
ParametrizedTestSource.DO_NOT_PARAMETRIZE ->
1049+
currentBlock = currentBlock.addAll(generateDeepEqualsAssertion(expected, actual))
1050+
ParametrizedTestSource.PARAMETRIZE -> {
1051+
val assertNullStmt = listOf(testFrameworkManager.assertions[testFramework.assertNull](actual).toStatement())
1052+
currentBlock = currentBlock.add(
1053+
CgIfStatement(
1054+
CgEqualTo(expected, nullLiteral()),
1055+
assertNullStmt,
1056+
generateDeepEqualsAssertion(expected, actual)
1057+
)
1058+
)
1059+
}
1060+
}
1061+
}
1062+
1063+
private fun generateDeepEqualsAssertion(
1064+
expected: CgValue,
1065+
actual: CgVariable,
1066+
): List<CgStatement> {
10411067
require(expected is CgVariable) {
10421068
"Expected value have to be Literal or Variable but `${expected::class}` found"
10431069
}
@@ -1051,7 +1077,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
10511077
depth = 0,
10521078
visitedModels = hashSetOf()
10531079
)
1054-
currentBlock = currentBlock.addAll(statements.dropLastWhile { it is CgEmptyLine })
1080+
1081+
return statements.dropLastWhile { it is CgEmptyLine }
10551082
}
10561083

10571084
private fun recordActualResult() {
@@ -1166,7 +1193,9 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
11661193
}
11671194

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

11721201
return withTestMethodScope(arbitraryExecution) {
@@ -1217,7 +1246,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12171246
//record result and generate result assertions
12181247
recordActualResult()
12191248
generateAssertionsForParameterizedTest()
1220-
12211249
}
12221250

12231251
methodType = PARAMETRIZED

0 commit comments

Comments
 (0)