Skip to content

Commit

Permalink
Use trim_some() everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Feb 3, 2025
1 parent a2af76b commit 5202604
Showing 1 changed file with 64 additions and 29 deletions.
93 changes: 64 additions & 29 deletions tests/testthat/test-assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,54 +67,75 @@ test_that("arguments handle <<- and ->/->> correctly", {
})

test_that("arguments handle trailing assignment operators correctly", {
linter <- assignment_linter(allow_trailing = FALSE)
expect_lint("x <- y", NULL, linter)
expect_lint("foo(bar = 1)", NULL, linter)
linter_default <- assignment_linter()
linter_no_trailing <- assignment_linter(allow_trailing = FALSE)
expect_lint("x <- y", NULL, linter_no_trailing)
expect_lint("foo(bar = 1)", NULL, linter_no_trailing)

expect_lint(
"foo(bar =\n1)",
trim_some("
foo(bar =
1)
"),
rex::rex("= should not be trailing at the end of a line."),
linter
linter_no_trailing
)

expect_lint("x <<-\ny", rex::rex("<<- should not be trailing"), linter)
expect_lint(
"x <<-\ny",
trim_some("
x <<-
y
"),
rex::rex("<<- should not be trailing"),
linter_no_trailing
)
expect_lint(
trim_some("
x <<-
y
"),
list(
rex::rex("Replace <<- by assigning to a specific environment"),
rex::rex("Assignment <<- should not be trailing")
),
assignment_linter(operator = "<-", allow_trailing = FALSE)
)

expect_lint("x <- #Test \ny", rex::rex("<- should not be trailing"), linter)

expect_lint(
"is_long <-\nis %>%\ngather(measure, value, -Species) %>%\narrange(-value)",
NULL,
assignment_linter()
)
expect_lint(
"is_long <-\nis %>%\ngather(measure, value, -Species) %>%\narrange(-value)",
trim_some("
x <- #Test
y
"),
rex::rex("<- should not be trailing"),
linter
linter_no_trailing
)

pipe_left_string <- trim_some("
is_long <-
is %>%
gather(measure, value, -Species) %>%
arrange(-value)
")
expect_lint(pipe_left_string, NULL, linter_default)
expect_lint(pipe_left_string, rex::rex("<- should not be trailing"), linter_no_trailing)

pipe_right_string <- trim_some("
is %>%
gather(measure, value, -Species) %>%
arrange(-value) ->
is_long
")
expect_lint(pipe_right_string, rex::rex("Use one of <-, <<- for assignment, not ->"), linter_default)
expect_lint(
"is %>%\ngather(measure, value, -Species) %>%\narrange(-value) ->\nis_long",
rex::rex("Use one of <-, <<- for assignment, not ->"),
assignment_linter()
)
expect_lint(
"is %>%\ngather(measure, value, -Species) %>%\narrange(-value) ->\nis_long",
pipe_right_string,
list(
rex::rex("Use one of <-, <<- for assignment, not ->"),
rex::rex("Assignment -> should not be trailing")
),
linter
linter_no_trailing
)
expect_lint(
"is %>%\ngather(measure, value, -Species) %>%\narrange(-value) ->\nis_long",
pipe_right_string,
rex::rex("-> should not be trailing"),
assignment_linter(operator = "->", allow_trailing = FALSE)
)
Expand All @@ -131,11 +152,14 @@ test_that("arguments handle trailing assignment operators correctly", {
list(message = "Assignment = should not be trailing at the end of a line", line_number = 1L, column_number = 6L),
list(message = "Assignment <- should not be trailing at the end of a line", line_number = 3L, column_number = 6L)
),
linter
linter_no_trailing
)

expect_lint(
"a =\n1",
trim_some("
a =
1
"),
"= should not be trailing",
assignment_linter(operator = "=", allow_trailing = FALSE)
)
Expand Down Expand Up @@ -198,7 +222,10 @@ test_that("%<>% throws a lint", {

# interaction with allow_trailing
expect_lint(
"x %<>%\n sum()",
trim_some("
x %<>%
sum()
"),
list(
"Avoid the assignment pipe %<>%",
"Assignment %<>% should not be trailing"
Expand Down Expand Up @@ -368,15 +395,23 @@ test_that("Deprecated arguments work & warn as intended", {
linter_no_cascade_yes_right
)
expect_lint(
"x <<-\ny",
trim_some("
x <<-
y
"),
list(
rex::rex("Replace <<- by assigning to a specific environment"),
rex::rex("Assignment <<- should not be trailing")
),
linter_no_cascade_no_trailing
)
expect_lint(
"is %>%\ngather(measure, value, -Species) %>%\narrange(-value) ->\nis_long",
trim_some("
is %>%
gather(measure, value, -Species) %>%
arrange(-value) ->
is_long
"),
rex::rex("-> should not be trailing"),
linter_yes_right_no_trailing
)
Expand Down

0 comments on commit 5202604

Please sign in to comment.