File: test-list_comparison_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 (34 lines) | stat: -rw-r--r-- 1,046 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
28
29
30
31
32
33
34
test_that("list_comparison_linter skips allowed usages", {
  expect_lint("sapply(x, sum) > 10", NULL, list_comparison_linter())
})

local({
  linter <- list_comparison_linter()
  lint_msg <- rex::rex("a list(), is being coerced for comparison")

  cases <- expand.grid(
    list_mapper = c("lapply", "map", "Map", ".mapply"),
    comparator = c("==", "!=", ">=", "<=", ">", "<")
  )
  cases$.test_name <- with(cases, paste(list_mapper, comparator))
  patrick::with_parameters_test_that(
    "list_comparison_linter blocks simple disallowed usages",
    expect_lint(sprintf("%s(x, sum) %s 10", list_mapper, comparator), lint_msg, linter),
    .cases = cases
  )
})

test_that("list_comparison_linter vectorizes", {
  expect_lint(
    trim_some("{
      sapply(x, sum) > 10
      .mapply(`+`, list(1:10, 1:10), NULL) == 2
      lapply(x, sum) < 5
    }"),
    list(
      list(rex::rex(".mapply()", anything, "`==`"), line_number = 3L),
      list(rex::rex("lapply()", anything, "`<`"), line_number = 4L)
    ),
    list_comparison_linter()
  )
})