Skip to content

Commit a4485ab

Browse files
committed
Use one variable for expectedResult everywhere
1 parent bf630ee commit a4485ab

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/IdUtil.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ fun ClassId.defaultValueModel(): UtModel = when (this) {
287287
else -> UtNullModel(this)
288288
}
289289

290+
fun ClassId.wrapPrimitives(): ClassId = wrapperByPrimitive[this] ?: this
291+
292+
290293
// FieldId utils
291294

292295
// TODO: maybe cache it somehow in the future

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

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ import org.utbot.framework.plugin.api.util.shortClassId
155155
import org.utbot.framework.plugin.api.util.shortWrapperClassId
156156
import org.utbot.framework.plugin.api.util.stringClassId
157157
import org.utbot.framework.plugin.api.util.voidClassId
158+
import org.utbot.framework.plugin.api.util.wrapPrimitives
158159
import org.utbot.framework.util.isUnit
159160
import org.utbot.summary.SummarySentenceConstants.TAB
160161
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
@@ -430,22 +431,15 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
430431
* Generates result assertions in parameterized tests for successful executions
431432
* and just runs the method if all executions are unsuccessful.
432433
*/
433-
private fun generateAssertionsForParameterizedTest() {
434+
private fun generateAssertionsForParameterizedTest(method: MethodId, expectedResult: CgVariable) {
434435
emptyLineIfNeeded()
435-
val method = currentExecutable as MethodId
436436
currentExecution!!.result
437437
.onSuccess { result ->
438438
if (result.isUnit()) {
439439
+thisInstance[method](*methodArguments.toTypedArray())
440440
} else {
441441
resultModel = result
442442

443-
val expected = variableConstructor.parameterizedVariable(
444-
result.classId,
445-
expectedResultVarName,
446-
isNotNull = true
447-
)
448-
449443
val actualVariableName = when (codegenLanguage) {
450444
CodegenLanguage.JAVA -> when (method.returnType) {
451445
intClassId -> "${intWrapperClassId.simpleName.capitalize()}.valueOf(actual)"
@@ -461,7 +455,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
461455
CodegenLanguage.KOTLIN -> "actual"
462456
}
463457

464-
assertEquality(expected, CgVariable(actualVariableName, method.returnType))
458+
assertEquality(expectedResult, CgVariable(actualVariableName, method.returnType))
465459
}
466460
}
467461
.onFailure { thisInstance[method](*methodArguments.toTypedArray()).intercepted() }
@@ -1254,15 +1248,19 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12541248
argument.name, argument.type, isReferenceType = !argument.type.isPrimitive
12551249
)
12561250
}
1251+
12571252
val method = currentExecutable as MethodId
12581253
val containsFailureExecution = containsFailureExecution(testSet)
12591254

1260-
val expectedResultClassId = wrapTypeIfRequired(method.returnType)
1255+
val expectedResult = variableConstructor.parameterizedVariable(
1256+
method.returnType.wrapPrimitives(),
1257+
expectedResultVarName,
1258+
)
12611259

1262-
if (expectedResultClassId != voidClassId) {
1260+
if (method.returnType != voidClassId) {
12631261
testArguments += CgParameterDeclaration(
1264-
expectedResultVarName, resultClassId(expectedResultClassId),
1265-
isReferenceType = containsFailureExecution || !expectedResultClassId.isPrimitive
1262+
expectedResult,
1263+
isReferenceType = containsFailureExecution || !expectedResult.type.isPrimitive,
12661264
)
12671265
}
12681266
if (containsFailureExecution) {
@@ -1275,7 +1273,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12751273

12761274
//record result and generate result assertions
12771275
recordActualResult()
1278-
generateAssertionsForParameterizedTest()
1276+
generateAssertionsForParameterizedTest(method, expectedResult)
12791277
}
12801278

12811279
methodType = PARAMETRIZED
@@ -1539,18 +1537,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
15391537
private fun containsFailureExecution(testSet: UtMethodTestSet) =
15401538
testSet.executions.any { it.result is UtExecutionFailure }
15411539

1542-
private fun resultClassId(returnType: ClassId): ClassId = when (returnType) {
1543-
booleanClassId -> booleanWrapperClassId
1544-
byteClassId -> byteWrapperClassId
1545-
charClassId -> charWrapperClassId
1546-
shortClassId -> shortWrapperClassId
1547-
intClassId -> intWrapperClassId
1548-
longClassId -> longWrapperClassId
1549-
floatClassId -> floatWrapperClassId
1550-
doubleClassId -> doubleWrapperClassId
1551-
else -> returnType
1552-
}
1553-
15541540
/**
15551541
* A [ClassId] for Class<Throwable>.
15561542
*/

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ internal class CgVariableConstructor(val context: CgContext) :
118118
* Creates general variable of type (to replace with concrete value in each test case).
119119
*/
120120
fun parameterizedVariable(classId: ClassId, baseName: String? = null, isNotNull: Boolean = false): CgVariable {
121-
val name = nameGenerator.variableName(type = classId, base = baseName, isMock = false)
122-
return if (isNotNull) CgNotNullVariable(name, classId) else CgVariable(name, classId)
121+
val returnType = classId.wrapByObjectIfRequired()
122+
val name = nameGenerator.variableName(type = returnType, base = baseName, isMock = false)
123+
return if (isNotNull) CgNotNullVariable(name, returnType) else CgVariable(name, returnType)
123124
}
124125

125126
private fun constructComposite(model: UtCompositeModel, baseName: String): CgVariable {

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/util/CgStatementConstructor.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ interface CgStatementConstructor {
152152

153153
fun guardExpression(baseType: ClassId, expression: CgExpression): ExpressionWithType
154154

155-
fun wrapTypeIfRequired(baseType: ClassId): ClassId
155+
fun ClassId.wrapByObjectIfRequired(): ClassId
156156
}
157157

158158
internal class CgStatementConstructorImpl(context: CgContext) :
@@ -387,8 +387,8 @@ internal class CgStatementConstructorImpl(context: CgContext) :
387387
updateVariableScope(it)
388388
}
389389

390-
override fun wrapTypeIfRequired(baseType: ClassId): ClassId =
391-
if (baseType.isAccessibleFrom(testClassPackageName)) baseType else objectClassId
390+
override fun ClassId.wrapByObjectIfRequired(): ClassId =
391+
if (this.isAccessibleFrom(testClassPackageName)) this else objectClassId
392392

393393
// utils
394394

@@ -449,7 +449,7 @@ internal class CgStatementConstructorImpl(context: CgContext) :
449449
if (call.executableId != mockMethodId) return guardExpression(baseType, call)
450450

451451
// call represents a call to mock() method
452-
val wrappedType = wrapTypeIfRequired(baseType)
452+
val wrappedType = baseType.wrapByObjectIfRequired()
453453
return ExpressionWithType(wrappedType, call)
454454
}
455455

0 commit comments

Comments
 (0)