Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/testthat/test-absolute_path_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ test_that("is_long_path", {


test_that("returns the correct linting", {
msg <- rex::escape("Do not use absolute paths.")
lint_msg <- rex::escape("Do not use absolute paths.")

# strict mode
linter <- absolute_path_linter(lax = FALSE)
Expand Down Expand Up @@ -158,8 +158,8 @@ test_that("returns the correct linting", {
"/as:df"
)
for (path in absolute_path_strings) {
expect_lint(single_quote(path), msg, linter)
expect_lint(double_quote(path), msg, linter)
expect_lint(single_quote(path), lint_msg, linter)
expect_lint(double_quote(path), lint_msg, linter)
}

# lax mode: no check for strings that are likely not paths (too short or with special characters)
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-assignment_linter.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
test_that("returns the correct linting", {
linter <- assignment_linter()
msg <- rex::rex("Use <-, not =, for assignment.")
lint_msg <- rex::rex("Use <-, not =, for assignment.")

expect_lint("blah", NULL, linter)
expect_lint("blah <- 1", NULL, linter)
expect_lint("blah<-1", NULL, linter)
expect_lint("fun(blah=1)", NULL, linter)

expect_lint("blah=1", msg, linter)
expect_lint("blah = 1", msg, linter)
expect_lint("blah = fun(1)", msg, linter)
expect_lint("fun((blah = fun(1)))", msg, linter)
expect_lint("blah=1", lint_msg, linter)
expect_lint("blah = 1", lint_msg, linter)
expect_lint("blah = fun(1)", lint_msg, linter)
expect_lint("fun((blah = fun(1)))", lint_msg, linter)

expect_lint(
"blah = fun(1) {",
list(
msg,
lint_msg,
c(type = "error", "unexpected")
),
linter
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-brace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ test_that("brace_linter lints braces correctly", {

test_that("brace_linter lints spaces before open braces", {
linter <- brace_linter()
msg <- rex::rex("There should be a space before an opening curly brace.")
lint_msg <- rex::rex("There should be a space before an opening curly brace.")

expect_lint(
"blah <- function(){\n}",
list(
message = msg,
message = lint_msg,
column_number = 19L
),
linter
Expand All @@ -238,7 +238,7 @@ test_that("brace_linter lints spaces before open braces", {
expect_lint(
"\nblah <- function(){\n\n\n}",
list(
message = msg,
message = lint_msg,
column_number = 19L
),
linter
Expand All @@ -248,16 +248,16 @@ test_that("brace_linter lints spaces before open braces", {
expect_lint(
"a <- if (a){\n} else{\n}",
list(
list(message = msg, line_number = 1L, column_number = 12L),
list(message = msg, line_number = 2L, column_number = 7L)
list(message = lint_msg, line_number = 1L, column_number = 12L),
list(message = lint_msg, line_number = 2L, column_number = 7L)
),
linter
)

# should lint repeat{
expect_lint(
"repeat{\nblah\n}",
list(message = msg, line_number = 1L, column_number = 7L),
list(message = lint_msg, line_number = 1L, column_number = 7L),
linter
)

Expand All @@ -277,7 +277,7 @@ test_that("brace_linter lints spaces before open braces", {
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.")
), # , but not msg
), # , but not lint_msg
linter
)
})
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-class_equals_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ test_that("class_equals_linter skips allowed usages", {

test_that("class_equals_linter blocks simple disallowed usages", {
linter <- class_equals_linter()
msg <- rex::rex("Instead of comparing class(x) with ==")
lint_msg <- rex::rex("Instead of comparing class(x) with ==")

expect_lint("if (class(x) == 'character') stop('no')", msg, linter)
expect_lint("is_regression <- class(x) == 'lm'", msg, linter)
expect_lint("is_regression <- 'lm' == class(x)", msg, linter)
expect_lint("if (class(x) == 'character') stop('no')", lint_msg, linter)
expect_lint("is_regression <- class(x) == 'lm'", lint_msg, linter)
expect_lint("is_regression <- 'lm' == class(x)", lint_msg, linter)
})

test_that("class_equals_linter blocks usage of %in% for checking class", {
linter <- class_equals_linter()
msg <- rex::rex("Instead of comparing class(x) with %in%")
lint_msg <- rex::rex("Instead of comparing class(x) with %in%")

expect_lint("if ('character' %in% class(x)) stop('no')", msg, linter)
expect_lint("if (class(x) %in% 'character') stop('no')", msg, linter)
expect_lint("if ('character' %in% class(x)) stop('no')", lint_msg, linter)
expect_lint("if (class(x) %in% 'character') stop('no')", lint_msg, linter)
})

test_that("class_equals_linter blocks class(x) != 'klass'", {
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-commented_code_linter.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
test_that("returns the correct linting", {
msg <- rex::rex("Commented code should be removed.")
lint_msg <- rex::rex("Commented code should be removed.")
linter <- commented_code_linter()
expect_s3_class(linter, "linter")

expect_lint("blah", NULL, linter)

expect_lint("# blah <- 1", msg, linter)
expect_lint("# blah <- 1", lint_msg, linter)

expect_lint(
"bleurgh <- fun_call(1) # other_call()",
list(message = msg, column_number = 26L),
list(message = lint_msg, column_number = 26L),
linter
)

expect_lint(
" #blah <- 1",
list(message = msg, column_number = 3L),
list(message = lint_msg, column_number = 3L),
linter
)

Expand All @@ -26,7 +26,7 @@ test_that("returns the correct linting", {
line_without_comment <- 42L
#blah <- 1
"),
list(message = msg, line_number = 3L, column_number = 3L),
list(message = lint_msg, line_number = 3L, column_number = 3L),
linter
)

Expand Down Expand Up @@ -59,7 +59,7 @@ test_that("returns the correct linting", {
mu = 175
)
"),
list(message = msg, line_number = 3L),
list(message = lint_msg, line_number = 3L),
linter
)

Expand All @@ -72,15 +72,15 @@ test_that("returns the correct linting", {
, mu = 175
)
"),
list(message = msg, line_number = 3L),
list(message = lint_msg, line_number = 3L),
linter
)

test_ops <- append(lintr:::ops[lintr:::ops != "%[^%]*%"], values = c("%>%", "%anything%"))
for (op in test_ops) {
expect_lint(paste("i", op, "1", collapse = ""), NULL, linter)
expect_lint(paste("# something like i", op, "1", collapse = ""), NULL, linter)
expect_lint(paste("# i", op, "1", collapse = ""), msg, linter)
expect_lint(paste("# i", op, "1", collapse = ""), lint_msg, linter)
}

expect_lint("TRUE", NULL, linter)
Expand All @@ -93,8 +93,8 @@ test_that("returns the correct linting", {

expect_lint("1+1 # for example cat(\"123\")", NULL, linter)

expect_lint("1+1 # cat('123')", msg, linter)
expect_lint("#expect_ftype(1e-12 , t)", msg, linter)
expect_lint("1+1 # cat('123')", lint_msg, linter)
expect_lint("#expect_ftype(1e-12 , t)", lint_msg, linter)

# regression test for #451
expect_lint("c('#a#' = 1)", NULL, linter)
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-cyclocomp_linter.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
test_that("returns the correct linting", {
cc_linter_1 <- cyclocomp_linter(1L)
cc_linter_2 <- cyclocomp_linter(2L)
msg <- rex::rex("Functions should have cyclomatic complexity")
lint_msg <- rex::rex("Functions should have cyclomatic complexity")

expect_lint("if (TRUE) 1 else 2", NULL, cc_linter_2)
expect_lint("if (TRUE) 1 else 2", msg, cc_linter_1)
expect_lint("if (TRUE) 1 else 2", lint_msg, cc_linter_1)

expect_lint(
"function(x) {not parsing}",
Expand All @@ -14,7 +14,7 @@ test_that("returns the correct linting", {
complexity <- readLines(
system.file("example/complexity.R", package = "lintr")
)
expect_lint(complexity, msg, cc_linter_2)
expect_lint(complexity, lint_msg, cc_linter_2)
expect_lint(
complexity,
"should have cyclomatic complexity of less than 2, this has 10",
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-duplicate_argument_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ test_that("duplicate_argument_linter doesn't block allowed usages", {

test_that("duplicate_argument_linter blocks disallowed usages", {
linter <- duplicate_argument_linter()
msg <- rex::rex("Duplicate arguments in function call.")
lint_msg <- rex::rex("Duplicate arguments in function call.")

expect_lint("fun(arg = 1, arg = 2)", msg, linter)
expect_lint("fun(arg = 1, 'arg' = 2)", msg, linter)
expect_lint("fun(arg = 1, `arg` = 2)", msg, linter)
expect_lint("'fun'(arg = 1, arg = 2)", msg, linter)
expect_lint("(function(x, y) x + y)(x = 1, x = 2)", msg, linter)
expect_lint("dt[i = 1, i = 2]", msg, linter)
expect_lint("fun(arg = 1, arg = 2)", lint_msg, linter)
expect_lint("fun(arg = 1, 'arg' = 2)", lint_msg, linter)
expect_lint("fun(arg = 1, `arg` = 2)", lint_msg, linter)
expect_lint("'fun'(arg = 1, arg = 2)", lint_msg, linter)
expect_lint("(function(x, y) x + y)(x = 1, x = 2)", lint_msg, linter)
expect_lint("dt[i = 1, i = 2]", lint_msg, linter)

expect_lint(
"list(
var = 1,
var = 2
)",
msg,
lint_msg,
linter
)
})
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-equals_na_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_that("equals_na_linter skips allowed usages", {

# equals_na_linter should ignore strings and comments
expect_lint("is.na(x) # dont flag x == NA if inside a comment", NULL, linter)
expect_lint("msg <- 'dont flag x == NA if inside a string'", NULL, linter)
expect_lint("lint_msg <- 'dont flag x == NA if inside a string'", NULL, linter)

# nested NAs are okay
expect_lint("x==f(1, ignore = NA)", NULL, linter)
Expand Down Expand Up @@ -45,14 +45,14 @@ patrick::with_parameters_test_that(

test_that("equals_na_linter blocks disallowed usages in edge cases", {
linter <- equals_na_linter()
msg <- rex::rex("Use is.na for comparisons to NA (not == or !=)")
lint_msg <- rex::rex("Use is.na for comparisons to NA (not == or !=)")

# missing spaces around operators
expect_lint("x==NA", list(message = msg, line_number = 1L, column_number = 1L), linter)
expect_lint("x!=NA", list(message = msg, line_number = 1L, column_number = 1L), linter)
expect_lint("x==NA", list(message = lint_msg, line_number = 1L, column_number = 1L), linter)
expect_lint("x!=NA", list(message = lint_msg, line_number = 1L, column_number = 1L), linter)

# order doesn't matter
expect_lint("NA == x", list(message = msg, line_number = 1L, column_number = 1L), linter)
expect_lint("NA == x", list(message = lint_msg, line_number = 1L, column_number = 1L), linter)

# correct line number for multiline code
expect_lint("x ==\nNA", list(line_number = 1L, column_number = 1L, ranges = list(c(1L, 4L))), linter)
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-expect_identical_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ test_that("expect_identical_linter skips allowed usages", {

test_that("expect_identical_linter blocks simple disallowed usages", {
linter <- expect_identical_linter()
msg <- rex::rex("Use expect_identical(x, y) by default; resort to expect_equal() only when needed")
lint_msg <- rex::rex("Use expect_identical(x, y) by default; resort to expect_equal() only when needed")

expect_lint("expect_equal(x, y)", msg, linter)
expect_lint("expect_equal(x, y)", lint_msg, linter)

# different usage to redirect to expect_identical
expect_lint("expect_true(identical(x, y))", msg, linter)
expect_lint("expect_true(identical(x, y))", lint_msg, linter)
})

test_that("expect_identical_linter skips cases likely testing numeric equality", {
linter <- expect_identical_linter()
msg <- rex::rex("Use expect_identical(x, y) by default; resort to expect_equal() only when needed")
lint_msg <- rex::rex("Use expect_identical(x, y) by default; resort to expect_equal() only when needed")

expect_lint("expect_equal(x, 1.034)", NULL, linter)
expect_lint("expect_equal(x, c(1.01, 1.02))", NULL, linter)
# whole numbers with explicit decimals are OK, even in mixed scenarios
expect_lint("expect_equal(x, c(1.0, 2))", NULL, linter)
# plain numbers are still caught, however
expect_lint("expect_equal(x, 1L)", msg, linter)
expect_lint("expect_equal(x, 1)", msg, linter)
expect_lint("expect_equal(x, 1L)", lint_msg, linter)
expect_lint("expect_equal(x, 1)", lint_msg, linter)
# NB: TRUE is a NUM_CONST so we want test matching it, even though this test is
# also a violation of expect_true_false_linter()
expect_lint("expect_equal(x, TRUE)", msg, linter)
expect_lint("expect_equal(x, TRUE)", lint_msg, linter)
})

test_that("expect_identical_linter skips 3e cases needing expect_equal", {
Expand Down
30 changes: 15 additions & 15 deletions tests/testthat/test-expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# for failure, always put the lint check or lint field that must fail first.

linter <- assignment_linter()
msg <- "Use <-, not ="
lint_msg <- "Use <-, not ="

test_that("no checks", {
expect_success(expect_lint("a", NULL, linter))
Expand All @@ -12,36 +12,36 @@ test_that("no checks", {
})

test_that("single check", {
expect_failure(expect_lint(character(), msg, linter))
expect_failure(expect_lint("", msg, linter))
expect_failure(expect_lint(character(), lint_msg, linter))
expect_failure(expect_lint("", lint_msg, linter))

expect_success(expect_lint(content = "a=1", checks = msg, linters = linter))
expect_success(expect_lint("a=1", msg, linter))
expect_success(expect_lint(content = "a=1", checks = lint_msg, linters = linter))
expect_success(expect_lint("a=1", lint_msg, linter))
expect_failure(expect_lint("a=1", "asdf", linter))
expect_success(expect_lint("a=1", c(message = msg), linter))
expect_success(expect_lint("a=1", c(message = lint_msg), linter))
expect_failure(expect_lint("a=1", c(message = NULL), linter))
expect_success(expect_lint("a=1", c(message = msg, line_number = 1L), linter))
expect_failure(expect_lint("a=1", c(line_number = 2L, message = msg), linter))
expect_success(expect_lint("a=1", c(message = lint_msg, line_number = 1L), linter))
expect_failure(expect_lint("a=1", c(line_number = 2L, message = lint_msg), linter))

expect_error(expect_lint("a=1", c(message = msg, lineXXX = 1L), linter), "invalid field")
expect_error(expect_lint("a=1", c(message = lint_msg, lineXXX = 1L), linter), "invalid field")

expect_failure(expect_lint("foo ()", list(ranges = list(c(2L, 2L))), function_left_parentheses_linter()))
expect_success(expect_lint("\t1", list(ranges = list(c(1L, 1L))), no_tab_linter()))
expect_success(expect_lint("a=1", list(message = msg, line_number = 1L), linter))
expect_failure(expect_lint("a=1", list(2L, msg), linter))
expect_success(expect_lint("a=1", list(message = lint_msg, line_number = 1L), linter))
expect_failure(expect_lint("a=1", list(2L, lint_msg), linter))

expect_error(expect_lint("1:nrow(x)", "(group)", seq_linter()), "Invalid regex result", fixed = TRUE)
})

test_that("multiple checks", {
expect_success(
expect_lint(file = "exclusions-test", checks = as.list(rep(msg, 9L)), linters = linter, parse_settings = FALSE)
expect_lint(file = "exclusions-test", checks = as.list(rep(lint_msg, 9L)), linters = linter, parse_settings = FALSE)
)

expect_success(expect_lint("a=1; b=2", list(msg, msg), linter))
expect_success(expect_lint("a=1; b=2", list(c(message = msg), c(message = msg)), linter))
expect_success(expect_lint("a=1; b=2", list(lint_msg, lint_msg), linter))
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(msg, c(line = "a=1; b=2", type = "warning")), 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))

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ test_that("Warns if encoding is misspecified", {
the_lint <- lint(filename = file, parse_settings = FALSE)[[1L]]
expect_s3_class(the_lint, "lint")

msg <- "Invalid multibyte character in parser. Is the encoding correct?"
lint_msg <- "Invalid multibyte character in parser. Is the encoding correct?"
if (!isTRUE(l10n_info()[["UTF-8"]])) {
# Prior to R 4.2.0, the Windows parser throws a different error message because the source code is converted to
# native encoding.
# This results in line 4 becoming <fc> <- 42 before the parser sees it.
msg <- "unexpected '<'"
lint_msg <- "unexpected '<'"
}

expect_identical(the_lint$linter, "error")
expect_identical(the_lint$message, msg)
expect_identical(the_lint$message, lint_msg)
expect_identical(the_lint$line_number, 4L)

file <- test_path("dummy_projects", "project", "cp1252_parseable.R")
Expand Down
Loading