-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathJsUtModelConstructor.kt
More file actions
72 lines (67 loc) · 2.59 KB
/
JsUtModelConstructor.kt
File metadata and controls
72 lines (67 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package api
import framework.api.js.JsClassId
import framework.api.js.JsEmptyClassId
import framework.api.js.JsNullModel
import framework.api.js.JsPrimitiveModel
import framework.api.js.JsUndefinedModel
import framework.api.js.util.defaultJsValueModel
import framework.api.js.util.jsErrorClassId
import fuzzer.JsIdProvider
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.UtArrayModel
import org.utbot.framework.plugin.api.UtAssembleModel
import org.utbot.framework.plugin.api.UtExecutableCallModel
import org.utbot.framework.plugin.api.UtModel
import org.utbot.instrumentation.instrumentation.execution.constructors.UtModelConstructorInterface
class JsUtModelConstructor : UtModelConstructorInterface {
// TODO SEVERE: Requires substantial expansion to other types
@Suppress("NAME_SHADOWING")
override fun construct(value: Any?, classId: ClassId): UtModel {
val classId = classId as JsClassId
if (classId == jsErrorClassId) return UtModel(jsErrorClassId)
return when (value) {
null -> JsNullModel(classId)
is Byte,
is Short,
is Char,
is Int,
is Long,
is Float,
is Double,
is String,
is Boolean -> JsPrimitiveModel(value)
is List<*> -> {
UtArrayModel(
id = JsIdProvider.createId(),
classId = classId,
stores = buildMap {
putAll(value.indices.zip(value.map {
construct(it, classId)
}))
} as MutableMap<Int, UtModel>,
length = value.size,
constModel = classId.defaultJsValueModel()
)
}
is Map<*, *> -> {
constructObject(classId, value)
}
else -> JsUndefinedModel(classId)
}
}
@Suppress("UNCHECKED_CAST")
private fun constructObject(classId: JsClassId, value: Any?): UtModel {
val constructor = classId.allConstructors.first()
val values = (value as Map<String, Any>).values.map {
construct(it, JsEmptyClassId())
}
val id = JsIdProvider.createId()
val instantiationCall = UtExecutableCallModel(null, constructor, values)
return UtAssembleModel(
id = id,
classId = constructor.classId,
modelName = "${constructor.classId.name}${constructor.parameters}#" + id.toString(16),
instantiationCall = instantiationCall,
)
}
}