-
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.
expect_s3_class_linter and expect_s4_class_linter (#943)
* expect_s3_class_linter and expect_s4_class_linter * missed s4 in inst db * extension for yoda tests * fix issues identified by linter * fix test * add a test vs. a vector of classes * nolint
- Loading branch information
1 parent
88b2bb9
commit 463cd86
Showing
12 changed files
with
268 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,108 @@ | ||
#' Require usage of expect_s3_class() | ||
#' | ||
#' [testthat::expect_s3_class()] exists specifically for testing the class | ||
#' of S3 objects. [testthat::expect_equal()], [testthat::expect_identical()], | ||
#' and [testthat::expect_true()] can also be used for such tests, | ||
#' but it is better to use the tailored function instead. | ||
#' | ||
#' @evalRd rd_tags("expect_s3_class_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
expect_s3_class_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# (1) expect_{equal,identical}(class(x), C) | ||
# (2) expect_true(is.<class>(x)) and expect_true(inherits(x, C)) | ||
is_class_call <- xp_text_in_table(c(is_s3_class_calls, "inherits")) # nolint: object_usage_linter. TODO(#942): fix this. | ||
xpath <- glue::glue("//expr[ | ||
( | ||
SYMBOL_FUNCTION_CALL[text() = 'expect_equal' or text() = 'expect_identical'] | ||
and following-sibling::expr[ | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'class']] | ||
and (position() = 1 or preceding-sibling::expr[STR_CONST]) | ||
] | ||
) or ( | ||
SYMBOL_FUNCTION_CALL[text() = 'expect_true'] | ||
and following-sibling::expr[1][expr[SYMBOL_FUNCTION_CALL[ {is_class_call} ]]] | ||
) | ||
]") | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
return(lapply(bad_expr, gen_expect_s3_class_lint, source_file)) | ||
}) | ||
} | ||
|
||
# NB: there is no easy way to make an exhaustive list of places where an | ||
# is.<x> call can be replaced by expect_s3_class(); this list was manually | ||
# populated from the default R packages by inspection. For example, | ||
# is.matrix(x) cannot be replaced by expect_s3_class(x, "matrix") because | ||
# it is not actually an S3 class (is.object(x) is not TRUE since there | ||
# is no class set for a matrix [I am not sure if this changes in R 4]. | ||
# Further, there are functions named is.<x> that have nothing to do with | ||
# object type, e.g. is.finite(), is.nan(), or is.R(). | ||
is_s3_class_calls <- paste0("is.", c( | ||
# base | ||
"data.frame", "factor", "numeric_version", | ||
"ordered", "package_version", "qr", "table", | ||
# utils grDevices tcltk tcltk grid grid | ||
"relistable", "raster", "tclObj", "tkwin", "grob", "unit", | ||
# stats | ||
"mts", "stepfun", "ts", "tskernel" | ||
)) | ||
|
||
gen_expect_s3_class_lint <- function(expr, source_file) { | ||
matched_function <- xml2::xml_text(xml2::xml_find_first(expr, "SYMBOL_FUNCTION_CALL")) | ||
if (matched_function %in% c("expect_equal", "expect_identical")) { | ||
lint_msg <- sprintf("expect_s3_class(x, k) is better than %s(class(x), k).", matched_function) | ||
} else { | ||
lint_msg <- "expect_s3_class(x, k) is better than expect_true(is.<k>(x)) or expect_true(inherits(x, k))." | ||
} | ||
lint_msg <- paste(lint_msg, "Note also expect_s4_class() available for testing S4 objects.") | ||
xml_nodes_to_lint(expr, source_file, lint_msg, type = "warning") | ||
} | ||
|
||
#' Require usage of expect_s4_class(x, k) over expect_true(is(x, k)) | ||
#' | ||
#' [testthat::expect_s4_class()] exists specifically for testing the class | ||
#' of S4 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_s3_class_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
expect_s4_class_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# TODO(michaelchirico): also catch expect_{equal,identical}(methods::is(x), k). | ||
# there are no hits for this on google3 as of now. | ||
|
||
# require 2 expressions because methods::is(x) alone is a valid call, even | ||
# though the character output wouldn't make any sense for expect_true(). | ||
xpath <- "//expr[ | ||
SYMBOL_FUNCTION_CALL[text() = 'expect_true'] | ||
and following-sibling::expr[1][count(expr) = 3 and expr[SYMBOL_FUNCTION_CALL[text() = 'is']]] | ||
]" | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file = source_file, | ||
message = paste( | ||
"expect_s4_class(x, k) is better than expect_true(is(x, k)).", | ||
"Note also expect_s3_class() available for testing S3 objects." | ||
), | ||
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,104 @@ | ||
test_that("expect_s3_class_linter skips allowed usages", { | ||
# expect_s3_class doesn't have an inverted version | ||
expect_lint("expect_true(!inherits(x, 'class'))", NULL, expect_s3_class_linter()) | ||
# NB: also applies to tinytest, but it's sufficient to test testthat | ||
expect_lint("testthat::expect_true(!inherits(x, 'class'))", NULL, expect_s3_class_linter()) | ||
|
||
# other is.<x> calls are not suitable for expect_s3_class in particular | ||
expect_lint("expect_true(is.na(x))", NULL, expect_s3_class_linter()) | ||
|
||
# case where expect_s3_class() *could* be used but we don't enforce | ||
expect_lint("expect_true(is.data.table(x))", NULL, expect_s3_class_linter()) | ||
}) | ||
|
||
test_that("expect_s3_class_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"expect_equal(class(x), 'data.frame')", | ||
rex::rex("expect_s3_class(x, k) is better than expect_equal(class(x), k)"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# works when testing against a sequence of classes too | ||
expect_lint( | ||
"expect_equal(class(x), c('data.table', 'data.frame'))", | ||
rex::rex("expect_s3_class(x, k) is better than expect_equal(class(x), k)"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# expect_identical is treated the same as expect_equal | ||
expect_lint( | ||
"testthat::expect_identical(class(x), 'lm')", | ||
rex::rex("expect_s3_class(x, k) is better than expect_identical(class(x), k)"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# yoda test with string literal in first arg also caught | ||
expect_lint( | ||
"expect_equal('data.frame', class(x))", | ||
rex::rex("expect_s3_class(x, k) is better than expect_equal(class(x), k)"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# different equivalent usages | ||
expect_lint( | ||
"expect_true(is.table(foo(x)))", | ||
rex::rex("expect_s3_class(x, k) is better than expect_true(is.<k>(x))"), | ||
expect_s3_class_linter() | ||
) | ||
expect_lint( | ||
"expect_true(inherits(x, 'table'))", | ||
rex::rex("expect_s3_class(x, k) is better than expect_true(is.<k>(x))"), | ||
expect_s3_class_linter() | ||
) | ||
|
||
# TODO(michaelchirico): consider more carefully which sorts of class(x) %in% . and | ||
# . %in% class(x) calls should be linted | ||
# expect_lint( | ||
# "expect_true('lm' %in% class(x))", | ||
# "expect_s3_class\\(x, k\\) is better than expect_equal\\(class\\(x\\), k", | ||
# expect_s3_class_linter | ||
# ) | ||
}) | ||
|
||
test_that("expect_s4_class_linter skips allowed usages", { | ||
# expect_s4_class doesn't have an inverted version | ||
expect_lint("expect_true(!is(x, 'class'))", NULL, expect_s4_class_linter()) | ||
# NB: also applies to tinytest, but it's sufficient to test testthat | ||
expect_lint("testthat::expect_s3_class(!is(x, 'class'))", NULL, expect_s4_class_linter()) | ||
}) | ||
|
||
test_that("expect_s4_class blocks simple disallowed usages", { | ||
expect_lint( | ||
"expect_true(is(x, 'data.frame'))", | ||
rex::rex("expect_s4_class(x, k) is better than expect_true(is(x, k))"), | ||
expect_s4_class_linter() | ||
) | ||
|
||
# namespace qualification is irrelevant | ||
expect_lint( | ||
"testthat::expect_true(methods::is(x, 'SpatialPolygonsDataFrame'))", | ||
rex::rex("expect_s4_class(x, k) is better than expect_true(is(x, k))"), | ||
expect_s4_class_linter() | ||
) | ||
}) | ||
|
||
skip_if_not_installed("patrick") | ||
local({ | ||
# test for lint errors appropriately raised for all is.<class> calls | ||
is_classes <- c( | ||
"data.frame", "factor", "numeric_version", | ||
"ordered", "package_version", "qr", "table", | ||
"relistable", "raster", "tclObj", "tkwin", "grob", "unit", | ||
"mts", "stepfun", "ts", "tskernel" | ||
) | ||
patrick::with_parameters_test_that( | ||
"expect_true(is.<base class>) is caught", | ||
expect_lint( | ||
sprintf("expect_true(is.%s(x))", is_class), | ||
rex::rex("expect_s3_class(x, k) is better than expect_true(is.<k>(x))"), | ||
expect_s3_class_linter() | ||
), | ||
.test_name = is_classes, | ||
is_class = is_classes | ||
) | ||
}) |
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