Skip to content

Commit 2e7f158

Browse files
committed
Add CgCustomAssertConstructor to CgComponents
1 parent ad943d1 commit 2e7f158

File tree

6 files changed

+64
-15
lines changed

6 files changed

+64
-15
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/language/CgLanguageAssistant.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import org.utbot.framework.codegen.services.access.CgCallableAccessManager
1111
import org.utbot.framework.codegen.services.access.CgCallableAccessManagerImpl
1212
import org.utbot.framework.codegen.services.access.CgFieldStateManager
1313
import org.utbot.framework.codegen.services.access.CgFieldStateManagerImpl
14+
import org.utbot.framework.codegen.tree.CgCustomAssertConstructor
1415
import org.utbot.framework.codegen.tree.CgMethodConstructor
16+
import org.utbot.framework.codegen.tree.CgSimpleCustomAssertConstructor
1517
import org.utbot.framework.codegen.tree.CgStatementConstructor
1618
import org.utbot.framework.codegen.tree.CgStatementConstructorImpl
1719
import org.utbot.framework.codegen.tree.CgVariableConstructor
@@ -47,6 +49,9 @@ interface CgLanguageAssistant {
4749
fun getVariableConstructorBy(context: CgContext): CgVariableConstructor
4850

4951
fun getMethodConstructorBy(context: CgContext): CgMethodConstructor
52+
53+
fun getCustomAssertConstructorBy(context: CgContext): CgCustomAssertConstructor
54+
5055
fun getCgFieldStateManager(context: CgContext): CgFieldStateManager
5156

5257
fun getLanguageTestFrameworkManager(): LanguageTestFrameworkManager
@@ -65,4 +70,7 @@ abstract class AbstractCgLanguageAssistant : CgLanguageAssistant {
6570

6671
override fun getMethodConstructorBy(context: CgContext): CgMethodConstructor = CgMethodConstructor(context)
6772
override fun getCgFieldStateManager(context: CgContext): CgFieldStateManager = CgFieldStateManagerImpl(context)
73+
74+
override fun getCustomAssertConstructorBy(context: CgContext): CgCustomAssertConstructor =
75+
CgSimpleCustomAssertConstructor(context)
6876
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgComponents.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ object CgComponents {
2222
mockFrameworkManagers.clear()
2323
variableConstructors.clear()
2424
methodConstructors.clear()
25+
customAssertConstructors.clear()
2526
}
2627

2728
private val nameGenerators: IdentityHashMap<CgContext, CgNameGenerator> = IdentityHashMap()
@@ -33,6 +34,7 @@ object CgComponents {
3334
private val statementConstructors: IdentityHashMap<CgContext, CgStatementConstructor> = IdentityHashMap()
3435
private val variableConstructors: IdentityHashMap<CgContext, CgVariableConstructor> = IdentityHashMap()
3536
private val methodConstructors: IdentityHashMap<CgContext, CgMethodConstructor> = IdentityHashMap()
37+
private val customAssertConstructors: IdentityHashMap<CgContext, CgCustomAssertConstructor> = IdentityHashMap()
3638

3739
fun getNameGeneratorBy(context: CgContext): CgNameGenerator = nameGenerators.getOrPut(context) {
3840
context.cgLanguageAssistant.getNameGeneratorBy(context)
@@ -62,4 +64,8 @@ object CgComponents {
6264
fun getMethodConstructorBy(context: CgContext): CgMethodConstructor = methodConstructors.getOrPut(context) {
6365
context.cgLanguageAssistant.getMethodConstructorBy(context)
6466
}
67+
68+
fun getCustomAssertConstructorBy(context: CgContext): CgCustomAssertConstructor = customAssertConstructors.getOrPut(context) {
69+
context.cgLanguageAssistant.getCustomAssertConstructorBy(context)
70+
}
6571
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.utbot.framework.codegen.tree
2+
3+
import org.utbot.framework.codegen.domain.context.CgContext
4+
import org.utbot.framework.codegen.domain.context.CgContextOwner
5+
import org.utbot.framework.codegen.domain.models.CgVariable
6+
import org.utbot.framework.plugin.api.UtCustomModel
7+
8+
interface CgCustomAssertConstructor {
9+
val context: CgContext
10+
fun tryConstructCustomAssert(expected: UtCustomModel, actual: CgVariable): Boolean
11+
}
12+
13+
class CgSimpleCustomAssertConstructor(
14+
override val context: CgContext
15+
) : CgCustomAssertConstructor,
16+
CgContextOwner by context {
17+
override fun tryConstructCustomAssert(expected: UtCustomModel, actual: CgVariable): Boolean =
18+
false
19+
}

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import org.utbot.framework.codegen.services.access.CgCallableAccessManager
5757
import org.utbot.framework.codegen.services.access.CgFieldStateManagerImpl
5858
import org.utbot.framework.codegen.services.framework.TestFrameworkManager
5959
import org.utbot.framework.codegen.tree.CgComponents.getCallableAccessManagerBy
60+
import org.utbot.framework.codegen.tree.CgComponents.getCustomAssertConstructorBy
6061
import org.utbot.framework.codegen.tree.CgComponents.getMockFrameworkManagerBy
6162
import org.utbot.framework.codegen.tree.CgComponents.getNameGeneratorBy
6263
import org.utbot.framework.codegen.tree.CgComponents.getStatementConstructorBy
@@ -175,6 +176,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
175176
protected val testFrameworkManager = getTestFrameworkManagerBy(context)
176177

177178
protected val variableConstructor = getVariableConstructorBy(context)
179+
private val customAssertConstructor = getCustomAssertConstructorBy(context)
178180
private val mockFrameworkManager = getMockFrameworkManagerBy(context)
179181

180182
private val floatDelta: Float = 1e-6f
@@ -323,10 +325,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
323325
+thisInstance[executable](*methodArguments.toTypedArray())
324326
} else {
325327
this.resultModel = resultModel
326-
327-
// TODO support custom way of rendering asserts when `resultModel` is `UtCustomModel`
328-
val expected = variableConstructor.getOrCreateVariable(resultModel, "expected")
329-
assertEquality(expected, actual)
328+
assertEquality(resultModel, actual)
330329
}
331330
}
332331
.onFailure { exception -> processExecutionFailure(exception, executionResult) }
@@ -572,9 +571,11 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
572571
}
573572

574573
if (afterModel !is UtReferenceModel) {
575-
val expectedAfter =
576-
variableConstructor.getOrCreateVariable(afterModel, "expected" + afterVariable.name.capitalize())
577-
assertEquality(expectedAfter, afterVariable)
574+
assertEquality(
575+
expected = afterModel,
576+
actual = afterVariable,
577+
expectedVariableName = "expected" + afterVariable.name.capitalize()
578+
)
578579
} else {
579580
if (beforeVariable != null)
580581
testFrameworkManager.assertBoolean(false, beforeVariable equalTo afterVariable)
@@ -1194,6 +1195,18 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
11941195
return ClassIdArrayInfo(classId, nestedElementClassId, dimensions)
11951196
}
11961197

1198+
fun assertEquality(
1199+
expected: UtModel,
1200+
actual: CgVariable,
1201+
expectedVariableName: String = "expected"
1202+
) {
1203+
if (expected !is UtCustomModel || !customAssertConstructor.tryConstructCustomAssert(expected, actual))
1204+
assertEquality(
1205+
variableConstructor.getOrCreateVariable(expected, expectedVariableName),
1206+
actual,
1207+
)
1208+
}
1209+
11971210
open fun assertEquality(expected: CgValue, actual: CgVariable) {
11981211
when {
11991212
expected.type.isArray -> {

utbot-js/src/main/kotlin/framework/codegen/model/constructor/tree/JsCgMethodConstructor.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ class JsCgMethodConstructor(ctx: CgContext) : CgMethodConstructor(ctx) {
6969
+thisInstance[method](*methodArguments.toTypedArray())
7070
} else {
7171
resultModel = result
72-
val expected = variableConstructor.getOrCreateVariable(result, "expected")
73-
assertEquality(expected, actual)
72+
assertEquality(result, actual)
7473
}
7574
}
7675
.onFailure { exception ->

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,21 @@ class PythonCgMethodConstructor(context: CgContext) : CgMethodConstructor(contex
171171
emptyLineIfNeeded()
172172
}
173173
stateAssertions.forEach { (index, it) ->
174-
val name = paramNames[executableId]?.get(index) + "_modified"
175-
val modifiedArgument = variableConstructor.getOrCreateVariable(it.second, name)
176-
assertEquality(modifiedArgument, it.first)
174+
assertEquality(
175+
expected = it.second,
176+
actual = it.first,
177+
expectedVariableName = paramNames[executableId]?.get(index) + "_modified"
178+
)
177179
}
178180
if (assertThisObject.isNotEmpty()) {
179181
emptyLineIfNeeded()
180182
}
181183
assertThisObject.forEach {
182-
val name = it.first.name + "_modified"
183-
val modifiedThisObject = variableConstructor.getOrCreateVariable(it.second, name)
184-
assertEquality(modifiedThisObject, it.first)
184+
assertEquality(
185+
expected = it.second,
186+
actual = it.first,
187+
expectedVariableName = it.first.name + "_modified"
188+
)
185189
}
186190
}
187191

0 commit comments

Comments
 (0)