From aa196e3086edaeeb0ca5cef0e08b18bcf2f5c510 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 16 Mar 2022 19:45:47 +0000 Subject: [PATCH] New expect_length_linter --- DESCRIPTION | 1 + NAMESPACE | 1 + NEWS.md | 1 + R/expect_length_linter.R | 36 ++++++++++++++++++ inst/lintr/linters.csv | 1 + man/best_practices_linters.Rd | 1 + man/expect_length_linter.Rd | 19 ++++++++++ man/linters.Rd | 7 ++-- man/package_development_linters.Rd | 1 + man/readability_linters.Rd | 1 + tests/testthat/test-expect_length_linter.R | 44 ++++++++++++++++++++++ 11 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 R/expect_length_linter.R create mode 100644 man/expect_length_linter.Rd create mode 100644 tests/testthat/test-expect_length_linter.R diff --git a/DESCRIPTION b/DESCRIPTION index 7d0f0325c..e944e6891 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -63,6 +63,7 @@ Collate: 'duplicate_argument_linter.R' 'equals_na_linter.R' 'exclude.R' + 'expect_length_linter.R' 'expect_lint.R' 'expect_not_linter.R' 'expect_null_linter.R' diff --git a/NAMESPACE b/NAMESPACE index 2724e6146..3db3748df 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ export(default_undesirable_functions) export(default_undesirable_operators) export(duplicate_argument_linter) export(equals_na_linter) +export(expect_length_linter) export(expect_lint) export(expect_lint_free) export(expect_not_linter) diff --git a/NEWS.md b/NEWS.md index 0a91a3bb4..6523cfa14 100644 --- a/NEWS.md +++ b/NEWS.md @@ -93,6 +93,7 @@ function calls. (#850, #851, @renkun-ken) + `expect_s4_class_linter()` Require usage of `expect_s4_class(x, k)` over `expect_true(methods::is(x, k))` + `expect_not_linter()` Require usage of `expect_false(x)` over `expect_true(!x)`, and _vice versa_. + `expect_true_false_linter()` Require usage of `expect_true(x)` over `expect_equal(x, TRUE)` and similar + * `expect_length_linter()` Require usage of `expect_length(x, n)` over `expect_equal(length(x), n)` and similar * `assignment_linter()` now lints right assignment (`->` and `->>`) and gains two arguments. `allow_cascading_assign` (`TRUE` by default) toggles whether to lint `<<-` and `->>`; `allow_right_assign` toggles whether to lint `->` and `->>` (#915, @michaelchirico) # lintr 2.0.1 diff --git a/R/expect_length_linter.R b/R/expect_length_linter.R new file mode 100644 index 000000000..ee147d4c8 --- /dev/null +++ b/R/expect_length_linter.R @@ -0,0 +1,36 @@ +#' Require usage of expect_length(x, n) over expect_equal(length(x), n) +#' +#' [testthat::expect_length()] exists specifically for testing the [length()] 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_length_linter") +#' @seealso [linters] for a complete list of linters available in lintr. +#' @export +expect_length_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_true(length(x) == 1) + xpath <- sprintf("//expr[ + SYMBOL_FUNCTION_CALL[text() = 'expect_equal' or text() = 'expect_identical'] + and following-sibling::expr[ + expr[SYMBOL_FUNCTION_CALL[text() = 'length']] + and (position() = 1 or preceding-sibling::expr[NUM_CONST]) + ] + ]") + + bad_expr <- xml2::xml_find_all(xml, xpath) + return(lapply(bad_expr, gen_expect_length_lint, source_file)) + }) +} + +gen_expect_length_lint <- function(expr, source_file) { + matched_function <- xml2::xml_text(xml2::xml_find_first(expr, "SYMBOL_FUNCTION_CALL")) + lint_msg <- sprintf("expect_length(x, n) is better than %s(length(x), n)", matched_function) + xml_nodes_to_lint(expr, source_file, lint_msg, type = "warning") +} diff --git a/inst/lintr/linters.csv b/inst/lintr/linters.csv index 7fcc331cd..3b3f574fd 100644 --- a/inst/lintr/linters.csv +++ b/inst/lintr/linters.csv @@ -9,6 +9,7 @@ commented_code_linter,style readability best_practices default cyclocomp_linter,style readability best_practices default configurable duplicate_argument_linter,correctness common_mistakes configurable equals_na_linter,robustness correctness common_mistakes default +expect_length_linter,package_development best_practices readability expect_not_linter,package_development best_practices readability expect_null_linter,package_development best_practices expect_s3_class_linter,package_development best_practices diff --git a/man/best_practices_linters.Rd b/man/best_practices_linters.Rd index 470c2cedc..65d489330 100644 --- a/man/best_practices_linters.Rd +++ b/man/best_practices_linters.Rd @@ -15,6 +15,7 @@ The following linters are tagged with 'best_practices': \item{\code{\link{absolute_path_linter}}} \item{\code{\link{commented_code_linter}}} \item{\code{\link{cyclocomp_linter}}} +\item{\code{\link{expect_length_linter}}} \item{\code{\link{expect_not_linter}}} \item{\code{\link{expect_null_linter}}} \item{\code{\link{expect_s3_class_linter}}} diff --git a/man/expect_length_linter.Rd b/man/expect_length_linter.Rd new file mode 100644 index 000000000..568dfaff1 --- /dev/null +++ b/man/expect_length_linter.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/expect_length_linter.R +\name{expect_length_linter} +\alias{expect_length_linter} +\title{Require usage of expect_length(x, n) over expect_equal(length(x), n)} +\usage{ +expect_length_linter() +} +\description{ +\code{\link[testthat:expect_length]{testthat::expect_length()}} exists specifically for testing the \code{\link[=length]{length()}} of +an object. \code{\link[testthat:equality-expectations]{testthat::expect_equal()}} can also be used for such tests, +but it is better to use the tailored function instead. +} +\seealso{ +\link{linters} for a complete list of linters available in lintr. +} +\section{Tags}{ +\link[=best_practices_linters]{best_practices}, \link[=package_development_linters]{package_development}, \link[=readability_linters]{readability} +} diff --git a/man/linters.Rd b/man/linters.Rd index 56cac5a9d..02ddc4cf6 100644 --- a/man/linters.Rd +++ b/man/linters.Rd @@ -17,15 +17,15 @@ Documentation for linters is structured into tags to allow for easier discovery. \section{Tags}{ The following tags exist: \itemize{ -\item{\link[=best_practices_linters]{best_practices} (16 linters)} +\item{\link[=best_practices_linters]{best_practices} (17 linters)} \item{\link[=common_mistakes_linters]{common_mistakes} (5 linters)} \item{\link[=configurable_linters]{configurable} (16 linters)} \item{\link[=consistency_linters]{consistency} (7 linters)} \item{\link[=correctness_linters]{correctness} (7 linters)} \item{\link[=default_linters]{default} (25 linters)} \item{\link[=efficiency_linters]{efficiency} (4 linters)} -\item{\link[=package_development_linters]{package_development} (8 linters)} -\item{\link[=readability_linters]{readability} (23 linters)} +\item{\link[=package_development_linters]{package_development} (9 linters)} +\item{\link[=readability_linters]{readability} (24 linters)} \item{\link[=robustness_linters]{robustness} (10 linters)} \item{\link[=style_linters]{style} (32 linters)} } @@ -43,6 +43,7 @@ The following linters exist: \item{\code{\link{cyclocomp_linter}} (tags: best_practices, configurable, default, readability, style)} \item{\code{\link{duplicate_argument_linter}} (tags: common_mistakes, configurable, correctness)} \item{\code{\link{equals_na_linter}} (tags: common_mistakes, correctness, default, robustness)} +\item{\code{\link{expect_length_linter}} (tags: best_practices, package_development, readability)} \item{\code{\link{expect_not_linter}} (tags: best_practices, package_development, readability)} \item{\code{\link{expect_null_linter}} (tags: best_practices, package_development)} \item{\code{\link{expect_s3_class_linter}} (tags: best_practices, package_development)} diff --git a/man/package_development_linters.Rd b/man/package_development_linters.Rd index 76cf00266..2b736cc24 100644 --- a/man/package_development_linters.Rd +++ b/man/package_development_linters.Rd @@ -13,6 +13,7 @@ Linters useful to package developers, for example for writing consistent tests. The following linters are tagged with 'package_development': \itemize{ \item{\code{\link{backport_linter}}} +\item{\code{\link{expect_length_linter}}} \item{\code{\link{expect_not_linter}}} \item{\code{\link{expect_null_linter}}} \item{\code{\link{expect_s3_class_linter}}} diff --git a/man/readability_linters.Rd b/man/readability_linters.Rd index f5cab44f2..d49702b3f 100644 --- a/man/readability_linters.Rd +++ b/man/readability_linters.Rd @@ -17,6 +17,7 @@ The following linters are tagged with 'readability': \item{\code{\link{commas_linter}}} \item{\code{\link{commented_code_linter}}} \item{\code{\link{cyclocomp_linter}}} +\item{\code{\link{expect_length_linter}}} \item{\code{\link{expect_not_linter}}} \item{\code{\link{expect_true_false_linter}}} \item{\code{\link{function_left_parentheses_linter}}} diff --git a/tests/testthat/test-expect_length_linter.R b/tests/testthat/test-expect_length_linter.R new file mode 100644 index 000000000..7a3454e43 --- /dev/null +++ b/tests/testthat/test-expect_length_linter.R @@ -0,0 +1,44 @@ +test_that("expect_length_linter skips allowed usages", { + expect_lint("expect_equal(nrow(x), 4L)", NULL, expect_length_linter()) + # NB: also applies to tinytest, but it's sufficient to test testthat + expect_lint("testthat::expect_equal(nrow(x), 4L)", NULL, expect_length_linter()) + + # only check the first argument. yoda tests in the second argument will be + # missed, but there are legitimate uses of length() in argument 2 + expect_lint("expect_equal(nrow(x), length(y))", NULL, expect_length_linter()) +}) + +test_that("expect_length_linter blocks simple disallowed usages", { + expect_lint( + "expect_equal(length(x), 2L)", + rex::rex("expect_length(x, n) is better than expect_equal(length(x), n)"), + expect_length_linter() + ) + + expect_lint( + "testthat::expect_equal(length(DF), length(old))", + rex::rex("expect_length(x, n) is better than expect_equal(length(x), n)"), + expect_length_linter() + ) + + # yoda test cases + expect_lint( + "expect_equal(2, length(x))", + rex::rex("expect_length(x, n) is better than expect_equal(length(x), n)"), + expect_length_linter() + ) + + expect_lint( + "expect_equal(2L, length(x))", + rex::rex("expect_length(x, n) is better than expect_equal(length(x), n)"), + expect_length_linter() + ) +}) + +test_that("expect_length_linter blocks expect_identical usage as well", { + expect_lint( + "expect_identical(length(x), 2L)", + rex::rex("expect_length(x, n) is better than expect_identical(length(x), n)"), + expect_length_linter() + ) +})