Skip to content

Commit f76bf03

Browse files
sofurihafetamarinvs19
authored andcommitted
Add constructorId support in parametrized test generation & refactor name generator (#637)
1 parent 4f6db75 commit f76bf03

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)
@@ -152,6 +148,13 @@ internal class CgNameGeneratorImpl(private val context: CgContext)
152148
}
153149
CodegenLanguage.PYTHON -> nextIndexedMethodName(baseName)
154150
}
151+
152+
private fun createExecutableName(executableId: ExecutableId): String {
153+
return when (executableId) {
154+
is ConstructorId -> executableId.classId.prettifiedName // TODO: maybe we need some suffix e.g. "Ctor"?
155+
is MethodId -> executableId.name
156+
}
157+
}
155158
}
156159

157160
/**

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.
@@ -1259,9 +1263,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12591263
currentMethodParameters[CgParameterKind.Argument(index)] = argument.parameter
12601264
}
12611265

1262-
val method = currentExecutable as MethodId
1263-
val containsFailureExecution = containsFailureExecution(testSet)
1264-
1266+
val method = currentExecutable!!
12651267
val expectedResultClassId = wrapTypeIfRequired(method.returnType)
12661268

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

1284+
val containsFailureExecution = containsFailureExecution(testSet)
12821285
if (containsFailureExecution) {
12831286
val classClassId = Class::class.id
12841287
val expectedException = CgParameterDeclaration(
@@ -1344,7 +1347,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13441347
arguments += variableConstructor.getOrCreateVariable(paramModel, argumentName)
13451348
}
13461349

1347-
val method = currentExecutable as MethodId
1350+
val method = currentExecutable!!
13481351
val needsReturnValue = method.returnType != voidClassId
13491352
val containsFailureExecution = containsFailureExecution(testSet)
13501353
execution.result

0 commit comments

Comments
 (0)