-
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.
* extend infix_spaces_linter to be more flexible & correct * git mangled * clarifying comments * test of grouped exclusion by %% * missed ~ in docs * use angle brackets for external URL * remove linter from DB & document() * sAF=FALSE for R<4 * sQuote version issue * more tests * switch from with() usage to appease object_usage_linter * initial conversion of expect_comparison_linter * mini-edit to create a commit * roxygenize * roxygenize * fix new lintr lints caught by the improvement * expect_comparison_linter * remove clutter * #nolint in right place * use new xp_or() * use new xml_nodes_to_lint feature Co-authored-by: AshesITR <[email protected]>
- Loading branch information
1 parent
4d1dac5
commit c7eb033
Showing
11 changed files
with
123 additions
and
3 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,48 @@ | ||
#' Require usage of expect_gt(x, y) over expect_true(x > y) (and similar) | ||
#' | ||
#' [testthat::expect_gt()], [testthat::expect_gte()], [testthat::expect_lt()], | ||
#' [testthat::expect_lte()], and [testthat::expect_equal()] exist specifically | ||
#' for testing comparisons between two objects. [testthat::expect_true()] can | ||
#' also be used for such tests, but it is better to use the tailored function | ||
#' instead. | ||
#' | ||
#' @evalRd rd_tags("expect_comparison_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
expect_comparison_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# != doesn't have a clean replacement | ||
comparator_nodes <- # nolint: object_usage_linter. TODO(#942): remove this. | ||
setdiff(as.list(infix_metadata$xml_tag[infix_metadata$comparator]), "NE") | ||
xpath <- glue::glue("//expr[ | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'expect_true']] | ||
and expr[2][ {xp_or(comparator_nodes)} ] | ||
]") | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
comparator_expectation_map <- c( | ||
`>` = "expect_gt", `>=` = "expect_gte", | ||
`<` = "expect_lt", `<=` = "expect_lte", | ||
`==` = "expect_identical" | ||
) | ||
|
||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file, | ||
lint_message = function(expr) { | ||
comparator <- xml2::xml_text(xml2::xml_find_first(expr, "expr[2]/*[2]")) | ||
expectation <- comparator_expectation_map[[comparator]] | ||
sprintf("%s(x, y) is better than expect_true(x %s y).", expectation, comparator) | ||
}, | ||
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
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.
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,42 @@ | ||
test_that("expect_comparison_linter skips allowed usages", { | ||
# there's no expect_ne() for this operator | ||
expect_lint("expect_true(x != y)", NULL, expect_comparison_linter()) | ||
# NB: also applies to tinytest, but it's sufficient to test testthat | ||
expect_lint("testthat::expect_true(x != y)", NULL, expect_comparison_linter()) | ||
|
||
# multiple comparisons are OK | ||
expect_lint("expect_true(x > y || x > z)", NULL, expect_comparison_linter()) | ||
}) | ||
|
||
test_that("expect_comparison_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"expect_true(x > y)", | ||
rex::rex("expect_gt(x, y) is better than expect_true(x > y)."), | ||
expect_comparison_linter() | ||
) | ||
|
||
# namespace qualification is irrelevant | ||
expect_lint( | ||
"testthat::expect_true(x < y)", | ||
rex::rex("expect_lt(x, y) is better than expect_true(x < y)."), | ||
expect_comparison_linter() | ||
) | ||
|
||
expect_lint( | ||
"expect_true(foo(x) >= y[[2]])", | ||
rex::rex("expect_gte(x, y) is better than expect_true(x >= y)."), | ||
expect_comparison_linter() | ||
) | ||
|
||
expect_lint( | ||
"expect_true(x <= y)", | ||
rex::rex("expect_lte(x, y) is better than expect_true(x <= y)."), | ||
expect_comparison_linter() | ||
) | ||
|
||
expect_lint( | ||
"expect_true(x == (y == 2))", | ||
rex::rex("expect_identical(x, y) is better than expect_true(x == y)."), | ||
expect_comparison_linter() | ||
) | ||
}) |