Skip to content

Commit 1415509

Browse files
authored
Add constructorId support in parametrized test generation & refactor name generator (#637)
1 parent 2c69326 commit 1415509

File tree

2 files changed

+58
-52
lines changed

2 files changed

+58
-52
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/name/CgNameGenerator.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,8 @@ internal class CgNameGeneratorImpl(private val context: CgContext)
8686
}
8787

8888
override fun testMethodNameFor(executableId: ExecutableId, customName: String?): String {
89-
val executableName = when (executableId) {
90-
is ConstructorId -> executableId.classId.prettifiedName // TODO: maybe we need some suffix e.g. "Ctor"?
91-
is MethodId -> executableId.name
92-
}
89+
val executableName = createExecutableName(executableId)
90+
9391
// no index suffix allowed only when there's a vacant custom name
9492
val name = if (customName != null && customName !in existingMethodNames) {
9593
customName
@@ -107,17 +105,15 @@ internal class CgNameGeneratorImpl(private val context: CgContext)
107105
dataProviderMethodName.replace(dataProviderMethodPrefix, "parameterizedTestsFor")
108106

109107
override fun dataProviderMethodNameFor(executableId: ExecutableId): String {
110-
val indexedName = nextIndexedMethodName(executableId.name.capitalize(), skipOne = true)
108+
val executableName = createExecutableName(executableId)
109+
val indexedName = nextIndexedMethodName(executableName.capitalize(), skipOne = true)
111110

112111
existingMethodNames += indexedName
113112
return "$dataProviderMethodPrefix$indexedName"
114113
}
115114

116115
override fun errorMethodNameFor(executableId: ExecutableId): String {
117-
val executableName = when (executableId) {
118-
is ConstructorId -> executableId.classId.prettifiedName
119-
is MethodId -> executableId.name
120-
}
116+
val executableName = createExecutableName(executableId)
121117
val newName = when (val base = "test${executableName.capitalize()}_errors") {
122118
!in existingMethodNames -> base
123119
else -> nextIndexedMethodName(base)
@@ -151,6 +147,13 @@ internal class CgNameGeneratorImpl(private val context: CgContext)
151147
if (baseName !in existingVariableNames) "`$baseName`" else nextIndexedVarName(baseName)
152148
}
153149
}
150+
151+
private fun createExecutableName(executableId: ExecutableId): String {
152+
return when (executableId) {
153+
is ConstructorId -> executableId.classId.prettifiedName // TODO: maybe we need some suffix e.g. "Ctor"?
154+
is MethodId -> executableId.name
155+
}
156+
}
154157
}
155158

156159
/**

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

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -282,30 +282,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
282282
*/
283283
private fun generateResultAssertions() {
284284
when (currentExecutable) {
285-
is ConstructorId -> {
286-
// we cannot generate any assertions for constructor testing
287-
// but we need to generate a constructor call
288-
val constructorCall = currentExecutable as ConstructorId
289-
val currentExecution = currentExecution!!
290-
currentExecution.result
291-
.onSuccess {
292-
methodType = SUCCESSFUL
293-
294-
// TODO engine returns UtCompositeModel sometimes (concrete execution?)
295-
296-
// TODO support inner classes constructors testing JIRA:1461
297-
require(!constructorCall.classId.isInner) {
298-
"Inner class ${constructorCall.classId} constructor testing is not supported yet"
299-
}
300-
301-
actual = newVar(constructorCall.classId, "actual") {
302-
constructorCall(*methodArguments.toTypedArray())
303-
}
304-
}
305-
.onFailure { exception ->
306-
processExecutionFailure(currentExecution, exception)
307-
}
308-
}
285+
is ConstructorId -> generateConstructorCall(currentExecutable!!, currentExecution!!)
309286
is BuiltinMethodId -> error("Unexpected BuiltinMethodId $currentExecutable while generating result assertions")
310287
is MethodId -> {
311288
emptyLineIfNeeded()
@@ -430,24 +407,30 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
430407
*/
431408
private fun generateAssertionsForParameterizedTest() {
432409
emptyLineIfNeeded()
433-
val method = currentExecutable as MethodId
434-
currentExecution!!.result
435-
.onSuccess { result ->
436-
if (result.isUnit()) {
437-
+thisInstance[method](*methodArguments.toTypedArray())
438-
} else {
439-
//"generic" expected variable is represented with a wrapper if
440-
//actual result is primitive to support cases with exceptions.
441-
resultModel = if (result is UtPrimitiveModel) assemble(result) else result
442410

443-
val expectedVariable = currentMethodParameters[CgParameterKind.ExpectedResult]!!
444-
val expectedExpression = CgNotNullAssertion(expectedVariable)
411+
when (currentExecutable) {
412+
is ConstructorId -> generateConstructorCall(currentExecutable!!, currentExecution!!)
413+
is MethodId -> {
414+
val method = currentExecutable as MethodId
415+
currentExecution!!.result
416+
.onSuccess { result ->
417+
if (result.isUnit()) {
418+
+thisInstance[method](*methodArguments.toTypedArray())
419+
} else {
420+
//"generic" expected variable is represented with a wrapper if
421+
//actual result is primitive to support cases with exceptions.
422+
resultModel = if (result is UtPrimitiveModel) assemble(result) else result
445423

446-
assertEquality(expectedExpression, actual)
447-
println()
448-
}
424+
val expectedVariable = currentMethodParameters[CgParameterKind.ExpectedResult]!!
425+
val expectedExpression = CgNotNullAssertion(expectedVariable)
426+
427+
assertEquality(expectedExpression, actual)
428+
println()
429+
}
430+
}
431+
.onFailure { thisInstance[method](*methodArguments.toTypedArray()).intercepted() }
449432
}
450-
.onFailure { thisInstance[method](*methodArguments.toTypedArray()).intercepted() }
433+
}
451434
}
452435

453436
/**
@@ -1020,6 +1003,27 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
10201003
generateDeepEqualsOrNullAssertion(expected.expression, actual)
10211004
}
10221005

1006+
private fun generateConstructorCall(currentExecutableId: ExecutableId, currentExecution: UtExecution) {
1007+
// we cannot generate any assertions for constructor testing
1008+
// but we need to generate a constructor call
1009+
val constructorCall = currentExecutableId as ConstructorId
1010+
currentExecution.result
1011+
.onSuccess {
1012+
methodType = SUCCESSFUL
1013+
1014+
require(!constructorCall.classId.isInner) {
1015+
"Inner class ${constructorCall.classId} constructor testing is not supported yet"
1016+
}
1017+
1018+
actual = newVar(constructorCall.classId, "actual") {
1019+
constructorCall(*methodArguments.toTypedArray())
1020+
}
1021+
}
1022+
.onFailure { exception ->
1023+
processExecutionFailure(currentExecution, exception)
1024+
}
1025+
}
1026+
10231027
/**
10241028
* We can't use standard deepEquals method in parametrized tests
10251029
* because nullable objects require different asserts.
@@ -1258,9 +1262,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12581262
currentMethodParameters[CgParameterKind.Argument(index)] = argument.parameter
12591263
}
12601264

1261-
val method = currentExecutable as MethodId
1262-
val containsFailureExecution = containsFailureExecution(testSet)
1263-
1265+
val method = currentExecutable!!
12641266
val expectedResultClassId = wrapTypeIfRequired(method.returnType)
12651267

12661268
if (expectedResultClassId != voidClassId) {
@@ -1278,6 +1280,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12781280
currentMethodParameters[CgParameterKind.ExpectedResult] = expectedResult.parameter
12791281
}
12801282

1283+
val containsFailureExecution = containsFailureExecution(testSet)
12811284
if (containsFailureExecution) {
12821285
val classClassId = Class::class.id
12831286
val expectedException = CgParameterDeclaration(
@@ -1343,7 +1346,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13431346
arguments += variableConstructor.getOrCreateVariable(paramModel, argumentName)
13441347
}
13451348

1346-
val method = currentExecutable as MethodId
1349+
val method = currentExecutable!!
13471350
val needsReturnValue = method.returnType != voidClassId
13481351
val containsFailureExecution = containsFailureExecution(testSet)
13491352
execution.result

0 commit comments

Comments
 (0)