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
|
test_that("ifelse_censor_linter skips allowed usages", {
linter <- ifelse_censor_linter()
expect_lint("ifelse(x == 2, x, y)", NULL, linter)
expect_lint("ifelse(x > 2, x, y)", NULL, linter)
})
test_that("ifelse_censor_linter blocks simple disallowed usages", {
linter <- ifelse_censor_linter()
expect_lint(
"ifelse(x < 0, 0, x)",
rex::rex("pmax(x, y) is preferable to ifelse(x < y, y, x)"),
linter
)
# other equivalents to base::ifelse()
expect_lint(
"if_else(x < 0, 0, x)",
rex::rex("pmax(x, y) is preferable to if_else(x < y, y, x)"),
linter
)
expect_lint(
"fifelse(x < 0, 0, x)",
rex::rex("pmax(x, y) is preferable to fifelse(x < y, y, x)"),
linter
)
# other equivalents for censoring
expect_lint(
"ifelse(x <= 0, 0, x)",
rex::rex("pmax(x, y) is preferable to ifelse(x <= y, y, x)"),
linter
)
expect_lint(
"ifelse(x > 0, x, 0)",
rex::rex("pmax(x, y) is preferable to ifelse(x > y, x, y)"),
linter
)
expect_lint(
"ifelse(x >= 0, x, 0)",
rex::rex("pmax(x, y) is preferable to ifelse(x >= y, x, y)"),
linter
)
# pairwise min/max (similar to censoring)
expect_lint(
"ifelse(x < y, x, y)",
rex::rex("pmin(x, y) is preferable to ifelse(x < y, x, y)"),
linter
)
expect_lint(
"ifelse(x >= y, y, x)",
rex::rex("pmin(x, y) is preferable to ifelse(x >= y, y, x)"),
linter
)
# more complicated expression still matches
lines <- trim_some("
ifelse(2 + p + 104 + 1 > ncols,
ncols, 2 + p + 104 + 1
)
")
expect_lint(
lines,
rex::rex("pmin(x, y) is preferable to ifelse(x > y, y, x)"),
linter
)
})
test_that("lints vectorize", {
expect_lint(
trim_some("{
ifelse(x >= y, y, x)
ifelse(x >= 0, x, 0)
}"),
list(
list("pmin", line_number = 2L),
list("pmax", line_number = 3L)
),
ifelse_censor_linter()
)
})
|