Skip to content

Commit e690398

Browse files
Fix review comments
1 parent 94a2d03 commit e690398

File tree

2 files changed

+0
-147
lines changed

2 files changed

+0
-147
lines changed

utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@file:Suppress("JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE")
21
package org.utbot.engine
32

43
import kotlinx.collections.immutable.persistentHashMapOf

utbot-framework/src/main/kotlin/org/utbot/framework/coverage/CoverageCalculator.kt

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
package org.utbot.framework.coverage
22

3-
import org.jacoco.core.analysis.Analyzer
4-
import org.jacoco.core.analysis.CoverageBuilder
5-
import org.jacoco.core.analysis.IClassCoverage
6-
import org.jacoco.core.analysis.ICounter
7-
import org.jacoco.core.analysis.IMethodCoverage
8-
import org.jacoco.core.data.ExecutionDataStore
9-
import org.jacoco.core.data.SessionInfoStore
10-
import org.jacoco.core.instr.Instrumenter
11-
import org.jacoco.core.runtime.IRuntime
12-
import org.jacoco.core.runtime.LoggerRuntime
13-
import org.jacoco.core.runtime.RuntimeData
143
import org.utbot.framework.plugin.api.ExecutableId
154
import org.utbot.framework.plugin.api.UtValueExecution
165
import org.utbot.framework.plugin.api.util.jClass
@@ -19,123 +8,8 @@ import org.utbot.instrumentation.instrumentation.coverage.CoverageInfo
198
import org.utbot.instrumentation.instrumentation.coverage.CoverageInstrumentation
209
import org.utbot.instrumentation.instrumentation.coverage.collectCoverage
2110
import org.utbot.instrumentation.util.StaticEnvironment
22-
import java.io.InputStream
23-
import kotlin.reflect.KClass
24-
import kotlin.reflect.full.createInstance
25-
import kotlin.reflect.full.declaredFunctions
2611
import kotlinx.coroutines.runBlocking
2712

