Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add some fixes
  • Loading branch information
amandelpie committed Aug 5, 2022
commit 707b6a119486194c0d226ef95125d1b9cfc48a0a
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ internal interface CgContextOwner {

// UtExecution we currently generate a test method for.
// It is null when no test method is being generated at the moment.
var currentExecution: UtSymbolicExecution?
var currentExecution: UtExecution?

val testFramework: TestFramework

Expand Down Expand Up @@ -389,7 +389,7 @@ internal data class CgContext(
override val existingMethodNames: MutableSet<String> = mutableSetOf(),
override val prevStaticFieldValues: MutableMap<FieldId, CgVariable> = mutableMapOf(),
override val paramNames: Map<ExecutableId, List<String>>,
override var currentExecution: UtSymbolicExecution? = null,
override var currentExecution: UtExecution? = null,
override val testFramework: TestFramework,
override val mockFramework: MockFramework,
override val staticsMocking: StaticsMocking,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import org.utbot.framework.fields.FieldPath
import org.utbot.framework.fields.ModifiedFields
import org.utbot.framework.fields.StateModificationInfo
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.UtSymbolicExecution
import org.utbot.framework.plugin.api.util.hasField
import org.utbot.framework.plugin.api.util.id
import org.utbot.framework.plugin.api.util.isArray
import org.utbot.framework.plugin.api.util.isRefType
import org.utbot.framework.plugin.api.util.objectClassId
import org.utbot.framework.util.hasThisInstance
import org.utbot.fuzzer.UtFuzzedExecution
import java.lang.reflect.Array

internal interface CgFieldStateManager {
Expand Down Expand Up @@ -67,13 +69,23 @@ internal class CgFieldStateManagerImpl(val context: CgContext)
}

private fun rememberThisInstanceState(info: StateModificationInfo, state: FieldState) {
if (!currentExecution!!.hasThisInstance()) {
return
when (currentExecution) {
is UtSymbolicExecution -> {
if (!(currentExecution!! as UtSymbolicExecution).hasThisInstance()) {
return
}
val thisInstance = context.thisInstance!!
val modifiedFields = info.thisInstance
// by now this instance variable must have already been created
saveFieldsState(thisInstance, modifiedFields, statesCache.thisInstance, state)
}
is UtFuzzedExecution -> {
return
}
else -> {
return
}
Comment thread
amandelpie marked this conversation as resolved.
}
val thisInstance = context.thisInstance!!
val modifiedFields = info.thisInstance
// by now this instance variable must have already been created
saveFieldsState(thisInstance, modifiedFields, statesCache.thisInstance, state)
}

private fun rememberArgumentsState(info: StateModificationInfo, state: FieldState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,29 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
private lateinit var methodType: CgTestMethodType

private fun setupInstrumentation() {
val instrumentation = currentExecution!!.instrumentation
if (instrumentation.isEmpty()) return

if (generateWarningsForStaticMocking && forceStaticMocking == ForceStaticMocking.DO_NOT_FORCE) {
// warn user about possible flaky tests
multilineComment(forceStaticMocking.warningMessage)
return
}
if (currentExecution is UtSymbolicExecution) {
val execution = currentExecution as UtSymbolicExecution
val instrumentation = execution.instrumentation
if (instrumentation.isEmpty()) return

if (generateWarningsForStaticMocking && forceStaticMocking == ForceStaticMocking.DO_NOT_FORCE) {
// warn user about possible flaky tests
multilineComment(forceStaticMocking.warningMessage)
return
}

instrumentation
.filterIsInstance<UtNewInstanceInstrumentation>()
.forEach { mockFrameworkManager.mockNewInstance(it) }
instrumentation
.filterIsInstance<UtStaticMethodInstrumentation>()
.groupBy { it.methodId.classId }
.forEach { (classId, methodMocks) -> mockFrameworkManager.mockStaticMethodsOfClass(classId, methodMocks) }

if (generateWarningsForStaticMocking && forceStaticMocking == ForceStaticMocking.FORCE) {
// warn user about forced using static mocks
multilineComment(forceStaticMocking.warningMessage)
instrumentation
.filterIsInstance<UtNewInstanceInstrumentation>()
.forEach { mockFrameworkManager.mockNewInstance(it) }
instrumentation
.filterIsInstance<UtStaticMethodInstrumentation>()
.groupBy { it.methodId.classId }
.forEach { (classId, methodMocks) -> mockFrameworkManager.mockStaticMethodsOfClass(classId, methodMocks) }

if (generateWarningsForStaticMocking && forceStaticMocking == ForceStaticMocking.FORCE) {
// warn user about forced using static mocks
multilineComment(forceStaticMocking.warningMessage)
}
}
}

Expand Down Expand Up @@ -1344,7 +1347,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
return arguments
}

private fun <R> withTestMethodScope(execution: UtSymbolicExecution, block: () -> R): R {
private fun <R> withTestMethodScope(execution: UtExecution, block: () -> R): R {
clearTestMethodScope()
currentExecution = execution
statesCache = EnvironmentFieldStateCache.emptyCacheFor(execution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.utbot.common.workaround
import org.utbot.framework.plugin.api.*
import org.utbot.framework.util.UtModelVisitor
import org.utbot.framework.util.hasThisInstance
import org.utbot.fuzzer.UtFuzzedExecution

class ExecutionStateAnalyzer(val execution: UtExecution) {
fun findModifiedFields(): StateModificationInfo {
Expand All @@ -17,14 +18,24 @@ class ExecutionStateAnalyzer(val execution: UtExecution) {
}

private fun StateModificationInfo.analyzeThisInstance(): StateModificationInfo {
if (!execution.hasThisInstance()) {
return this
when (execution) {
is UtSymbolicExecution -> {
if (!execution.hasThisInstance()) {
return this
}
val thisInstanceBefore = execution.stateBefore.thisInstance!!
val thisInstanceAfter = execution.stateAfter.thisInstance!!
val info = analyzeModelStates(thisInstanceBefore, thisInstanceAfter)
val modifiedFields = getModifiedFields(info)
return this.copy(thisInstance = modifiedFields)
}
is UtFuzzedExecution -> {
return this
Comment thread
amandelpie marked this conversation as resolved.
}
else -> {
return this
}
Comment thread
amandelpie marked this conversation as resolved.
}
val thisInstanceBefore = execution.stateBefore.thisInstance!!
val thisInstanceAfter = execution.stateAfter.thisInstance!!
val info = analyzeModelStates(thisInstanceBefore, thisInstanceAfter)
val modifiedFields = getModifiedFields(info)
return this.copy(thisInstance = modifiedFields)
}

private fun StateModificationInfo.analyzeParameters(): StateModificationInfo {
Expand All @@ -40,25 +51,35 @@ class ExecutionStateAnalyzer(val execution: UtExecution) {
}

private fun StateModificationInfo.analyzeStatics(): StateModificationInfo {
if (execution.stateAfter == MissingState) return this

val staticsBefore = execution.stateBefore.statics
val staticsAfter = execution.stateAfter.statics

val staticFieldsByClass = execution.staticFields.groupBy { it.declaringClass }
val modificationsByClass = mutableMapOf<ClassId, ModifiedFields>()
for ((classId, fields) in staticFieldsByClass) {
val staticFieldModifications = mutableListOf<ModifiedField>()
for (field in fields) {
val before = staticsBefore[field]!!
val after = staticsAfter[field]!!
val path = FieldPath() + FieldAccess(field)
val info = analyzeModelStates(before, after, path)
staticFieldModifications += getModifiedFields(info)
when (execution) {
is UtSymbolicExecution -> {
if (execution.stateAfter == MissingState) return this

val staticsBefore = execution.stateBefore.statics
val staticsAfter = execution.stateAfter.statics

val staticFieldsByClass = execution.staticFields.groupBy { it.declaringClass }
val modificationsByClass = mutableMapOf<ClassId, ModifiedFields>()
for ((classId, fields) in staticFieldsByClass) {
val staticFieldModifications = mutableListOf<ModifiedField>()
for (field in fields) {
val before = staticsBefore[field]!!
val after = staticsAfter[field]!!
val path = FieldPath() + FieldAccess(field)
val info = analyzeModelStates(before, after, path)
staticFieldModifications += getModifiedFields(info)
}
modificationsByClass[classId] = staticFieldModifications
}
return this.copy(staticFields = modificationsByClass)
}
is UtFuzzedExecution -> {
return this
Comment thread
amandelpie marked this conversation as resolved.
}
else -> {
return this
Comment thread
amandelpie marked this conversation as resolved.
}
modificationsByClass[classId] = staticFieldModifications
}
return this.copy(staticFields = modificationsByClass)
}

private fun analyzeModelStates(
Expand All @@ -78,11 +99,11 @@ class ExecutionStateAnalyzer(val execution: UtExecution) {
// therefore, AssembleModelGenerator won't be able to transform the given composite model

val reason = if (before is UtAssembleModel && after is UtCompositeModel) {
"ModelBefore is an AssembleModel and ModelAfter " +
"is a CompositeModel, but modelBefore doesn't have an origin model."
"ModelBefore is an AssembleModel and ModelAfter " +
"is a CompositeModel, but modelBefore doesn't have an origin model."
} else {
"The model before and the model after have different types: " +
"model before is ${before::class}, but model after is ${after::class}."
"The model before and the model after have different types: " +
"model before is ${before::class}, but model after is ${after::class}."
}

error("Cannot analyze fields modification. $reason")
Expand Down