Skip to content

Rename UtTestCase #519 #517

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 3 commits into from
Jul 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ class BunchTestGeneratorCommand : GenerateTestsAbstractCommand(

val testClassName = "${classUnderTest.simpleName}Test"

val testCases = generateTestCases(
val testSets = generateTestSets(
targetMethods,
searchDirectory = workingDirectory,
chosenClassesToMockAlways = (Mocker.defaultSuperClassesToMockAlwaysNames + classesToMockAlways)
.mapTo(mutableSetOf()) { ClassId(it) }
)

val testClassBody = generateTest(classUnderTest, testClassName, testCases)
val testClassBody = generateTest(classUnderTest, testClassName, testSets)

val outputArgAsFile = File(output ?: "")
if (!outputArgAsFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.utbot.framework.plugin.api.MockStrategyApi
import org.utbot.framework.plugin.api.TreatOverflowAsError
import org.utbot.framework.plugin.api.TestCaseGenerator
import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.UtMethodTestSet
import org.utbot.summary.summarize
import java.io.File
import java.lang.reflect.Method
Expand Down Expand Up @@ -153,12 +153,12 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
protected fun loadClassBySpecifiedFqn(classFqn: String): KClass<*> =
classLoader.loadClass(classFqn).kotlin

protected fun generateTestCases(
protected fun generateTestSets(
targetMethods: List<UtMethod<*>>,
sourceCodeFile: Path? = null,
searchDirectory: Path,
chosenClassesToMockAlways: Set<ClassId>
): List<UtTestCase> =
): List<UtMethodTestSet> =
TestCaseGenerator.generate(
targetMethods,
mockStrategy,
Expand All @@ -184,11 +184,11 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
}
}

protected fun generateTest(classUnderTest: KClass<*>, testClassname: String, testCases: List<UtTestCase>): String =
protected fun generateTest(classUnderTest: KClass<*>, testClassname: String, testSets: List<UtMethodTestSet>): String =
initializeCodeGenerator(
testFramework,
classUnderTest
).generateAsString(testCases, testClassname)
).generateAsString(testSets, testClassname)

protected fun initializeEngine(workingDirectory: Path) {
val classPathNormalized =
Expand Down
12 changes: 6 additions & 6 deletions utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.utbot.common.PathUtil.toPath
import org.utbot.engine.Mocker
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.UtMethodTestSet
import org.utbot.framework.plugin.api.util.UtContext
import org.utbot.framework.plugin.api.util.withUtContext
import org.utbot.sarif.SarifReport
Expand Down Expand Up @@ -102,20 +102,20 @@ class GenerateTestsCommand :

val testClassName = output?.toPath()?.toFile()?.nameWithoutExtension
?: "${classUnderTest.simpleName}Test"
val testCases = generateTestCases(
val testSets = generateTestSets(
targetMethods,
Paths.get(sourceCodeFile),
searchDirectory = workingDirectory,
chosenClassesToMockAlways = (Mocker.defaultSuperClassesToMockAlwaysNames + classesToMockAlways)
.mapTo(mutableSetOf()) { ClassId(it) }
)
val testClassBody = generateTest(classUnderTest, testClassName, testCases)
val testClassBody = generateTest(classUnderTest, testClassName, testSets)

if (printToStdOut) {
logger.info { testClassBody }
}
if (sarifReport != null) {
generateReport(targetClassFqn, testCases, testClassBody)
generateReport(targetClassFqn, testSets, testClassBody)
}
saveToFile(testClassBody, output)
}
Expand All @@ -128,7 +128,7 @@ class GenerateTestsCommand :
}
}

