File: test-implicit_integer_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 (106 lines) | stat: -rw-r--r-- 3,726 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
# styler: off
skip_if_not_installed("tibble")
local({
  # Note: cases indicated by "*" are whole numbers, but don't lint because the user has
  #   effectively declared "this is a double" much as adding '.0' is otherwise accepted.
  cases <- tibble::tribble(
    ~num_value_str,         ~lint_msg,
    "Inf",                  "",
    "NaN",                  "",
    "TRUE",                 "",
    "FALSE",                "",
    "NA",                   "",
    "NA_character_",        "",
    "2.000",                "",
    "2.",                   "",
    "2L",                   "",
    "2.0",                  "",
    "2.1",                  "",
    "2",                    "2L or 2.0",
    "1e3",                  "1000L or 1000.0",
    "1e3L",                 "",
    "1.0e3L",               "",
    "1.2e3",                "", # * ( = 1200)
    "1.2e-3",               "",
    "1e-3",                 "",
    "1e-33",                "",
    "1.2e0",                "",
    "0x1p+0",               "", # * ( = 1)
    "0x1.ecp+6L",           "",
    "0x1.ecp+6",            "", # * ( = 123)
    "0x1.ec66666666666p+6", "",
    "8i",                   "",
    "8.0i",                 ""
  )
  # for convenience of coercing these to string (since tribble doesn't support auto-conversion)
  int_max <- .Machine[["integer.max"]]  # largest number that R can represent as an integer
  cases_int_max <- tibble::tribble(
    ~num_value_str, ~lint_msg,
    -int_max - 1.0, "",
    -int_max,       sprintf("%1$dL or %1$d.0", -int_max),
    int_max,        sprintf("%1$dL or %1$d.0", int_max),
    int_max + 1.0,  ""
  )
  cases_int_max$num_value_str <- as.character(cases_int_max$num_value_str)
  cases <- rbind(cases, cases_int_max)

  linter <- implicit_integer_linter()
  patrick::with_parameters_test_that(
    "single numerical constants are properly identified ",
    expect_lint(num_value_str, if (nzchar(lint_msg)) lint_msg, linter),
    .cases = cases
  )
})
# styler: on

test_that("linter returns the correct linting", {
  linter <- implicit_integer_linter()
  lint_msg <- rex::rex("Use 1L or 1.0 to avoid implicit integers.")

  expect_lint("x <<- 1L", NULL, linter)
  expect_lint("1.0/-Inf -> y", NULL, linter)
  expect_lint(
    "y <- 1+i",
    list(message = lint_msg, line_number = 1L, column_number = 7L),
    linter
  )
  expect_lint(
    "z <- 1e5",
    list(message = rex::rex("100000L or 100000.0"), line_number = 1L, column_number = 9L),
    linter
  )
  expect_lint(
    "cat(1:n)",
    list(message = lint_msg, line_number = 1L, column_number = 6L),
    linter
  )
  expect_lint(
    "552^9",
    list(
      list(message = rex::rex("552L or 552.0"), line_number = 1L, column_number = 4L),
      list(message = rex::rex("9L or 9.0"), line_number = 1L, column_number = 6L)
    ),
    linter
  )
})

skip_if_not_installed("tibble")
patrick::with_parameters_test_that(
  "numbers in a:b input are optionally not linted",
  expect_lint(
    paste0(left, ":", right),
    if (n_lints > 0L) rep(list(rex::rex("Use 1L or 1.0")), n_lints),
    implicit_integer_linter(allow_colon = allow_colon)
  ),
  .cases = tibble::tribble(
    ~left,  ~right, ~n_lints, ~allow_colon, ~.test_name,
    "1",    "1",    2L,       FALSE,        "1:1, !allow_colon",
    "1",    "1",    0L,       TRUE,         "1:1, allow_colon",
    "1",    "1L",   1L,       FALSE,        "1:1L, !allow_colon",
    "1",    "1L",   0L,       TRUE,         "1:1L, allow_colon",
    "1L",   "1",    1L,       FALSE,        "1L:1, !allow_colon",
    "1L",   "1",    0L,       TRUE,         "1L:1, allow_colon",
    "1L",   "1L",   0L,       FALSE,        "1L:1L, !allow_colon",
    "1L",   "1L",   0L,       TRUE,         "1L:1L, allow_colon"
  )
)