-
Notifications
You must be signed in to change notification settings - Fork 46
Introduce Spring codegen instances and collect types of mocked class variables #1791
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dd8d491
Collect mocked class variables in Spring tests
EgorkaKulikov 791f619
Merge branch 'main' into egor/mocks_traverser
EgorkaKulikov 49814bf
Temporarily disable functionality
EgorkaKulikov ab3daed
Apply review fixes
EgorkaKulikov f9fe95d
Merge branch 'main' into egor/mocks_traverser
EgorkaKulikov 82e864b
Set default value for main
EgorkaKulikov fe87c3e
Merge branch 'main' into egor/mocks_traverser
EgorkaKulikov 4599f1b
Little review fixes
EgorkaKulikov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 22 additions & 45 deletions
67
utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/TestClassModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,35 @@ | ||
package org.utbot.framework.codegen.domain.models | ||
|
||
import org.utbot.framework.plugin.api.ClassId | ||
import org.utbot.framework.plugin.api.util.enclosingClass | ||
|
||
// TODO: seems like this class needs to be renamed | ||
/** | ||
* Stores method testsets in a structure that replicates structure of their methods in [classUnderTest]. | ||
* Stores method test sets in a structure that replicates structure of their methods in [classUnderTest]. | ||
* I.e., if some method is declared in nested class of [classUnderTest], its testset will be put | ||
* in [TestClassModel] in one of [nestedClasses] | ||
*/ | ||
data class TestClassModel( | ||
abstract class TestClassModel( | ||
val classUnderTest: ClassId, | ||
val methodTestSets: List<CgMethodTestSet>, | ||
val nestedClasses: List<TestClassModel> = listOf() | ||
) { | ||
companion object { | ||
fun fromTestSets(classUnderTest: ClassId, testSets: List<CgMethodTestSet>): TestClassModel { | ||
// For each class stores list of methods declared in this class (methods from nested classes are excluded) | ||
val class2methodTestSets = testSets.groupBy { it.executableId.classId } | ||
val nestedClasses: List<SimpleTestClassModel>, | ||
) | ||
|
||
val classesWithMethodsUnderTest = testSets | ||
.map { it.executableId.classId } | ||
.distinct() | ||
open class SimpleTestClassModel( | ||
classUnderTest: ClassId, | ||
methodTestSets: List<CgMethodTestSet>, | ||
nestedClasses: List<SimpleTestClassModel> = listOf(), | ||
): TestClassModel(classUnderTest, methodTestSets, nestedClasses) | ||
|
||
// For each class stores list of its "direct" nested classes | ||
val class2nestedClasses = mutableMapOf<ClassId, MutableSet<ClassId>>() | ||
|
||
for (classId in classesWithMethodsUnderTest) { | ||
var currentClass = classId | ||
var enclosingClass = currentClass.enclosingClass | ||
// while we haven't reached the top of nested class hierarchy or the main class under test | ||
while (enclosingClass != null && currentClass != classUnderTest) { | ||
class2nestedClasses.getOrPut(enclosingClass) { mutableSetOf() } += currentClass | ||
currentClass = enclosingClass | ||
enclosingClass = enclosingClass.enclosingClass | ||
} | ||
} | ||
return constructRecursively(classUnderTest, class2methodTestSets, class2nestedClasses) | ||
} | ||
/** | ||
* Extended [SimpleTestClassModel] for Spring analysis reasons | ||
* | ||
* @param injectingMocksClass a class to inject other mocks into | ||
* @param mockedClasses variables of test class to represent mocked instances | ||
*/ | ||
class SpringTestClassModel( | ||
classUnderTest: ClassId, | ||
methodTestSets: List<CgMethodTestSet>, | ||
nestedClasses: List<SimpleTestClassModel>, | ||
val injectingMocksClass: ClassId? = null, | ||
val mockedClasses: Set<ClassId> = setOf(), | ||
): TestClassModel(classUnderTest, methodTestSets, nestedClasses) | ||
|
||
private fun constructRecursively( | ||
clazz: ClassId, | ||
class2methodTestSets: Map<ClassId, List<CgMethodTestSet>>, | ||
class2nestedClasses: Map<ClassId, Set<ClassId>> | ||
): TestClassModel { | ||
val currentNestedClasses = class2nestedClasses.getOrDefault(clazz, listOf()) | ||
val currentMethodTestSets = class2methodTestSets.getOrDefault(clazz, listOf()) | ||
return TestClassModel( | ||
clazz, | ||
currentMethodTestSets, | ||
currentNestedClasses.map { | ||
constructRecursively(it, class2methodTestSets, class2nestedClasses) | ||
} | ||
) | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../kotlin/org/utbot/framework/codegen/domain/models/builders/SimpleTestClassModelBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.utbot.framework.codegen.domain.models.builders | ||
|
||
import org.utbot.framework.codegen.domain.models.CgMethodTestSet | ||
import org.utbot.framework.codegen.domain.models.SimpleTestClassModel | ||
import org.utbot.framework.plugin.api.ClassId | ||
import org.utbot.framework.plugin.api.util.enclosingClass | ||
|
||
open class SimpleTestClassModelBuilder: TestClassModelBuilder() { | ||
override fun createTestClassModel(classUnderTest: ClassId, testSets: List<CgMethodTestSet>): SimpleTestClassModel { | ||
// For each class stores list of methods declared in this class (methods from nested classes are excluded) | ||
val class2methodTestSets = testSets.groupBy { it.executableId.classId } | ||
|
||
val classesWithMethodsUnderTest = testSets | ||
.map { it.executableId.classId } | ||
.distinct() | ||
|
||
// For each class stores list of its "direct" nested classes | ||
val class2nestedClasses = mutableMapOf<ClassId, MutableSet<ClassId>>() | ||
|
||
for (classId in classesWithMethodsUnderTest) { | ||
var currentClass = classId | ||
var enclosingClass = currentClass.enclosingClass | ||
// while we haven't reached the top of nested class hierarchy or the main class under test | ||
while (enclosingClass != null && currentClass != classUnderTest) { | ||
class2nestedClasses.getOrPut(enclosingClass) { mutableSetOf() } += currentClass | ||
currentClass = enclosingClass | ||
enclosingClass = enclosingClass.enclosingClass | ||
} | ||
} | ||
|
||
return constructRecursively(classUnderTest, class2methodTestSets, class2nestedClasses) | ||
} | ||
|
||
private fun constructRecursively( | ||
clazz: ClassId, | ||
class2methodTestSets: Map<ClassId, List<CgMethodTestSet>>, | ||
class2nestedClasses: Map<ClassId, Set<ClassId>> | ||
): SimpleTestClassModel { | ||
val currentNestedClasses = class2nestedClasses.getOrDefault(clazz, listOf()) | ||
val currentMethodTestSets = class2methodTestSets.getOrDefault(clazz, listOf()) | ||
return SimpleTestClassModel( | ||
clazz, | ||
currentMethodTestSets, | ||
currentNestedClasses.map { | ||
constructRecursively(it, class2methodTestSets, class2nestedClasses) | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.