Skip to content

Commit

Permalink
use trim_some() consistently (#2461)
Browse files Browse the repository at this point in the history
Co-authored-by: AshesITR <[email protected]>
  • Loading branch information
MichaelChirico and AshesITR authored Dec 19, 2023
1 parent 033c0d9 commit 72eeefd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
5 changes: 2 additions & 3 deletions tests/testthat/test-commented_code_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ test_that("commented_code_linter skips allowed usages", {

expect_lint("blah", NULL, linter)
expect_lint("#' blah <- 1", NULL, linter)
expect_lint(c("a <- 1", "# comment without code"), NULL, linter)
expect_lint(c("a <- 1", "# comment without code"), NULL, linter)
expect_lint(c("a <- 1", "## whatever"), NULL, linter)
expect_lint("a <- 1\n# comment without code", NULL, linter)
expect_lint("a <- 1\n## whatever", NULL, linter)

expect_lint("TRUE", NULL, linter)
expect_lint("#' @examples", NULL, linter)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ test_that("multiple checks", {
expect_success(expect_lint("a=1; b=2", list(c(message = lint_msg), c(message = lint_msg)), linter))
expect_success(expect_lint("a=1; b=2", list(c(line_number = 1L), c(linter = "assignment_linter")), linter))
expect_success(expect_lint("a=1; b=2", list(lint_msg, c(line = "a=1; b=2", type = "warning")), linter))
expect_success(expect_lint(c("a=1", "b=2"), list(c(line_number = 1L), c(line_number = 2L)), linter))
expect_failure(expect_lint(c("a=1", "b=2"), list(c(line_number = 2L), c(line_number = 2L)), linter))
expect_success(expect_lint("a=1\nb=2", list(c(line_number = 1L), c(line_number = 2L)), linter))
expect_failure(expect_lint("a=1\nb=2", list(c(line_number = 2L), c(line_number = 2L)), linter))

expect_success(expect_lint("a=1; b=2", list(list(line_number = 1L), list(line_number = 2L)), linter))
expect_failure(expect_lint("a=1; b=2", list(list(line_number = 2L), list(line_number = 2L)), linter))
Expand Down
63 changes: 39 additions & 24 deletions tests/testthat/test-library_call_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,26 @@ patrick::with_parameters_test_that(
expect_lint(sprintf("%1$s(x); y; %1$s(z)", call), NULL, linter)

# inline or potentially with gaps don't matter
lines <- c(
sprintf("%s(x)", call),
"y",
"",
"stopifnot(z)"
expect_lint(
trim_some(glue::glue("
{call}(x)
y
stopifnot(z)
")),
NULL,
linter
)
expect_lint(lines, NULL, linter)

# only suppressing calls with library()
lines_consecutive <- c(
sprintf("%s(x)", call),
sprintf("%s(y)", call)
expect_lint(
trim_some(glue::glue("
{call}(x)
{call}(y)
")),
NULL,
linter
)
expect_lint(lines_consecutive, NULL, linter)
},
.test_name = c("suppressMessages", "suppressPackageStartupMessages"),
call = c("suppressMessages", "suppressPackageStartupMessages")
Expand All @@ -359,25 +365,34 @@ patrick::with_parameters_test_that(
# one test of inline usage
expect_lint(sprintf("%1$s(library(x)); %1$s(library(y))", call), message, linter)

lines_gap <- c(
sprintf("%s(library(x))", call),
"",
sprintf("%s(library(y))", call)
expect_lint(
trim_some(glue::glue("
{call}(library(x))
{call}(library(y))
")),
message,
linter
)
expect_lint(lines_gap, message, linter)

lines_consecutive <- c(
sprintf("%s(require(x))", call),
sprintf("%s(require(y))", call)
expect_lint(
trim_some(glue::glue("
{call}(require(x))
{call}(require(y))
")),
message,
linter
)
expect_lint(lines_consecutive, message, linter)

lines_comment <- c(
sprintf("%s(library(x))", call),
"# a comment on y",
sprintf("%s(library(y))", call)
expect_lint(
trim_some(glue::glue("
{call}(library(x))
# a comment on y
{call}(library(y))
")),
message,
linter
)
expect_lint(lines_comment, message, linter)
},
.test_name = c("suppressMessages", "suppressPackageStartupMessages"),
call = c("suppressMessages", "suppressPackageStartupMessages")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-object_name_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ test_that("linter accepts vector of styles", {
linter <- object_name_linter(styles = c("camelCase", "dotted.case"))

expect_lint(
c("var.one <- 1", "varTwo <- 2", "var_three <- 3"),
"var.one <- 1\nvarTwo <- 2\nvar_three <- 3",
list(message = lint_msg, line_number = 3L, column_number = 1L),
linter
)
Expand Down

0 comments on commit 72eeefd

Please sign in to comment.