File: test-pipe_continuation_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 (204 lines) | stat: -rw-r--r-- 4,324 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
test_that("pipe-continuation correctly handles stand-alone expressions", {
  linter <- pipe_continuation_linter()
  lint_msg <- rex::rex("Put a space before `%>%` and a new line after it,")

  # Expressions without pipes are ignored
  expect_lint("blah", NULL, linter)

  # Pipe expressions on a single line are ignored
  expect_lint("foo %>% bar() %>% baz()", NULL, linter)

  # Pipe expressions spanning multiple lines with each expression on a line are ignored
  expect_lint(
    trim_some("
      foo %>%
        bar() %>%
        baz()
    "),
    NULL,
    linter
  )

  # Pipe expressions with multiple expression on a line are linted
  expect_lint(
    trim_some("
      foo %>% bar() %>%
        baz()
    "),
    lint_msg,
    linter
  )

  expect_lint(
    trim_some("
      foo %>% bar() %>% baz() %>%
       qux()
    "),
    lint_msg,
    linter
  )
})

test_that("pipe-continuation linter correctly handles nesting", {
  linter <- pipe_continuation_linter()
  lint_msg <- rex::rex("Put a space before `%>%` and a new line after it,")

  expect_lint(
    trim_some("
      my_fun <- function(){
        a %>% b() %>%
          c()
      }
    "),
    list(list(message = lint_msg, line_number = 2L)),
    linter
  )

  expect_lint(
    trim_some("
      my_fun <- function(){
        a %>%
          b() %>% c()
      }
    "),
    list(list(message = lint_msg, line_number = 3L)),
    linter
  )

  # but no lints here
  expect_lint(
    trim_some("
      1:4 %>% {
       (.) %>% sum()
      }
    "),
    NULL,
    linter
  )
})

test_that("pipe-continuation linter handles native pipe", {
  skip_if_not_r_version("4.1.0")

  linter <- pipe_continuation_linter()
  lint_msg_native <- rex::rex("Put a space before `|>` and a new line after it,")
  lint_msg_magrittr <- rex::rex("Put a space before `%>%` and a new line after it,")

  expect_lint("foo |> bar() |> baz()", NULL, linter)
  expect_lint(
    trim_some("
      foo |>
        bar() |>
        baz()
    "),
    NULL,
    linter
  )
  expect_lint(
    trim_some("
      foo |> bar() |>
        baz()
    "),
    lint_msg_native,
    linter
  )
  # mixing pipes
  expect_lint(
    trim_some("
      foo %>% bar() |>
        baz()
    "),
    lint_msg_native,
    linter
  )
  expect_lint(
    trim_some("
      foo |> bar() %>%
        baz()
    "),
    lint_msg_magrittr,
    linter
  )
  expect_lint(
    trim_some("
      list(
        foo |> bar() |>
          baz(),
        foo %>% bar() %>%
          baz()
      )
    "),
    list(
      lint_msg_native,
      lint_msg_magrittr
    ),
    linter
  )
})

local({
  linter <- pipe_continuation_linter()
  .cases <- tibble::tribble(
    ~code_string,            ~.test_name,
    trim_some("
      my_fun <- function() {
        a %>% b()
      }
    "),                      "two on one line",
    trim_some("
      my_fun <- function() {
        a %>% b() %>% c()
      }
    "),                      "three on one line",
    trim_some("
      with(
        diamonds,
        x %>% head(10) %>% tail(5)
      )
    "),                      "three inside with()",
    trim_some("
      test_that('blah', {
        test_data <- diamonds %>% head(10) %>% tail(5)
      })
    "),                      "three inside test_that()",
    trim_some("
      {
        x <- a %>% b %>% c
        y <- c %>% b %>% a
      }
    "),                      "two different single-line pipelines",
    trim_some("
      my_fun <- function() {
        a %>%
          b() %>%
          c()
      }
    "),                      "at most one pipe-character per line"
  )
  patrick::with_parameters_test_that(
    "valid nesting is handled",
    # nolint next: unnecessary_nesting_linter. TODO(#2334): Remove this nolint.
    {
      expect_lint(code_string, NULL, linter)
    },
    .cases = .cases
  )
})

local({
  linter <- pipe_continuation_linter()
  pipes <- pipes()
  cases <- expand.grid(pipe1 = pipes, pipe2 = pipes, stringsAsFactors = FALSE)
  cases <- within(cases, {
    .test_name <- sprintf("(%s, %s)", pipe1, pipe2)
  })
  patrick::with_parameters_test_that(
    "Various pipes are linted correctly",
    expect_lint(
      sprintf("a %s b() %s\n  c()", pipe1, pipe2),
      rex::rex(sprintf("Put a space before `%s` and a new line after it", pipe2)),
      linter
    ),
    .cases = cases
  )
})