Skip to content

Commit f249d28

Browse files
sofurihafedenis-fokin
authored andcommitted
Add constructorId support in parametrized test generation & refactor name generator (#637)
1 parent 9737f4e commit f249d28

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
@@ -284,30 +284,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
284284
*/
285285
private fun generateResultAssertions() {
286286
when (currentExecutable) {
287-
is ConstructorId -> {
288-
// we cannot generate any assertions for constructor testing
289-
// but we need to generate a constructor call
290-
val constructorCall = currentExecutable as ConstructorId
291-
val currentExecution = currentExecution!!
292-
currentExecution.result
293-
.onSuccess {
294-
methodType = SUCCESSFUL
295-
296-
// TODO engine returns UtCompositeModel sometimes (concrete execution?)
297-
298-
// TODO support inner classes constructors testing JIRA:1461
299-
require(!constructorCall.classId.isInner) {
300-
"Inner class ${constructorCall.classId} constructor testing is not supported yet"
301-
}
302-
303-
actual = newVar(constructorCall.classId, "actual") {
304-
constructorCall(*methodArguments.toTypedArray())
305-
}
306-
}
307-
.onFailure { exception ->
308-
processExecutionFailure(currentExecution, exception)
309-
}
310-
}
287+
is ConstructorId -> generateConstructorCall(currentExecutable!!, currentExecution!!)
311288
is BuiltinMethodId -> error("Unexpected BuiltinMethodId $currentExecutable while generating result assertions")
312289
is MethodId -> {
313290
emptyLineIfNeeded()
@@ -434,24 +411,30 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
434411
*/
435412
private fun generateAssertionsForParameterizedTest() {
436413
emptyLineIfNeeded()
437-
val method = currentExecutable as MethodId
438-
currentExecution!!.result
439-
.onSuccess { result ->
440-
if (result.isUnit()) {
441-
+thisInstance[method](*methodArguments.toTypedArray())
442-
} else {
443-
//"generic" expected variable is represented with a wrapper if
444-
//actual result is primitive to support cases with exceptions.
445-
resultModel = if (result is UtPrimitiveModel) assemble(result) else result
446414

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

450-
assertEquality(expectedExpression, actual)
451-
println()
452-
}
428+
val expectedVariable = currentMethodParameters[CgParameterKind.ExpectedResult]!!
429+
val expectedExpression = CgNotNullAssertion(expectedVariable)
430+
431+
assertEquality(expectedExpression, actual)
432+
println()
433+
}
434+
}
435+
.onFailure { thisInstance[method](*methodArguments.toTypedArray()).intercepted() }
453436
}
454-
.onFailure { thisInstance[method](*methodArguments.toTypedArray()).intercepted() }
437+
}
455438
}
456439

457440
/**
@@ -1024,6 +1007,27 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
10241007
generateDeepEqualsOrNullAssertion(expected.expression, actual)
10251008
}
10261009

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

1267-
val method = currentExecutable as MethodId
1268-
val containsFailureExecution = containsFailureExecution(testSet)
1269-
1271+
val method = currentExecutable!!
12701272
val expectedResultClassId = wrapTypeIfRequired(method.returnType)
12711273

12721274
if (expectedResultClassId != voidClassId) {
@@ -1284,6 +1286,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12841286
currentMethodParameters[CgParameterKind.ExpectedResult] = expectedResult.parameter
12851287
}
12861288

1289+
val containsFailureExecution = containsFailureExecution(testSet)
12871290
if (containsFailureExecution) {
12881291
val classClassId = Class::class.id
12891292
val expectedException = CgParameterDeclaration(
@@ -1349,7 +1352,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13491352
arguments += variableConstructor.getOrCreateVariable(paramModel, argumentName)
13501353
}
13511354

1352-
val method = currentExecutable as MethodId
1355+
val method = currentExecutable!!
13531356
val needsReturnValue = method.returnType != voidClassId
13541357
val containsFailureExecution = containsFailureExecution(testSet)
13551358
execution.result

0 commit comments

Comments
 (0)