File: test-expect_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 (66 lines) | stat: -rw-r--r-- 1,834 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
test_that("expect_comparison_linter skips allowed usages", {
  linter <- expect_comparison_linter()

  # there's no expect_ne() for this operator
  expect_lint("expect_true(x != y)", NULL, linter)
  # NB: also applies to tinytest, but it's sufficient to test testthat
  expect_lint("testthat::expect_true(x != y)", NULL, linter)

  # multiple comparisons are OK
  expect_lint("expect_true(x > y || x > z)", NULL, linter)

  # expect_gt() and friends don't have an info= argument
  expect_lint("expect_true(x > y, info = 'x is bigger than y yo')", NULL, linter)

  # expect_true() used incorrectly, and as executed the first argument is not a lint
  expect_lint("expect_true(is.count(n_draws), n_draws > 1)", NULL, linter)
})

test_that("expect_comparison_linter blocks simple disallowed usages", {
  linter <- expect_comparison_linter()

  expect_lint(
    "expect_true(x > y)",
    rex::rex("expect_gt(x, y) is better than expect_true(x > y)."),
    linter
  )

  # namespace qualification is irrelevant
  expect_lint(
    "testthat::expect_true(x < y)",
    rex::rex("expect_lt(x, y) is better than expect_true(x < y)."),
    linter
  )

  expect_lint(
    "expect_true(foo(x) >= y[[2]])",
    rex::rex("expect_gte(x, y) is better than expect_true(x >= y)."),
    linter
  )

  expect_lint(
    "expect_true(x <= y)",
    rex::rex("expect_lte(x, y) is better than expect_true(x <= y)."),
    linter
  )

  expect_lint(
    "expect_true(x == (y == 2))",
    rex::rex("expect_identical(x, y) is better than expect_true(x == y)."),
    linter
  )
})

test_that("lints vectorize", {
  expect_lint(
    trim_some("{
      expect_true(x < y)
      expect_true(y > z)
    }"),
    list(
      list(rex::rex("expect_lt("), line_number = 2L),
      list(rex::rex("expect_gt("), line_number = 3L)
    ),
    expect_comparison_linter()
  )
})