From 4a384af57e31b067c0411d06fa32a65dcaab8388 Mon Sep 17 00:00:00 2001 From: Aleksander Chlebowski <114988527+chlebowa@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:27:09 +0100 Subject: [PATCH] 511 remove `check_ellipsis` function (#515) Closes #511 --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- R/utils.R | 56 ------------------------------- man/check_ellipsis.Rd | 33 ------------------ tests/testthat/test-utils.R | 67 ------------------------------------- 3 files changed, 156 deletions(-) delete mode 100644 man/check_ellipsis.Rd diff --git a/R/utils.R b/R/utils.R index 713b773da..d4cecf760 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,59 +1,3 @@ -#' Ensure the ellipsis, ..., in method arguments are empty -#' -#' Ellipsis, ..., are needed as part of method arguments to allow for its arguments to be different from its generic's -#' arguments and for this to pass check(). Hence, ..., should always be empty. This function will check for this -#' condition. -#' -#' @param ... it should literally just be ... -#' @param stop TRUE to raise an error; FALSE will output warning message -#' @param allowed_args character vector naming arguments that are allowed in the \code{...}. -#' to allow for unnamed arguments, let "" be one of the elements in this character vector. -#' -#' @return \code{NULL} if ... is empty -#' -#' @keywords internal -#' -#' @examples -#' method.class <- function(a, b, c, ...) { -#' check_ellipsis(...) -#' } -#' method.class <- function(a, b, c, ...) { -#' check_ellipsis(..., allowed_args = c("y", "z")) -#' } -check_ellipsis <- function(..., stop = FALSE, allowed_args = character(0)) { - if (!missing(...)) { - checkmate::assert_flag(stop) - checkmate::assert_character(allowed_args, min.len = 0, null.ok = TRUE, any.missing = FALSE) - args <- list(...) - arg_names <- names(args) - if (is.null(arg_names)) { - arg_names <- rep("", length(args)) - } - extra_args <- arg_names[!is.element(arg_names, allowed_args)] - if (length(extra_args) == 0) { - return(invisible(NULL)) - } - message <- paste(length(extra_args), "total unused argument(s).") - - named_extra_args <- extra_args[!vapply(extra_args, identical, logical(1), "")] - if (length(named_extra_args) > 0) { - message <- paste0( - message, - " ", - length(named_extra_args), - " with name(s): ", - paste(named_extra_args, collapse = ", "), - "." - ) - } - if (stop) { - stop(message) - } else { - warning(message) - } - } -} - #' Whether the variable name is good to use within Show R Code #' #' Spaces are problematic because the variables must be escaped with backticks. diff --git a/man/check_ellipsis.Rd b/man/check_ellipsis.Rd deleted file mode 100644 index 308dfaead..000000000 --- a/man/check_ellipsis.Rd +++ /dev/null @@ -1,33 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{check_ellipsis} -\alias{check_ellipsis} -\title{Ensure the ellipsis, ..., in method arguments are empty} -\usage{ -check_ellipsis(..., stop = FALSE, allowed_args = character(0)) -} -\arguments{ -\item{...}{it should literally just be ...} - -\item{stop}{TRUE to raise an error; FALSE will output warning message} - -\item{allowed_args}{character vector naming arguments that are allowed in the \code{...}. -to allow for unnamed arguments, let "" be one of the elements in this character vector.} -} -\value{ -\code{NULL} if ... is empty -} -\description{ -Ellipsis, ..., are needed as part of method arguments to allow for its arguments to be different from its generic's -arguments and for this to pass check(). Hence, ..., should always be empty. This function will check for this -condition. -} -\examples{ -method.class <- function(a, b, c, ...) { - check_ellipsis(...) -} -method.class <- function(a, b, c, ...) { - check_ellipsis(..., allowed_args = c("y", "z")) -} -} -\keyword{internal} diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index bbc2e47a4..d799d9575 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -1,70 +1,3 @@ -# check_ellipsis ---- -method <- function(a, b, ..., stop = TRUE, allowed_args = character()) { - check_ellipsis(..., stop = stop, allowed_args = allowed_args) -} - -testthat::test_that("check_ellipsis with no unused", { - testthat::expect_silent(method(a = 1, b = 2)) -}) - -testthat::test_that("check_ellipsis with extra unamed arguments", { - testthat::expect_error(method(a = 1, b = 2, 5, 6), "2 total unused argument\\(s\\)\\.") - testthat::expect_warning(method(a = 1, b = 2, 5, 6, stop = FALSE), "2 total unused argument\\(s\\)\\.") -}) - -testthat::test_that("check_ellipsis with extra named arguments", { - testthat::expect_error( - method(a = 1, b = 2, c = 5, d = 6), - "2 total unused argument\\(s\\)\\. 2 with name\\(s\\): c, d\\." - ) - testthat::expect_warning( - method(a = 1, b = 2, c = 5, d = 6, stop = FALSE), - "2 total unused argument\\(s\\)\\. 2 with name\\(s\\): c, d\\." - ) -}) - -testthat::test_that("check_ellipsis with extra named and unamed arguments", { - testthat::expect_error(method(a = 1, b = 2, c = 5, 6), "2 total unused argument\\(s\\). 1 with name\\(s\\): c\\.") - testthat::expect_warning( - method(a = 1, b = 2, c = 5, 6, stop = FALSE), - "2 total unused argument\\(s\\). 1 with name\\(s\\): c\\." - ) -}) - -testthat::test_that("check_ellipsis with extra named and unamed arguments in wrong order", { - testthat::expect_error(method(c = 5, 6, a = 1, b = 2), "2 total unused argument\\(s\\)\\. 1 with name\\(s\\): c\\.") - testthat::expect_warning( - method(a = 1, c = 5, b = 2, 6, stop = FALSE), - "2 total unused argument\\(s\\)\\. 1 with name\\(s\\): c\\." - ) -}) - -testthat::test_that("check_ellipsis with allowed args", { - testthat::expect_silent(method(a = 1, b = 2, z = 3, allowed_args = c("z"))) - testthat::expect_error( - method(a = 1, b = 2, y = 3, allowed_args = c("z")), - "1 total unused argument\\(s\\)\\. 1 with name\\(s\\): y\\." - ) - testthat::expect_error( - method(a = 1, b = 2, y = 3, z = 3, allowed_args = c("z")), - "1 total unused argument\\(s\\)\\. 1 with name\\(s\\): y\\." - ) - testthat::expect_error( - method(a = 1, b = 2, 3, allowed_args = c("z")), - "1 total unused argument\\(s\\)\\." - ) - testthat::expect_silent(method(a = 1, b = 2, 3, allowed_args = c(""))) - testthat::expect_error( - method(a = 1, b = 2, 3, z = 9, allowed_args = c("")), - "1 total unused argument\\(s\\)\\. 1 with name\\(s\\): z\\." - ) - testthat::expect_silent(method(a = 1, b = 2, 3, z = 5, allowed_args = c("", "z", "y"))) - testthat::expect_silent(method(a = 1, b = 2, 3, z = 5, y = 4, allowed_args = c("", "z", "y"))) - testthat::expect_silent(method(a = 1, b = 2, allowed_args = c("", "z", "y"))) -}) - - - # check_simple_name ---- test_that("check_simple_name behaves as expected", { testthat::expect_silent(check_simple_name("aas2df"))