Skip to content

Commit 3f19f45

Browse files
Damtevdenis-fokin
authored andcommitted
Fixed unavailable mock classes (#1066)
(cherry picked from commit 1260c8f)
1 parent 47a0e3d commit 3f19f45

File tree

7 files changed

+66
-17
lines changed

7 files changed

+66
-17
lines changed

utbot-framework-test/src/test/kotlin/org/utbot/examples/algorithms/SortTest.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ import org.utbot.framework.plugin.api.DocPreTagStatement
99
import org.utbot.framework.plugin.api.DocRegularStmt
1010
import org.utbot.framework.plugin.api.MockStrategyApi
1111
import org.junit.jupiter.api.Test
12+
import org.utbot.framework.plugin.api.CodegenLanguage
1213
import org.utbot.testcheckers.eq
1314
import org.utbot.testcheckers.ge
14-
15-
internal class SortTest : UtValueTestCaseChecker(testClass = Sort::class) {
15+
import org.utbot.tests.infrastructure.CodeGeneration
16+
17+
// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88
18+
internal class SortTest : UtValueTestCaseChecker(
19+
testClass = Sort::class,
20+
testCodeGeneration = true,
21+
languagePipelines = listOf(
22+
CodeGenerationLanguageLastStage(CodegenLanguage.JAVA),
23+
CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration)
24+
)
25+
) {
1626
@Test
1727
fun testQuickSort() {
1828
check(

utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockRandomTest.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ import org.utbot.framework.plugin.api.UtCompositeModel
1111
import org.utbot.framework.plugin.api.UtNewInstanceInstrumentation
1212
import java.util.Random
1313
import org.junit.jupiter.api.Test
14+
import org.utbot.framework.plugin.api.CodegenLanguage
1415
import org.utbot.testcheckers.eq
15-
16-
internal class MockRandomTest : UtValueTestCaseChecker(testClass = MockRandomExamples::class) {
16+
import org.utbot.tests.infrastructure.CodeGeneration
17+
18+
// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88
19+
internal class MockRandomTest : UtValueTestCaseChecker(
20+
testClass = MockRandomExamples::class,
21+
testCodeGeneration = true,
22+
languagePipelines = listOf(
23+
CodeGenerationLanguageLastStage(CodegenLanguage.JAVA),
24+
CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration)
25+
)
26+
) {
1727
@Test
1828
fun testRandomAsParameter() {
1929
val method: Random.() -> Int = Random::nextInt

utbot-framework-test/src/test/kotlin/org/utbot/examples/mock/MockStaticMethodExampleTest.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ import org.utbot.framework.util.singleModel
88
import org.utbot.framework.util.singleStaticMethod
99
import org.utbot.framework.util.singleValue
1010
import org.junit.jupiter.api.Test
11+
import org.utbot.framework.plugin.api.CodegenLanguage
1112
import org.utbot.testcheckers.eq
13+
import org.utbot.tests.infrastructure.CodeGeneration
1214

13-
internal class MockStaticMethodExampleTest : UtValueTestCaseChecker(testClass = MockStaticMethodExample::class) {
15+
// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88
16+
internal class MockStaticMethodExampleTest : UtValueTestCaseChecker(
17+
testClass = MockStaticMethodExample::class,
18+
testCodeGeneration = true,
19+
languagePipelines = listOf(
20+
CodeGenerationLanguageLastStage(CodegenLanguage.JAVA),
21+
CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration)
22+
)
23+
) {
1424
@Test
1525
fun testUseStaticMethod() {
1626
checkMocksAndInstrumentation(

utbot-framework-test/src/test/kotlin/org/utbot/examples/natives/NativeExamplesTest.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ package org.utbot.examples.natives
33
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
44
import org.utbot.tests.infrastructure.DoNotCalculate
55
import org.junit.jupiter.api.Test
6+
import org.utbot.framework.plugin.api.CodegenLanguage
67
import org.utbot.testcheckers.eq
78
import org.utbot.testcheckers.ge
89
import org.utbot.testcheckers.withSolverTimeoutInMillis
10+
import org.utbot.tests.infrastructure.CodeGeneration
911

10-
internal class NativeExamplesTest : UtValueTestCaseChecker(testClass = NativeExamples::class) {
12+
// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88
13+
internal class NativeExamplesTest : UtValueTestCaseChecker(
14+
testClass = NativeExamples::class,
15+
testCodeGeneration = true,
16+
languagePipelines = listOf(
17+
CodeGenerationLanguageLastStage(CodegenLanguage.JAVA),
18+
CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration)
19+
)
20+
) {
1121

1222
@Test
1323
fun testFindAndPrintSum() {

utbot-framework-test/src/test/kotlin/org/utbot/examples/recursion/RecursionTest.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@ import org.utbot.framework.plugin.api.DocStatement
1313
import kotlin.math.pow
1414
import org.junit.jupiter.api.Disabled
1515
import org.junit.jupiter.api.Test
16+
import org.utbot.framework.plugin.api.CodegenLanguage
1617
import org.utbot.testcheckers.eq
1718
import org.utbot.testcheckers.ge
19+
import org.utbot.tests.infrastructure.CodeGeneration
1820

19-
internal class RecursionTest : UtValueTestCaseChecker(testClass = Recursion::class) {
21+
// TODO Kotlin mocks generics https://github.com/UnitTestBot/UTBotJava/issues/88
22+
internal class RecursionTest : UtValueTestCaseChecker(
23+
testClass = Recursion::class,
24+
testCodeGeneration = true,
25+
languagePipelines = listOf(
26+
CodeGenerationLanguageLastStage(CodegenLanguage.JAVA),
27+
CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration)
28+
)
29+
) {
2030
@Test
2131
fun testFactorial() {
2232
val factorialSummary = listOf<DocStatement>(

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,21 +1318,20 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13181318
resources.forEach {
13191319
// First argument for mocked resource declaration initializer is a target type.
13201320
// Pass this argument as a type parameter for the mocked resource
1321+
1322+
// TODO this type parameter (required for Kotlin test) is unused until the proper implementation
1323+
// of generics in code generation https://github.com/UnitTestBot/UTBotJava/issues/88
1324+
@Suppress("UNUSED_VARIABLE")
13211325
val typeParameter = when (val firstArg = (it.initializer as CgMethodCall).arguments.first()) {
13221326
is CgGetJavaClass -> firstArg.classId
13231327
is CgVariable -> firstArg.type
13241328
else -> error("Unexpected mocked resource declaration argument $firstArg")
13251329
}
1326-
val varType = CgClassId(
1327-
it.variableType,
1328-
TypeParameters(listOf(typeParameter)),
1329-
isNullable = true,
1330-
)
1330+
13311331
+CgDeclaration(
1332-
varType,
1332+
it.variableType,
13331333
it.variableName,
1334-
// guard initializer to reuse typecast creation logic
1335-
initializer = guardExpression(varType, nullLiteral()).expression,
1334+
initializer = nullLiteral(),
13361335
isMutable = true,
13371336
)
13381337
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object
237237
mockClassCounter.variable
238238
)
239239
val mockedConstructionDeclaration = CgDeclaration(
240-
CgClassId(MockitoStaticMocking.mockedConstructionClassId),
240+
MockitoStaticMocking.mockedConstructionClassId,
241241
variableConstructor.constructVarName(MOCKED_CONSTRUCTION_NAME),
242242
mockConstructionInitializer
243243
)
@@ -295,7 +295,7 @@ private class MockitoStaticMocker(context: CgContext, private val mocker: Object
295295
val classMockStaticCall = mockStatic(modelClass)
296296
val mockedStaticVariableName = variableConstructor.constructVarName(MOCKED_STATIC_NAME)
297297
CgDeclaration(
298-
CgClassId(MockitoStaticMocking.mockedStaticClassId),
298+
MockitoStaticMocking.mockedStaticClassId,
299299
mockedStaticVariableName,
300300
classMockStaticCall
301301
).also {

0 commit comments

Comments
 (0)