Skip to content

Commit ce1f2c8

Browse files
committed
Update util methods collection in code generation taking into account multiple possible util method providers
1 parent 9c0eb8a commit ce1f2c8

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.utbot.common.appendHtmlLine
44
import org.utbot.engine.displayName
55
import org.utbot.framework.codegen.ParametrizedTestSource
66
import org.utbot.framework.codegen.model.constructor.CgMethodTestSet
7+
import org.utbot.framework.codegen.model.constructor.builtin.TestClassUtilMethodProvider
78
import org.utbot.framework.codegen.model.constructor.context.CgContext
89
import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
910
import org.utbot.framework.codegen.model.constructor.util.CgComponents
@@ -67,8 +68,10 @@ internal class CgTestClassConstructor(val context: CgContext) :
6768
staticDeclarationRegions += CgStaticsRegion("Data providers", cgDataProviderMethods)
6869
}
6970

70-
val utilMethods = createUtilMethods()
71-
if (utilMethods.isNotEmpty()) {
71+
val utilMethods = collectUtilMethods()
72+
// If utilMethodProvider is TestClassUtilMethodProvider, then util methods should be declared
73+
// in the test class. Otherwise, util methods will be located elsewhere (e.g. another class or library).
74+
if (utilMethodProvider is TestClassUtilMethodProvider && utilMethods.isNotEmpty()) {
7275
staticDeclarationRegions += CgStaticsRegion("Util methods", utilMethods)
7376
}
7477
}
@@ -164,8 +167,17 @@ internal class CgTestClassConstructor(val context: CgContext) :
164167
}.onFailure { error -> processFailure(testSet, error) }
165168
}
166169

167-
// TODO: collect imports of util methods
168-
private fun createUtilMethods(): List<CgUtilMethod> {
170+
/**
171+
* This method collects a list of util methods needed by the class.
172+
* By the end of the test method generation [requiredUtilMethods] may not contain all the methods needed.
173+
* That's because some util methods may not be directly used in tests, but they may be used from other util methods.
174+
* We define such method dependencies in [MethodId.dependencies].
175+
*
176+
* Once all dependencies are collected, they are also added to [requiredUtilMethods].
177+
*
178+
* @return a list of [CgUtilMethod] representing required util methods (including dependencies).
179+
*/
180+
private fun collectUtilMethods(): List<CgUtilMethod> {
169181
val utilMethods = mutableListOf<CgUtilMethod>()
170182
// some util methods depend on the others
171183
// using this loop we make sure that all the
@@ -175,11 +187,18 @@ internal class CgTestClassConstructor(val context: CgContext) :
175187
requiredUtilMethods.remove(method)
176188
if (method.name !in existingMethodNames) {
177189
utilMethods += CgUtilMethod(method)
178-
importUtilMethodDependencies(method)
190+
// we only need imports from util methods if these util methods are declared in the test class
191+
if (utilMethodProvider is TestClassUtilMethodProvider) {
192+
importUtilMethodDependencies(method)
193+
}
179194
existingMethodNames += method.name
180195
requiredUtilMethods += method.dependencies()
181196
}
182197
}
198+
// Collect all util methods back into requiredUtilMethods.
199+
// Now there will also be util methods that weren't present in requiredUtilMethods at first,
200+
// but were needed for the present util methods to work.
201+
requiredUtilMethods += utilMethods.map { method -> method.id }
183202
return utilMethods
184203
}
185204

0 commit comments

Comments
 (0)