Skip to content

Commit

Permalink
Refactor everything related to codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorkaKulikov committed Jul 11, 2022
1 parent f47e064 commit b255cbf
Show file tree
Hide file tree
Showing 166 changed files with 844 additions and 1,099 deletions.
4 changes: 2 additions & 2 deletions HowToUseLoggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The file is usually in the resource folder.

The easiest way is:

- Go in the code that you are going to debug. Let’s assume it is a method in org.utbot.framework.plugin.api.UtBotTestCaseGenerator.
- Go in the code that you are going to debug. Let’s assume it is a method in org.utbot.framework.plugin.api.TestCaseGenerator.
- Find out if there is a KotlinLogging object that is used to create a **logger**
- If such a logger exists, use the fully qualified class name as the logger name in the next steps
<br/>
Expand All @@ -28,7 +28,7 @@ The easiest way is:
Open log4j2.xml and add the logger in the loggers section like this

```
<Logger name=" org.utbot.framework.plugin.api.UtBotTestCaseGenerator " level="info">
<Logger name=" org.utbot.framework.plugin.api.TestCaseGenerator " level="info">
<AppenderRef ref="Console"/>
</Logger>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.utbot.analytics.EngineAnalyticsContext
import org.utbot.examples.AbstractTestCaseGeneratorTest
import org.utbot.examples.UtTestCaseChecker
import org.utbot.examples.eq
import org.utbot.examples.withFeaturePath
import java.io.File
import java.io.FileInputStream

class FeatureProcessorWithRepetitionTest : AbstractTestCaseGeneratorTest(OnePath::class, false) {
class FeatureProcessorWithRepetitionTest : UtTestCaseChecker(OnePath::class, false) {
companion object {
const val featureDir = "src/test/resources/features"
fun reward(coverage: Double, time: Double) = RewardEstimator.reward(coverage, time)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.unique
import com.github.ajalt.clikt.parameters.types.choice
import com.github.ajalt.clikt.parameters.types.long
import mu.KotlinLogging
import org.utbot.common.PathUtil.classFqnToPath
import org.utbot.common.PathUtil.replaceSeparator
import org.utbot.common.PathUtil.toPath
Expand All @@ -19,14 +20,13 @@ import org.utbot.framework.codegen.ForceStaticMocking
import org.utbot.framework.codegen.MockitoStaticMocking
import org.utbot.framework.codegen.NoStaticMocking
import org.utbot.framework.codegen.StaticsMocking
import org.utbot.framework.codegen.model.ModelBasedTestCodeGenerator
import org.utbot.framework.codegen.model.CodeGenerator
import org.utbot.framework.codegen.testFrameworkByName
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.MockFramework
import org.utbot.framework.plugin.api.MockStrategyApi
import org.utbot.framework.plugin.api.TreatOverflowAsError
import org.utbot.framework.plugin.api.UtBotTestCaseGenerator
import org.utbot.framework.plugin.api.TestCaseGenerator
import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.summary.summarize
Expand All @@ -41,7 +41,6 @@ import java.time.temporal.ChronoUnit
import kotlin.reflect.KCallable
import kotlin.reflect.KClass
import kotlin.reflect.jvm.kotlinFunction
import mu.KotlinLogging

private const val LONG_GENERATION_TIMEOUT = 1_200_000L

Expand Down Expand Up @@ -160,7 +159,7 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
searchDirectory: Path,
chosenClassesToMockAlways: Set<ClassId>
): List<UtTestCase> =
UtBotTestCaseGenerator.generateForSeveralMethods(
TestCaseGenerator.generateTestCases(
targetMethods,
mockStrategy,
chosenClassesToMockAlways,
Expand Down Expand Up @@ -199,21 +198,20 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
// Set UtSettings parameters.
UtSettings.treatOverflowAsError = treatOverflowAsError == TreatOverflowAsError.AS_ERROR

UtBotTestCaseGenerator.init(workingDirectory, classPathNormalized, System.getProperty("java.class.path")) { false }
TestCaseGenerator.init(workingDirectory, classPathNormalized, System.getProperty("java.class.path"))
}

private fun initializeCodeGenerator(testFramework: String, classUnderTest: KClass<*>): ModelBasedTestCodeGenerator {
private fun initializeCodeGenerator(testFramework: String, classUnderTest: KClass<*>): CodeGenerator {
val generateWarningsForStaticMocking =
forceStaticMocking == ForceStaticMocking.FORCE && staticsMocking is NoStaticMocking
return ModelBasedTestCodeGenerator().apply {
return CodeGenerator().apply {
init(
testFramework = testFrameworkByName(testFramework),
classUnderTest = classUnderTest.java,
mockFramework = MockFramework.MOCKITO, // TODO: rewrite default mock framework system
codegenLanguage = codegenLanguage,
staticsMocking = staticsMocking,
forceStaticMocking = forceStaticMocking,
generateWarningsForStaticMocking = generateWarningsForStaticMocking
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ import org.utbot.framework.plugin.api.util.primitiveTypeJvmNameOrNull
import org.utbot.framework.plugin.api.util.shortClassId
import org.utbot.framework.plugin.api.util.toReferenceTypeBytecodeSignature
import org.utbot.framework.plugin.api.util.voidClassId
import java.io.File
import java.lang.reflect.Modifier
import java.nio.file.Path
import kotlin.jvm.internal.CallableReference
import kotlin.reflect.KCallable
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.full.instanceParameter
import kotlin.reflect.jvm.javaConstructor
import kotlin.reflect.jvm.javaType
import soot.ArrayType
import soot.BooleanType
import soot.ByteType
Expand All @@ -58,6 +48,15 @@ import soot.Type
import soot.VoidType
import soot.jimple.JimpleBody
import soot.jimple.Stmt
import java.io.File
import java.lang.reflect.Modifier
import kotlin.jvm.internal.CallableReference
import kotlin.reflect.KCallable
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.full.instanceParameter
import kotlin.reflect.jvm.javaConstructor
import kotlin.reflect.jvm.javaType

data class UtMethod<R>(
val callable: KCallable<R>,
Expand Down Expand Up @@ -1017,17 +1016,6 @@ open class TypeParameters(val parameters: List<ClassId> = emptyList())

class WildcardTypeParameter: TypeParameters(emptyList())

interface TestCaseGenerator {
fun init(
buildDir: Path,
classpath: String? = null,
dependencyPaths: String,
isCanceled: () -> Boolean = { false }
)

fun generate(method: UtMethod<*>, mockStrategy: MockStrategyApi): UtTestCase
}

interface CodeGenerationSettingItem {
val displayName: String
val description: String
Expand Down Expand Up @@ -1186,13 +1174,6 @@ fun isolateCommandLineArgumentsToArgumentFile(arguments: List<String>): String {
return argumentFile.absolutePath.let { "@$it" }
}

interface UtService<T> {
val displayName: String
val serviceProvider: T
}

interface TestGeneratorService : UtService<TestCaseGenerator>

private fun StringBuilder.appendOptional(name: String, value: Collection<*>) {
if (value.isNotEmpty()) {
append(", $name=$value")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.utbot.engine.overrides.collections.UtHashSet
import org.utbot.engine.overrides.collections.UtLinkedList
import org.utbot.engine.pc.UtAddrExpression
import org.utbot.engine.pc.UtExpression
import org.utbot.engine.pc.UtFalse
import org.utbot.engine.pc.select
import org.utbot.engine.symbolic.asHardConstraint
import org.utbot.engine.z3.intValue
Expand All @@ -26,7 +25,7 @@ import org.utbot.framework.plugin.api.UtNullModel
import org.utbot.framework.plugin.api.UtReferenceModel
import org.utbot.framework.plugin.api.UtStatementModel
import org.utbot.framework.plugin.api.classId
import org.utbot.framework.plugin.api.graph
import org.utbot.framework.util.graph
import org.utbot.framework.plugin.api.id
import org.utbot.framework.plugin.api.util.booleanClassId
import org.utbot.framework.plugin.api.util.constructorId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ import org.utbot.framework.plugin.api.FieldId
import org.utbot.framework.plugin.api.MethodId
import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.classId
import org.utbot.framework.plugin.api.graph
import org.utbot.framework.plugin.api.id
import org.utbot.framework.plugin.api.util.id
import org.utbot.framework.plugin.api.util.jClass
import org.utbot.framework.plugin.api.util.signature
import org.utbot.framework.plugin.api.util.utContext
import org.utbot.framework.util.executableId
import org.utbot.framework.util.graph
import java.lang.reflect.ParameterizedType
import kotlin.collections.plus
import kotlin.collections.plusAssign
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtNullModel
import org.utbot.framework.plugin.api.UtOverflowFailure
import org.utbot.framework.plugin.api.UtResult
import org.utbot.framework.plugin.api.graph
import org.utbot.framework.plugin.api.jimpleBody
import org.utbot.framework.util.graph
import org.utbot.framework.plugin.api.onSuccess
import org.utbot.framework.plugin.api.util.executableId
import org.utbot.framework.plugin.api.util.id
import org.utbot.framework.plugin.api.util.utContext
import org.utbot.framework.plugin.api.util.description
import org.utbot.framework.util.jimpleBody
import org.utbot.fuzzer.FallbackModelProvider
import org.utbot.fuzzer.FuzzedMethodDescription
import org.utbot.fuzzer.ModelProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import org.utbot.framework.codegen.Junit5
import org.utbot.framework.codegen.NoStaticMocking
import org.utbot.framework.codegen.StaticsMocking
import org.utbot.framework.codegen.TestFramework
import org.utbot.framework.codegen.model.ModelBasedCodeGeneratorService
import org.utbot.framework.codegen.model.CodeGenerator
import org.utbot.framework.concrete.UtConcreteExecutionData
import org.utbot.framework.concrete.UtConcreteExecutionResult
import org.utbot.framework.concrete.UtExecutionInstrumentation
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.MockFramework
import org.utbot.framework.plugin.api.MockStrategyApi
import org.utbot.framework.plugin.api.UtBotTestCaseGenerator
import org.utbot.framework.plugin.api.TestCaseGenerator
import org.utbot.framework.plugin.api.UtExecution
import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtPrimitiveModel
Expand Down Expand Up @@ -81,10 +81,9 @@ object UtBotJavaApi {
}

return withUtContext(utContext) {
val testGenerator = ModelBasedCodeGeneratorService().serviceProvider.apply {
val testGenerator = CodeGenerator().apply {
init(
classUnderTest = classUnderTest,
params = mutableMapOf(),
testFramework = testFramework,
mockFramework = mockFramework,
codegenLanguage = codegenLanguage,
Expand Down Expand Up @@ -122,13 +121,13 @@ object UtBotJavaApi {
val testCases: MutableList<UtTestCase> = mutableListOf()

testCases.addAll(withUtContext(utContext) {
UtBotTestCaseGenerator
TestCaseGenerator
.apply {
init(
FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath(), classpath, dependencyClassPath
)
}
.generateForSeveralMethods(
.generateTestCases(
methodsForAutomaticGeneration.map {
toUtMethod(
it.methodToBeTestedFromUserInput,
Expand Down Expand Up @@ -187,12 +186,12 @@ object UtBotJavaApi {
}

return withUtContext(UtContext(classUnderTest.classLoader)) {
UtBotTestCaseGenerator
TestCaseGenerator
.apply {
init(
FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath(), classpath, dependencyClassPath
)
}.generateForSeveralMethods(
}.generateTestCases(
methodsForAutomaticGeneration.map {
toUtMethod(
it.methodToBeTestedFromUserInput,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.utbot.framework.codegen.model

import org.utbot.common.packageName
import org.utbot.framework.codegen.ForceStaticMocking
import org.utbot.framework.codegen.HangingTestsTimeout
import org.utbot.framework.codegen.ParametrizedTestSource
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour
import org.utbot.framework.codegen.StaticsMocking
import org.utbot.framework.codegen.TestCodeGenerator
import org.utbot.framework.codegen.TestFramework
import org.utbot.framework.codegen.TestsCodeWithTestReport
import org.utbot.framework.codegen.model.constructor.context.CgContext
import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor
import org.utbot.framework.codegen.model.constructor.tree.TestsGenerationReport
import org.utbot.framework.codegen.model.tree.CgTestClassFile
import org.utbot.framework.codegen.model.visitor.CgAbstractRenderer
import org.utbot.framework.plugin.api.CodegenLanguage
Expand All @@ -18,23 +18,23 @@ import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.util.id

class ModelBasedTestCodeGenerator : TestCodeGenerator {
class CodeGenerator {
private lateinit var context: CgContext

override fun init(
fun init(
classUnderTest: Class<*>,
params: MutableMap<UtMethod<*>, List<String>>,
testFramework: TestFramework,
mockFramework: MockFramework?,
staticsMocking: StaticsMocking,
forceStaticMocking: ForceStaticMocking,
generateWarningsForStaticMocking: Boolean,
codegenLanguage: CodegenLanguage,
parameterizedTestSource: ParametrizedTestSource,
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour,
hangingTestsTimeout: HangingTestsTimeout,
enableTestsTimeout: Boolean,
testClassPackageName: String
params: MutableMap<UtMethod<*>, List<String>> = mutableMapOf(),
testFramework: TestFramework = TestFramework.defaultItem,
mockFramework: MockFramework? = MockFramework.defaultItem,
staticsMocking: StaticsMocking = StaticsMocking.defaultItem,
forceStaticMocking: ForceStaticMocking = ForceStaticMocking.defaultItem,
generateWarningsForStaticMocking: Boolean = true,
codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
parameterizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour = RuntimeExceptionTestsBehaviour.defaultItem,
hangingTestsTimeout: HangingTestsTimeout = HangingTestsTimeout(),
enableTestsTimeout: Boolean = true,
testClassPackageName: String = classUnderTest.packageName,
) {
context = CgContext(
classUnderTest = classUnderTest.id,
Expand All @@ -56,13 +56,13 @@ class ModelBasedTestCodeGenerator : TestCodeGenerator {
}

//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
override fun generateAsString(testCases: Collection<UtTestCase>, testClassCustomName: String?): String =
fun generateAsString(testCases: Collection<UtTestCase>, testClassCustomName: String? = null): String =
generateAsStringWithTestReport(testCases, testClassCustomName).generatedCode

//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
override fun generateAsStringWithTestReport(
fun generateAsStringWithTestReport(
testCases: Collection<UtTestCase>,
testClassCustomName: String?
testClassCustomName: String? = null,
): TestsCodeWithTestReport =
withCustomContext(testClassCustomName) {
context.withClassScope {
Expand Down Expand Up @@ -95,3 +95,6 @@ class ModelBasedTestCodeGenerator : TestCodeGenerator {
return renderer.toString()
}
}

data class TestsCodeWithTestReport(val generatedCode: String, val testsGenerationReport: TestsGenerationReport)

Loading

0 comments on commit b255cbf

Please sign in to comment.