我正在使用Jacoco检查我的测试的覆盖范围,我尝试了很多方法,但是它仍然警告4个分支中的一个没有通过。
fun countingDeleteDemo() : Int {
return list.count { it.isDeleted() }
}我怎么知道错过了哪个分支?我读过一些关于逻辑true && false的文章,但是是否有关于覆盖工具错误的文档或官方链接?
发布于 2022-04-15 11:56:14
Jacoco检查字节代码的代码覆盖率,而不是源代码。据我所知,Jacoco没有显示哪个分支不见了。
如果使用IntelliJ IDEA,可以检查kotlin字节码。
在这里,我的IDE中丢失的分支。
// ... omitted
INVOKESTATIC kotlin/collections/CollectionsKt.throwCountOverflow ()V
// ... omittedKotlin's有许多内建扩展函数count for Iterable,Array .
我猜想你的list是一个简单的List,所以是一个Iterable。您没有比Int.MAX_VALUE更多的元素,这将使计数为负数。但是,可以将一个可迭代性传递给Int.MAX_VALUE元素,它可能超出了count元素。
作为一个好公民,kotlin jvm实现检查计数溢出。
您可以查看实现细节。
public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Int {
if (this is Collection && isEmpty()) return 0
var count = 0
for (element in this) if (predicate(element)) checkCountOverflow(++count)
return count
}@PublishedApi
@SinceKotlin("1.3")
@InlineOnly
internal actual inline fun checkCountOverflow(count: Int): Int {
if (count < 0) {
if (apiVersionIsAtLeast(1, 3, 0))
throwCountOverflow()
else
throw ArithmeticException("Count overflow has happened.")
}
return count
}https://stackoverflow.com/questions/71879875
复制相似问题