File: test-which_grepl_linter.R

package info (click to toggle)
r-cran-lintr 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,396 kB
  • sloc: sh: 13; xml: 10; makefile: 2
file content (27 lines) | stat: -rw-r--r-- 898 bytes parent folder | download
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
test_that("which_grepl_linter skips allowed usages", {
  # this _could_ be combined as p1|p2, but often it's cleaner to read this way
  expect_lint("which(grepl(p1, x) | grepl(p2, x))", NULL, which_grepl_linter())
})

test_that("which_grepl_linter blocks simple disallowed usages", {
  linter <- which_grepl_linter()
  lint_msg <- rex::rex("grep(pattern, x) is better than which(grepl(pattern, x)).")

  expect_lint("which(grepl('^a', x))", lint_msg, linter)
  # options also don't matter (grep has more arguments: value, invert)
  expect_lint("which(grepl('^a', x, perl = TRUE, fixed = TRUE))", lint_msg, linter)

  expect_lint(
    trim_some('{
      which(x)
      grepl(y)
      which(grepl("pt1", x))
      which(grepl("pt2", y))
    }'),
    list(
      list(lint_msg, line_number = 4L, column_number = 3L),
      list(lint_msg, line_number = 5L, column_number = 3L)
    ),
    linter
  )
})