File: test-expect_identical_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 (75 lines) | stat: -rw-r--r-- 3,085 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
67
68
69
70
71
72
73
74
75
test_that("expect_identical_linter skips allowed usages", {
  linter <- expect_identical_linter()

  # expect_type doesn't have an inverted version
  expect_lint("expect_true(identical(x, y) || identical(y, z))", NULL, linter)
  # NB: also applies to tinytest, but it's sufficient to test testthat
  expect_lint("testthat::expect_true(identical(x, y) || identical(y, z))", NULL, linter)

  # expect_equal calls with explicit tolerance= are OK
  expect_lint("expect_equal(x, y, tolerance = 1e-6)", NULL, linter)
  # ditto if the argument is passed quoted
  expect_lint("expect_equal(x, y, 'tolerance' = 1e-6)", NULL, linter)

  # ditto for check.attributes = FALSE
  expect_lint("expect_equal(x, y, check.attributes = FALSE)", NULL, linter)
})

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

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

  # different usage to redirect to expect_identical
  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()
  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, -1.034)", NULL, linter)
  expect_lint("expect_equal(x, c(-1.034))", NULL, linter)
  expect_lint("expect_equal(x, -c(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)", 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)", lint_msg, linter)

  expect_lint("expect_equal(x, 1.01 - y)", lint_msg, linter)
  expect_lint("expect_equal(x, foo() - 0.01)", lint_msg, linter)
})

test_that("expect_identical_linter skips 3e cases needing expect_equal", {
  expect_lint("expect_equal(x, y, ignore_attr = 'names')", NULL, expect_identical_linter())
})

# this arose where a helper function was wrapping expect_equal() and
#   some of the "allowed" arguments were being passed --> false positive
test_that("expect_identical_linter skips calls using ...", {
  expect_lint("expect_equal(x, y, ...)", NULL, expect_identical_linter())
})

test_that("lints vectorize", {
  lint_msg <- rex::rex("Use expect_identical(x, y) by default; resort to expect_equal() only when needed")

  expect_lint(
    trim_some("{
      expect_equal(x, 1)
      expect_true(identical(x, y))
    }"),
    list(
      list(lint_msg, line_number = 2L),
      list(lint_msg, line_number = 3L)
    ),
    expect_identical_linter()
  )
})