@@ -12,7 +12,14 @@ get_n_warning <- function(f) {
12
12
13
13
get_n_data.frame <- function (f ) {
14
14
d <- getParseData(parse(f , keep.source = TRUE ))
15
- sum(d $ token == " SYMBOL_FUNCTION_CALL" & d $ text == " data.frame" )
15
+
16
+ idx_base <- d $ token == " SYMBOL_PACKAGE" & d $ text == " base"
17
+ idx_colons <- d $ token == " NS_GET" & d $ text == " ::"
18
+ # exclude the case when the `data.frame` is prefixed with `base::`
19
+ idx_base_prefixed <- c(FALSE , FALSE , idx_base [1 : (nrow(d ) - 2 )]) & c(FALSE , idx_colons [1 : (nrow(d ) - 1 )])
20
+
21
+ idx_data.frame <- d $ token == " SYMBOL_FUNCTION_CALL" & d $ text == " data.frame"
22
+ sum(idx_data.frame & ! idx_base_prefixed )
16
23
}
17
24
18
25
test_that(" `get_n_*() detects number of calls properly" , {
@@ -21,7 +28,8 @@ test_that("`get_n_*() detects number of calls properly", {
21
28
c(
22
29
' stop("foo!")' ,
23
30
' warning("bar!")' ,
24
- " data.frame(x = 1)"
31
+ " data.frame(x = 1)" ,
32
+ " base::data.frame(x = 1)" # this is not counted
25
33
),
26
34
" tmp.R"
27
35
)
@@ -32,7 +40,15 @@ test_that("`get_n_*() detects number of calls properly", {
32
40
})
33
41
34
42
# Pattern is needed filter out files such as ggplot2.rdb, which is created when running covr::package_coverage()
35
- R_files <- list.files(" ../../R" , pattern = " .*\\ .(R|r)$" , full.names = TRUE )
43
+ R_paths <- c(
44
+ " ../../R" , # in the case of devtools::test()
45
+ " ../../00_pkg_src/ggplot2/R" # in the case of R CMD check
46
+ )
47
+ R_files <- list.files(R_paths , pattern = " .*\\ .(R|r)$" , full.names = TRUE )
48
+
49
+ test_that(" list up R files properly" , {
50
+ expect_true(length(R_files ) > 0 )
51
+ })
36
52
37
53
test_that(" do not use stop()" , {
38
54
stops <- vapply(R_files , get_n_stop , integer(1 ))
@@ -44,7 +60,7 @@ test_that("do not use warning()", {
44
60
expect_equal(sum(warnings ), 0 )
45
61
})
46
62
47
- test_that(" do not use data.frame(), use `data_frame()` or `new_data_frame()`" , {
63
+ test_that(" do not use data.frame(), use `data_frame()` or `new_data_frame()`, or add `base::` prefix " , {
48
64
data.frames <- vapply(R_files , get_n_data.frame , integer(1 ))
49
65
expect_equal(sum(data.frames ), 0 )
50
66
})
0 commit comments