-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec5a625
commit 88b2bb9
Showing
11 changed files
with
102 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,35 @@ | ||
#' Require usage of expect_false(.) over expect_true(!.) | ||
#' | ||
#' [testthat::expect_false()] exists specifically for testing that an output is | ||
#' `FALSE`. [testthat::expect_true()] can also be used for such tests by | ||
#' negating the output, but it is better to use the tailored function instead. | ||
#' The reverse is also true -- use `expect_false(A)` instead of | ||
#' `expect_true(!A)`. | ||
#' | ||
#' @evalRd rd_tags("expect_not_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
expect_not_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_true' or text() = 'expect_false'] | ||
and following-sibling::expr[1][OP-EXCLAMATION] | ||
]" | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file = source_file, | ||
message = "expect_false(x) is better than expect_true(!x), and vice versa.", | ||
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,37 @@ | ||
test_that("expect_not_linter skips allowed usages", { | ||
expect_lint("expect_true(x)", NULL, expect_not_linter()) | ||
# NB: also applies to tinytest, but it's sufficient to test testthat | ||
expect_lint("testthat::expect_true(x)", NULL, expect_not_linter()) | ||
expect_lint("expect_false(x)", NULL, expect_not_linter()) | ||
expect_lint("testthat::expect_false(x)", NULL, expect_not_linter()) | ||
|
||
# not a strict ban on ! | ||
## (expect_false(x && y) is the same, but it's not clear which to prefer) | ||
expect_lint("expect_true(!x || !y)", NULL, expect_not_linter()) | ||
}) | ||
|
||
test_that("expect_not_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"expect_true(!x)", | ||
"expect_false\\(x\\) is better than expect_true\\(!x\\)", | ||
expect_not_linter() | ||
) | ||
|
||
expect_lint( | ||
"testthat::expect_true(!x)", | ||
"expect_false\\(x\\) is better than expect_true\\(!x\\)", | ||
expect_not_linter() | ||
) | ||
|
||
expect_lint( | ||
"expect_false(!foo(x))", | ||
"expect_false\\(x\\) is better than expect_true\\(!x\\)", | ||
expect_not_linter() | ||
) | ||
|
||
expect_lint( | ||
"testthat::expect_true(!(x && y))", | ||
"expect_false\\(x\\) is better than expect_true\\(!x\\)", | ||
expect_not_linter() | ||
) | ||
}) |