Skip to content

Commit 9061641

Browse files
Egor KulikovEgorkaKulikov
Egor Kulikov
authored andcommitted
Remove unnecessary initial variables
1 parent b239b2d commit 9061641

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.utbot.framework.codegen.model.constructor.util.FieldStateCache
1111
import org.utbot.framework.codegen.model.constructor.util.classCgClassId
1212
import org.utbot.framework.codegen.model.constructor.util.getFieldVariableName
1313
import org.utbot.framework.codegen.model.constructor.util.getStaticFieldVariableName
14+
import org.utbot.framework.codegen.model.constructor.util.needExpectedDeclaration
1415
import org.utbot.framework.codegen.model.tree.CgExpression
1516
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
1617
import org.utbot.framework.codegen.model.tree.CgValue
@@ -118,15 +119,17 @@ internal class CgFieldStateManagerImpl(val context: CgContext)
118119
* `assertEquals(5, finalState);`
119120
*/
120121
private fun saveFieldsState(
121-
owner: CgValue,
122-
modifiedFields: ModifiedFields,
123-
cache: FieldStateCache,
124-
state: FieldState
122+
owner: CgValue,
123+
modifiedFields: ModifiedFields,
124+
cache: FieldStateCache,
125+
state: FieldState
125126
) {
126127
if (modifiedFields.isEmpty()) return
127128
emptyLineIfNeeded()
128129
val fields = when (state) {
129-
FieldState.INITIAL -> modifiedFields.filter { it.path.elements.isNotEmpty() && it.path.fieldType.isRefType }
130+
FieldState.INITIAL -> modifiedFields
131+
.filter { it.path.elements.isNotEmpty() && it.path.fieldType.isRefType }
132+
.filter { needExpectedDeclaration(it.after) }
130133
FieldState.FINAL -> modifiedFields
131134
}
132135
for ((path, before, after) in fields) {

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.utbot.framework.codegen.model.constructor.tree
22

33
import org.utbot.common.PathUtil
4-
import org.utbot.common.WorkaroundReason.*
54
import org.utbot.common.packageName
65
import org.utbot.engine.isStatic
76
import org.utbot.framework.codegen.ForceStaticMocking
@@ -24,12 +23,14 @@ import org.utbot.framework.codegen.model.constructor.util.CgStatementConstructor
2423
import org.utbot.framework.codegen.model.constructor.util.EnvironmentFieldStateCache
2524
import org.utbot.framework.codegen.model.constructor.util.FieldStateCache
2625
import org.utbot.framework.codegen.model.constructor.util.classCgClassId
26+
import org.utbot.framework.codegen.model.constructor.util.needExpectedDeclaration
2727
import org.utbot.framework.codegen.model.constructor.util.overridesEquals
2828
import org.utbot.framework.codegen.model.constructor.util.typeCast
2929
import org.utbot.framework.codegen.model.tree.CgAllocateArray
3030
import org.utbot.framework.codegen.model.tree.CgAnnotation
3131
import org.utbot.framework.codegen.model.tree.CgArrayElementAccess
3232
import org.utbot.framework.codegen.model.tree.CgAssignment
33+
import org.utbot.framework.codegen.model.tree.CgClassId
3334
import org.utbot.framework.codegen.model.tree.CgConstructorCall
3435
import org.utbot.framework.codegen.model.tree.CgDeclaration
3536
import org.utbot.framework.codegen.model.tree.CgDocPreTagStatement
@@ -95,6 +96,7 @@ import org.utbot.framework.plugin.api.ConstructorId
9596
import org.utbot.framework.plugin.api.FieldId
9697
import org.utbot.framework.plugin.api.MethodId
9798
import org.utbot.framework.plugin.api.TimeoutException
99+
import org.utbot.framework.plugin.api.TypeParameters
98100
import org.utbot.framework.plugin.api.UtArrayModel
99101
import org.utbot.framework.plugin.api.UtAssembleModel
100102
import org.utbot.framework.plugin.api.UtClassRefModel
@@ -151,11 +153,9 @@ import org.utbot.framework.plugin.api.util.shortWrapperClassId
151153
import org.utbot.framework.plugin.api.util.stringClassId
152154
import org.utbot.framework.plugin.api.util.voidClassId
153155
import org.utbot.framework.util.isUnit
154-
import org.utbot.framework.codegen.model.tree.CgClassId
155-
import org.utbot.framework.plugin.api.TypeParameters
156+
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
156157
import java.lang.reflect.InvocationTargetException
157158
import kotlin.reflect.jvm.javaType
158-
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
159159

160160
private const val DEEP_EQUALS_MAX_DEPTH = 5 // TODO move it to plugin settings?
161161

@@ -499,7 +499,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
499499

500500
var expected = expected
501501
if (expected == null) {
502-
require(!expectedDeclarationIsNeeded(expectedModel))
502+
require(!needExpectedDeclaration(expectedModel))
503503
expected = actual
504504
}
505505

@@ -852,7 +852,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
852852
val fieldName = fieldId.name
853853
var expectedVariable: CgVariable? = null
854854

855-
if (expectedDeclarationIsNeeded(fieldModel)) {
855+
if (needExpectedDeclaration(fieldModel)) {
856856
val expectedFieldDeclaration = createDeclarationForFieldFromVariable(fieldId, expected, fieldName)
857857

858858
statements += expectedFieldDeclaration
@@ -873,9 +873,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
873873
statements.addEmptyLineIfNeeded()
874874
}
875875

876-
private fun expectedDeclarationIsNeeded(fieldModel: UtModel) =
877-
!(fieldModel is UtNullModel || fieldModel is UtPrimitiveModel && fieldModel.value is Boolean)
878-
879876
@Suppress("UNUSED_ANONYMOUS_PARAMETER")
880877
private fun createDeclarationForFieldFromVariable(
881878
fieldId: FieldId,
@@ -1301,15 +1298,11 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13011298
emptyLineIfNeeded()
13021299
}
13031300

1304-
13051301
//create a block for current test case
13061302
parametersStatements += innerBlock(
13071303
{},
1308-
block(executionArgumentsBody) + createArgumentsCallRepresentation(
1309-
execIndex,
1310-
argListVariable,
1311-
arguments
1312-
)
1304+
block(executionArgumentsBody)
1305+
+ createArgumentsCallRepresentation(execIndex, argListVariable, arguments)
13131306
)
13141307
}
13151308
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.utbot.framework.codegen.model.constructor.util
2+
3+
import org.utbot.framework.plugin.api.UtModel
4+
import org.utbot.framework.plugin.api.UtNullModel
5+
import org.utbot.framework.plugin.api.UtPrimitiveModel
6+
7+
/**
8+
* Checks if an expected variable is needed,
9+
* or it will be further asserted with assertNull or assertTrue/False.
10+
*/
11+
fun needExpectedDeclaration(model: UtModel): Boolean {
12+
val representsNull = model is UtNullModel
13+
val representsBoolean = model is UtPrimitiveModel && model.value is Boolean
14+
return !(representsNull || representsBoolean)
15+
}

0 commit comments

Comments
 (0)