@@ -4,6 +4,7 @@ import org.utbot.common.appendHtmlLine
4
4
import org.utbot.engine.displayName
5
5
import org.utbot.framework.codegen.ParametrizedTestSource
6
6
import org.utbot.framework.codegen.model.constructor.CgMethodTestSet
7
+ import org.utbot.framework.codegen.model.constructor.builtin.TestClassUtilMethodProvider
7
8
import org.utbot.framework.codegen.model.constructor.context.CgContext
8
9
import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
9
10
import org.utbot.framework.codegen.model.constructor.util.CgComponents
@@ -67,8 +68,10 @@ internal class CgTestClassConstructor(val context: CgContext) :
67
68
staticDeclarationRegions + = CgStaticsRegion (" Data providers" , cgDataProviderMethods)
68
69
}
69
70
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()) {
72
75
staticDeclarationRegions + = CgStaticsRegion (" Util methods" , utilMethods)
73
76
}
74
77
}
@@ -164,8 +167,17 @@ internal class CgTestClassConstructor(val context: CgContext) :
164
167
}.onFailure { error -> processFailure(testSet, error) }
165
168
}
166
169
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 > {
169
181
val utilMethods = mutableListOf<CgUtilMethod >()
170
182
// some util methods depend on the others
171
183
// using this loop we make sure that all the
@@ -175,11 +187,18 @@ internal class CgTestClassConstructor(val context: CgContext) :
175
187
requiredUtilMethods.remove(method)
176
188
if (method.name !in existingMethodNames) {
177
189
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
+ }
179
194
existingMethodNames + = method.name
180
195
requiredUtilMethods + = method.dependencies()
181
196
}
182
197
}
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 }
183
202
return utilMethods
184
203
}
185
204
0 commit comments