28-
fun instrument(clazz: KClass<*>, instrumenter: Instrumenter): ByteArray =
29-
clazz.asInputStream().use {
30-
instrumenter.instrument(it, clazz.qualifiedName)
31-
}
32-
33-
fun calculateClassCoverage(targetClass: KClass<*>, testClass: KClass<*>): Coverage {
34-
val targetName = targetClass.qualifiedName!!
35-
val testClassName = testClass.qualifiedName!!
36-
37-
// IRuntime instance to collect execution data
38-
val runtime: IRuntime = LoggerRuntime()
39-
40-
// create a modified version of target class with probes
41-
val instrumenter = Instrumenter(runtime)
42-
val instrumentedTarget = instrument(targetClass, instrumenter)
43-
val instrumentedTestClass = instrument(testClass, instrumenter)
44-
45-
// startup the runtime
46-
val data = RuntimeData()
47-
runtime.startup(data)
48-
49-
// load class from byte[] instances
50-
val memoryClassLoader = MemoryClassLoader()
51-
memoryClassLoader.addDefinition(targetName, instrumentedTarget)
52-
memoryClassLoader.addDefinition(testClassName, instrumentedTestClass)
53-
54-
val instrumentedTests = memoryClassLoader.loadClass(testClassName).kotlin
55-
56-
val tests = instrumentedTests.declaredFunctions
57-
val testClassInstance = instrumentedTests.createInstance()
58-
tests.forEach {
59-
it.call(testClassInstance)
60-
}
61-
62-
// shutdown the runtime
63-
val executionData = ExecutionDataStore()
64-
val sessionInfos = SessionInfoStore()
65-
data.collect(executionData, sessionInfos, false)
66-
runtime.shutdown()
67-
68-
// get coverage builder
69-
val coverageBuilder = CoverageBuilder().apply {
70-
val analyzer = Analyzer(executionData, this)
71-
targetClass.asInputStream().use {
72-
analyzer.analyzeClass(it, targetName)
73-
}
74-
}
75-
76-
val methodsCoverage = coverageBuilder.classes
77-
.single { it.qualifiedName == targetName }
78-
.methods
79-
.map { it.toMethodCoverage() }
80-
81-
return methodsCoverage.toClassCoverage()
82-
}
83-
84-
fun calculateCoverage(clazz: KClass<*>, block: (KClass<*>) -> Unit): CoverageBuilder {
85-
val targetName = clazz.qualifiedName!!
86-
87-
// IRuntime instance to collect execution data
88-
val runtime: IRuntime = LoggerRuntime()
89-
90-
// create a modified version of target class with probes
91-
val instrumenter = Instrumenter(runtime)
92-
val instrumented = instrument(clazz, instrumenter)
93-
94-
// startup the runtime
95-
val data = RuntimeData()
96-
runtime.startup(data)
97-
98-
// load class from byte[] instances
99-
val memoryClassLoader = MemoryClassLoader()
100-
memoryClassLoader.addDefinition(targetName, instrumented)
101-
val targetClass = memoryClassLoader.loadClass(targetName).kotlin
102-
103-
// execute code
104-
block(targetClass)
105-
106-
// shutdown the runtime
107-
val executionData = ExecutionDataStore()
108-
val sessionInfos = SessionInfoStore()
109-
data.collect(executionData, sessionInfos, false)
110-
runtime.shutdown()
111-
112-
// calculate coverage
113-
return CoverageBuilder().apply {
114-
val analyzer = Analyzer(executionData, this)
115-
clazz.asInputStream().use {
116-
analyzer.analyzeClass(it, targetName)
117-
}
118-
}
119-
}
120-
121-
fun KClass<*>.asInputStream(): InputStream =
122-
java.getResourceAsStream("/${qualifiedName!!.replace('.', '/')}.class")!!
123-
124-
class MemoryClassLoader : ClassLoader() {
125-
private val definitions: MutableMap<String, ByteArray> = HashMap()
126-
127-
fun addDefinition(name: String, bytes: ByteArray) {
128-
definitions[name] = bytes
129-
}
130-
131-
override fun loadClass(name: String, resolve: Boolean): Class<*> {
132-
val bytes = definitions[name]
133-
return if (bytes != null) {
134-
defineClass(name, bytes, 0, bytes.size)
135-
} else super.loadClass(name, resolve)
136-
}
137-
}
138-
13913
fun methodCoverage(executable: ExecutableId, executions: List<UtValueExecution<*>>, classpath: String): Coverage {
14014
val methodSignature = executable.signature
14115
val classId = executable.classId
@@ -174,11 +48,6 @@ fun CoverageInfo.toMethodCoverage(methodSignature: String): Coverage {
17448
)
17549
}
17650

177-
fun IMethodCoverage.toMethodCoverage(): Coverage =
178-
Coverage(branchCounter.toCounter(), instructionCounter.toCounter(), lineCounter.toCounter())
179-
180-
private fun ICounter.toCounter(): Counter = Counter(totalCount, coveredCount, missedCount)
181-
18251
data class Coverage(
18352
val branchCounter: Counter = Counter(),
18453
val instructionCounter: Counter = Counter(),
@@ -187,28 +56,13 @@ data class Coverage(
18756
override fun toString() = "(branches: $branchCounter, instructions: $instructionCounter, lines: $lineCounter)"
18857
}
18958

190-
fun List<Coverage>.toClassCoverage(): Coverage {
191-
var branchCounter = Counter()
192-
var instructionCounter = Counter()
193-
var lineCounter = Counter()
194-
forEach {
195-
branchCounter += it.branchCounter
196-
instructionCounter += it.instructionCounter
197-
lineCounter += it.lineCounter
198-
}
199-
return Coverage(branchCounter, instructionCounter, lineCounter)
200-
}
201-
20259
operator fun Counter.plus(other: Counter): Counter =
20360
Counter(
20461
total + other.total,
20562
covered + other.covered,
20663
missed + other.missed
20764
)
20865

209-
private val IClassCoverage.qualifiedName: String
210-
get() = this.name.replace('/', '.')
211-
21266
data class Counter(val total: Int = 0, val covered: Int = 0, val missed: Int = 0) {
21367
override fun toString() = "$covered/$total"
21468
}

0 commit comments

Comments
 (0)