Skip to content

Commit

Permalink
Fixed unavailable mock classes (UnitTestBot#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
Damtev authored and AbdullinAM committed Oct 17, 2022
1 parent e5d8b5f commit 8016349
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocStatement>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1286,21 +1286,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,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8016349

Please sign in to comment.