Skip to content

Commit

Permalink
Add allow_empty to check_logical() and check_character()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Aug 18, 2024
1 parent 69659c4 commit ccdc266
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
27 changes: 23 additions & 4 deletions R/standalone-types-check.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# ---
# repo: r-lib/rlang
# file: standalone-types-check.R
# last-updated: 2023-03-13
# last-updated: 2024-08-18
# license: https://unlicense.org
# dependencies: standalone-obj-type.R
# imports: rlang (>= 1.1.0)
# ---
#
# ## Changelog
#
# 2024-08-18
# - `check_logical()` and `check_character()` gain `allow_empty` to disallow
# `logical(0)` and `character(0)` respectively.
#
# - `check_logical()` gain `allow_na` to disallow NA values.
#
# 2024-08-15:
# - `check_character()` gains an `allow_na` argument (@martaalcalde, #1724)
#
Expand Down Expand Up @@ -464,14 +470,16 @@ check_formula <- function(x,

check_character <- function(x,
...,
allow_empty = TRUE,
allow_na = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()) {

if (!missing(x)) {
if (is_character(x)) {
if (!allow_na && any(is.na(x))) {
problematic <- !allow_empty && length(x) == 0L
if (!problematic && is_character(x)) {
if (!allow_na && anyNA(x)) {
abort(
sprintf("`%s` can't contain NA values.", arg),
arg = arg,
Expand Down Expand Up @@ -499,11 +507,22 @@ check_character <- function(x,

check_logical <- function(x,
...,
allow_empty = TRUE,
allow_na = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()) {
if (!missing(x)) {
if (is_logical(x)) {
if (!allow_na && anyNA(x)) {
abort(
sprintf("`%s` can't contain NA values.", arg),
arg = arg,
call = call
)
}
problematic <- !allow_empty && length(x) == 0

if (!problematic && is_logical(x)) {
return(invisible(NULL))
}
if (allow_null && is_null(x)) {
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/_snaps/standalone-types-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@
<error/rlang_error>
Error in `checker()`:
! `foo` can't contain NA values.
Code
err(checker(character(0), check_character, allow_empty = FALSE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a character vector, not an empty character vector.

# `check_logical()` checks

Expand Down Expand Up @@ -489,6 +495,12 @@
<error/rlang_error>
Error in `checker()`:
! `foo` must be a logical vector or `NULL`, not a list.
Code
err(checker(logical(0), check_logical, allow_empty = FALSE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a logical vector, not an empty logical vector.

# non-numeric types are not numbers

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ test_that("`check_character()` checks", {
err(checker(1, check_character))
err(checker(list("foo", "bar"), check_character, allow_null = TRUE))
err(checker(c("a", NA), check_character, allow_na = FALSE))
err(checker(character(0), check_character, allow_empty = FALSE))

})
})

Expand All @@ -183,6 +185,8 @@ test_that("`check_logical()` checks", {
err(checker(NA_integer_, check_logical))
err(checker(1, check_logical))
err(checker(list("foo", "bar"), check_logical, allow_null = TRUE))
err(checker(logical(0), check_logical, allow_empty = FALSE))

})
})

Expand Down

0 comments on commit ccdc266

Please sign in to comment.