Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPietzschmann committed Nov 21, 2024
1 parent a748bf7 commit e7bca36
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 30 deletions.
2 changes: 2 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
configure(return_covr_result = TRUE)

tryCatch(get_connection(),
warning = function(e) {
skip(sprintf("flowr is not reachable under %s:%s", get_option("flowr_host"), get_option("flowr_port")))
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-config.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_that("Reading config options set with 'configure' yields the set value", {
configure(flowr_host = "test.host", flowr_port = 1234, measure_time = TRUE, return_covr_result = TRUE)

expect_equal(get_option("flowr_host"), "test.host")
expect_equal(get_option("flowr_port"), 1234)
expect_equal(get_option("return_covr_result"), TRUE)
expect_equal(get_option("measure_time"), TRUE)
})
138 changes: 138 additions & 0 deletions tests/testthat/test-coverage.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
test_that("empty tests have n:wo coverage", {
file <- file_with_content("add <- function(a,b) a+b")
test <- file_with_content("")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), 0)

file <- file_with_content("")
test <- file_with_content("")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), NaN)

file <- file_with_content("")
test <- file_with_content("
library(testthat)
expect_true(TRUE)
")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), NaN)
})

test_that("test without (real) assertion has zero slicing coverage", {
file <- file_with_content("add <- function(a,b) a+b")
test <- file_with_content("
library(testthat)
add(1,2)
")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), 0)

file <- file_with_content("add <- function(a,b) a+b")
test <- file_with_content("
library(testthat)
add(1,2)
expect_true(TRUE)
")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), 0)
})

test_that("assertion in source file does is not sliced for", {
file <- file_with_content("
add <- function(a,b){
testthat::expect_equal(1+2, 3)
a+b
}
")
test <- file_with_content("add(1,2)")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), 0)

test <- file_with_content("
library(testthat)
expect_equal(add(1,2), 3)
")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), 50)
})

test_that("slicing coverage can equal normal coverage", {
file <- file_with_content("add <- function(a,b) a+b")
test <- file_with_content("
library(testthat)
expect_equal(add(1,2), 3)
")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$covr), covr::percent_coverage(cov$coverage))
})

test_that("slicing coverage does not equal normal coverage for simple inputs", {
file <- file_with_content("
add <- function(a,b) {
x <<- 2;
a + b
}")
test <- file_with_content("
library(testthat)
expect_equal(add(1,2), 3)
")

cov <- file_coverage(file, test)
expect(covr::percent_coverage(cov$covr) != covr::percent_coverage(cov$coverage), "coverage should not be equal")
})

test_that("Coverage over multiple functions", {
file <- file_with_content("
add <- function(a,b) {
x <- add_helper(a)
y <- add_helper(b)
x + y
}
add_helper <- function(n) {
x <- 0
while(x < n) {
x <- x + 1
}
return(x)
}
")
test <- file_with_content("
library(testthat)
expect_equal(add(1,2), 3)
")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), 100)

file <- file_with_content("
add <- function(a,b) {
x <- add_helper(a)
y <- add_helper(b)
x + y
}
add_helper <- function(n) {
x <- 0
while(x < n) {
x <- x + 1
}
return(x)
}
")
test <- file_with_content("
library(testthat)
expect_equal(add(1,2), 3)
")

cov <- file_coverage(file, test)
expect_equal(covr::percent_coverage(cov$coverage), 100)
})
17 changes: 17 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that("we can find all pakage test files", {
tmp_dir <- tempdir()
dest_zip <- file.path(tmp_dir, "covr.zip")
covr_dir <- file.path(tmp_dir, "covr-3.6.0")
tryCatch(download.file(
url = "https://github.com/r-lib/covr/archive/refs/tags/v3.6.0.zip",
destfile = dest_zip,
quiet = TRUE
), error = function(e) skip("could not download package"))
unzip(dest_zip, exdir = tmp_dir)

sources <- get_pkg_source_files(covr_dir)
tests <- get_pkg_test_files(covr_dir)

expect_equal(length(sources$files), 27)
expect_equal(length(tests$files), 73)
})
30 changes: 0 additions & 30 deletions tests/testthat/test_file_coverage.R

This file was deleted.

0 comments on commit e7bca36

Please sign in to comment.