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

Add allow_empty to check_logical() and check_character() #1745

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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
8 changes: 6 additions & 2 deletions tests/testthat/test-standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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 @@ -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))

olivroy marked this conversation as resolved.
Show resolved Hide resolved
})
})

Expand All @@ -175,14 +177,16 @@ 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))

olivroy marked this conversation as resolved.
Show resolved Hide resolved
})
})

Expand Down
Loading