diff --git a/R/assert.R b/R/assert.R index 71e6d08..626ad7f 100644 --- a/R/assert.R +++ b/R/assert.R @@ -77,14 +77,16 @@ needs <- assert_package #' @param x An object to be checked. #' @param expected An appropriate expected value. #' @param allow_empty A [`logical`] scalar: should [empty][is_empty()] object be -#' allowed? -#' @param empty Deprecated. +#' ignored? +#' @param allow_null A [`logical`] scalar: should `NULL` object be ignored? #' @return #' Throws an error, if any, and returns `x` invisibly otherwise. #' @author N. Frerebeau #' @family checking methods #' @export -assert_length <- function(x, expected, allow_empty = empty, empty = FALSE) { +assert_length <- function(x, expected, allow_empty = FALSE, allow_null = FALSE) { + if (is.null(x) && isTRUE(allow_null)) return(invisible(NULL)) + arg <- deparse(substitute(x)) if (!(allow_empty && is_empty(x)) && !has_length(x, n = expected)) { txt <- tr_("%s must be of length %d; not %d.") @@ -312,7 +314,7 @@ assert_unique <- function(x) { #' "`integer`", "`double`", "`character`" or "`logical`". #' @param allow_empty A [`logical`] scalar: should [empty][is_empty()] object be #' allowed? -#' @param allow_null A [`logical`] scalar: should `NULL` object be allowed? +#' @param allow_null A [`logical`] scalar: should `NULL` object be ignored? #' @return #' Throws an error, if any, and returns `x` invisibly otherwise. #' @author N. Frerebeau diff --git a/inst/tinytest/test_assert.R b/inst/tinytest/test_assert.R index 5967672..65a8bd2 100644 --- a/inst/tinytest/test_assert.R +++ b/inst/tinytest/test_assert.R @@ -49,6 +49,7 @@ expect_inherits(cnd[[1]], "error_bad_length") expect_identical(assert_length(numeric(), expected = 10, allow_empty = TRUE), numeric()) expect_identical(assert_length(LETTERS, expected = 26), LETTERS) +expect_null(assert_length(NULL, expected = 10, allow_null = TRUE)) ## Lengths --------------------------------------------------------------------- k <- list(1:10, 1:20) diff --git a/man/assert_length.Rd b/man/assert_length.Rd index 41401da..e9a55ba 100644 --- a/man/assert_length.Rd +++ b/man/assert_length.Rd @@ -5,7 +5,7 @@ \alias{assert_lengths} \title{Check Object Length(s)} \usage{ -assert_length(x, expected, allow_empty = empty, empty = FALSE) +assert_length(x, expected, allow_empty = FALSE, allow_null = FALSE) assert_lengths(x, expected) } @@ -15,9 +15,9 @@ assert_lengths(x, expected) \item{expected}{An appropriate expected value.} \item{allow_empty}{A \code{\link{logical}} scalar: should \link[=is_empty]{empty} object be -allowed?} +ignored?} -\item{empty}{Deprecated.} +\item{allow_null}{A \code{\link{logical}} scalar: should \code{NULL} object be ignored?} } \value{ Throws an error, if any, and returns \code{x} invisibly otherwise. diff --git a/man/assert_type.Rd b/man/assert_type.Rd index 5fec80a..da5dd33 100644 --- a/man/assert_type.Rd +++ b/man/assert_type.Rd @@ -22,7 +22,7 @@ type. It must be one of "\code{list}", "\code{atomic}", "\code{vector}", "\code{ \item{allow_empty}{A \code{\link{logical}} scalar: should \link[=is_empty]{empty} object be allowed?} -\item{allow_null}{A \code{\link{logical}} scalar: should \code{NULL} object be allowed?} +\item{allow_null}{A \code{\link{logical}} scalar: should \code{NULL} object be ignored?} } \value{ Throws an error, if any, and returns \code{x} invisibly otherwise.