Skip to content

Commit e7bca36

Browse files
Added more tests
1 parent a748bf7 commit e7bca36

File tree

5 files changed

+165
-30
lines changed

5 files changed

+165
-30
lines changed

tests/testthat/setup.R

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
configure(return_covr_result = TRUE)
2+
13
tryCatch(get_connection(),
24
warning = function(e) {
35
skip(sprintf("flowr is not reachable under %s:%s", get_option("flowr_host"), get_option("flowr_port")))

tests/testthat/test-config.R

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test_that("Reading config options set with 'configure' yields the set value", {
2+
configure(flowr_host = "test.host", flowr_port = 1234, measure_time = TRUE, return_covr_result = TRUE)
3+
4+
expect_equal(get_option("flowr_host"), "test.host")
5+
expect_equal(get_option("flowr_port"), 1234)
6+
expect_equal(get_option("return_covr_result"), TRUE)
7+
expect_equal(get_option("measure_time"), TRUE)
8+
})

tests/testthat/test-coverage.R

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
test_that("empty tests have n:wo coverage", {
2+
file <- file_with_content("add <- function(a,b) a+b")
3+
test <- file_with_content("")
4+
5+
cov <- file_coverage(file, test)
6+
expect_equal(covr::percent_coverage(cov$coverage), 0)
7+
8+
file <- file_with_content("")
9+
test <- file_with_content("")
10+
11+
cov <- file_coverage(file, test)
12+
expect_equal(covr::percent_coverage(cov$coverage), NaN)
13+
14+
file <- file_with_content("")
15+
test <- file_with_content("
16+
library(testthat)
17+
expect_true(TRUE)
18+
")
19+
20+
cov <- file_coverage(file, test)
21+
expect_equal(covr::percent_coverage(cov$coverage), NaN)
22+
})
23+
24+
test_that("test without (real) assertion has zero slicing coverage", {
25+
file <- file_with_content("add <- function(a,b) a+b")
26+
test <- file_with_content("
27+
library(testthat)
28+
add(1,2)
29+
")
30+
31+
cov <- file_coverage(file, test)
32+
expect_equal(covr::percent_coverage(cov$coverage), 0)
33+
34+
file <- file_with_content("add <- function(a,b) a+b")
35+
test <- file_with_content("
36+
library(testthat)
37+
add(1,2)
38+
expect_true(TRUE)
39+
")
40+
41+
cov <- file_coverage(file, test)
42+
expect_equal(covr::percent_coverage(cov$coverage), 0)
43+
})
44+
45+
test_that("assertion in source file does is not sliced for", {
46+
file <- file_with_content("
47+
add <- function(a,b){
48+
testthat::expect_equal(1+2, 3)
49+
a+b
50+
}
51+
")
52+
test <- file_with_content("add(1,2)")
53+
54+
cov <- file_coverage(file, test)
55+
expect_equal(covr::percent_coverage(cov$coverage), 0)
56+
57+
test <- file_with_content("
58+
library(testthat)
59+
expect_equal(add(1,2), 3)
60+
")
61+
62+
cov <- file_coverage(file, test)
63+
expect_equal(covr::percent_coverage(cov$coverage), 50)
64+
})
65+
66+
test_that("slicing coverage can equal normal coverage", {
67+
file <- file_with_content("add <- function(a,b) a+b")
68+
test <- file_with_content("
69+
library(testthat)
70+
expect_equal(add(1,2), 3)
71+
")
72+
73+
cov <- file_coverage(file, test)
74+
expect_equal(covr::percent_coverage(cov$covr), covr::percent_coverage(cov$coverage))
75+
})
76+
77+
test_that("slicing coverage does not equal normal coverage for simple inputs", {
78+
file <- file_with_content("
79+
add <- function(a,b) {
80+
x <<- 2;
81+
a + b
82+
}")
83+
test <- file_with_content("
84+
library(testthat)
85+
expect_equal(add(1,2), 3)
86+
")
87+
88+
cov <- file_coverage(file, test)
89+
expect(covr::percent_coverage(cov$covr) != covr::percent_coverage(cov$coverage), "coverage should not be equal")
90+
})
91+
92+
test_that("Coverage over multiple functions", {
93+
file <- file_with_content("
94+
add <- function(a,b) {
95+
x <- add_helper(a)
96+
y <- add_helper(b)
97+
x + y
98+
}
99+
100+
add_helper <- function(n) {
101+
x <- 0
102+
while(x < n) {
103+
x <- x + 1
104+
}
105+
return(x)
106+
}
107+
")
108+
test <- file_with_content("
109+
library(testthat)
110+
expect_equal(add(1,2), 3)
111+
")
112+
113+
cov <- file_coverage(file, test)
114+
expect_equal(covr::percent_coverage(cov$coverage), 100)
115+
116+
file <- file_with_content("
117+
add <- function(a,b) {
118+
x <- add_helper(a)
119+
y <- add_helper(b)
120+
x + y
121+
}
122+
123+
add_helper <- function(n) {
124+
x <- 0
125+
while(x < n) {
126+
x <- x + 1
127+
}
128+
return(x)
129+
}
130+
")
131+
test <- file_with_content("
132+
library(testthat)
133+
expect_equal(add(1,2), 3)
134+
")
135+
136+
cov <- file_coverage(file, test)
137+
expect_equal(covr::percent_coverage(cov$coverage), 100)
138+
})

tests/testthat/test-utils.R

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
test_that("we can find all pakage test files", {
2+
tmp_dir <- tempdir()
3+
dest_zip <- file.path(tmp_dir, "covr.zip")
4+
covr_dir <- file.path(tmp_dir, "covr-3.6.0")
5+
tryCatch(download.file(
6+
url = "https://github.com/r-lib/covr/archive/refs/tags/v3.6.0.zip",
7+
destfile = dest_zip,
8+
quiet = TRUE
9+
), error = function(e) skip("could not download package"))
10+
unzip(dest_zip, exdir = tmp_dir)
11+
12+
sources <- get_pkg_source_files(covr_dir)
13+
tests <- get_pkg_test_files(covr_dir)
14+
15+
expect_equal(length(sources$files), 27)
16+
expect_equal(length(tests$files), 73)
17+
})

tests/testthat/test_file_coverage.R

-30
This file was deleted.

0 commit comments

Comments
 (0)