From a53d1795f4326ff1c6c0586050c2bcf0f9cdaeab Mon Sep 17 00:00:00 2001 From: Kamenev Yury Date: Tue, 4 Oct 2022 15:48:36 +0800 Subject: [PATCH 1/2] Fixed error with unavailable mock classes from mock framework --- .../model/constructor/tree/CgMethodConstructor.kt | 15 +++++++-------- .../constructor/tree/MockFrameworkManager.kt | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt index 1fdd1de474..e664b005cd 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt @@ -1316,21 +1316,20 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c resources.forEach { // First argument for mocked resource declaration initializer is a target type. // Pass this argument as a type parameter for the mocked resource + + // TODO this type parameter (required for Kotlin test) is unused until the proper implementation + // of generics in code generation https://github.com/UnitTestBot/UTBotJava/issues/88 + @Suppress("UNUSED_VARIABLE") val typeParameter = when (val firstArg = (it.initializer as CgMethodCall).arguments.first()) { is CgGetJavaClass -> firstArg.classId is CgVariable -> firstArg.type else -> error("Unexpected mocked resource declaration argument $firstArg") } - val varType = CgClassId( - it.variableType, - TypeParameters(listOf(typeParameter)), - isNullable = true, - ) + +CgDeclaration( - varType, + it.variableType, it.variableName, - // guard initializer to reuse typecast creation logic - initializer = guardExpression(varType, nullLiteral()).expression, + initializer = nullLiteral(), isMutable = true, ) } diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/MockFrameworkManager.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/MockFrameworkManager.kt index 9660ab1347..2aaa63a50c 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/MockFrameworkManager.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/MockFrameworkManager.kt @@ -237,7 +237,7 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object mockClassCounter.variable ) val mockedConstructionDeclaration = CgDeclaration( - CgClassId(MockitoStaticMocking.mockedConstructionClassId), + MockitoStaticMocking.mockedConstructionClassId, variableConstructor.constructVarName(MOCKED_CONSTRUCTION_NAME), mockConstructionInitializer ) @@ -295,7 +295,7 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object val classMockStaticCall = mockStatic(modelClass) val mockedStaticVariableName = variableConstructor.constructVarName(MOCKED_STATIC_NAME) CgDeclaration( - CgClassId(MockitoStaticMocking.mockedStaticClassId), + MockitoStaticMocking.mockedStaticClassId, mockedStaticVariableName, classMockStaticCall ).also { From 4c2933194d99da14a676f7f22e12d537a77aeeb2 Mon Sep 17 00:00:00 2001 From: Kamenev Yury Date: Tue, 4 Oct 2022 17:17:00 +0800 Subject: [PATCH 2/2] Disabled Kotlin codegen tests with static mocks --- .../org/utbot/examples/algorithms/SortTest.kt | 14 ++++++++++++-- .../org/utbot/examples/mock/MockRandomTest.kt | 14 ++++++++++++-- .../examples/mock/MockStaticMethodExampleTest.kt | 12 +++++++++++- .../utbot/examples/natives/NativeExamplesTest.kt | 12 +++++++++++- .../org/utbot/examples/recursion/RecursionTest.kt | 12 +++++++++++- 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/utbot-framework-test/src/test/kotlin/org/utbot/examples/algorithms/SortTest.kt b/utbot-framework-test/src/test/kotlin/org/utbot/examples/algorithms/SortTest.kt index 438571905e..c761456052 100644 --- a/utbot-framework-test/src/test/kotlin/org/utbot/examples/algorithms/SortTest.kt +++ b/utbot-framework-test/src/test/kotlin/org/utbot/examples/algorithms/SortTest.kt @@ -9,10 +9,20 @@ import org.utbot.framework.plugin.api.DocPreTagStatement import org.utbot.framework.plugin.api.DocRegularStmt import org.utbot.framework.plugin.api.MockStrategyApi import org.junit.jupiter.api.Test +import org.utbot.framework.plugin.api.CodegenLanguage import org.utbot.testcheckers.eq import org.utbot.testcheckers.ge - -internal class SortTest : UtValueTestCaseChecker(testClass = Sort::class) { +import org.utbot.tests.infrastructure.CodeGeneration + +// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88 +internal class SortTest : UtValueTestCaseChecker( + testClass = Sort::class, + testCodeGeneration = true, + languagePipelines = listOf( + CodeGenerationLanguageLastStage(CodegenLanguage.JAVA), + CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration) + ) +) { @Test fun testQuickSort() { check( diff --git a/utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockRandomTest.kt b/utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockRandomTest.kt index fe974f373f..05e1793014 100644 --- a/utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockRandomTest.kt +++ b/utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockRandomTest.kt @@ -11,9 +11,19 @@ import org.utbot.framework.plugin.api.UtCompositeModel import org.utbot.framework.plugin.api.UtNewInstanceInstrumentation import java.util.Random import org.junit.jupiter.api.Test +import org.utbot.framework.plugin.api.CodegenLanguage import org.utbot.testcheckers.eq - -internal class MockRandomTest : UtValueTestCaseChecker(testClass = MockRandomExamples::class) { +import org.utbot.tests.infrastructure.CodeGeneration + +// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88 +internal class MockRandomTest : UtValueTestCaseChecker( + testClass = MockRandomExamples::class, + testCodeGeneration = true, + languagePipelines = listOf( + CodeGenerationLanguageLastStage(CodegenLanguage.JAVA), + CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration) + ) +) { @Test fun testRandomAsParameter() { val method: Random.() -> Int = Random::nextInt diff --git a/utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockStaticMethodExampleTest.kt b/utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockStaticMethodExampleTest.kt index 784498fd51..37ffbefa2f 100644 --- a/utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockStaticMethodExampleTest.kt +++ b/utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockStaticMethodExampleTest.kt @@ -8,9 +8,19 @@ import org.utbot.framework.util.singleModel import org.utbot.framework.util.singleStaticMethod import org.utbot.framework.util.singleValue import org.junit.jupiter.api.Test +import org.utbot.framework.plugin.api.CodegenLanguage import org.utbot.testcheckers.eq +import org.utbot.tests.infrastructure.CodeGeneration -internal class MockStaticMethodExampleTest : UtValueTestCaseChecker(testClass = MockStaticMethodExample::class) { +// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88 +internal class MockStaticMethodExampleTest : UtValueTestCaseChecker( + testClass = MockStaticMethodExample::class, + testCodeGeneration = true, + languagePipelines = listOf( + CodeGenerationLanguageLastStage(CodegenLanguage.JAVA), + CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration) + ) +) { @Test fun testUseStaticMethod() { checkMocksAndInstrumentation( diff --git a/utbot-framework-test/src/test/kotlin/org/utbot/examples/natives/NativeExamplesTest.kt b/utbot-framework-test/src/test/kotlin/org/utbot/examples/natives/NativeExamplesTest.kt index e0969950fc..863bff2d53 100644 --- a/utbot-framework-test/src/test/kotlin/org/utbot/examples/natives/NativeExamplesTest.kt +++ b/utbot-framework-test/src/test/kotlin/org/utbot/examples/natives/NativeExamplesTest.kt @@ -3,11 +3,21 @@ package org.utbot.examples.natives import org.utbot.tests.infrastructure.UtValueTestCaseChecker import org.utbot.tests.infrastructure.DoNotCalculate import org.junit.jupiter.api.Test +import org.utbot.framework.plugin.api.CodegenLanguage import org.utbot.testcheckers.eq import org.utbot.testcheckers.ge import org.utbot.testcheckers.withSolverTimeoutInMillis +import org.utbot.tests.infrastructure.CodeGeneration -internal class NativeExamplesTest : UtValueTestCaseChecker(testClass = NativeExamples::class) { +// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88 +internal class NativeExamplesTest : UtValueTestCaseChecker( + testClass = NativeExamples::class, + testCodeGeneration = true, + languagePipelines = listOf( + CodeGenerationLanguageLastStage(CodegenLanguage.JAVA), + CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration) + ) +) { @Test fun testFindAndPrintSum() { diff --git a/utbot-framework-test/src/test/kotlin/org/utbot/examples/recursion/RecursionTest.kt b/utbot-framework-test/src/test/kotlin/org/utbot/examples/recursion/RecursionTest.kt index e9a9fe3363..aa30f2ace5 100644 --- a/utbot-framework-test/src/test/kotlin/org/utbot/examples/recursion/RecursionTest.kt +++ b/utbot-framework-test/src/test/kotlin/org/utbot/examples/recursion/RecursionTest.kt @@ -13,10 +13,20 @@ import org.utbot.framework.plugin.api.DocStatement import kotlin.math.pow import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test +import org.utbot.framework.plugin.api.CodegenLanguage import org.utbot.testcheckers.eq import org.utbot.testcheckers.ge +import org.utbot.tests.infrastructure.CodeGeneration -internal class RecursionTest : UtValueTestCaseChecker(testClass = Recursion::class) { +// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88 +internal class RecursionTest : UtValueTestCaseChecker( + testClass = Recursion::class, + testCodeGeneration = true, + languagePipelines = listOf( + CodeGenerationLanguageLastStage(CodegenLanguage.JAVA), + CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration) + ) +) { @Test fun testFactorial() { val factorialSummary = listOf(