tests/testthat/test-quotes_linter.R

test_that("quotes_linter skips allowed usages", {
  linter <- quotes_linter()

  expect_lint("blah", NULL, linter)
  expect_lint('"blah"', NULL, linter)
  expect_lint('"\'blah"', NULL, linter)
  expect_lint('"blah\'"', NULL, linter)
  expect_lint('"\'blah\'"', NULL, linter)
  expect_lint("'\"'", NULL, linter)
  expect_lint("'\"blah\"'", NULL, linter)
})

test_that("quotes_linter blocks disallowed usages", {
  linter <- quotes_linter()
  lint_msg <- rex::rex("Only use double-quotes.")

  expect_lint("'blah'", lint_msg, linter)
  expect_lint("fun('blah')", lint_msg, linter)
  expect_lint("{'blah'}", lint_msg, linter)

  expect_lint(
    "
    x = 'test
    '",
    list(message = lint_msg, ranges = list(c(9L, 13L))),
    linter
  )
})

# NB: repeat of above tests with (mostly) opposite results
test_that("quotes_linter skips allowed usages of delimiter='", {
  linter <- quotes_linter(delimiter = "'")

  expect_lint("blah", NULL, linter)
  expect_lint('"\'blah"', NULL, linter)
  expect_lint('"blah\'"', NULL, linter)
  expect_lint('"\'blah\'"', NULL, linter)
  expect_lint("'\"'", NULL, linter)
  expect_lint("'\"blah\"'", NULL, linter)
  expect_lint("'blah'", NULL, linter)
  expect_lint("fun('blah')", NULL, linter)
  expect_lint("{'blah'}", NULL, linter)
})

test_that("quotes_linter blocks disallowed usages of delimiter='", {
  linter <- quotes_linter(delimiter = "'")
  lint_msg <- rex::rex("Only use single-quotes.")

  expect_lint('"blah"', lint_msg, linter)
  expect_lint(
    '
    x = "test
    "',
    list(message = lint_msg, ranges = list(c(9L, 13L))),
    linter
  )
})

test_that("handles R>=4.0.0 strings", {
  skip_if_not_r_version("4.0.0")
  linter <- quotes_linter()
  expect_lint('R"(hello \'\' there)"', NULL, linter)
  expect_lint("R'( whoops )'", rex::rex("Only use double-quotes."), linter)
  expect_lint("R'---[ daisy ]---'", rex::rex("Only use double-quotes."), linter)
  expect_lint("r'(\")'", NULL, linter)
})

test_that("single_quotes_linter is deprecated", {
  expect_warning(
    {
      old_linter <- single_quotes_linter()
    },
    "Use quotes_linter instead",
    fixed = TRUE
  )
  expect_lint('"blah"', NULL, old_linter)
  expect_lint("'blah'", "Only use double-quotes", old_linter)
})

Try the lintr package in your browser

Any scripts or data that you put into this service are public.

lintr documentation built on Nov. 7, 2023, 5:07 p.m.