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
|
test_that("pipe_consistency skips allowed usage", {
skip_if_not_r_version("4.1.0")
linter <- pipe_consistency_linter()
expect_lint("1:3 %>% mean() %>% as.character()", NULL, linter)
expect_lint("1:3 |> mean() |> as.character()", NULL, linter)
# With no pipes
expect_lint("x <- 1:5", NULL, linter)
# Across multiple lines
expect_lint(
trim_some("
1:3 %>%
mean() %>%
as.character()
"),
NULL,
linter
)
})
test_that("pipe_consistency lints inconsistent usage", {
skip_if_not_r_version("4.1.0")
linter <- pipe_consistency_linter()
expected_msg <- rex::rex("Stick to one pipe operator; found 1 instances of %>% and 1 instances of |>.")
expect_lint(
"1:3 |> mean() %>% as.character()",
list(
list(message = expected_msg, line_number = 1L, column_number = 5L),
list(message = expected_msg, line_number = 1L, column_number = 15L)
),
linter
)
expect_lint(
"1:3 %>% mean() |> as.character()",
list(
list(message = expected_msg, line_number = 1L, column_number = 5L),
list(message = expected_msg, line_number = 1L, column_number = 16L)
),
linter
)
expect_lint(
trim_some("
1:3 %>%
mean() |>
as.character()
"),
list(
list(message = expected_msg, line_number = 1L, column_number = 5L),
list(message = expected_msg, line_number = 2L, column_number = 10L)
),
linter
)
expected_msg_multi <- rex::rex("Stick to one pipe operator; found 1 instances of %>% and 2 instances of |>.")
expect_lint(
"1:3 |> sort() |> mean() %>% as.character()",
list(
list(message = expected_msg_multi, line_number = 1L, column_number = 5L),
list(message = expected_msg_multi, line_number = 1L, column_number = 15L),
list(message = expected_msg_multi, line_number = 1L, column_number = 25L)
),
linter
)
})
test_that("pipe_consistency_linter works with |> argument", {
skip_if_not_r_version("4.1.0")
linter <- pipe_consistency_linter(pipe = "|>")
expected_message <- rex::rex("Use the |> pipe operator instead of the %>% pipe operator.")
expect_lint(
trim_some("
1:3 %>%
mean() %>%
as.character()
"),
list(
list(message = expected_message, line_number = 1L, column_number = 5L),
list(message = expected_message, line_number = 2L, column_number = 10L)
),
linter
)
expect_lint(
trim_some("
1:3 |>
mean() %>%
as.character()
"),
list(message = expected_message, line_number = 2L, column_number = 10L),
linter
)
expect_lint(
"1:3 |> mean() |> as.character()",
NULL,
linter
)
expect_lint(
trim_some("
1:3 |>
mean() %>%
as.character()
"),
list(message = expected_message, line_number = 2L, column_number = 10L),
linter
)
})
test_that("pipe_consistency_linter works with %>% argument", {
skip_if_not_r_version("4.1.0")
linter <- pipe_consistency_linter(pipe = "%>%")
expected_message <- rex::rex("Use the %>% pipe operator instead of the |> pipe operator.")
expect_lint(
"1:3 |> mean() |> as.character()",
list(
list(message = expected_message, line_number = 1L, column_number = 5L),
list(message = expected_message, line_number = 1L, column_number = 15L)
),
linter
)
expect_lint(
"1:3 %>% mean() |> as.character()",
list(message = expected_message, line_number = 1L, column_number = 16L),
linter
)
expect_lint(
"1:3 %>% mean() %>% as.character()",
NULL,
linter
)
expect_lint(
trim_some("
1:3 %>%
mean() |>
as.character()
"),
list(message = expected_message, line_number = 2L, column_number = 10L),
linter
)
})
test_that("pipe_consistency_linter works with other magrittr pipes", {
skip_if_not_r_version("4.1.0")
linter <- pipe_consistency_linter()
expected_message <- rex::rex("Stick to one pipe operator; found 1 instances of %>% and 1 instances of |>.")
expect_lint("1:3 %>% mean() %T% print()", NULL, linter)
expect_lint(
"1:3 |> mean() %T>% print()",
list(
list(message = expected_message, line_number = 1L, column_number = 5L),
list(message = expected_message, line_number = 1L, column_number = 15L)
),
linter
)
})
|