-
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 conjunct_expecation_linter (#948)
* New expect_true_false_and_condition_linter * dedup NEWS * rename to conjunct_expectation_linter * NEWS * decruft * correct logic error re: expect_false(); NEWS * NEWS typo * fix terminology * extend tests * <- not = * add operator precedence tests * use new xml_nodes_to_lint Co-authored-by: AshesITR <[email protected]>
- Loading branch information
1 parent
dc734b7
commit 9e3ddab
Showing
11 changed files
with
137 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,47 @@ | ||
#' Force && conditions in expect_true(), expect_false() to be written separately | ||
#' | ||
#' For readability of test outputs, testing only one thing per call to | ||
#' [testthat::expect_true()] is preferable, i.e., | ||
#' `expect_true(A); expect_true(B)` is better than `expect_true(A && B)`, and | ||
#' `expect_false(A); expect_false(B)` is better than `expect_false(A || B)`. | ||
#' | ||
#' @evalRd rd_tags("expect_true_false_and_condition_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
conjunct_expectation_linter <- function() { | ||
Linter(function(source_file) { | ||
# need the full file to also catch usages at the top level | ||
if (length(source_file$full_parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$full_xml_parsed_content | ||
|
||
xpath <- "//expr[ | ||
( | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'expect_true']] | ||
and expr[2][AND2] | ||
) or ( | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'expect_false']] | ||
and expr[2][OR2] | ||
) | ||
]" | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file, | ||
lint_message = function(expr) { | ||
matched_fun <- xml2::xml_text(xml2::xml_find_first(expr, "expr/SYMBOL_FUNCTION_CALL")) | ||
op <- if (matched_fun == "expect_true") "&&" else "||" | ||
message <- | ||
sprintf("Instead of %1$s(A %2$s B), write multiple expectations like %1$s(A) and %1$s(B)", matched_fun, op) | ||
paste(message, "The latter will produce better error messages in the case of failure.") | ||
}, | ||
type = "warning", | ||
global = TRUE | ||
)) | ||
}) | ||
} |
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,59 @@ | ||
test_that("conjunct_expectation_linter skips allowed usages of expect_true", { | ||
expect_lint("expect_true(x)", NULL, conjunct_expectation_linter()) | ||
expect_lint("testthat::expect_true(x, y, z)", NULL, conjunct_expectation_linter()) | ||
|
||
# more complicated expression | ||
expect_lint("expect_true(x || (y && z))", NULL, conjunct_expectation_linter()) | ||
# the same by operator precedence, though not obvious a priori | ||
expect_lint("expect_true(x || y && z)", NULL, conjunct_expectation_linter()) | ||
expect_lint("expect_true(x && y || z)", NULL, conjunct_expectation_linter()) | ||
}) | ||
|
||
test_that("conjunct_expectation_linter skips allowed usages of expect_true", { | ||
expect_lint("expect_false(x)", NULL, conjunct_expectation_linter()) | ||
expect_lint("testthat::expect_false(x, y, z)", NULL, conjunct_expectation_linter()) | ||
|
||
# more complicated expression | ||
# (NB: xx && yy || zz and xx || yy && zz both parse with || first) | ||
expect_lint("expect_false(x && (y || z))", NULL, conjunct_expectation_linter()) | ||
}) | ||
|
||
test_that("conjunct_expectation_linter blocks && conditions with expect_true()", { | ||
expect_lint( | ||
"expect_true(x && y)", | ||
rex::rex("Instead of expect_true(A && B), write multiple expectations"), | ||
conjunct_expectation_linter() | ||
) | ||
|
||
expect_lint( | ||
"expect_true(x && y && z)", | ||
rex::rex("Instead of expect_true(A && B), write multiple expectations"), | ||
conjunct_expectation_linter() | ||
) | ||
}) | ||
|
||
test_that("conjunct_expectation_linter blocks || conditions with expect_false()", { | ||
expect_lint( | ||
"expect_false(x || y)", | ||
rex::rex("Instead of expect_false(A || B), write multiple expectations"), | ||
conjunct_expectation_linter() | ||
) | ||
|
||
expect_lint( | ||
"expect_false(x || y || z)", | ||
rex::rex("Instead of expect_false(A || B), write multiple expectations"), | ||
conjunct_expectation_linter() | ||
) | ||
|
||
# these lint because `||` is always outer by operator precedence | ||
expect_lint( | ||
"expect_false(x || y && z)", | ||
rex::rex("Instead of expect_false(A || B), write multiple expectations"), | ||
conjunct_expectation_linter() | ||
) | ||
expect_lint( | ||
"expect_false(x && y || z)", | ||
rex::rex("Instead of expect_false(A || B), write multiple expectations"), | ||
conjunct_expectation_linter() | ||
) | ||
}) |