1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
testthat::test_that("paren_body_linter returns correct lints", {
linter <- paren_body_linter()
lint_msg <- rex::rex("Put a space between a right parenthesis and a body expression.")
# No space after the closing parenthesis prompts a lint
expect_lint("function()test", lint_msg, linter)
expect_lint("print('hello')\nx <- function(x)NULL\nprint('hello')", lint_msg, linter)
expect_lint("if (TRUE)test", lint_msg, linter)
expect_lint("while (TRUE)test", lint_msg, linter)
expect_lint("for (i in seq_along(1))test", lint_msg, linter)
# A space after the closing parenthesis does not prompt a lint
expect_lint("function() test", NULL, linter)
# Symbols after the closing parenthesis of a function call do not prompt a lint
expect_lint("head(mtcars)$cyl", NULL, linter)
# paren_body_linter returns the correct line number
expect_lint(
"print('hello')\nx <- function(x)NULL\nprint('hello')",
list(line_number = 2L),
linter
)
expect_lint(
"function()test",
list(
line_number = 1L,
column_number = 11L,
type = "style",
line = "function()test",
ranges = list(c(11L, 14L))
),
linter
)
# paren_body_linter does not lint when the function body is defined on a new line
expect_lint("function()\n test", NULL, linter)
# paren_body_linter does not lint comments
expect_lint("#function()test", NULL, linter)
# multiple lints on the same line
expect_lint("function()if(TRUE)while(TRUE)test", list(lint_msg, lint_msg, lint_msg), linter)
# No space after the closing parenthesis of an anonymous function prompts a lint
skip_if_not_r_version("4.1.0")
expect_lint("\\()test", lint_msg, linter)
})
test_that("multi-line versions are caught", {
linter <- paren_body_linter()
lint_msg <- rex::rex("Put a space between a right parenthesis and a body expression.")
expect_lint(
trim_some("
function(var
)x
"),
lint_msg,
linter
)
expect_lint(
trim_some("
if (cond
)x
"),
lint_msg,
linter
)
expect_lint(
trim_some("
while (cond
)x
"),
lint_msg,
linter
)
skip_if_not_r_version("4.1.0")
expect_lint(
trim_some("
\\(var
)x
"),
lint_msg,
linter
)
})
test_that("function shorthand is handled", {
skip_if_not_r_version("4.1.0")
linter <- paren_body_linter()
lint_msg <- rex::rex("Put a space between a right parenthesis and a body expression.")
expect_lint("\\()test", lint_msg, linter)
})
|