From ccdc2666434cb0f2a241404f0a2154b8d635cbbb Mon Sep 17 00:00:00 2001 From: olivroy Date: Sun, 18 Aug 2024 11:50:45 -0400 Subject: [PATCH 1/4] Add `allow_empty` to `check_logical()` and `check_character()` --- R/standalone-types-check.R | 27 ++++++++++++++++--- .../testthat/_snaps/standalone-types-check.md | 12 +++++++++ tests/testthat/test-standalone-types-check.R | 4 +++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/R/standalone-types-check.R b/R/standalone-types-check.R index 22ea57ba8..5eeea7d57 100644 --- a/R/standalone-types-check.R +++ b/R/standalone-types-check.R @@ -1,7 +1,7 @@ # --- # 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) @@ -9,6 +9,12 @@ # # ## 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) # @@ -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, @@ -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)) { diff --git a/tests/testthat/_snaps/standalone-types-check.md b/tests/testthat/_snaps/standalone-types-check.md index a54db03a6..f5fc554f3 100644 --- a/tests/testthat/_snaps/standalone-types-check.md +++ b/tests/testthat/_snaps/standalone-types-check.md @@ -456,6 +456,12 @@ Error in `checker()`: ! `foo` can't contain NA values. + Code + err(checker(character(0), check_character, allow_empty = FALSE)) + Output + + Error in `checker()`: + ! `foo` must be a character vector, not an empty character vector. # `check_logical()` checks @@ -489,6 +495,12 @@ 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 in `checker()`: + ! `foo` must be a logical vector, not an empty logical vector. # non-numeric types are not numbers diff --git a/tests/testthat/test-standalone-types-check.R b/tests/testthat/test-standalone-types-check.R index a8a6e177c..c96cd9def 100644 --- a/tests/testthat/test-standalone-types-check.R +++ b/tests/testthat/test-standalone-types-check.R @@ -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)) + }) }) @@ -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)) + }) }) From a7bf9bbfbb9078badfe6381082d963816c6863df Mon Sep 17 00:00:00 2001 From: olivroy Date: Sun, 18 Aug 2024 11:55:43 -0400 Subject: [PATCH 2/4] Add a test for allow empty --- tests/testthat/test-standalone-types-check.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-standalone-types-check.R b/tests/testthat/test-standalone-types-check.R index c96cd9def..1b453f05f 100644 --- a/tests/testthat/test-standalone-types-check.R +++ b/tests/testthat/test-standalone-types-check.R @@ -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)) @@ -177,7 +177,7 @@ 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)) From 4edb5d6ea3449e8e139449022faa222dd73afa23 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Sun, 18 Aug 2024 14:30:10 -0400 Subject: [PATCH 3/4] Update test-standalone-types-check.R --- tests/testthat/test-standalone-types-check.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-standalone-types-check.R b/tests/testthat/test-standalone-types-check.R index 1b453f05f..e51ccf428 100644 --- a/tests/testthat/test-standalone-types-check.R +++ b/tests/testthat/test-standalone-types-check.R @@ -167,7 +167,6 @@ test_that("`check_character()` checks", { 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)) - }) }) From 5daa36dd8fa2d8a934d56d72c7517d03072dd11d Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Sun, 18 Aug 2024 14:30:22 -0400 Subject: [PATCH 4/4] Update test-standalone-types-check.R --- tests/testthat/test-standalone-types-check.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-standalone-types-check.R b/tests/testthat/test-standalone-types-check.R index e51ccf428..7cfce6e25 100644 --- a/tests/testthat/test-standalone-types-check.R +++ b/tests/testthat/test-standalone-types-check.R @@ -185,7 +185,6 @@ test_that("`check_logical()` checks", { err(checker(1, check_logical)) err(checker(list("foo", "bar"), check_logical, allow_null = TRUE)) err(checker(logical(0), check_logical, allow_empty = FALSE)) - }) })