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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
# styler: off
test_that("styles are correctly identified", {
do_style_check <- function(nms) lapply(unname(style_regexes), lintr:::check_style, nms = nms)
# symbl UpC lowC snake SNAKE dot alllow ALLUP
expect_identical(do_style_check("x"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_identical(do_style_check(".x"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_identical(do_style_check("X"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_identical(do_style_check("x."), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("X."), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("x_"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("X_"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("xy"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_identical(do_style_check("xY"), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("Xy"), list(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("XY"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_identical(do_style_check("x1"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_identical(do_style_check("X1"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_identical(do_style_check("x_y"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("X_Y"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("X.Y"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("x_2"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("X_2"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("x.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))
expect_identical(do_style_check("X.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
# symbl UpC lowC snake SNAKE dot alllow ALLUP
expect_identical(do_style_check("IHave1Cat"), list(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("iHave1Cat"), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("i_have_1_cat"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("I_HAVE_1_CAT"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("i.have.1.cat"), list(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))
expect_identical(do_style_check("ihave1cat"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE))
expect_identical(do_style_check("IHAVE1CAT"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE))
expect_identical(do_style_check("I.HAVE_ONECAT"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("."), list(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
expect_identical(do_style_check("%^%"), list(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
})
# styler: on
test_that("linter ignores some objects", {
# names for which style check is ignored
expect_lint("`%X%` <- t", NULL, object_name_linter("SNAKE_CASE")) # operator
expect_lint("`%x%` <- t", NULL, object_name_linter("snake_case")) # operator
expect_lint("`t.test` <- t", NULL, object_name_linter("UPPERCASE")) # std pkg
expect_lint(".Deprecated('x')", NULL, object_name_linter("lowercase")) # std pkg
expect_lint("print.foo <- t", NULL, object_name_linter("CamelCase")) # S3 generic
expect_lint("names.foo <- t", NULL, object_name_linter("CamelCase")) # int generic
expect_lint("sapply(x,f,USE.NAMES=T)", NULL, object_name_linter("snake_case")) # defined elsewhere
expect_lint(".onLoad <- function(...) TRUE", NULL, object_name_linter("snake_case")) # namespace hooks, #500
expect_lint(".First <- function(...) TRUE", NULL, object_name_linter("snake_case")) # namespace hooks
expect_lint("`%++%` <- `+`", NULL, object_name_linter("symbols")) # all-symbol operator
expect_lint("`%<-%` <- `+`", NULL, object_name_linter("symbols")) # all-symbol operator #495
# S3 group generic, #1841
expect_lint(
"`==.snake_case` <- function(a, b) unclass(a) == unclass(b)",
NULL,
object_name_linter("snake_case")
)
})
test_that("linter returns correct linting", {
lint_msg <- "Variable and function name style should match camelCase."
linter <- object_name_linter("camelCase")
expect_lint("myObject <- 123", NULL, linter)
expect_lint("`myObject` <- 123", NULL, linter)
expect_lint("my.confused_NAME <- 1;", list(message = lint_msg, line_number = 1L, column_number = 1L), linter)
expect_lint("1 ->> read.data.frame;", list(message = lint_msg, line_number = 1L, column_number = 7L), linter)
expect_lint(
"object_name_linter <- function(...) {}",
list(message = lint_msg, line_number = 1L, column_number = 1L), linter
)
expect_lint(
"Z = sapply('function', function(x=function(x){1}, b.a.z=F, ...){identity(b.a.z)}, USE.NAMES=TRUE)",
list(
list(message = lint_msg, line_number = 1L, column_number = 1L),
list(message = lint_msg, line_number = 1L, column_number = 51L)
),
linter
)
expect_lint("blah", NULL, linter)
expect_lint("invokeRestartInteractively", NULL, linter)
expect_lint("camelCase", NULL, linter)
expect_lint("camelCase()", NULL, linter)
expect_lint("pack::camelCase", NULL, linter)
expect_lint("pack:::camelCase", NULL, linter)
expect_lint("a(camelCase = 1)", NULL, linter)
expect_lint("a$b <- 1", NULL, linter)
})
test_that("linter accepts vector of styles", {
lint_msg <- "Variable and function name style should match camelCase or dotted.case."
linter <- object_name_linter(styles = c("camelCase", "dotted.case"))
expect_lint(
"var.one <- 1\nvarTwo <- 2\nvar_three <- 3",
list(message = lint_msg, line_number = 3L, column_number = 1L),
linter
)
})
test_that("dollar subsetting only lints the first expression", {
# Regression test for #582
linter <- object_name_linter()
lint_msg <- rex::rex("Variable and function name style should match snake_case or symbols.")
expect_lint("my_var$MY_COL <- 42L", NULL, linter)
expect_lint("MY_VAR$MY_COL <- 42L", lint_msg, linter)
expect_lint("my_var@MY_SUB <- 42L", NULL, linter)
expect_lint("MY_VAR@MY_SUB <- 42L", lint_msg, linter)
})
patrick::with_parameters_test_that(
"nested extraction only lints on the first symbol",
expect_lint(
sprintf("%s%sMY_SUB%sMY_COL <- 42L", if (should_lint) "MY_VAR" else "my_var", op1, op2),
if (should_lint) rex::rex("Variable and function name style should match snake_case or symbols."),
object_name_linter()
),
.cases = within(
expand.grid(should_lint = c(TRUE, FALSE), op1 = c("$", "@"), op2 = c("$", "@"), stringsAsFactors = FALSE),
{
.test_name <- sprintf("(should lint? %s, op1=%s, op2=%s)", should_lint, op1, op2)
}
)
)
test_that("assignment targets of compound lhs are correctly identified", {
linter <- object_name_linter()
lint_msg <- "Variable and function name style should match snake_case or symbols."
# (recursive) [, $, and [[ subsetting
expect_lint("good_name[badName] <- badName2", NULL, linter)
expect_lint("good_name[1L][badName] <- badName2", NULL, linter)
expect_lint("good_name[[badName]] <- badName2", NULL, linter)
expect_lint("good_name[[1L]][[badName]] <- badName2", NULL, linter)
expect_lint("good_name[[fun(badName)]] <- badName2", NULL, linter)
expect_lint("good_name[[badName]]$badName2 <- badName3", NULL, linter)
expect_lint("good_name$badName[[badName2]][badName3]$badName4 <- badName5", NULL, linter)
expect_lint("badName[badName] <- badName2", lint_msg, linter)
expect_lint("badName[1L][badName] <- badName2", lint_msg, linter)
expect_lint("badName[[badName]] <- badName2", lint_msg, linter)
expect_lint("badName[[1L]][[badName]] <- badName2", lint_msg, linter)
expect_lint("badName[[fun(badName)]] <- badName2", lint_msg, linter)
expect_lint("badName[[badName]]$badName2 <- badName3", lint_msg, linter)
expect_lint("badName$badName[[badName2]][badName3]$badName4 <- badName5", lint_msg, linter)
# setters
expect_lint("setter(badName) <- good_name", lint_msg, linter)
expect_lint("setter(good_name[[badName]]) <- good_name2", NULL, linter)
# quotation
expect_lint("\"good_name\" <- 42", NULL, linter)
expect_lint("\"badName\" <- 42", lint_msg, linter)
expect_lint("'good_name' <- 42", NULL, linter)
expect_lint("'badName' <- 42", lint_msg, linter)
expect_lint("`good_name` <- 42", NULL, linter)
expect_lint("`badName` <- 42", lint_msg, linter)
# subsetting with quotation
expect_lint("good_name$\"badName\" <- 42", NULL, linter)
expect_lint("good_name$'badName' <- 42", NULL, linter)
expect_lint("badName$\"good_name\" <- 42", lint_msg, linter)
expect_lint("badName$'good_name' <- 42", lint_msg, linter)
expect_lint("`badName`$\"good_name\" <- 42", lint_msg, linter)
expect_lint("`badName`$'good_name' <- 42", lint_msg, linter)
})
test_that("object_name_linter won't fail if an imported namespace is unavailable", {
expect_length(
lint_package(test_path("dummy_packages", "missing_dep"), linters = object_name_linter(), parse_settings = FALSE),
3L
)
})
test_that("object_name_linter supports custom regexes", {
# disables default styles
linter <- object_name_linter(
regexes = c(shinyModule = rex::rex(start, lower, zero_or_more(alnum), "UI" %or% "Server", end))
)
msg <- rex::rex("Variable and function name style should match shinyModule.")
linter2 <- object_name_linter(
styles = c("snake_case", "symbols"),
regexes = c(shinyModule = rex::rex(start, lower, zero_or_more(alnum), "UI" %or% "Server", end))
)
msg2 <- rex::rex("Variable and function name style should match snake_case, symbols or shinyModule.")
# Can't allow 0 styles
expect_error(
object_name_linter(NULL),
rex::rex("At least one style must be specified using `styles` or `regexes`.")
)
expect_lint(
trim_some('
snake_case <- 42L
"%+%" <- function(...) ..1 + ..2
myModuleUI <- function(id) {
# blah
}
myModuleServer <- function(id) {
# blah
}
myBadName <- 20L
'),
list(
list(line_number = 1L, message = msg),
list(line_number = 2L, message = msg),
# argument "id" is linted if we only allow shinyModule names
list(line_number = 4L, column_number = 24L, message = msg),
list(line_number = 8L, column_number = 28L, message = msg),
list(line_number = 12L, message = msg)
),
linter
)
expect_lint(
trim_some('
snake_case <- 42L
"%+%" <- function(...) ..1 + ..2
myModuleUI <- function(id) {
# blah
}
myModuleServer <- function(id) {
# blah
}
myBadName <- 20L
'),
list(line_number = 12L, message = msg2),
linter2
)
# Default regex naming works
expect_lint(
trim_some("
a <- 42L
b <- 1L
c <- 2L
"),
list(line_number = 3L, message = rex::rex("Variable and function name style should match /^a$/ or /^b$/.")),
object_name_linter(regexes = c("^a$", "^b$"))
)
expect_lint(
trim_some("
a <- 42L
b <- 1L
c <- 2L
"),
list(line_number = 3L, message = rex::rex("Variable and function name style should match a or /^b$/.")),
object_name_linter(regexes = c(a = "^a$", "^b$"))
)
})
test_that("complex LHS of := doesn't cause false positive", {
# "_l" would be included under previous logic which tried ancestor::expr[ASSIGN] for STR_CONST,
# but only parent::expr[ASSIGN] is needed for strings.
expect_lint('dplyr::mutate(df, !!paste0(v, "_l") := df$a * 2)', NULL, object_name_linter())
})
test_that("function shorthand also lints", {
skip_if_not_r_version("4.1.0")
expect_lint("aBc <- \\() NULL", "function name style", object_name_linter())
})
test_that("capture groups in style are fine", {
expect_lint("a <- 1\nab <- 2", NULL, object_name_linter(regexes = c(capture = "^(a)")))
expect_lint("ab <- 1\nabc <- 2", NULL, object_name_linter(regexes = c(capture = "^(a)(b)")))
})
test_that("rlang name injection is handled", {
linter <- object_name_linter()
expect_lint('tibble("{name}" := 2)', NULL, linter)
expect_lint('x %>% mutate("{name}" := 2)', NULL, linter)
expect_lint('DT[, "{name}" := 2]', "style should match snake_case", linter)
})
|