Skip to content

Improve code generation pipeline #455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.UtValueTestCaseChecker
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 : UtValueTestCaseChecker(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 @@ -99,7 +99,7 @@ class BunchTestGeneratorCommand : GenerateTestsAbstractCommand(

initializeEngine(workingDirectory)

// utContext is used in `generateTestCases`, `generateTest`, `generateReport`
// utContext is used in `generate`, `generateTest`, `generateReport`
withUtContext(UtContext(classLoader)) {

val testClassName = "${classUnderTest.simpleName}Test"
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.generate(
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 @@ -97,7 +97,7 @@ class GenerateTestsCommand :
if (targetMethods.isEmpty()) {
throw Exception("Nothing to process. No methods were provided")
}
// utContext is used in `generateTestCases`, `generateTest`, `generateReport`
// utContext is used in `generate`, `generateTest`, `generateReport`
withUtContext(UtContext(targetMethods.first().clazz.java.classLoader)) {

val testClassName = output?.toPath()?.toFile()?.nameWithoutExtension
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 @@ -1024,17 +1023,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 @@ -1193,13 +1181,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.FuzzedValue
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(
.generate(
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(
}.generate(
methodsForAutomaticGeneration.map {
toUtMethod(
it.methodToBeTestedFromUserInput,
Expand Down

This file was deleted.

This file was deleted.

Loading