Skip to content

Commit e4edd79

Browse files
Added tests for finding assertions
1 parent 1c8ab24 commit e4edd79

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

tests/testthat/setup.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configure(return_covr_result = TRUE)
1+
configure(return_covr_result = TRUE, slicing_points = TRUE)
22

33
tryCatch(get_connection(),
44
warning = function(e) {

tests/testthat/test-coverage.R

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,121 @@ test_that("Coverage over multiple functions", {
139139
# The slice is a bit weird here. It excludes `add_helper` and the calls to it
140140
# But 33.3 is the correct value for the given slice (independent of its correctness)
141141
expect_equal(covr::percent_coverage(cov$coverage), 33.3, tolerance = 0.1)
142+
143+
file <- file_with_content("
144+
create_custom_list <- function(names, base_value) {
145+
custom_list <- setNames(
146+
lapply(seq_along(names), function(i) base_value * i),
147+
names
148+
)
149+
return(custom_list)
150+
}
151+
152+
transform_list_elements <- function(input_list, filter_func, transform_func) {
153+
filtered_list <- input_list[sapply(input_list, filter_func)]
154+
transformed_list <- lapply(filtered_list, transform_func)
155+
return(transformed_list)
156+
}
157+
158+
filtered_list <- function() transform_list_elements(
159+
create_custom_list(c('apple', 'banana', 'cherry'), 10),
160+
function(x) x > 15,
161+
function(x) x * 2
162+
)
163+
")
164+
test <- file_with_content("
165+
library(testthat)
166+
expect_equal(length(filtered_list()), 2)
167+
")
168+
169+
cov <- file_coverage(file, test)
170+
expect_equal(covr::percent_coverage(cov$coverage), 100)
171+
})
172+
173+
test_that("Coverage over multiple functions and files", {
174+
file1 <- file_with_content("
175+
add <- function(a,b) {
176+
x <- a+b+1
177+
return(sub_one(x))
178+
}
179+
")
180+
file2 <- file_with_content("
181+
i_was_forgotten <- function() print('What a bummer')
182+
sub_one <- function(x) x-1
183+
")
184+
test1 <- file_with_content("
185+
library(testthat)
186+
expect_equal(add(1,2), 3)
187+
")
188+
test2 <- file_with_content("
189+
library(testthat)
190+
expect_equal(sub_one(0), -1)
191+
")
192+
193+
cov <- file_coverage(c(file1, file2), c(test1, test2))
194+
expect_equal(covr::percent_coverage(cov$coverage), 75)
142195
})
143196

197+
test_that("We can find all assertions", {
198+
file <- file_with_content("")
199+
200+
file_testthat <- file_with_content("
201+
library(testthat)
202+
expect_true(TRUE)
203+
expect_length(c(1,2,3), 3)
204+
tryCatch(fail(), error = function(e) NULL)
205+
expect(TRUE, 'When thats not true, I dont know what is')
206+
")
207+
208+
file_unitizer <- file_with_content("
209+
library(unitizer)
210+
unitizer_sect('test', {
211+
1+1
212+
})
213+
")
214+
215+
file_rlang <- file_with_content("
216+
library(rlang)
217+
tryCatch(warn('Warning'), warning = function(w) NULL)
218+
tryCatch(abort('Yeet'), error = function(e) NULL)
219+
")
220+
221+
file_xpectr <- file_with_content("
222+
library(xpectr)
223+
stop_if(FALSE)
224+
warn_if(FALSE)
225+
")
226+
227+
file_testit <- file_with_content("
228+
library(testit)
229+
assert(1+1 == 2)
230+
")
231+
232+
file_runit <- file_with_content("
233+
library(RUnit)
234+
checkEquals(2, 1+1)
235+
checkTrue(TRUE)
236+
")
237+
238+
file_r <- file_with_content("
239+
tryCatch(stop('Stopperino'), error = function(e) NULL)
240+
stopifnot(1+1 == 2)
241+
")
242+
243+
file_assertthat <- file_with_content("
244+
library(assertthat)
245+
assert_that(1+1 == 2)
246+
")
247+
248+
for (p in list(list("testthat", 4), list("unitizer", 1), list("rlang", 2), list("xpectr", 2), list("testit", 1), list("runit", 2), list("r", 2), list("assertthat", 1))) {
249+
pkg <- p[[1]]
250+
expected_assertions <- p[[2]]
251+
252+
test_file <- get(paste0("file_", pkg), inherits = TRUE)
253+
slicing_points <- file_coverage(file, test_file)$slicing_points
254+
255+
test_that(paste("we can find all assertions in", pkg), {
256+
expect_length(slicing_points, expected_assertions)
257+
})
258+
}
259+
})

0 commit comments

Comments
 (0)