private fun generateReport(classFqn: String, testCases: List<UtTestCase>, testClassBody: String) = try {
private fun generateReport(classFqn: String, testSets: List<UtMethodTestSet>, testClassBody: String) = try {
// reassignments for smart casts
val testsFilePath = output
val projectRootPath = projectRoot
Expand All @@ -143,7 +143,7 @@ class GenerateTestsCommand :
else -> {
val sourceFinding =
SourceFindingStrategyDefault(classFqn, sourceCodeFile, testsFilePath, projectRootPath)
val report = SarifReport(testCases, testClassBody, sourceFinding).createReport()
val report = SarifReport(testSets, testClassBody, sourceFinding).createReport()
saveToFile(report, sarifReport)
println("The report was saved to \"$sarifReport\". You can open it using the VS Code extension \"Sarif Viewer\".")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,13 @@ data class UtMethod<R>(
}
}

/**
* Test case.
*
* Note: it should not be transformed into data class since it is used as a key in maps.
* The clusters in it are mutable objects, therefore, we might have problems with hash because of it.
*/
@Suppress("unused")
class UtTestCase(
data class UtMethodTestSet(
val method: UtMethod<*>,
val executions: List<UtExecution> = emptyList(),
val jimpleBody: JimpleBody? = null,
val errors: Map<String, Int> = emptyMap(),
private val clustersInfo: List<Pair<UtClusterInfo?, IntRange>> = listOf(null to executions.indices)
) {
operator fun component1() = method
operator fun component2() = executions
operator fun component3() = jimpleBody
operator fun component4() = errors
operator fun component5() = clustersInfo

fun copy(
method: UtMethod<*> = this.method,
executions: List<UtExecution> = this.executions,
jimpleBody: JimpleBody? = this.jimpleBody,
errors: Map<String, Int> = this.errors,
clustersInfo: List<Pair<UtClusterInfo?, IntRange>> = this.clustersInfo
) = UtTestCase(method, executions, jimpleBody, errors, clustersInfo)
}
val clustersInfo: List<Pair<UtClusterInfo?, IntRange>> = listOf(null to executions.indices)
)

data class Step(
val stmt: Stmt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

package org.utbot.framework.plugin.api

import kotlin.reflect.KClass
import org.apache.commons.lang3.builder.RecursiveToStringStyle
import org.apache.commons.lang3.builder.ReflectionToStringBuilder
import soot.jimple.JimpleBody

data class UtValueTestCase<R>(
data class UtMethodValueTestSet<R>(
val method: UtMethod<out R>,
val executions: List<UtValueExecution<out R>> = emptyList(),
val jimpleBody: JimpleBody? = null,
val errors: Map<String, Int> = emptyMap(),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.UtMethodTestSet
import org.utbot.framework.plugin.api.util.UtContext
import org.utbot.framework.plugin.api.util.id
import org.utbot.framework.plugin.api.util.isPrimitive
Expand Down Expand Up @@ -50,7 +50,7 @@ object UtBotJavaApi {
@JvmOverloads
fun generate(
methodsForGeneration: List<TestMethodInfo>,
generatedTestCases: List<UtTestCase> = mutableListOf(),
generatedTestCases: List<UtMethodTestSet> = mutableListOf(),
destinationClassName: String,
classpath: String,
dependencyClassPath: String,
Expand All @@ -66,15 +66,15 @@ object UtBotJavaApi {

val utContext = UtContext(classUnderTest.classLoader)

val testCases: MutableList<UtTestCase> = generatedTestCases.toMutableList()
val testSets: MutableList<UtMethodTestSet> = generatedTestCases.toMutableList()

val concreteExecutor = ConcreteExecutor(
UtExecutionInstrumentation,
classpath,
dependencyClassPath
)

testCases.addAll(generateUnitTests(concreteExecutor, methodsForGeneration, classUnderTest))
testSets.addAll(generateUnitTests(concreteExecutor, methodsForGeneration, classUnderTest))

if (stopConcreteExecutorOnExit) {
concreteExecutor.close()
Expand All @@ -95,32 +95,32 @@ object UtBotJavaApi {
}

testGenerator.generateAsString(
testCases,
testSets,
destinationClassName
)
}
}

/**
* Generates test cases using default workflow.
* Generates test sets using default workflow.
*
* @see [fuzzingTestCases]
* @see [fuzzingTestSets]
*/
@JvmStatic
@JvmOverloads
fun generateTestCases(
fun generateTestSets(
methodsForAutomaticGeneration: List<TestMethodInfo>,
classUnderTest: Class<*>,
classpath: String,
dependencyClassPath: String,
mockStrategyApi: MockStrategyApi = MockStrategyApi.OTHER_PACKAGES,
generationTimeoutInMillis: Long = UtSettings.utBotGenerationTimeoutInMillis
): MutableList<UtTestCase> {
): MutableList<UtMethodTestSet> {

val utContext = UtContext(classUnderTest.classLoader)
val testCases: MutableList<UtTestCase> = mutableListOf()
val testSets: MutableList<UtMethodTestSet> = mutableListOf()

testCases.addAll(withUtContext(utContext) {
testSets.addAll(withUtContext(utContext) {
TestCaseGenerator
.apply {
init(
Expand All @@ -140,25 +140,25 @@ object UtBotJavaApi {
)
})

return testCases
return testSets
}

/**
* Generates test cases using only fuzzing workflow.
*
* @see [generateTestCases]
* @see [generateTestSets]
*/
@JvmStatic
@JvmOverloads
fun fuzzingTestCases(
fun fuzzingTestSets(
methodsForAutomaticGeneration: List<TestMethodInfo>,
classUnderTest: Class<*>,
classpath: String,
dependencyClassPath: String,
mockStrategyApi: MockStrategyApi = MockStrategyApi.OTHER_PACKAGES,
generationTimeoutInMillis: Long = UtSettings.utBotGenerationTimeoutInMillis,
primitiveValuesSupplier: CustomFuzzerValueSupplier = CustomFuzzerValueSupplier { null }
): MutableList<UtTestCase> {
): MutableList<UtMethodTestSet> {
fun createPrimitiveModels(supplier: CustomFuzzerValueSupplier, classId: ClassId): Sequence<UtPrimitiveModel> =
supplier
.takeIf { classId.isPrimitive || classId.isPrimitiveWrapper || classId == stringClassId }
Expand Down Expand Up @@ -259,7 +259,7 @@ object UtBotJavaApi {

val utMethod = UtMethod(methodCallable, containingClass.kotlin)

UtTestCase(
UtMethodTestSet(
utMethod,
listOf(utExecution)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.utbot.framework.codegen.model.visitor.CgAbstractRenderer
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.MockFramework
import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.UtMethodTestSet
import org.utbot.framework.plugin.api.util.id

class CodeGenerator {
Expand Down Expand Up @@ -56,17 +56,17 @@ class CodeGenerator {
}

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

//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
fun generateAsStringWithTestReport(
testCases: Collection<UtTestCase>,
testSets: Collection<UtMethodTestSet>,
testClassCustomName: String? = null,
): TestsCodeWithTestReport =
withCustomContext(testClassCustomName) {
context.withClassScope {
val testClassFile = CgTestClassConstructor(context).construct(testCases)
val testClassFile = CgTestClassConstructor(context).construct(testSets)
TestsCodeWithTestReport(renderClassFile(testClassFile), testClassFile.testsGenerationReport)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import org.utbot.framework.plugin.api.UtExecution
import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtModel
import org.utbot.framework.plugin.api.UtReferenceModel
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.UtMethodTestSet
import java.util.IdentityHashMap
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.PersistentMap
Expand Down Expand Up @@ -178,7 +178,7 @@ internal interface CgContextOwner {
// map from a set of tests for a method to another map
// which connects code generation error message
// with the number of times it occurred
val codeGenerationErrors: MutableMap<UtTestCase, MutableMap<String, Int>>
val codeGenerationErrors: MutableMap<UtMethodTestSet, MutableMap<String, Int>>

// package for generated test class
val testClassPackageName: String
Expand Down Expand Up @@ -418,7 +418,7 @@ internal data class CgContext(
override var declaredExecutableRefs: PersistentMap<ExecutableId, CgVariable> = persistentMapOf(),
override var thisInstance: CgValue? = null,
override val methodArguments: MutableList<CgValue> = mutableListOf(),
override val codeGenerationErrors: MutableMap<UtTestCase, MutableMap<String, Int>> = mutableMapOf(),
override val codeGenerationErrors: MutableMap<UtMethodTestSet, MutableMap<String, Int>> = mutableMapOf(),
override val testClassPackageName: String = classUnderTest.packageName,
override var shouldOptimizeImports: Boolean = false,
override var testClassCustomName: String? = null,
Expand Down
Loading