Skip to content

Assert nullable executions in parametrized tests with special approach #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.utbot.framework.codegen.ForceStaticMocking
import org.utbot.framework.codegen.JUNIT5_PARAMETERIZED_PACKAGE
import org.utbot.framework.codegen.Junit4
import org.utbot.framework.codegen.Junit5
import org.utbot.framework.codegen.ParametrizedTestSource
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour.PASS
import org.utbot.framework.codegen.TestNg
import org.utbot.framework.codegen.model.constructor.builtin.closeMethodIdOrNull
Expand Down Expand Up @@ -996,7 +997,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
"but `${resultModel::class}` found"
}

generateDeepEqualsAssertion(expected, actual)
generateDeepEqualsOrNullAssertion(expected, actual)
}
}
}
Expand Down Expand Up @@ -1028,16 +1029,41 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
return
}

generateDeepEqualsAssertion(expected, actual)
generateDeepEqualsOrNullAssertion(expected, actual)
}
}
}
}

private fun generateDeepEqualsAssertion(
/**
* We can't use standard deepEquals method in parametrized tests
* because nullable objects require different asserts.
* See https://github.com/UnitTestBot/UTBotJava/issues/252 for more details.
*/
private fun generateDeepEqualsOrNullAssertion(
expected: CgValue,
actual: CgVariable
actual: CgVariable,
) {
when (parameterizedTestSource) {
ParametrizedTestSource.DO_NOT_PARAMETRIZE ->
currentBlock = currentBlock.addAll(generateDeepEqualsAssertion(expected, actual))
ParametrizedTestSource.PARAMETRIZE -> {
val assertNullStmt = listOf(testFrameworkManager.assertions[testFramework.assertNull](actual).toStatement())
currentBlock = currentBlock.add(
CgIfStatement(
CgEqualTo(expected, nullLiteral()),
assertNullStmt,
generateDeepEqualsAssertion(expected, actual)
)
)
}
}
}

private fun generateDeepEqualsAssertion(
expected: CgValue,
actual: CgVariable,
): List<CgStatement> {
require(expected is CgVariable) {
"Expected value have to be Literal or Variable but `${expected::class}` found"
}
Expand All @@ -1051,7 +1077,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
depth = 0,
visitedModels = hashSetOf()
)
currentBlock = currentBlock.addAll(statements.dropLastWhile { it is CgEmptyLine })

return statements.dropLastWhile { it is CgEmptyLine }
}

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

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

return withTestMethodScope(arbitraryExecution) {
Expand Down Expand Up @@ -1217,7 +1246,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
//record result and generate result assertions
recordActualResult()
generateAssertionsForParameterizedTest()

}

methodType = PARAMETRIZED
Expand Down