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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
context("braceless")
test_that("if", {
f <-
'f <- function(x) {
if (FALSE)
FALSE # never covered, used as anchor
if (x)
TRUE
else
FALSE
}'
cov <- code_coverage(f, "f(TRUE)")
expect_equal(zero_coverage(code_coverage(f, "f(TRUE)"))$line, c(3, 7))
expect_equal(zero_coverage(code_coverage(f, "f(FALSE)"))$line, c(3, 5))
expect_equal(zero_coverage(code_coverage(f, "f(TRUE);f(FALSE)"))$line, 3)
})
test_that("nested if else", {
f <-
'f <- function(x) {
if (FALSE)
FALSE # never covered, used as anchor
else if (x)
TRUE
else
FALSE
}'
cov <- code_coverage(f, "f(TRUE)")
expect_equal(zero_coverage(code_coverage(f, "f(TRUE)"))$line, c(3, 7))
expect_equal(zero_coverage(code_coverage(f, "f(FALSE)"))$line, c(3, 5))
expect_equal(zero_coverage(code_coverage(f, "f(TRUE);f(FALSE)"))$line, 3)
})
test_that("switch", {
f <-
'f <- function(x) {
switch(x,
a = 1,
b = 2,
c = d <- 1
)
}'
expect_equal(length(zero_coverage(code_coverage(f, "f(\"a\"); f(\"b\")"))$line),
1)
expect_equal(length(zero_coverage(code_coverage(f, "f(\"a\"); f(\"c\")"))$line),
1)
expect_equal(diff(zero_coverage(code_coverage(f, "f(\"a\"); f(\"d\")"))$line),
1)
})
test_that("switch with default value", {
f <-
'f <- function(x) {
switch(x,
a = 1,
b = 2,
c = d <- 1,
NULL
)
}'
expect_equal(length(zero_coverage(code_coverage(f, "f(\"a\"); f(\"b\"); f(\"c\")"))$line),
1)
expect_equal(length(zero_coverage(code_coverage(f, "f(\"a\"); f(\"c\")"))$line),
2)
})
test_that("switch with drop through", {
f <-
'f <- function(x) {
switch(x,
a = ,
b = 2,
c = d <- 1,
NULL
)
}'
res <- as.data.frame(code_coverage(f, 'f("a");f("b");f("c")'))
expect_equal(res$first_line, c(2, 4, 5, 6))
expect_equal(res$value, c(3, 2, 1, 0))
})
test_that("switch with ellipses", {
f <-
'f <- function(x, ...) {
switch(typeof(x), ...)
}'
res <- as.data.frame(code_coverage(f, "f(\"a\", character = TRUE)"))
expect_equal(res$first_line, 2)
expect_equal(res$value, 1)
f <-
'f <- function(x, ...) {
switch(typeof(x),
...,
character = TRUE)
}'
res <- as.data.frame(code_coverage(f, "f(\"a\")"))
expect_equal(res$first_line, c(2, 4))
expect_equal(res$value, c(1, 1))
})
|