File: test-commented_code_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 (115 lines) | stat: -rw-r--r-- 3,173 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
test_that("commented_code_linter skips allowed usages", {
  linter <- commented_code_linter()

  expect_lint("blah", NULL, linter)
  expect_lint("#' blah <- 1", NULL, linter)
  expect_lint("a <- 1\n# comment without code", NULL, linter)
  expect_lint("a <- 1\n## whatever", NULL, linter)

  expect_lint("TRUE", NULL, linter)
  expect_lint("#' @examples", NULL, linter)
  expect_lint("#' foo(1) # list(1)", NULL, linter) # comment in roxygen block ignored
  expect_lint("1+1 # gives 2", NULL, linter)
  expect_lint("# Non-existent:", NULL, linter)
  expect_lint("# 1-a", NULL, linter) # "-" removed from code operators
  expect_lint('1+1  # for example cat("123")', NULL, linter)

  # regression test for #451
  expect_lint("c('#a#' = 1)", NULL, linter)
})

test_that("commented_code_linter blocks disallowed usages", {
  lint_msg <- rex::rex("Remove commented code.")
  linter <- commented_code_linter()

  expect_lint("# blah <- 1", lint_msg, linter)

  expect_lint(
    "bleurgh <- fun_call(1) # other_call()",
    list(message = lint_msg, column_number = 26L),
    linter
  )

  expect_lint(
    " #blah <- 1",
    list(message = lint_msg, column_number = 3L),
    linter
  )

  # Regression test for #742, line number and comment number don't match up
  expect_lint(
    trim_some("
    # non-code comment
    line_without_comment <- 42L
     #blah <- 1
    "),
    list(message = lint_msg, line_number = 3L, column_number = 3L),
    linter
  )

  expect_lint(
    trim_some("
      d <- t.test(
        x = dplyr::starwars$height,
        # var.equal = TRUE,
        conf.level = .99,
        mu = 175
      )
    "),
    list(message = lint_msg, line_number = 3L),
    linter
  )

  expect_lint(
    trim_some("
      d <- t.test(
        x = dplyr::starwars$height
        #, var.equal = TRUE
        , conf.level = .99
        , mu = 175
      )
    "),
    list(message = lint_msg, line_number = 3L),
    linter
  )

  expect_lint("1+1 # cat('123')", lint_msg, linter)
  expect_lint("#expect_ftype(1e-12 , t)", lint_msg, linter)
})

test_that("commented_code_linter can detect operators in comments and lint correctly", {
  linter <- commented_code_linter()
  lint_msg <- rex::rex("Remove commented code.")

  test_ops <- c(
    "+", "=", "==", "!=", "<=", ">=", "<-", "<<-", "<", ">", "->",
    "->>", "%%", "/", "^", "*", "**", "|", "||", "&", "&&", "%>%",
    "%anything%"
  )

  for (op in test_ops) {
    expect_lint(paste("i", op, "1", collapse = ""), NULL, linter)
    expect_lint(paste("# something like i", op, "1", collapse = ""), NULL, linter)
    expect_lint(paste("# i", op, "1", collapse = ""), lint_msg, linter)
  }
})

test_that("commented_code_linter can detect operators in comments and lint correctly", {
  skip_if_not_r_version("4.1.0")

  expect_lint(
    "# 1:3 |> sum()",
    rex::rex("Remove commented code."),
    commented_code_linter()
  )
})

test_that("commented_code_linter can detect commented code ending with pipes", {
  linter <- commented_code_linter()
  lint_msg <- rex::rex("Remove commented code.")

  expect_lint("# f() %>%", lint_msg, linter)

  skip_if_not_r_version("4.1.0")
  expect_lint("# f() |>", lint_msg, linter)
})