Skip to content

Fix tests/testthat/test-prohibited-functions.R #4446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions tests/testthat/test-prohibited-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ get_n_warning <- function(f) {

get_n_data.frame <- function(f) {
d <- getParseData(parse(f, keep.source = TRUE))
sum(d$token == "SYMBOL_FUNCTION_CALL" & d$text == "data.frame")

idx_base <- d$token == "SYMBOL_PACKAGE" & d$text == "base"
idx_colons <- d$token == "NS_GET" & d$text == "::"
# exclude the case when the `data.frame` is prefixed with `base::`
idx_base_prefixed <- c(FALSE, FALSE, idx_base[1:(nrow(d) - 2)]) & c(FALSE, idx_colons[1:(nrow(d) - 1)])

idx_data.frame <- d$token == "SYMBOL_FUNCTION_CALL" & d$text == "data.frame"
sum(idx_data.frame & !idx_base_prefixed)
}

test_that("`get_n_*() detects number of calls properly", {
Expand All @@ -21,7 +28,8 @@ test_that("`get_n_*() detects number of calls properly", {
c(
'stop("foo!")',
'warning("bar!")',
"data.frame(x = 1)"
"data.frame(x = 1)",
"base::data.frame(x = 1)" # this is not counted
),
"tmp.R"
)
Expand All @@ -32,7 +40,15 @@ test_that("`get_n_*() detects number of calls properly", {
})

# Pattern is needed filter out files such as ggplot2.rdb, which is created when running covr::package_coverage()
R_files <- list.files("../../R", pattern = ".*\\.(R|r)$", full.names = TRUE)
R_paths <- c(
"../../R", # in the case of devtools::test()
"../../00_pkg_src/ggplot2/R" # in the case of R CMD check
)
R_files <- list.files(R_paths, pattern = ".*\\.(R|r)$", full.names = TRUE)

test_that("list up R files properly", {
expect_true(length(R_files) > 0)
})

test_that("do not use stop()", {
stops <- vapply(R_files, get_n_stop, integer(1))
Expand All @@ -44,7 +60,7 @@ test_that("do not use warning()", {
expect_equal(sum(warnings), 0)
})

test_that("do not use data.frame(), use `data_frame()` or `new_data_frame()`", {
test_that("do not use data.frame(), use `data_frame()` or `new_data_frame()`, or add `base::` prefix", {
data.frames <- vapply(R_files, get_n_data.frame, integer(1))
expect_equal(sum(data.frames), 0)
})