Skip to content

Commit

Permalink
Another batch
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Dec 7, 2023
1 parent 7ce25e6 commit e3b18e1
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 170 deletions.
6 changes: 5 additions & 1 deletion tests/testthat/test-length_levels_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ test_that("length_levels_linter blocks simple disallowed usages", {
})

test_that("lints vectorize", {
lint_msg <- rex::rex("nlevels(x) is better than length(levels(x)).")

expect_lint(
trim_some("{
length(levels(x))
length(levels(y))
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
),
linter
length_levels_linter()
)
})
8 changes: 5 additions & 3 deletions tests/testthat/test-length_test_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ local({
test_that("lints vectorize", {
expect_lint(
trim_some("{
length(x == y)
length(y == z)
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
list(rex::rex("length(x) == y"), line_number = 2L),
list(rex::rex("length(y) == z"), line_number = 3L)
),
linter
length_test_linter()
)
})
6 changes: 5 additions & 1 deletion tests/testthat/test-lengths_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ test_that("lengths_linter blocks simple disallowed usages with pipes", {
})

test_that("lints vectorize", {
lint_msg <- rex::rex("Use lengths() to find the length of each element in a list.")

expect_lint(
trim_some("{
sapply(x, length)
ma_int(x, length)
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
),
linter
lengths_linter()
)
})
5 changes: 4 additions & 1 deletion tests/testthat/test-line_length_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ test_that("Multiple lints give custom messages", {
abcdefg
hijklmnop
}"),
list("9 characters", "11 characters"),
list(
list("9 characters", line_number = 2L),
list("11 characters", line_number = 3L)
),
line_length_linter(5L)
)
})
13 changes: 10 additions & 3 deletions tests/testthat/test-namespace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ test_that("namespace_linter blocks disallowed usages", {
)

expect_lint(
"stats::sd(c(1,2,3))\nstats::sdd(c(1,2,3))",
trim_some("
stats::sd(c(1,2,3))
stats::sdd(c(1,2,3))
"),
list(line = "stats::sdd(c(1,2,3))"),
linter
)
Expand All @@ -78,10 +81,14 @@ test_that("namespace_linter blocks disallowed usages", {
test_that("lints vectorize", {
expect_lint(
trim_some("{
statts::sd(c(1,2,3))
stats::ssd(c(1,2,3))
stats:::sd(c(1,2,3))
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
list(rex::rex("Package 'statts' is not installed."), line_number = 2L),
list(rex::rex("'ssd' is not exported from {stats}"), line_number = 3L),
list(rex::rex("Don't use `:::` to access sd"), line_number = 4L)
),
linter
)
Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-nested_ifelse_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ test_that("nested_ifelse_linter also catches data.table::fifelse", {
test_that("lints vectorize", {
expect_lint(
trim_some("{
ifelse(x < 0, ifelse(x == 0, 0, 1), -1)
fifelse(y < 0, fifelse(y == 0, 0, 1), -1)
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
list("ifelse", line_number = 2L),
list("fifelse", line_number = 3L)
),
linter
nested_ifelse_linter()
)
})
6 changes: 5 additions & 1 deletion tests/testthat/test-nonportable_path_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ test_that("nonportable_path_linter's lax argument works", {
})

test_that("lints vectorize", {
lint_msg <- rex::escape("Use file.path() to construct portable file paths.")

expect_lint(
trim_some("{
'~/'
'C:/'
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
),
linter
nonportable_path_linter()
)
})
6 changes: 5 additions & 1 deletion tests/testthat/test-numeric_leading_zero_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ test_that("numeric_leading_zero_linter blocks simple disallowed usages", {
})

test_that("lints vectorize", {
lint_msg <- rex::rex("Include the leading zero for fractional numeric constants.")

expect_lint(
trim_some("{
.1
-.2
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
),
linter
numeric_leading_zero_linter()
)
})
57 changes: 17 additions & 40 deletions tests/testthat/test-outer_negation_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,39 @@ test_that("outer_negation_linter skips allowed usages", {

test_that("outer_negation_linter blocks simple disallowed usages", {
linter <- outer_negation_linter()
not_all_msg <- rex::rex("!all(x) is better than any(!x)")
not_any_msg <- rex::rex("!any(x) is better than all(!x)")

expect_lint(
"any(!x)",
rex::rex("!all(x) is better than any(!x)"),
linter
)

expect_lint(
"all(!foo(x))",
rex::rex("!any(x) is better than all(!x)"),
linter
)

expect_lint("any(!x)", not_all_msg, linter)
expect_lint("all(!foo(x))", not_any_msg, linter)
# na.rm doesn't change the recommendation
expect_lint(
"any(!x, na.rm = TRUE)",
rex::rex("!all(x) is better than any(!x)"),
linter
)

expect_lint("any(!x, na.rm = TRUE)", not_all_msg, linter)
# also catch nested usage
expect_lint(
"all(!(x + y))",
rex::rex("!any(x) is better than all(!x)"),
linter
)

expect_lint("all(!(x + y))", not_any_msg, linter)
# catch when all inputs are negated
expect_lint(
"any(!x, !y)",
rex::rex("!all(x) is better than any(!x)"),
linter
)

expect_lint(
"all(!x, !y, na.rm = TRUE)",
rex::rex("!any(x) is better than all(!x)"),
linter
)
expect_lint("any(!x, !y)", not_all_msg, linter)
expect_lint("all(!x, !y, na.rm = TRUE)", not_any_msg, linter)
})

test_that("outer_negation_linter doesn't trigger on empty calls", {

Check warning on line 36 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=36,col=67,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.
linter <- outer_negation_linter())

Check warning on line 37 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=37,col=2,[indentation_linter] Hanging indent should be 10 spaces but is 2 spaces.

Check warning on line 37 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=37,col=36,[error] unexpected ')'

# minimal version of issue

Check warning on line 39 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=39,col=2,[indentation_linter] Indentation should be 0 spaces but is 2 spaces.
expect_lint("any()", NULL, outer_negation_linter())
expect_lint("any()", NULL, linter)
# closer to what was is practically relevant, as another regression test
expect_lint("x %>% any()", NULL, outer_negation_linter())
expect_lint("x %>% any()", NULL, linter)
})

test_that("lints vectorize", {
expect_lint(

Check warning on line 46 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=46,col=2,[indentation_linter] Indentation should be 0 spaces but is 2 spaces.
trim_some("{

Check warning on line 47 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=47,col=4,[indentation_linter] Indentation should be 0 spaces but is 4 spaces.
any(!x)

Check warning on line 48 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=48,col=6,[indentation_linter] Indentation should be 0 spaces but is 6 spaces.
all(!y)
}"),

Check warning on line 50 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=50,col=4,[indentation_linter] Indentation should be 0 spaces but is 4 spaces.
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
list(rex::rex("!all(x)"), line_number = 2L),

Check warning on line 52 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=52,col=6,[indentation_linter] Indentation should be 0 spaces but is 6 spaces.
list(rex::rex("!any(x)"), line_number = 3L)
),

Check warning on line 54 in tests/testthat/test-outer_negation_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-outer_negation_linter.R,line=54,col=4,[indentation_linter] Indentation should be 0 spaces but is 4 spaces.
linter
outer_negation_linter()
)
})
Loading

0 comments on commit e3b18e1

Please sign in to comment.