-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTestBranchCoverageInstrumentation.kt
More file actions
93 lines (77 loc) · 3.76 KB
/
TestBranchCoverageInstrumentation.kt
File metadata and controls
93 lines (77 loc) · 3.76 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.utbot.examples
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.utbot.examples.samples.ExampleClass
import org.utbot.examples.samples.et.ClassSimpleCatch
import org.utbot.examples.statics.substitution.StaticSubstitution
import org.utbot.examples.statics.substitution.StaticSubstitutionExamples
import org.utbot.framework.plugin.api.util.fieldId
import org.utbot.framework.plugin.api.util.signature
import org.utbot.instrumentation.execute
import org.utbot.instrumentation.instrumentation.coverage.BranchCoverageInstrumentation
import org.utbot.instrumentation.instrumentation.coverage.collectCoverage
import org.utbot.instrumentation.util.StaticEnvironment
import org.utbot.instrumentation.withInstrumentation
class TestBranchCoverageInstrumentation {
lateinit var utContext: AutoCloseable
@Test
fun testIfBranches() {
withInstrumentation(
BranchCoverageInstrumentation.Factory,
ExampleClass::class.java.protectionDomain.codeSource.location.path
) { executor ->
val testObject = ExampleClass()
executor.execute(ExampleClass::bar, arrayOf(testObject, 2))
val coverageInfo1 = executor.collectCoverage(ExampleClass::class.java)
assertEquals(2, coverageInfo1.visitedInstrs.size)
assertEquals(1..3, coverageInfo1.methodToInstrRange[ExampleClass::bar.signature])
executor.execute(ExampleClass::bar, arrayOf(testObject, 0))
val coverageInfo2 = executor.collectCoverage(ExampleClass::class.java)
assertEquals(2, coverageInfo2.visitedInstrs.size)
assertEquals(1..3, coverageInfo2.methodToInstrRange[ExampleClass::bar.signature])
}
}
@Test
fun testTryCatch() {
withInstrumentation(
BranchCoverageInstrumentation.Factory,
ClassSimpleCatch::class.java.protectionDomain.codeSource.location.path
) { executor ->
executor.execute(ClassSimpleCatch::A_catches, emptyArray())
val coverageInfo1 = executor.collectCoverage(ClassSimpleCatch::class.java)
assertEquals(2, coverageInfo1.visitedInstrs.size)
assertEquals(3..5, coverageInfo1.methodToInstrRange[ClassSimpleCatch::A_catches.signature])
executor.execute(ClassSimpleCatch::A_catchesWrongException, emptyArray())
val coverageInfo2 = executor.collectCoverage(ClassSimpleCatch::class.java)
assertEquals(0, coverageInfo2.visitedInstrs.size)
assertEquals(7..9, coverageInfo2.methodToInstrRange[ClassSimpleCatch::A_catchesWrongException.signature])
}
}
@Test
fun testTernaryOperator() {
withInstrumentation(
BranchCoverageInstrumentation.Factory,
StaticSubstitutionExamples::class.java.protectionDomain.codeSource.location.path
) { executor ->
val testObject = StaticSubstitutionExamples()
val emptyStaticEnvironment = StaticEnvironment()
val res1 = executor.execute(
StaticSubstitutionExamples::lessThanZero,
arrayOf(testObject),
parameters = emptyStaticEnvironment
)
val staticEnvironment = StaticEnvironment(
StaticSubstitution::mutableValue.fieldId to -1
)
val res2 = executor.execute(
StaticSubstitutionExamples::lessThanZero,
arrayOf(testObject),
parameters = staticEnvironment
)
val coverageInfo = executor.collectCoverage(StaticSubstitutionExamples::class.java)
assertEquals(res1.getOrNull(), 5)
assertEquals(res2.getOrNull(), 0)
assertEquals(coverageInfo.visitedInstrs, (1..3).toList())
}
}
}