Skip to content

Commit 9a9affd

Browse files
Use public interface to test internal functions: Part-3
Part of #1692 And cleanup to break the tests down into skip vs block blocks.
1 parent 080613e commit 9a9affd

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed
+35-40
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
test_that("returns the correct linting", {
2-
lint_msg <- rex::rex("Commented code should be removed.")
1+
test_that("commented_code_linter skips allowed usages", {
32
linter <- commented_code_linter()
4-
expect_s3_class(linter, "linter")
53

64
expect_lint("blah", NULL, linter)
5+
expect_lint("#' blah <- 1", NULL, linter)
6+
expect_lint(c("a <- 1", "# comment without code"), NULL, linter)
7+
expect_lint(c("a <- 1", "# comment without code"), NULL, linter)
8+
expect_lint(c("a <- 1", "## whatever"), NULL, linter)
9+
10+
expect_lint("TRUE", NULL, linter)
11+
expect_lint("#' @examples", NULL, linter)
12+
expect_lint("#' foo(1) # list(1)", NULL, linter) # comment in roxygen block ignored
13+
expect_lint("1+1 # gives 2", NULL, linter)
14+
expect_lint("# Non-existent:", NULL, linter)
15+
expect_lint("# 1-a", NULL, linter) # "-" removed from code operators
16+
expect_lint("1+1 # for example cat(\"123\")", NULL, linter)
17+
18+
# regression test for #451
19+
expect_lint("c('#a#' = 1)", NULL, linter)
20+
})
21+
22+
test_that("commented_code_linter blocks disallowed usages", {
23+
lint_msg <- rex::rex("Commented code should be removed.")
24+
linter <- commented_code_linter()
725

826
expect_lint("# blah <- 1", lint_msg, linter)
927

@@ -30,26 +48,6 @@ test_that("returns the correct linting", {
3048
linter
3149
)
3250

33-
expect_lint("#' blah <- 1", NULL, linter)
34-
35-
expect_lint(
36-
c("a <- 1", "# comment without code"),
37-
NULL,
38-
linter
39-
)
40-
41-
expect_lint(
42-
c("a <- 1", "# comment without code"),
43-
NULL,
44-
linter
45-
)
46-
47-
expect_lint(
48-
c("a <- 1", "## whatever"),
49-
NULL,
50-
linter
51-
)
52-
5351
expect_lint(
5452
trim_some("
5553
d <- t.test(
@@ -76,26 +74,23 @@ test_that("returns the correct linting", {
7674
linter
7775
)
7876

79-
test_ops <- append(lintr:::ops[lintr:::ops != "%[^%]*%"], values = c("%>%", "%anything%"))
77+
expect_lint("1+1 # cat('123')", lint_msg, linter)
78+
expect_lint("#expect_ftype(1e-12 , t)", lint_msg, linter)
79+
})
80+
81+
test_that("commented_code_linter can detect operators in comments and lint correctly", {
82+
linter <- commented_code_linter()
83+
lint_msg <- rex::rex("Commented code should be removed.")
84+
85+
test_ops <- c(
86+
"+", "=", "==", "!=", "<=", ">=", "<-", "<<-", "<", ">", "->",
87+
"->>", "%%", "/", "^", "*", "**", "|", "||", "&", "&&", "%>%",
88+
"%anything%"
89+
)
90+
8091
for (op in test_ops) {
8192
expect_lint(paste("i", op, "1", collapse = ""), NULL, linter)
8293
expect_lint(paste("# something like i", op, "1", collapse = ""), NULL, linter)
8394
expect_lint(paste("# i", op, "1", collapse = ""), lint_msg, linter)
8495
}
85-
86-
expect_lint("TRUE", NULL, linter)
87-
expect_lint("#' @examples", NULL, linter)
88-
expect_lint("#' foo(1) # list(1)", NULL, linter) # comment in roxygen block ignored
89-
expect_lint("1+1 # gives 2", NULL, linter)
90-
91-
expect_lint("# Non-existent:", NULL, linter) # "-" removed from code operators
92-
expect_lint("# 1-a", NULL, linter)
93-
94-
expect_lint("1+1 # for example cat(\"123\")", NULL, linter)
95-
96-
expect_lint("1+1 # cat('123')", lint_msg, linter)
97-
expect_lint("#expect_ftype(1e-12 , t)", lint_msg, linter)
98-
99-
# regression test for #451
100-
expect_lint("c('#a#' = 1)", NULL, linter)
10196
})

0 commit comments

Comments
 (0)