1
1
package org.utbot.framework.codegen.tree.fieldmanager
2
2
3
+ import org.utbot.framework.codegen.domain.UtModelWrapper
3
4
import org.utbot.framework.codegen.domain.builtin.spyClassId
4
5
import org.utbot.framework.codegen.domain.context.CgContext
5
6
import org.utbot.framework.codegen.domain.models.CgFieldDeclaration
@@ -12,6 +13,8 @@ import org.utbot.framework.plugin.api.UtAssembleModel
12
13
import org.utbot.framework.plugin.api.UtModel
13
14
import org.utbot.framework.plugin.api.canBeSpied
14
15
import org.utbot.framework.plugin.api.isMockModel
16
+ import org.utbot.framework.plugin.api.spiedTypes
17
+ import org.utbot.framework.plugin.api.util.jClass
15
18
16
19
class CgSpiedFieldsManager (context : CgContext ) : CgAbstractClassFieldManager(context) {
17
20
@@ -38,10 +41,10 @@ class CgSpiedFieldsManager(context: CgContext) : CgAbstractClassFieldManager(con
38
41
cgModel !in dependentMockModels
39
42
}
40
43
41
- return constructFieldsWithAnnotation(dependentSpyModels)
44
+ val suitableSpyModels = getSuitableSpyModels(dependentSpyModels)
45
+ return constructFieldsWithAnnotation(suitableSpyModels)
42
46
}
43
47
44
-
45
48
private val spyFrameworkManager = SpyFrameworkManager (context)
46
49
47
50
override fun useVariableForModel (model : UtModel , variable : CgValue ) {
@@ -57,4 +60,29 @@ class CgSpiedFieldsManager(context: CgContext) : CgAbstractClassFieldManager(con
57
60
classId.canBeInjectedByTypeInto(classUnderTest)
58
61
59
62
override fun constructBaseVarName (model : UtModel ): String = super .constructBaseVarName(model) + " Spy"
63
+
64
+ private fun getSuitableSpyModels (potentialSpyModels : MutableSet <UtModelWrapper >): Set <UtModelWrapper > =
65
+ spiedTypes.fold(setOf ()) { spyModels, type ->
66
+ spyModels + getSuitableSpyModelsOfType(type, potentialSpyModels)
67
+ }
68
+
69
+ /*
70
+ * Filters out cases when different tests use different [clazz]
71
+ * implementations and hence we need to inject different types.
72
+ *
73
+ * This limitation is reasoned by @InjectMocks behaviour.
74
+ * Otherwise, injection may be misleading:
75
+ * for example, several spies may be injected into one field.
76
+ */
77
+ private fun getSuitableSpyModelsOfType (
78
+ clazz : Class <* >,
79
+ potentialSpyModels : MutableSet <UtModelWrapper >
80
+ ): Set <UtModelWrapper > {
81
+ val spyModelsAssignableFrom = potentialSpyModels
82
+ .filter { clazz.isAssignableFrom(it.model.classId.jClass) }
83
+ .toSet()
84
+ val spyModelsTypesCount = spyModelsAssignableFrom.map { it.model.classId }.toSet().size
85
+
86
+ return if (spyModelsTypesCount == 1 ) spyModelsAssignableFrom else emptySet()
87
+ }
60
88
}
0 commit comments