Skip to content

Commit e81dcc2

Browse files
committed
Revert "Use one variable for expectedResult everywhere"
This reverts commit a4485ab.
1 parent a4485ab commit e81dcc2

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

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

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

290-
fun ClassId.wrapPrimitives(): ClassId = wrapperByPrimitive[this] ?: this
291-
292-
293290
// FieldId utils
294291

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

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ 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
159158
import org.utbot.framework.util.isUnit
160159
import org.utbot.summary.SummarySentenceConstants.TAB
161160
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
@@ -431,15 +430,22 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
431430
* Generates result assertions in parameterized tests for successful executions
432431
* and just runs the method if all executions are unsuccessful.
433432
*/
434-
private fun generateAssertionsForParameterizedTest(method: MethodId, expectedResult: CgVariable) {
433+
private fun generateAssertionsForParameterizedTest() {
435434
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+
443449
val actualVariableName = when (codegenLanguage) {
444450
CodegenLanguage.JAVA -> when (method.returnType) {
445451
intClassId -> "${intWrapperClassId.simpleName.capitalize()}.valueOf(actual)"
@@ -455,7 +461,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
455461
CodegenLanguage.KOTLIN -> "actual"
456462
}
457463

458-
assertEquality(expectedResult, CgVariable(actualVariableName, method.returnType))
464+
assertEquality(expected, CgVariable(actualVariableName, method.returnType))
459465
}
460466
}
461467
.onFailure { thisInstance[method](*methodArguments.toTypedArray()).intercepted() }
@@ -1248,19 +1254,15 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12481254
argument.name, argument.type, isReferenceType = !argument.type.isPrimitive
12491255
)
12501256
}
1251-
12521257
val method = currentExecutable as MethodId
12531258
val containsFailureExecution = containsFailureExecution(testSet)
12541259

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

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

12741276
//record result and generate result assertions
12751277
recordActualResult()
1276-
generateAssertionsForParameterizedTest(method, expectedResult)
1278+
generateAssertionsForParameterizedTest()
12771279
}
12781280

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

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+
15401554
/**
15411555
* A [ClassId] for Class<Throwable>.
15421556
*/

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ 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 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)
121+
val name = nameGenerator.variableName(type = classId, base = baseName, isMock = false)
122+
return if (isNotNull) CgNotNullVariable(name, classId) else CgVariable(name, classId)
124123
}
125124

126125
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 ClassId.wrapByObjectIfRequired(): ClassId
155+
fun wrapTypeIfRequired(baseType: ClassId): 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 ClassId.wrapByObjectIfRequired(): ClassId =
391-
if (this.isAccessibleFrom(testClassPackageName)) this else objectClassId
390+
override fun wrapTypeIfRequired(baseType: ClassId): ClassId =
391+
if (baseType.isAccessibleFrom(testClassPackageName)) baseType 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 = baseType.wrapByObjectIfRequired()
452+
val wrappedType = wrapTypeIfRequired(baseType)
453453
return ExpressionWithType(wrappedType, call)
454454
}
455455

0 commit comments

Comments
 (0)