covr icon indicating copy to clipboard operation
covr copied to clipboard

Feature request: Condition plus branch coverage

Open IndrajeetPatil opened this issue 2 years ago • 1 comments

Preamble

Consider the following code and its corresponding test:

empty <- function(df) {
  (is.null(df) || nrow(df) == 0 || ncol(df) == 0)
}

test_that("empty works", {
  expect_true(empty(NULL))
})

If you use {covr} to compute code coverage for a package that contains this function, it will be 100%. But, note that the test covers only one of the conditions from the control statement (is.null(df)); the other two are untested and may contain bugs.

FR

Is it possible for {covr} to also take into account all conditions—either on the same line or on different lines—while computing code coverage?

With such an update, in the example above, the code coverage should be 0% because the tests don't fully cover all the conditions on that line. I am not sure what should happen if the conditional statement spans multiple lines, but I can see how 0% could be reasonable in that case as well.

Related to, but still distinct from https://github.com/r-lib/covr/issues/279

IndrajeetPatil avatar Mar 07 '24 06:03 IndrajeetPatil

This is not really distinct from #279. It's still about how R creates srcrefs. https://github.com/r-lib/covr/issues/279#issuecomment-567044540

Your conditional statement is seen by R as one expression. There are no branches. Hence, covr sees it as one expression. You can try to wrap each of your conditionals with curly braces and separate them into new lines if you want.

radbasa avatar Mar 15 '24 14:03 radbasa