Skip to content

Commit

Permalink
More tests for brace_linter() with pipes and formula syntax (#1828)
Browse files Browse the repository at this point in the history
* More tests for `brace_linter()` with pipes and formula syntax

* user shorter lint messages
  • Loading branch information
IndrajeetPatil authored Dec 10, 2022
1 parent 7de378f commit d4c484b
Showing 1 changed file with 162 additions and 29 deletions.
191 changes: 162 additions & 29 deletions tests/testthat/test-brace_linter.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("brace_linter lints braces correctly", {
open_curly_msg <- rex::rex(
"Opening curly braces should never go on their own line and should always be followed by a new line."
"Opening curly braces should never go on their own line"
)
closed_curly_msg <- rex::rex(paste(
"Closing curly-braces should always be on their own line,",
Expand Down Expand Up @@ -73,16 +73,6 @@ test_that("brace_linter lints braces correctly", {
linter
)

expect_lint(
trim_some("
out <- lapply(stuff, function(i) {
do_something(i)
}) %>% unlist
"),
NULL,
linter
)

# ,<\n>{ is allowed
expect_lint(
trim_some("
Expand Down Expand Up @@ -148,22 +138,6 @@ test_that("brace_linter lints braces correctly", {
linter
)

# %>%\n{ is allowed
expect_lint(
trim_some("
letters %>%
{
tibble(
lo = .,
hi = toupper(.)
)
} %>%
mutate(row_id = row_number())
"),
NULL,
linter
)

# (\n{ is allowed optionally
expect_lint(
trim_some("
Expand Down Expand Up @@ -275,8 +249,8 @@ test_that("brace_linter lints spaces before open braces", {
{2}
"),
list(
rex::rex("Opening curly braces should never go on their own line and should always be followed by a new line."),
rex::rex("Closing curly-braces should always be on their own line, unless they are followed by an else.")
rex::rex("Opening curly braces should never go on their own line"),
rex::rex("Closing curly-braces should always be on their own line")
), # , but not lint_msg
linter
)
Expand Down Expand Up @@ -348,6 +322,7 @@ test_that("brace_linter lints function expressions correctly", {

test_that("brace_linter lints if/else matching braces correctly", {
linter <- brace_linter()

expect_lint("if (TRUE) 1 else 2", NULL, linter)
expect_lint("if (TRUE) 1", NULL, linter)

Expand Down Expand Up @@ -420,3 +395,161 @@ test_that("empty brace expressions are always allowed inline", {
expect_lint("while (FALSE) {}", NULL, brace_linter(allow_single_line = TRUE))
expect_lint("while (FALSE) { }", NULL, brace_linter(allow_single_line = TRUE))
})

test_that("formula syntax is linted properly", {
linter <- brace_linter()
lint_msg_open <- rex::rex("Opening curly braces should never go on their own line")
lint_msg_closed <- rex::rex("Closing curly-braces should always be on their own line")

expect_lint(
trim_some("
map(
.x = 1:4,
.f = ~ {
.x + 1
}
)"),
NULL,
linter
)

expect_lint(
trim_some("
map(
.x = 1:4,
.f = ~ {.x + 1}
)"),
list(
list(message = lint_msg_open, line_number = 3L, column_number = 10L),
list(message = lint_msg_closed, line_number = 3L, column_number = 17L)
),
linter
)

expect_lint(
trim_some("
map(
.x = 1:4,
.f = ~ { .x + 1
}
)"),
list(
list(message = lint_msg_open, line_number = 3L, column_number = 10L)
),
linter
)

expect_lint(
trim_some("
map(
.x = 1:4,
.f = ~ {
.x + 1}
)"),
list(
list(message = lint_msg_closed, line_number = 4L, column_number = 17L)
),
linter
)
})

test_that("code with pipes is handled correctly", {
linter <- brace_linter()
lint_msg_open <- rex::rex("Opening curly braces should never go on their own line")
lint_msg_closed <- rex::rex("Closing curly-braces should always be on their own line")

expect_lint(
trim_some("
out <- lapply(stuff, function(i) {
do_something(i)
}) %>% unlist
"),
NULL,
linter
)

expect_lint(
trim_some("
1:4 %>% {
sum(.)
}
"),
NULL,
linter
)

# %>%\n{ is allowed
expect_lint(
trim_some("
1:4 %>%
{
sum(.)
}
"),
NULL,
linter
)

expect_lint(
trim_some("
1:4 %>% { sum(.)
}
"),
list(
list(message = lint_msg_open, line_number = 1L, column_number = 9L)
),
linter
)

expect_lint(
trim_some("
1:4 %>%
{
sum(.) }
"),
list(
list(message = lint_msg_closed, line_number = 3L, column_number = 12L)
),
linter
)

expect_lint(
trim_some("
1:4 %>%
{ sum(.) }
"),
list(
list(message = lint_msg_closed, line_number = 2L, column_number = 12L)
),
linter
)

expect_lint(
"1:4 %>% { sum(.) }",
list(
list(message = lint_msg_open, line_number = 1L, column_number = 9L),
list(message = lint_msg_closed, line_number = 1L, column_number = 18L)
),
linter
)

skip_if_not_r_version("4.1.0")

expect_lint(
trim_some("
out <- lapply(stuff, function(i) {
do_something(i)
}) |> unlist()
"),
NULL,
linter
)

expect_lint(
"local({ 1:4 |> sum() })",
list(
list(message = lint_msg_open, line_number = 1L, column_number = 7L)
),
linter
)
})

0 comments on commit d4c484b

Please sign in to comment.