-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New expect_named_linter * NEWS * fix lints in lintr
- Loading branch information
1 parent
2580ab2
commit e53a2c8
Showing
12 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#' Require usage of expect_named(x, n) over expect_equal(names(x), n) | ||
#' | ||
#' [testthat::expect_named()] exists specifically for testing the [names()] of | ||
#' an object. [testthat::expect_equal()] can also be used for such tests, | ||
#' but it is better to use the tailored function instead. | ||
#' | ||
#' @evalRd rd_tags("expect_named_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
expect_named_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
xpath <- "//expr[ | ||
SYMBOL_FUNCTION_CALL[text() = 'expect_equal' or text() = 'expect_identical'] | ||
and following-sibling::expr[ | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'names']] | ||
and (position() = 1 or preceding-sibling::expr[STR_CONST]) | ||
] | ||
]" | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
return(lapply(bad_expr, gen_expect_named_lint, source_file)) | ||
}) | ||
} | ||
|
||
gen_expect_named_lint <- function(expr, source_file) { | ||
matched_function <- xml2::xml_text(xml2::xml_find_first(expr, "SYMBOL_FUNCTION_CALL")) | ||
lint_msg <- sprintf("expect_named(x, n) is better than %s(names(x), n)", matched_function) | ||
xml_nodes_to_lint(expr, source_file, lint_msg, type = "warning") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
test_that("expect_named_linter doesn't raise false positive lints", { | ||
expect_lint("expect_equal(nrow(x), 4L)", NULL, expect_named_linter()) | ||
# NB: also applies to tinytest, but it's sufficient to test testthat | ||
expect_lint("testthat::expect_equal(nrow(x), 4L)", NULL, expect_named_linter()) | ||
}) | ||
|
||
test_that("expect_named_linter skips allowed usages", { | ||
# colnames(), rownames(), and dimnames() tests are not equivalent | ||
expect_lint("expect_equal(colnames(x), 'a')", NULL, expect_named_linter()) | ||
expect_lint("expect_equal(rownames(x), 'a')", NULL, expect_named_linter()) | ||
expect_lint("expect_equal(dimnames(x), 'a')", NULL, expect_named_linter()) | ||
|
||
# only check the first argument. yoda tests in the second argument will be | ||
# missed, but there are legitimate uses of names() in argument 2 | ||
expect_lint("expect_equal(colnames(x), names(y))", NULL, expect_named_linter()) | ||
}) | ||
|
||
test_that("expect_named_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"expect_equal(names(x), 'a')", | ||
rex::rex("expect_named(x, n) is better than expect_equal(names(x), n)"), | ||
expect_named_linter() | ||
) | ||
|
||
expect_lint( | ||
"testthat::expect_equal(names(DF), names(old))", | ||
rex::rex("expect_named(x, n) is better than expect_equal(names(x), n)"), | ||
expect_named_linter() | ||
) | ||
|
||
expect_lint( | ||
"expect_equal('a', names(x))", | ||
rex::rex("expect_named(x, n) is better than expect_equal(names(x), n)"), | ||
expect_named_linter() | ||
) | ||
}) | ||
|
||
test_that("expect_named_linter blocks expect_identical usage as well", { | ||
expect_lint( | ||
"expect_identical(names(x), 'a')", | ||
rex::rex("expect_named(x, n) is better than expect_identical(names(x), n)"), | ||
expect_named_linter() | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters