Skip to content

Commit

Permalink
Added functional test on branch counter
Browse files Browse the repository at this point in the history
  • Loading branch information
shanshin committed Jan 21, 2022
1 parent 2cc1e93 commit 8c8055b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kotlinx.kover.test.functional.cases.counters

import kotlinx.kover.api.*
import kotlinx.kover.test.functional.cases.utils.*
import kotlinx.kover.test.functional.core.BaseGradleScriptTest
import kotlin.test.*

internal class BranchCounterTests: BaseGradleScriptTest() {
@Test
fun testBranchCounter() {
builder("Test simple branch counters")
.engines(CoverageEngine.INTELLIJ, CoverageEngine.JACOCO)
.sources("branches")
.build()
.run("build") {
xml(defaultXmlReport()) {
val counter = methodCounter("org.jetbrains.MyBranchedClass", "foo", type = "BRANCH")
assertNotNull(counter)
assertEquals(1, counter.covered)
assertEquals(3, counter.missed)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,52 @@ private class XmlReportImpl(file: File) : XmlReport {

val reportElement = ((document.getElementsByTagName("report").item(0)) as Element)

var classElement: Element? = null

reportElement.forEach("package") loop@{
if (getAttribute("name") == packageName) {
forEach("class") {
if (getAttribute("name") == correctedClassName) {
classElement = this
return@loop
}
}
}
val counterElement: Element? = reportElement
.filter("package", "name", packageName)
?.filter("class", "name", correctedClassName)
?.filter("counter", "type", type)

return counterElement?.let {
Counter(
type,
it.getAttribute("missed").toInt(),
it.getAttribute("covered").toInt()
)
}
}

classElement?.forEach("counter") {
if (getAttribute("type") == type) {
return Counter(type, this.getAttribute("missed").toInt(), this.getAttribute("covered").toInt())
}
override fun methodCounter(className: String, methodName: String, type: String): Counter? {
val correctedClassName = className.replace('.', '/')
val packageName = correctedClassName.substringBeforeLast('/')

val reportElement = ((document.getElementsByTagName("report").item(0)) as Element)

val counterElement: Element? = reportElement
.filter("package", "name", packageName)
?.filter("class", "name", correctedClassName)
?.filter("method", "name", methodName)
?.filter("counter", "type", type)

return counterElement?.let {
Counter(
type,
it.getAttribute("missed").toInt(),
it.getAttribute("covered").toInt()
)
}
return null
}
}

private inline fun Element.forEach(tag: String, block: Element.() -> Unit) {
private fun Element.filter(tag: String, attributeName: String, attributeValue: String): Element? {
val elements = getElementsByTagName(tag)

for (i in 0 until elements.length) {
val element = elements.item(i) as Element
if (element.parentNode == this) {
element.block()
if (element.getAttribute(attributeName) == attributeValue) {
return element
}
}
}
return null
}

Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ internal class Counter(val type: String, val missed: Int, val covered: Int) {

internal interface XmlReport {
fun classCounter(className: String, type: String = "INSTRUCTION"): Counter?
fun methodCounter(className: String, methodName: String, type: String = "INSTRUCTION"): Counter?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jetbrains

class MyBranchedClass {
fun foo(value: Int) {
if (value < 0) {
println("LE")
} else if (value == 0) {
println("EQ")
} else {
println("GE")
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jetbrains.serialuser

import org.jetbrains.MyBranchedClass
import kotlin.test.Test

class TestClass {
@Test
fun testBranches() {
MyBranchedClass().foo(-20)
}
}

0 comments on commit 8c8055b

Please sign in to comment.