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" , {
3
2
linter <- commented_code_linter()
4
- expect_s3_class(linter , " linter" )
5
3
6
4
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()
7
25
8
26
expect_lint(" # blah <- 1" , lint_msg , linter )
9
27
@@ -30,26 +48,6 @@ test_that("returns the correct linting", {
30
48
linter
31
49
)
32
50
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
-
53
51
expect_lint(
54
52
trim_some("
55
53
d <- t.test(
@@ -76,26 +74,23 @@ test_that("returns the correct linting", {
76
74
linter
77
75
)
78
76
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
+
80
91
for (op in test_ops ) {
81
92
expect_lint(paste(" i" , op , " 1" , collapse = " " ), NULL , linter )
82
93
expect_lint(paste(" # something like i" , op , " 1" , collapse = " " ), NULL , linter )
83
94
expect_lint(paste(" # i" , op , " 1" , collapse = " " ), lint_msg , linter )
84
95
}
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 )
101
96
})
0 commit comments