@@ -28,42 +28,28 @@ test_that("assignment_linter blocks disallowed usages", {
28
28
29
29
test_that(" arguments handle <<- and ->/->> correctly" , {
30
30
linter <- assignment_linter()
31
+ linter_yes_right <- assignment_linter(operator = c(" ->" , " ->>" ))
31
32
lint_msg_right <- rex :: rex(" Replace ->> by assigning to a specific environment" )
32
33
33
34
expect_lint(" 1 -> blah" , rex :: rex(" Use one of <-, <<- for assignment, not ->." ), linter )
34
35
expect_lint(" 1 ->> blah" , lint_msg_right , linter )
35
36
36
37
# <<- is only blocked optionally
37
38
expect_lint(" 1 <<- blah" , NULL , linter )
38
- expect_warning(
39
- expect_lint(
40
- " 1 <<- blah" ,
41
- rex :: rex(" Replace <<- by assigning to a specific environment" ),
42
- assignment_linter(allow_cascading_assign = FALSE )
43
- ),
44
- " allow_cascading_assign"
39
+ expect_lint(
40
+ " 1 <<- blah" ,
41
+ rex :: rex(" Replace <<- by assigning to a specific environment" ),
42
+ assignment_linter(operator = " <-" )
45
43
)
46
44
47
45
# blocking -> can be disabled
48
- expect_warning(
49
- expect_lint(" 1 -> blah" , NULL , assignment_linter(allow_right_assign = TRUE )),
50
- " allow_right_assign"
51
- )
52
- expect_warning(
53
- expect_lint(" 1 ->> blah" , NULL , assignment_linter(allow_right_assign = TRUE )),
54
- " allow_right_assign"
55
- )
56
- # blocked under cascading assign but not under right assign --> blocked
57
- expect_warning(
58
- expect_warning(
59
- expect_lint(
60
- " 1 ->> blah" ,
61
- lint_msg_right ,
62
- assignment_linter(allow_cascading_assign = FALSE , allow_right_assign = TRUE )
63
- ),
64
- " allow_cascading_assign"
65
- ),
66
- " allow_right_assign"
46
+ expect_lint(" 1 -> blah" , NULL , linter_yes_right )
47
+ expect_lint(" 1 ->> blah" , NULL , linter_yes_right )
48
+ # we can also differentiate '->' and '->>'
49
+ expect_lint(
50
+ " 1 ->> blah" ,
51
+ lint_msg_right ,
52
+ assignment_linter(operator = c(" <-" , " ->" ))
67
53
)
68
54
})
69
55
@@ -79,16 +65,13 @@ test_that("arguments handle trailing assignment operators correctly", {
79
65
)
80
66
81
67
expect_lint(" x <<-\n y" , rex :: rex(" <<- should not be trailing" ), linter )
82
- expect_warning(
83
- expect_lint(
84
- " x <<-\n y" ,
85
- list (
86
- rex :: rex(" Replace <<- by assigning to a specific environment" ),
87
- rex :: rex(" Assignment <<- should not be trailing" )
88
- ),
89
- assignment_linter(allow_trailing = FALSE , allow_cascading_assign = FALSE )
68
+ expect_lint(
69
+ " x <<-\n y" ,
70
+ list (
71
+ rex :: rex(" Replace <<- by assigning to a specific environment" ),
72
+ rex :: rex(" Assignment <<- should not be trailing" )
90
73
),
91
- " allow_cascading_assign "
74
+ assignment_linter( operator = " <- " , allow_trailing = FALSE )
92
75
)
93
76
94
77
expect_lint(" x <- #Test \n y" , rex :: rex(" <- should not be trailing" ), linter )
@@ -117,13 +100,10 @@ test_that("arguments handle trailing assignment operators correctly", {
117
100
),
118
101
linter
119
102
)
120
- expect_warning(
121
- expect_lint(
122
- " is %>%\n gather(measure, value, -Species) %>%\n arrange(-value) ->\n is_long" ,
123
- rex :: rex(" -> should not be trailing" ),
124
- assignment_linter(allow_right_assign = TRUE , allow_trailing = FALSE )
125
- ),
126
- " allow_right_assign"
103
+ expect_lint(
104
+ " is %>%\n gather(measure, value, -Species) %>%\n arrange(-value) ->\n is_long" ,
105
+ rex :: rex(" -> should not be trailing" ),
106
+ assignment_linter(operator = " ->" , allow_trailing = FALSE )
127
107
)
128
108
129
109
expect_lint(
@@ -201,10 +181,7 @@ test_that("allow_trailing interacts correctly with comments in braced expression
201
181
202
182
test_that(" %<>% throws a lint" , {
203
183
expect_lint(" x %<>% sum()" , " Avoid the assignment pipe %<>%" , assignment_linter())
204
- expect_warning(
205
- expect_lint(" x %<>% sum()" , NULL , assignment_linter(allow_pipe_assign = TRUE )),
206
- " allow_pipe_assign"
207
- )
184
+ expect_lint(" x %<>% sum()" , NULL , assignment_linter(operator = " %<>%" ))
208
185
209
186
# interaction with allow_trailing
210
187
expect_lint(
@@ -218,23 +195,20 @@ test_that("%<>% throws a lint", {
218
195
})
219
196
220
197
test_that(" multiple lints throw correct messages" , {
221
- expect_warning(
222
- expect_lint(
223
- trim_some(" {
224
- x <<- 1
225
- y ->> 2
226
- z -> 3
227
- x %<>% as.character()
228
- }" ),
229
- list (
230
- list (message = " Replace <<- by assigning to a specific environment" , line_number = 2L ),
231
- list (message = " Replace ->> by assigning to a specific environment" , line_number = 3L ),
232
- list (message = " Use <- for assignment, not ->" , line_number = 4L ),
233
- list (message = " Avoid the assignment pipe %<>%" , line_number = 5L )
234
- ),
235
- assignment_linter(allow_cascading_assign = FALSE )
198
+ expect_lint(
199
+ trim_some(" {
200
+ x <<- 1
201
+ y ->> 2
202
+ z -> 3
203
+ x %<>% as.character()
204
+ }" ),
205
+ list (
206
+ list (message = " Replace <<- by assigning to a specific environment" , line_number = 2L ),
207
+ list (message = " Replace ->> by assigning to a specific environment" , line_number = 3L ),
208
+ list (message = " Use <- for assignment, not ->" , line_number = 4L ),
209
+ list (message = " Avoid the assignment pipe %<>%" , line_number = 5L )
236
210
),
237
- " allow_cascading_assign "
211
+ assignment_linter( operator = " <- " )
238
212
)
239
213
})
240
214
@@ -304,48 +278,107 @@ test_that("assignment operator can be toggled", {
304
278
})
305
279
306
280
test_that(" multiple lints throw correct messages when both = and <- are allowed" , {
307
- expect_warning(
308
- expect_lint(
309
- trim_some(" {
310
- x <<- 1
311
- y ->> 2
312
- z -> 3
313
- x %<>% as.character()
314
- foo <- 1
315
- bar = 2
316
- }" ),
317
- list (
318
- list (message = " Replace <<- by assigning to a specific environment" , line_number = 2L ),
319
- list (message = " Replace ->> by assigning to a specific environment" , line_number = 3L ),
320
- list (message = " Use one of =, <- for assignment, not ->" , line_number = 4L ),
321
- list (message = " Avoid the assignment pipe %<>%" , line_number = 5L )
322
- ),
323
- assignment_linter(allow_cascading_assign = FALSE , operator = c(" =" , " <-" ))
281
+ expect_lint(
282
+ trim_some(" {
283
+ x <<- 1
284
+ y ->> 2
285
+ z -> 3
286
+ x %<>% as.character()
287
+ foo <- 1
288
+ bar = 2
289
+ }" ),
290
+ list (
291
+ list (message = " Replace <<- by assigning to a specific environment" , line_number = 2L ),
292
+ list (message = " Replace ->> by assigning to a specific environment" , line_number = 3L ),
293
+ list (message = " Use one of =, <- for assignment, not ->" , line_number = 4L ),
294
+ list (message = " Avoid the assignment pipe %<>%" , line_number = 5L )
324
295
),
325
- " allow_cascading_assign "
296
+ assignment_linter( operator = c( " = " , " <- " ))
326
297
)
327
298
})
328
299
329
300
test_that(" multiple lints throw correct messages when = is required" , {
330
- expect_warning(
331
- expect_lint(
332
- trim_some(" {
333
- x <<- 1
334
- y ->> 2
335
- z -> 3
336
- x %<>% as.character()
337
- foo <- 1
338
- bar = 2
339
- }" ),
340
- list (
341
- list (message = " Replace <<- by assigning to a specific environment" , line_number = 2L ),
342
- list (message = " Replace ->> by assigning to a specific environment" , line_number = 3L ),
343
- list (message = " Use = for assignment, not ->" , line_number = 4L ),
344
- list (message = " Avoid the assignment pipe %<>%" , line_number = 5L ),
345
- list (message = " Use = for assignment, not <-" , line_number = 6L )
346
- ),
347
- assignment_linter(allow_cascading_assign = FALSE , operator = " =" )
301
+ expect_lint(
302
+ trim_some(" {
303
+ x <<- 1
304
+ y ->> 2
305
+ z -> 3
306
+ x %<>% as.character()
307
+ foo <- 1
308
+ bar = 2
309
+ }" ),
310
+ list (
311
+ list (message = " Replace <<- by assigning to a specific environment" , line_number = 2L ),
312
+ list (message = " Replace ->> by assigning to a specific environment" , line_number = 3L ),
313
+ list (message = " Use = for assignment, not ->" , line_number = 4L ),
314
+ list (message = " Avoid the assignment pipe %<>%" , line_number = 5L ),
315
+ list (message = " Use = for assignment, not <-" , line_number = 6L )
316
+ ),
317
+ assignment_linter(operator = " =" )
318
+ )
319
+ })
320
+
321
+ # tests copy-pasted from earlier suite and embellished with warnings; to be removed
322
+ test_that(" Deprecated arguments work & warn as intended" , {
323
+ expect_warning(regexp = " allow_cascading_assign" , {
324
+ linter_no_cascade <- assignment_linter(allow_cascading_assign = FALSE )
325
+ })
326
+ expect_warning(regexp = " allow_right_assign" , {
327
+ linter_yes_right <- assignment_linter(allow_right_assign = TRUE )
328
+ })
329
+ expect_warning(regexp = " allow_right_assign" , expect_warning(regexp = " allow_cascading_assign" , {
330
+ linter_no_cascade_yes_right <- assignment_linter(allow_cascading_assign = FALSE , allow_right_assign = TRUE )
331
+ }))
332
+ expect_warning(regexp = " allow_cascading_assign" , {
333
+ linter_no_cascade_no_trailing <- assignment_linter(allow_trailing = FALSE , allow_cascading_assign = FALSE )
334
+ })
335
+ expect_warning(regexp = " allow_right_assign" , {
336
+ linter_yes_right_no_trailing <- assignment_linter(allow_right_assign = TRUE , allow_trailing = FALSE )
337
+ })
338
+ expect_warning(regexp = " allow_pipe_assign" , {
339
+ linter_yes_pipe <- assignment_linter(allow_pipe_assign = TRUE )
340
+ })
341
+
342
+ expect_lint(
343
+ " 1 <<- blah" ,
344
+ rex :: rex(" Replace <<- by assigning to a specific environment" ),
345
+ linter_no_cascade
346
+ )
347
+ expect_lint(" 1 -> blah" , NULL , linter_yes_right )
348
+ expect_lint(" 1 ->> blah" , NULL , linter_yes_right )
349
+
350
+ expect_lint(
351
+ " 1 ->> blah" ,
352
+ lint_msg_right ,
353
+ linter_no_cascade_yes_right
354
+ )
355
+ expect_lint(
356
+ " x <<-\n y" ,
357
+ list (
358
+ rex :: rex(" Replace <<- by assigning to a specific environment" ),
359
+ rex :: rex(" Assignment <<- should not be trailing" )
360
+ ),
361
+ linter_no_cascade_no_trailing
362
+ )
363
+ expect_lint(
364
+ " is %>%\n gather(measure, value, -Species) %>%\n arrange(-value) ->\n is_long" ,
365
+ rex :: rex(" -> should not be trailing" ),
366
+ linter_yes_right_no_trailing
367
+ )
368
+ expect_lint(" x %<>% sum()" , NULL , linter_yes_pipe )
369
+ expect_lint(
370
+ trim_some(" {
371
+ x <<- 1
372
+ y ->> 2
373
+ z -> 3
374
+ x %<>% as.character()
375
+ }" ),
376
+ list (
377
+ list (message = " Replace <<- by assigning to a specific environment" , line_number = 2L ),
378
+ list (message = " Replace ->> by assigning to a specific environment" , line_number = 3L ),
379
+ list (message = " Use <- for assignment, not ->" , line_number = 4L ),
380
+ list (message = " Avoid the assignment pipe %<>%" , line_number = 5L )
348
381
),
349
- " allow_cascading_assign "
382
+ linter_no_cascade
350
383
)
351
384
})
0 commit comments