File: test-commas_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,872 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("returns the correct linting (with default parameters)", {
  linter <- commas_linter()
  msg_after <- rex::rex("Put a space after a comma.")
  msg_before <- rex::rex("Remove spaces before a comma.")

  expect_lint("blah", NULL, linter)
  expect_lint("fun(1, 1)", NULL, linter)
  expect_lint("fun(1,\n  1)", NULL, linter)
  expect_lint("fun(1,\n1)", NULL, linter)
  expect_lint("fun(1\n,\n1)", NULL, linter)
  expect_lint("fun(1\n  ,\n1)", NULL, linter)

  expect_lint("fun(1\n,1)", msg_after, linter)
  expect_lint("fun(1,1)", msg_after, linter)
  expect_lint("\nfun(1,1)", msg_after, linter)
  expect_lint("a(1,)", msg_after, linter)
  expect_lint("a[1,]", msg_after, linter)
  expect_lint("a[[1,]]", msg_after, linter)
  expect_lint(
    "fun(1 ,1)",
    list(
      list(msg_before, column_number = 6L),
      list(msg_after, column_number = 8L)
    ),
    linter
  )

  expect_lint("\"fun(1 ,1)\"", NULL, linter)
  expect_lint("a[1, , 2]", NULL, linter)
  expect_lint("a[1, , 2, , 3]", NULL, linter)

  expect_lint("switch(op, x = foo, y = bar)", NULL, linter)
  expect_lint("switch(op, x = , y = bar)", NULL, linter)
  expect_lint("switch(op, \"x\" = , y = bar)", NULL, linter)
  expect_lint("switch(op, x = ,\ny = bar)", NULL, linter)

  expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
  expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
  expect_lint("switch(op , x = foo, y = bar)", msg_before, linter)
  expect_lint("switch(op, x = foo, y = bar(a = 4 , b = 5))", msg_before, linter)
  expect_lint("fun(op, x = foo , y = switch(bar, a = 4, b = 5))", msg_before, linter)
  expect_lint(
    trim_some("
      switch(op ,
        x = foo,y = bar
      )
    "),
    list(
      list(msg_before, line_number = 1L),
      list(msg_after, line_number = 2L)
    ),
    linter
  )

  expect_lint(
    "fun(op    ,bar)",
    list(
      list(message = msg_before, column_number = 7L, ranges = list(c(7L, 10L))),
      list(message = msg_after, column_number = 12L, ranges = list(c(12L, 12L)))
    ),
    linter
  )
})

test_that("returns the correct linting (with 'allow_trailing' set)", {
  linter <- commas_linter(allow_trailing = TRUE)
  msg_after <- rex::rex("Put a space after a comma.")
  msg_before <- rex::rex("Remove spaces before a comma.")

  expect_lint("blah", NULL, linter)
  expect_lint("fun(1, 1)", NULL, linter)
  expect_lint("fun(1,\n  1)", NULL, linter)
  expect_lint("fun(1,\n1)", NULL, linter)
  expect_lint("fun(1\n,\n1)", NULL, linter)
  expect_lint("fun(1\n  ,\n1)", NULL, linter)
  expect_lint("a[1,]", NULL, linter)
  expect_lint("a(1,)", NULL, linter)

  expect_lint("fun(1\n,1)", msg_after, linter)
  expect_lint("fun(1,1)", msg_after, linter)
  expect_lint("\nfun(1,1)", msg_after, linter)
  expect_lint(
    "fun(1 ,1)",
    list(
      msg_before,
      msg_after
    ),
    linter
  )

  expect_lint("\"fun(1 ,1)\"", NULL, linter)
  expect_lint("a[1, , 2]", NULL, linter)
  expect_lint("a[1, , 2, , 3]", NULL, linter)
  expect_lint("a[[1,]]", NULL, linter)

  expect_lint("switch(op, x = foo, y = bar)", NULL, linter)
  expect_lint("switch(op, x = , y = bar)", NULL, linter)
  expect_lint("switch(op, \"x\" = , y = bar)", NULL, linter)
  expect_lint("switch(op, x = ,\ny = bar)", NULL, linter)

  expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
  expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
  expect_lint("switch(op , x = foo, y = bar)", msg_before, linter)
  expect_lint("switch(op, x = foo, y = bar(a = 4 , b = 5))", msg_before, linter)
  expect_lint("fun(op, x = foo , y = switch(bar, a = 4, b = 5))", msg_before, linter)

  expect_lint(
    "fun(op    ,bar)",
    list(
      list(message = msg_before, column_number = 7L, ranges = list(c(7L, 10L))),
      list(message = msg_after, column_number = 12L, ranges = list(c(12L, 12L)))
    ),
    linter
  )
})