|
1 | 1 | test_that("vector_logic_linter skips allowed usages", { |
2 | 2 | linter <- vector_logic_linter() |
3 | 3 |
|
4 | | - expect_lint("if (TRUE) 5 else if (TRUE) 2", NULL, linter) |
5 | | - expect_lint("if (TRUE || FALSE) 1; while (TRUE && FALSE) 2", NULL, linter) |
| 4 | + expect_no_lint("if (TRUE) 5 else if (TRUE) 2", linter) |
| 5 | + expect_no_lint("if (TRUE || FALSE) 1; while (TRUE && FALSE) 2", linter) |
6 | 6 |
|
7 | 7 | # function calls and extractions may aggregate to scalars -- only catch |
8 | 8 | # usages at the highest logical level |
9 | | - expect_lint("if (agg_function(x & y)) 1", NULL, linter) |
10 | | - expect_lint("if (DT[x | y, cond]) 1", NULL, linter) |
| 9 | + expect_no_lint("if (agg_function(x & y)) 1", linter) |
| 10 | + expect_no_lint("if (DT[x | y, cond]) 1", linter) |
11 | 11 |
|
12 | 12 | # don't match potentially OK usages nested within calls |
13 | | - expect_lint("if (TRUE && any(TRUE | FALSE)) 4", NULL, linter) |
| 13 | + expect_no_lint("if (TRUE && any(TRUE | FALSE)) 4", linter) |
14 | 14 | # even if the usage is nested in those calls (b/181915948) |
15 | | - expect_lint("if (TRUE && any(TRUE | FALSE | TRUE)) 4", NULL, linter) |
| 15 | + expect_no_lint("if (TRUE && any(TRUE | FALSE | TRUE)) 4", linter) |
16 | 16 |
|
17 | 17 | # don't match potentially OK usages in the branch itself |
18 | | - lines <- trim_some(" |
19 | | - if (TRUE) { |
20 | | - x | y |
21 | | - } |
22 | | - ") |
23 | | - expect_lint(lines, NULL, linter) |
24 | | - |
| 18 | + expect_no_lint( |
| 19 | + trim_some(" |
| 20 | + if (TRUE) { |
| 21 | + x | y |
| 22 | + } |
| 23 | + "), |
| 24 | + linter |
| 25 | + ) |
25 | 26 |
|
26 | 27 | # valid nested usage within aggregator |
27 | | - expect_lint("testthat::expect_false(any(TRUE | TRUE))", NULL, linter) |
| 28 | + expect_no_lint("testthat::expect_false(any(TRUE | TRUE))", linter) |
28 | 29 | }) |
29 | 30 |
|
30 | 31 | test_that("vector_logic_linter blocks simple disallowed usages", { |
@@ -63,41 +64,40 @@ test_that("vector_logic_linter catches usages in expect_true()/expect_false()", |
63 | 64 | }) |
64 | 65 |
|
65 | 66 | test_that("vector_logic_linter doesn't get mixed up from complex usage", { |
66 | | - expect_lint( |
| 67 | + expect_no_lint( |
67 | 68 | trim_some(" |
68 | 69 | if (a) { |
69 | 70 | expect_true(ok) |
70 | 71 | x <- 2 |
71 | 72 | a | b |
72 | 73 | } |
73 | 74 | "), |
74 | | - NULL, |
75 | 75 | vector_logic_linter() |
76 | 76 | ) |
77 | 77 | }) |
78 | 78 |
|
79 | 79 | test_that("vector_logic_linter recognizes some false positves around bitwise &/|", { |
80 | 80 | linter <- vector_logic_linter() |
81 | 81 |
|
82 | | - expect_lint("if (info & as.raw(12)) { }", NULL, linter) |
83 | | - expect_lint("if (as.raw(12) & info) { }", NULL, linter) |
84 | | - expect_lint("if (info | as.raw(12)) { }", NULL, linter) |
85 | | - expect_lint("if (info & as.octmode('100')) { }", NULL, linter) |
86 | | - expect_lint("if (info | as.octmode('011')) { }", NULL, linter) |
87 | | - expect_lint("if (info & as.hexmode('100')) { }", NULL, linter) |
88 | | - expect_lint("if (info | as.hexmode('011')) { }", NULL, linter) |
| 82 | + expect_no_lint("if (info & as.raw(12)) { }", linter) |
| 83 | + expect_no_lint("if (as.raw(12) & info) { }", linter) |
| 84 | + expect_no_lint("if (info | as.raw(12)) { }", linter) |
| 85 | + expect_no_lint("if (info & as.octmode('100')) { }", linter) |
| 86 | + expect_no_lint("if (info | as.octmode('011')) { }", linter) |
| 87 | + expect_no_lint("if (info & as.hexmode('100')) { }", linter) |
| 88 | + expect_no_lint("if (info | as.hexmode('011')) { }", linter) |
89 | 89 | # implicit as.octmode() coercion |
90 | | - expect_lint("if (info & '100') { }", NULL, linter) |
91 | | - expect_lint("if (info | '011') { }", NULL, linter) |
92 | | - expect_lint("if ('011' | info) { }", NULL, linter) |
| 90 | + expect_no_lint("if (info & '100') { }", linter) |
| 91 | + expect_no_lint("if (info | '011') { }", linter) |
| 92 | + expect_no_lint("if ('011' | info) { }", linter) |
93 | 93 |
|
94 | 94 | # further nesting |
95 | | - expect_lint("if ((info & as.raw(12)) == as.raw(12)) { }", NULL, linter) |
96 | | - expect_lint("if ((info | as.raw(12)) == as.raw(12)) { }", NULL, linter) |
97 | | - expect_lint('if ((mode & "111") != as.octmode("111")) { }', NULL, linter) |
98 | | - expect_lint('if ((mode | "111") != as.octmode("111")) { }', NULL, linter) |
99 | | - expect_lint('if ((mode & "111") != as.hexmode("111")) { }', NULL, linter) |
100 | | - expect_lint('if ((mode | "111") != as.hexmode("111")) { }', NULL, linter) |
| 95 | + expect_no_lint("if ((info & as.raw(12)) == as.raw(12)) { }", linter) |
| 96 | + expect_no_lint("if ((info | as.raw(12)) == as.raw(12)) { }", linter) |
| 97 | + expect_no_lint('if ((mode & "111") != as.octmode("111")) { }', linter) |
| 98 | + expect_no_lint('if ((mode | "111") != as.octmode("111")) { }', linter) |
| 99 | + expect_no_lint('if ((mode & "111") != as.hexmode("111")) { }', linter) |
| 100 | + expect_no_lint('if ((mode | "111") != as.hexmode("111")) { }', linter) |
101 | 101 | }) |
102 | 102 |
|
103 | 103 | test_that("incorrect subset/filter usage is caught", { |
@@ -128,46 +128,62 @@ test_that("subsetting logic handles nesting", { |
128 | 128 | expect_lint("filter(x, a & b || c)", or_msg, linter) |
129 | 129 | expect_lint("filter(x, a && b | c)", and_msg, linter) |
130 | 130 |
|
| 131 | + # adversarial commenting |
| 132 | + expect_lint( |
| 133 | + trim_some(" |
| 134 | + filter(x, a #comment |
| 135 | + && b | c) |
| 136 | + "), |
| 137 | + and_msg, |
| 138 | + linter |
| 139 | + ) |
| 140 | + |
| 141 | + expect_lint( |
| 142 | + trim_some(" |
| 143 | + filter(x, a && #comment |
| 144 | + b | c) |
| 145 | + "), |
| 146 | + and_msg, |
| 147 | + linter |
| 148 | + ) |
| 149 | + |
131 | 150 | # but not valid usage |
132 | | - expect_lint("filter(x, y < mean(y, na.rm = AA && BB))", NULL, linter) |
133 | | - expect_lint("subset(x, y < mean(y, na.rm = AA && BB) & y > 0)", NULL, linter) |
134 | | - expect_lint("subset(x, y < x[y > 0, drop = AA && BB, y])", NULL, linter) |
| 151 | + expect_no_lint("filter(x, y < mean(y, na.rm = AA && BB))", linter) |
| 152 | + expect_no_lint("subset(x, y < mean(y, na.rm = AA && BB) & y > 0)", linter) |
| 153 | + expect_no_lint("subset(x, y < x[y > 0, drop = AA && BB, y])", linter) |
135 | 154 | }) |
136 | 155 |
|
137 | 156 | test_that("filter() handling is conservative about stats::filter()", { |
138 | 157 | linter <- vector_logic_linter() |
139 | 158 | and_msg <- rex::rex("Use `&` in subsetting expressions") |
140 | 159 |
|
141 | 160 | # NB: this should be invalid, filter= is a vector argument |
142 | | - expect_lint("stats::filter(x, y && z)", NULL, linter) |
| 161 | + expect_no_lint("stats::filter(x, y && z)", linter) |
143 | 162 | # The only logical argument to stats::filter(), exclude by keyword |
144 | | - expect_lint("filter(x, circular = y && z)", NULL, linter) |
| 163 | + expect_no_lint("filter(x, circular = y && z)", linter) |
145 | 164 | # But presence of circular= doesn't invalidate lint |
146 | 165 | expect_lint("filter(x, circular = TRUE, y && z)", and_msg, linter) |
147 | 166 | expect_lint("filter(x, y && z, circular = TRUE)", and_msg, linter) |
148 | | - expect_lint( |
| 167 | + expect_no_lint( |
149 | 168 | trim_some(" |
150 | 169 | filter(x, circular # comment |
151 | 170 | = y && z) |
152 | 171 | "), |
153 | | - NULL, |
154 | 172 | linter |
155 | 173 | ) |
156 | | - expect_lint( |
| 174 | + expect_no_lint( |
157 | 175 | trim_some(" |
158 | 176 | filter(x, circular = # comment |
159 | 177 | y && z) |
160 | 178 | "), |
161 | | - NULL, |
162 | 179 | linter |
163 | 180 | ) |
164 | | - expect_lint( |
| 181 | + expect_no_lint( |
165 | 182 | trim_some(" |
166 | 183 | filter(x, circular # comment |
167 | 184 | = # comment |
168 | 185 | y && z) |
169 | 186 | "), |
170 | | - NULL, |
171 | 187 | linter |
172 | 188 | ) |
173 | 189 | }) |
|
0 commit comments