File: test-object_length_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 (83 lines) | stat: -rw-r--r-- 2,322 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
test_that("returns the correct linting", {
  linter <- object_length_linter()
  lint_msg <- rex::rex("Variable and function names should not be longer than 30 characters.")

  expect_lint("blah", NULL, linter)

  expect_lint("very_very_very_very_long_variable_names_are_not_ideal <- 1", lint_msg, linter)

  expect_lint(
    "very_very_very_very_long_variable_names_are_not_ideal <<- 'foo'",
    rex::rex("Variable and function names should not be longer than 40 characters."),
    object_length_linter(length = 40L)
  )
})

# Regression tests for #871
test_that("lints S3 generics correctly", {
  linter <- object_length_linter()
  lint_msg <- rex::rex("Variable and function names should not be longer than 30 characters.")

  expect_lint("print.very_very_long_class_name <- 1", NULL, linter)
  expect_lint("print.very_very_very_very_long_class_name <- 1", lint_msg, linter)

  expect_lint(
    trim_some("
    very_very_very_long_generic_name <- function(x, ...) {
      UseMethod(\"very_very_very_long_generic_name\")
    }

    very_very_very_long_generic_name.short_class <- function(x, ...) {
      42L
    }

    very_very_very_long_generic_name.very_very_very_very_long_class_name <- function(x, ...) {
      2L
    }
  "),
    list(
      list(line_number = 1L),
      list(line_number = 9L)
    ),
    linter
  )
})

test_that("object_length_linter won't fail if an imported namespace is unavailable", {
  expect_length(
    lint_package(
      test_path("dummy_packages", "missing_dep"),
      linters = object_length_linter(),
      parse_settings = FALSE
    ),
    3L
  )
})

test_that("object_length_linter won't fail if dependency has no exports", {
  expect_length(
    lint_package(
      test_path("dummy_packages", "no_export_dep"),
      linters = object_length_linter(),
      parse_settings = FALSE
    ),
    1L
  )
})

test_that("function shorthand is caught", {
  skip_if_not_r_version("4.1.0")

  expect_lint(
    "abcdefghijklm <- \\() NULL",
    "function names",
    object_length_linter(length = 10L)
  )
})

test_that("rlang name injection is handled", {
  linter <- object_length_linter(length = 10L)

  expect_lint("tibble('{foo() |> bar() |> baz()}' := TRUE)", NULL, linter)
  expect_lint("DT[, 'a_very_long_name' := FALSE]", "names should not be longer than 10 characters", linter)
})