Skip to content

Commit

Permalink
Add allow_null arg
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Sep 5, 2024
1 parent 4d90647 commit 35c1ce4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
25 changes: 22 additions & 3 deletions R/standalone-utils-assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,40 +76,59 @@ assert_nonmissing <- function(x, name = deparse(substitute(x)),


assert_scalar_character <- function(x, name = deparse(substitute(x)),
arg = name, call = parent.frame()) {
allow_null = FALSE,
arg = name, call = parent.frame()) {
if (allow_null && is.null(x)) {
return(invisible(x))
}
assert_scalar(x, name, arg = arg, call = call)
assert_character(x, name, arg = arg, call = call)
assert_nonmissing(x, name, arg = arg, call = call)
}


assert_scalar_numeric <- function(x, name = deparse(substitute(x)),
allow_null = FALSE,
arg = name, call = parent.frame()) {
if (allow_null && is.null(x)) {
return(invisible(x))
}
assert_scalar(x, name, arg = arg, call = call)
assert_numeric(x, name, arg = arg, call = call)
assert_nonmissing(x, name, arg = arg, call = call)
}


assert_scalar_integer <- function(x, name = deparse(substitute(x)),
tolerance = NULL, arg = name, call = parent.frame()) {
tolerance = NULL, allow_null = FALSE,
arg = name, call = parent.frame()) {
if (allow_null && is.null(x)) {
return(invisible(x))
}
assert_scalar(x, name, arg = arg, call = call)
assert_integer(x, name, tolerance = tolerance, arg = arg, call = call)
assert_nonmissing(x, name, arg = arg, call = call)
}


assert_scalar_logical <- function(x, name = deparse(substitute(x)),
allow_null = FALSE,
arg = name, call = parent.frame()) {
if (allow_null && is.null(x)) {
return(invisible(x))
}
assert_scalar(x, name, arg = arg, call = call)
assert_logical(x, name, arg = arg, call = call)
assert_nonmissing(x, name, arg = arg, call = call)
}


assert_scalar_size <- function(x, allow_zero = TRUE,
assert_scalar_size <- function(x, allow_zero = TRUE, allow_null = FALSE,
name = deparse(substitute(x)),
arg = name, call = parent.frame()) {
if (allow_null && is.null(x)) {
return(invisible(x))
}
assert_scalar_integer(x, name = name, arg = arg, call = call)
assert_nonmissing(x, name, arg = arg, call = call)
min <- if (allow_zero) 0 else 1
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-util-assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,21 @@ test_that("assert_scalar_positive numeric", {
expect_error(assert_scalar_positive_numeric(x, allow_zero = FALSE),
"'x' must be greater than 0")
})


test_that("assert_scalar_x supports null on request", {
expect_error(assert_scalar_character(NULL), "has length 0")
expect_no_error(assert_scalar_character(NULL, allow_null = TRUE))

expect_error(assert_scalar_integer(NULL), "has length 0")
expect_no_error(assert_scalar_integer(NULL, allow_null = TRUE))

expect_error(assert_scalar_numeric(NULL), "has length 0")
expect_no_error(assert_scalar_numeric(NULL, allow_null = TRUE))

expect_error(assert_scalar_logical(NULL), "has length 0")
expect_no_error(assert_scalar_logical(NULL, allow_null = TRUE))

expect_error(assert_scalar_size(NULL), "has length 0")
expect_no_error(assert_scalar_size(NULL, allow_null = TRUE))
})

0 comments on commit 35c1ce4

Please sign in to comment.