Skip to content

Commit 9e216c9

Browse files
Duplicate tests to separate deprecated vs. new behavior
1 parent 9792001 commit 9e216c9

File tree

1 file changed

+132
-99
lines changed

1 file changed

+132
-99
lines changed

tests/testthat/test-assignment_linter.R

Lines changed: 132 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,28 @@ test_that("assignment_linter blocks disallowed usages", {
2828

2929
test_that("arguments handle <<- and ->/->> correctly", {
3030
linter <- assignment_linter()
31+
linter_yes_right <- assignment_linter(operator = c("->", "->>"))
3132
lint_msg_right <- rex::rex("Replace ->> by assigning to a specific environment")
3233

3334
expect_lint("1 -> blah", rex::rex("Use one of <-, <<- for assignment, not ->."), linter)
3435
expect_lint("1 ->> blah", lint_msg_right, linter)
3536

3637
# <<- is only blocked optionally
3738
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 = "<-")
4543
)
4644

4745
# 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("<-", "->"))
6753
)
6854
})
6955

@@ -79,16 +65,13 @@ test_that("arguments handle trailing assignment operators correctly", {
7965
)
8066

8167
expect_lint("x <<-\ny", rex::rex("<<- should not be trailing"), linter)
82-
expect_warning(
83-
expect_lint(
84-
"x <<-\ny",
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 <<-\ny",
70+
list(
71+
rex::rex("Replace <<- by assigning to a specific environment"),
72+
rex::rex("Assignment <<- should not be trailing")
9073
),
91-
"allow_cascading_assign"
74+
assignment_linter(operator = "<-", allow_trailing = FALSE)
9275
)
9376

9477
expect_lint("x <- #Test \ny", rex::rex("<- should not be trailing"), linter)
@@ -117,13 +100,10 @@ test_that("arguments handle trailing assignment operators correctly", {
117100
),
118101
linter
119102
)
120-
expect_warning(
121-
expect_lint(
122-
"is %>%\ngather(measure, value, -Species) %>%\narrange(-value) ->\nis_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 %>%\ngather(measure, value, -Species) %>%\narrange(-value) ->\nis_long",
105+
rex::rex("-> should not be trailing"),
106+
assignment_linter(operator = "->", allow_trailing = FALSE)
127107
)
128108

129109
expect_lint(
@@ -201,10 +181,7 @@ test_that("allow_trailing interacts correctly with comments in braced expression
201181

202182
test_that("%<>% throws a lint", {
203183
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 = "%<>%"))
208185

209186
# interaction with allow_trailing
210187
expect_lint(
@@ -218,23 +195,20 @@ test_that("%<>% throws a lint", {
218195
})
219196

220197
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)
236210
),
237-
"allow_cascading_assign"
211+
assignment_linter(operator = "<-")
238212
)
239213
})
240214

@@ -304,48 +278,107 @@ test_that("assignment operator can be toggled", {
304278
})
305279

306280
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)
324295
),
325-
"allow_cascading_assign"
296+
assignment_linter(operator = c("=", "<-"))
326297
)
327298
})
328299

329300
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 <<-\ny",
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 %>%\ngather(measure, value, -Species) %>%\narrange(-value) ->\nis_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)
348381
),
349-
"allow_cascading_assign"
382+
linter_no_cascade
350383
)
351384
})

0 commit comments

Comments
 (0)