From 8e30c4c36a32426cedec73bf899d58e170decea5 Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Tue, 3 Oct 2023 22:26:03 +0300 Subject: [PATCH 1/3] Use assertSame instead of deepEquals if actual is a mock --- .../org/utbot/framework/codegen/domain/Domain.kt | 2 ++ .../codegen/services/framework/TestFrameworkManager.kt | 6 ++++++ .../framework/codegen/tree/CgMethodConstructor.kt | 8 ++++++++ .../model/constructor/tree/JsTestFrameworkManager.kt | 4 ++++ .../constructor/tree/PythonTestFrameworkManager.kt | 10 +++++++++- 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt index d64f54c186..622c3eb534 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt @@ -190,6 +190,8 @@ abstract class TestFramework( open val testSuperClass: ClassId? = null + open val assertSame by lazy { assertionId("assertSame", objectClassId, objectClassId) } + open val assertEquals by lazy { assertionId("assertEquals", objectClassId, objectClassId) } val assertFloatEquals by lazy { assertionId("assertEquals", floatClassId, floatClassId, floatClassId) } diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/TestFrameworkManager.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/TestFrameworkManager.kt index 18c1068bdb..6294be3d2a 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/TestFrameworkManager.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/TestFrameworkManager.kt @@ -44,6 +44,8 @@ abstract class TestFrameworkManager(val context: CgContext) val assertions = context.testFramework.assertionsClass + val assertSame = context.testFramework.assertSame + val assertEquals = context.testFramework.assertEquals val assertFloatEquals = context.testFramework.assertFloatEquals val assertDoubleEquals = context.testFramework.assertDoubleEquals @@ -86,6 +88,10 @@ abstract class TestFrameworkManager(val context: CgContext) +assertions[assertEquals](expected, actual) } + open fun assertSame(expected: CgValue, actual: CgValue) { + +assertions[assertSame](expected, actual) + } + open fun assertFloatEquals(expected: CgExpression, actual: CgExpression, delta: Any) { +assertions[assertFloatEquals](expected, actual, delta) } diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt index 72d5a601e1..46e2362f8d 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt @@ -115,6 +115,7 @@ import org.utbot.framework.plugin.api.UtSymbolicExecution import org.utbot.framework.plugin.api.UtTaintAnalysisFailure import org.utbot.framework.plugin.api.UtTimeoutException import org.utbot.framework.plugin.api.UtVoidModel +import org.utbot.framework.plugin.api.isMockModel import org.utbot.framework.plugin.api.isNotNull import org.utbot.framework.plugin.api.isNull import org.utbot.framework.plugin.api.onFailure @@ -1302,6 +1303,13 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte } } else -> { + currentExecution!!.result.onSuccess { resultModel -> + if (resultModel.isMockModel()) { + testFrameworkManager.assertSame(expected, actual) + return + } + } + when (expected) { is CgLiteral -> testFrameworkManager.assertEquals(expected, actual) is CgNotNullAssertion -> generateForNotNullAssertion(expected, actual) diff --git a/utbot-js/src/main/kotlin/framework/codegen/model/constructor/tree/JsTestFrameworkManager.kt b/utbot-js/src/main/kotlin/framework/codegen/model/constructor/tree/JsTestFrameworkManager.kt index f7490fd439..13466f194e 100644 --- a/utbot-js/src/main/kotlin/framework/codegen/model/constructor/tree/JsTestFrameworkManager.kt +++ b/utbot-js/src/main/kotlin/framework/codegen/model/constructor/tree/JsTestFrameworkManager.kt @@ -51,6 +51,10 @@ class MochaManager(context: CgContext) : TestFrameworkManager(context) { +assertions[jsAssertEquals](expected, actual) } + override fun assertSame(expected: CgValue, actual: CgValue) { + error("assertSame does not exist in Mocha") + } + override fun disableTestMethod(reason: String) { } diff --git a/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonTestFrameworkManager.kt b/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonTestFrameworkManager.kt index 8726c9af13..cce442c13a 100644 --- a/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonTestFrameworkManager.kt +++ b/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonTestFrameworkManager.kt @@ -81,6 +81,10 @@ internal class PytestManager(context: CgContext) : TestFrameworkManager(context) ) } + override fun assertSame(expected: CgValue, actual: CgValue) { + error("assertSame does not exist in PyTest") + } + fun assertIsinstance(types: List, actual: CgVariable) { +CgPythonAssertEquals( CgPythonFunctionCall( @@ -102,12 +106,16 @@ internal class UnittestManager(context: CgContext) : TestFrameworkManager(contex override val isExpectedExceptionExecutionBreaking: Boolean = true override val dataProviderMethodsHolder: TestClassContext - get() = error("Parametrized tests are not supported for JavaScript") + get() = error("Parametrized tests are not supported in Unittest") override fun addAnnotationForNestedClasses() { error("Nested classes annotation does not exist in Unittest") } + override fun assertSame(expected: CgValue, actual: CgValue) { + error("assertSame does not exist in Unittest") + } + override fun expectException(exception: ClassId, block: () -> Unit) { require(testFramework is Unittest) { "According to settings, Unittest was expected, but got: $testFramework" } require(exception is PythonClassId) { "Exceptions must be PythonClassId" } From 662680850091edba4bf72bb29ce4f335474ff381 Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Tue, 3 Oct 2023 23:16:58 +0300 Subject: [PATCH 2/3] Use assertSame instead of deepEquals if expected is a mock --- .../framework/codegen/tree/CgMethodConstructor.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt index 46e2362f8d..eccfb45a6f 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt @@ -1226,7 +1226,12 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte if (!successfullyConstructedCustomAssert) { val expectedVariable = variableConstructor.getOrCreateVariable(expected, expectedVariableName) if (emptyLineIfNeeded) emptyLineIfNeeded() - assertEquality(expectedVariable, actual) + + if (expected.isMockModel()) { + testFrameworkManager.assertSame(expectedVariable, actual) + } else { + assertEquality(expectedVariable, actual) + } } } @@ -1303,13 +1308,6 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte } } else -> { - currentExecution!!.result.onSuccess { resultModel -> - if (resultModel.isMockModel()) { - testFrameworkManager.assertSame(expected, actual) - return - } - } - when (expected) { is CgLiteral -> testFrameworkManager.assertEquals(expected, actual) is CgNotNullAssertion -> generateForNotNullAssertion(expected, actual) From 883af21efc7154a050a81c9f9eca0de325cba4ef Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Wed, 4 Oct 2023 10:47:42 +0300 Subject: [PATCH 3/3] assertSame is used on each depth of deepEquals --- .../framework/codegen/tree/CgMethodConstructor.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt index eccfb45a6f..cc709f84ea 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt @@ -619,6 +619,11 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte visitedModels += modelWithField with(testFrameworkManager) { + if (expectedModel.isMockModel()) { + currentBlock += assertions[assertSame](expected, actual).toStatement() + return + } + if (depth >= DEEP_EQUALS_MAX_DEPTH) { currentBlock += CgSingleLineComment("Current deep equals depth exceeds max depth $DEEP_EQUALS_MAX_DEPTH") currentBlock += getDeepEqualsAssertion(expected, actual).toStatement() @@ -1226,12 +1231,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte if (!successfullyConstructedCustomAssert) { val expectedVariable = variableConstructor.getOrCreateVariable(expected, expectedVariableName) if (emptyLineIfNeeded) emptyLineIfNeeded() - - if (expected.isMockModel()) { - testFrameworkManager.assertSame(expectedVariable, actual) - } else { - assertEquality(expectedVariable, actual) - } + assertEquality(expectedVariable, actual) } }