Skip to content

Commit

Permalink
fix broken tests, finish metadata tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Dec 7, 2023
1 parent 3f5b4b1 commit 3ada1c6
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 69 deletions.
2 changes: 1 addition & 1 deletion tests/testthat/test-expect_s4_class_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_that("lints vectorize", {
expect_lint(
trim_some("{
expect_true(is(x, 'data.frame'))
expect_true(is(x, 'SpatialPolygonsDataFrame))
expect_true(is(x, 'SpatialPolygonsDataFrame'))
}"),
list(
list(lint_msg, line_number = 2L),
Expand Down
14 changes: 6 additions & 8 deletions tests/testthat/test-fixed_regex_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,13 @@ test_that("fixed replacements vectorize and recognize str_detect", {
linter <- fixed_regex_linter()
# properly vectorized
expect_lint(
trim_some("
c(
grepl('abcdefg', x),
grepl('a[.]\\\\.b\\n', x)
)
"),
trim_some("{
grepl('abcdefg', x)
grepl('a[.]\\\\.b\\n', x)
}"),
list(
rex::rex('Use "abcdefg" with fixed = TRUE'),
rex::rex('Use "a..b\\n" with fixed = TRUE')
list(rex::rex('Use "abcdefg" with fixed = TRUE'), line_number = 2L),
list(rex::rex('Use "a..b\\n" with fixed = TRUE'), line_number = 3L)
),
linter
)
Expand Down
37 changes: 22 additions & 15 deletions tests/testthat/test-infix_spaces_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,12 @@ test_that("assignment cases return the correct linting", {
})

test_that("infix_spaces_linter can allow >1 spaces optionally", {
expect_lint(
"x ~ 1",
rex::rex("Put exactly one space on each side of infix operators."),
infix_spaces_linter(allow_multiple_spaces = FALSE)
)
expect_lint(
"x - 1",
rex::rex("Put exactly one space on each side of infix operators."),
infix_spaces_linter(allow_multiple_spaces = FALSE)
)
expect_lint(
"x / 1",
rex::rex("Put exactly one space on each side of infix operators."),
infix_spaces_linter(allow_multiple_spaces = FALSE)
)
linter <- infix_spaces_linter(allow_multiple_spaces = FALSE)
lint_msg <- rex::rex("Put exactly one space on each side of infix operators.")

expect_lint("x ~ 1", lint_msg, linter)
expect_lint("x - 1", lint_msg, linter)
expect_lint("x / 1", lint_msg, linter)
})

test_that("exception for box::use()", {
Expand Down Expand Up @@ -223,3 +214,19 @@ test_that("parse tags are accepted by exclude_operators", {
expect_lint(text, list(col_assign, col_sub), infix_spaces_linter(exclude_operators = "EQ_FORMALS"))
expect_lint(text, list(col_formals, col_sub), infix_spaces_linter(exclude_operators = "EQ_ASSIGN"))
})

test_that("lints vectorize", {
lint_msg <- rex::rex("Put spaces around all infix operators.")

expect_lint(
trim_some("{
a<-1
1/2
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
),
infix_spaces_linter()
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-matrix_apply_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ test_that("matrix_apply_linter works with multiple lints in a single expression"

expect_lint(
trim_some("{
apply(x, 1, sum),
apply(x, 1, sum)
apply(y, 2:4, mean, na.rm = TRUE)
}"),
list(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-namespace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ test_that("lints vectorize", {
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
namespace_linter()
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-nonportable_path_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ test_that("lints vectorize", {
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
),
nonportable_path_linter()
nonportable_path_linter(lax = FALSE)
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-outer_negation_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test_that("outer_negation_linter blocks simple disallowed usages", {
})

test_that("outer_negation_linter doesn't trigger on empty calls", {
linter <- outer_negation_linter())
linter <- outer_negation_linter()

# minimal version of issue
expect_lint("any()", NULL, linter)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-package_hooks_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ test_that("package_hooks_linter catches usage of library.dynam.unload()", {

test_that("package_hooks_linter detects bad argument names in .onDetach()/.Last.lib()", {
linter <- package_hooks_linter()
lint_msg_part <- "should take one argument starting with 'lib'"
lint_msg_part <- " should take one argument starting with 'lib'"

expect_lint(
".onDetach <- function(xxx) { }",
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-pipe_return_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,23 @@ test_that("pipe_return_linter blocks simple disallowed usages", {
pipe_return_linter()
)
})

test_that("lints vectorize", {
lint_msg <- rex::rex("Avoid return() as the final step of a magrittr pipeline")

expect_lint(
trim_some("{
function(x) {
x %>% return()
}
function(y) {
y %>% return()
}
}"),
list(
list(lint_msg, line_number = 3L),
list(lint_msg, line_number = 6L)
),
pipe_return_linter()
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-quotes_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test_that("single_quotes_linter is deprecated", {
})

test_that("lints vectorize", {
lint_msg <- rex::rex("Only use single-quotes.")
lint_msg <- rex::rex("Only use double-quotes.")

expect_lint(
trim_some("{
Expand Down
23 changes: 8 additions & 15 deletions tests/testthat/test-redundant_equals_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ test_that("redundant_equals_linter skips allowed usages", {

test_that("multiple lints return correct custom messages", {
expect_lint(
"list(x == TRUE, y != TRUE)",
trim_some("
list(
x == TRUE,
y != TRUE
)
"),
list(
"Using == on a logical vector",
"Using != on a logical vector"
list("Using == on a logical vector", line_number = 2L),
list("Using != on a logical vector", line_number = 3L)
),
redundant_equals_linter()
)
Expand All @@ -35,15 +40,3 @@ patrick::with_parameters_test_that(
"!=, FALSE", "!=", "FALSE"
)
)

test_that("lints vectorize", {
expect_lint(
trim_some("{
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
),
linter
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-redundant_ifelse_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ test_that("lints vectorize", {
fifelse(y == 0, 1, 0)
}"),
list(
list("Juse use the logical condition", line_number = 2L),
list("Just use the logical condition", line_number = 2L),
list(rex::rex("refer as.numeric(x)"), line_number = 3L)
),
redundant_ifelse_linter()
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-sort_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ test_that("lints vectorize", {
expect_lint(
trim_some("{
x == sort(x)
x != sort(x)
y[order(y)]
}"),
list(
list(rex::rex("is.unsorted(x)"), line_number = 2L),
list(rex::rex("!is.unsorted(x)"), line_number = 3L)
list(rex::rex("sort(y"), line_number = 3L)
),
sort_linter()
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-system_file_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test_that("lints vectorize", {
expect_lint(
trim_some("{
file.path(system.file(package = 'foo'), 'bar')
system.file(file.path('bar', data'), package = 'foo')
system.file(file.path('bar', 'data'), package = 'foo')
}"),
list(
list(lint_msg, line_number = 2L),
Expand Down
17 changes: 9 additions & 8 deletions tests/testthat/test-unnecessary_nesting_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,20 +326,21 @@ test_that("lints vectorize", {
lint_msg <- rex::rex("Reduce the nesting of this if/else")

expect_lint(
trim_some("
trim_some("{
if (A) {
stop('no')
} else {
if (B) {
stop('really no')
} else {
1
}
0
}
"),
if (B) {
stop('really no')
} else {
1
}
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
list(lint_msg, line_number = 7L)
),
unnecessary_nesting_linter()
)
Expand Down
12 changes: 0 additions & 12 deletions tests/testthat/test-unused_import_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,3 @@ test_that("glue usages are seen", {
expect_lint(lines, NULL, unused_import_linter())
expect_lint(lines, lint_msg, unused_import_linter(interpret_glue = FALSE))
})

test_that("lints vectorize", {
expect_lint(
trim_some("{
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L)
),
linter
)
})

0 comments on commit 3ada1c6

Please sign in to comment.