File: test-consecutive_assertion_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 (145 lines) | stat: -rw-r--r-- 2,913 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
test_that("consecutive_assertion_linter skips allowed usages", {
  linter <- consecutive_assertion_linter()
  expect_lint("stopifnot(x)", NULL, linter)
  expect_lint("stopifnot(x, y, z)", NULL, linter)

  # intervening expression
  expect_lint("stopifnot(x); y; stopifnot(z)", NULL, linter)

  # inline or potentially with gaps don't matter
  expect_lint(
    trim_some("
      stopifnot(x)
      y
  
      stopifnot(z)
    "),
    NULL,
    linter
  )
})

test_that("consecutive_assertion_linter blocks simple disallowed usages", {
  linter <- consecutive_assertion_linter()
  lint_msg <- rex::rex("Unify consecutive calls to stopifnot().")

  # one test of inline usage
  expect_lint(
    "stopifnot(x); stopifnot(y)",
    lint_msg,
    linter
  )

  expect_lint(
    trim_some("
      stopifnot(x)
  
      stopifnot(y, z)
    "),
    lint_msg,
    linter
  )

  expect_lint(
    trim_some("
      stopifnot(x)
      stopifnot(y)
    "),
    lint_msg,
    linter
  )

  expect_lint(
    trim_some("
      stopifnot(x)
      # a comment on y
      stopifnot(y)
    "),
    lint_msg,
    linter
  )
})

test_that("assert_that usages are handled correctly too", {
  linter <- consecutive_assertion_linter()
  lint_msg <- rex::rex("Unify consecutive calls to assert_that().")

  expect_lint("assert_that(x)", NULL, linter)
  expect_lint("assertthat::assert_that(x, y, z)", NULL, linter)

  # if msg= is used, can't necessarily combine
  lines <- trim_some("
    assert_that(x, msg = 'bad x')
    assert_that(y, msg = 'bad y')
  ")
  expect_lint(lines, NULL, linter)

  # one test of inline usage
  expect_lint(
    "assert_that(x); assert_that(y)",
    lint_msg,
    linter
  )

  lines_gap <- trim_some("
    assert_that(x)

    assertthat::assert_that(y, z)
  ")
  expect_lint(lines_gap, lint_msg, linter)
})

test_that("Mixing test functions is fine", {
  expect_lint(
    trim_some("
      assert_that(x)
      stopifnot(y)
    "),
    NULL,
    consecutive_assertion_linter()
  )
})

test_that("lints vectorize", {
  expect_lint(
    trim_some("{
      stopifnot(A)
      stopifnot(B)
      assert_that(C)
      assert_that(D)
    }"),
    list(
      list("stopifnot", line_number = 2L),
      list("assert_that", line_number = 4L)
    ),
    consecutive_assertion_linter()
  )
})

test_that("old name consecutive_stopifnot_linter() is deprecated", {
  expect_warning(
    {
      old_linter <- consecutive_stopifnot_linter()
    },
    "Use consecutive_assertion_linter instead",
    fixed = TRUE
  )
  expect_lint("stopifnot(x); y; stopifnot(z)", NULL, old_linter)
  expect_lint("stopifnot(x); stopifnot(y)", "Unify consecutive calls", old_linter)
})

test_that("interceding = assignments aren't linted", {
  expect_lint(
    trim_some("{
      stopifnot(A)
      x = 1
      stopifnot(B)

      assert_that(C)
      z = 3
      assert_that(D)
    }"),
    NULL,
    consecutive_assertion_linter()
  )
})