Skip to content
Open
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
17 changes: 13 additions & 4 deletions R/standalone-types-check.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# ---
# 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
#
# 2025-09-19
# - `check_logical()` and `check_character()` gain `allow_empty` to disallow
# `logical(0)` and `character(0)` respectively.
#
# 2025-09-19:
# - `check_logical()` gains an `allow_na` argument (@jonthegeek, #1724)
#
Expand Down Expand Up @@ -505,14 +509,16 @@ check_formula <- function(
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 @@ -541,13 +547,16 @@ check_character <- function(
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)) {
problematic <- !allow_empty && length(x) == 0

if (!problematic && is_logical(x)) {
if (!allow_na && any(is.na(x))) {
abort(
sprintf("`%s` can't contain NA values.", arg),
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.
Code
err(checker(NA, check_logical, allow_na = FALSE))
Output
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test_that("`check_character()` checks", {
expect_null(check_character(chr()))
expect_null(check_character("foo"))
expect_null(check_character(letters))
expect_null(check_character(NULL, allow_null = TRUE))
expect_null(check_character(NULL, allow_null = TRUE, allow_empty = FALSE))

expect_snapshot({
err(checker(, check_character))
Expand All @@ -171,6 +171,7 @@ 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 @@ -180,14 +181,15 @@ test_that("`check_logical()` checks", {
expect_null(check_logical(na_lgl))
expect_null(check_logical(lgl()))
expect_null(check_logical(c(TRUE, FALSE, NA)))
expect_null(check_logical(NULL, allow_null = TRUE))
expect_null(check_logical(NULL, allow_null = TRUE, allow_empty = FALSE))

expect_snapshot({
err(checker(, check_logical))
err(checker(NULL, check_logical))
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))
err(checker(NA, check_logical, allow_na = FALSE))
})
})
Expand Down
Loading