Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

511 remove check_ellipsis function #515

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
33 changes: 0 additions & 33 deletions man/check_ellipsis.Rd

This file was deleted.

67 changes: 0 additions & 67 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down