Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions tests/testthat/test-any_is_na_linter.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
test_that("any_is_na_linter skips allowed usages", {
linter <- any_is_na_linter()

expect_lint("x <- any(y)", NULL, linter)
expect_no_lint("x <- any(y)", linter)

expect_lint("y <- is.na(z)", NULL, linter)
expect_no_lint("y <- is.na(z)", linter)

# extended usage of ... arguments to any is not covered
expect_lint("any(is.na(y), b)", NULL, linter)
expect_lint("any(b, is.na(y))", NULL, linter)
expect_no_lint("any(is.na(y), b)", linter)
expect_no_lint("any(b, is.na(y))", linter)

# negation shouldn't list
expect_lint("any(!is.na(x))", NULL, linter)
expect_lint("any(!is.na(foo(x)))", NULL, linter)
expect_no_lint("any(!is.na(x))", linter)
expect_no_lint("any(!is.na(foo(x)))", linter)
})

test_that("any_is_na_linter blocks simple disallowed usages", {
Expand All @@ -32,20 +32,22 @@ test_that("NA %in% x is also found", {

expect_lint("NA %in% x", lint_message, linter)
expect_lint("NA_real_ %in% x", lint_message, linter)
expect_lint("NA_not_a_sentinel_ %in% x", NULL, linter)
expect_no_lint("NA_not_a_sentinel_ %in% x", linter)
})

test_that("lints vectorize", {
any_message <- rex::rex("any(is.na(x))")
in_message <- rex::rex("NA %in% x")

expect_lint(
trim_some("{
trim_some(
"{
any(is.na(foo(x)))
any(is.na(y), na.rm = TRUE)
NA %in% a
NA_complex_ %in% b
}"),
}"
),
list(
list(any_message, line_number = 2L),
list(any_message, line_number = 3L),
Expand Down
14 changes: 5 additions & 9 deletions tests/testthat/test-backport_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ test_that("backport_linter produces error when R version misspecified", {

test_that("backport_linter detects backwards-incompatibility", {
# default should be current R version; all of these are included on our dependency
expect_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", NULL, backport_linter())
expect_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", NULL, backport_linter("release"))
expect_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", NULL, backport_linter("devel"))
expect_no_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", backport_linter())
expect_no_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", backport_linter("release"))
expect_no_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", backport_linter("devel"))

expect_lint(
"numToBits(2)",
Expand Down Expand Up @@ -61,14 +61,10 @@ test_that("backport_linter detects backwards-incompatibility", {
)

# except is honored
expect_lint(
trim_some("
expect_no_lint(trim_some("
numToBits(2)
R_user_dir('mypkg')
"),
NULL,
backport_linter("3.0.0", except = c("numToBits", "R_user_dir"))
)
"), backport_linter("3.0.0", except = c("numToBits", "R_user_dir")))
})

test_that("backport_linter generates expected warnings", {
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-class_equals_linter.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
test_that("class_equals_linter skips allowed usages", {
linter <- class_equals_linter()

expect_lint("class(x) <- 'character'", NULL, linter)
expect_lint("class(x) = 'character'", NULL, linter)
expect_no_lint("class(x) <- 'character'", linter)
expect_no_lint("class(x) = 'character'", linter)

# proper way to test exact class
expect_lint("identical(class(x), c('glue', 'character'))", NULL, linter)
expect_lint("is_lm <- inherits(x, 'lm')", NULL, linter)
expect_no_lint("identical(class(x), c('glue', 'character'))", linter)
expect_no_lint("is_lm <- inherits(x, 'lm')", linter)
})

test_that("class_equals_linter blocks simple disallowed usages", {
Expand Down Expand Up @@ -38,7 +38,7 @@ test_that("class_equals_linter blocks class(x) != 'klass'", {
test_that("class_equals_linter skips usage for subsetting", {
linter <- class_equals_linter()

expect_lint("class(x)[class(x) == 'foo']", NULL, linter)
expect_no_lint("class(x)[class(x) == 'foo']", linter)

# but not further nesting
expect_lint(
Expand Down
58 changes: 29 additions & 29 deletions tests/testthat/test-commas_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ test_that("returns the correct linting (with default parameters)", {
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_no_lint("blah", linter)
expect_no_lint("fun(1, 1)", linter)
expect_no_lint("fun(1,\n 1)", linter)
expect_no_lint("fun(1,\n1)", linter)
expect_no_lint("fun(1\n,\n1)", linter)
expect_no_lint("fun(1\n ,\n1)", linter)

expect_lint("fun(1\n,1)", msg_after, linter)
expect_lint("fun(1,1)", msg_after, linter)
Expand All @@ -25,14 +25,14 @@ test_that("returns the correct linting (with default parameters)", {
linter
)

expect_lint("\"fun(1 ,1)\"", NULL, linter)
expect_lint("a[1, , 2]", NULL, linter)
expect_lint("a[1, , 2, , 3]", NULL, linter)
expect_no_lint("\"fun(1 ,1)\"", linter)
expect_no_lint("a[1, , 2]", linter)
expect_no_lint("a[1, , 2, , 3]", 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_no_lint("switch(op, x = foo, y = bar)", linter)
expect_no_lint("switch(op, x = , y = bar)", linter)
expect_no_lint("switch(op, \"x\" = , y = bar)", linter)
expect_no_lint("switch(op, x = ,\ny = bar)", linter)

expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
Expand Down Expand Up @@ -67,14 +67,14 @@ test_that("returns the correct linting (with 'allow_trailing' set)", {
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_no_lint("blah", linter)
expect_no_lint("fun(1, 1)", linter)
expect_no_lint("fun(1,\n 1)", linter)
expect_no_lint("fun(1,\n1)", linter)
expect_no_lint("fun(1\n,\n1)", linter)
expect_no_lint("fun(1\n ,\n1)", linter)
expect_no_lint("a[1,]", linter)
expect_no_lint("a(1,)", linter)

expect_lint("fun(1\n,1)", msg_after, linter)
expect_lint("fun(1,1)", msg_after, linter)
Expand All @@ -88,15 +88,15 @@ test_that("returns the correct linting (with 'allow_trailing' set)", {
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_no_lint("\"fun(1 ,1)\"", linter)
expect_no_lint("a[1, , 2]", linter)
expect_no_lint("a[1, , 2, , 3]", linter)
expect_no_lint("a[[1,]]", 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_no_lint("switch(op, x = foo, y = bar)", linter)
expect_no_lint("switch(op, x = , y = bar)", linter)
expect_no_lint("switch(op, \"x\" = , y = bar)", linter)
expect_no_lint("switch(op, x = ,\ny = bar)", linter)

expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
expect_lint("switch(op, x = foo , y = bar)", msg_before, linter)
Expand Down
40 changes: 14 additions & 26 deletions tests/testthat/test-consecutive_assertion_linter.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
test_that("consecutive_assertion_linter skips allowed usages", {
linter <- consecutive_assertion_linter()
expect_lint("stopifnot(x)", NULL, linter)
expect_lint("stopifnot(x, y, z)", NULL, linter)
expect_no_lint("stopifnot(x)", linter)
expect_no_lint("stopifnot(x, y, z)", linter)

# intervening expression
expect_lint("stopifnot(x); y; stopifnot(z)", NULL, linter)
expect_no_lint("stopifnot(x); y; stopifnot(z)", linter)

# inline or potentially with gaps don't matter
expect_lint(
trim_some("
expect_no_lint(trim_some("
stopifnot(x)
y

stopifnot(z)
"),
NULL,
linter
)
"), linter)
})

test_that("consecutive_assertion_linter blocks simple disallowed usages", {
Expand All @@ -33,7 +29,7 @@ test_that("consecutive_assertion_linter blocks simple disallowed usages", {
expect_lint(
trim_some("
stopifnot(x)

stopifnot(y, z)
"),
lint_msg,
Expand Down Expand Up @@ -64,15 +60,15 @@ test_that("assert_that usages are handled correctly too", {
linter <- consecutive_assertion_linter()
lint_msg <- rex::rex("Unify consecutive calls to assert_that().")

expect_lint("assert_that(x)", NULL, linter)
expect_lint("assertthat::assert_that(x, y, z)", NULL, linter)
expect_no_lint("assert_that(x)", linter)
expect_no_lint("assertthat::assert_that(x, y, z)", linter)

# if msg= is used, can't necessarily combine
lines <- trim_some("
assert_that(x, msg = 'bad x')
assert_that(y, msg = 'bad y')
")
expect_lint(lines, NULL, linter)
expect_no_lint(lines, linter)

# one test of inline usage
expect_lint(
Expand All @@ -90,14 +86,10 @@ test_that("assert_that usages are handled correctly too", {
})

test_that("Mixing test functions is fine", {
expect_lint(
trim_some("
expect_no_lint(trim_some("
assert_that(x)
stopifnot(y)
"),
NULL,
consecutive_assertion_linter()
)
"), consecutive_assertion_linter())
})

test_that("lints vectorize", {
Expand All @@ -121,17 +113,13 @@ test_that("old name consecutive_stopifnot_linter() is defunct", {
})

test_that("interceding = assignments aren't linted", {
expect_lint(
trim_some("{
expect_no_lint(trim_some("{
stopifnot(A)
x = 1
stopifnot(B)

assert_that(C)
z = 3
assert_that(D)
}"),
NULL,
consecutive_assertion_linter()
)
}"), consecutive_assertion_linter())
})
Loading
Loading