Skip to content

Commit

Permalink
Tweak implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Aug 16, 2024
1 parent 9abd31c commit e643a05
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
16 changes: 8 additions & 8 deletions R/standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -459,20 +459,21 @@ check_formula <- function(x,

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

if (!missing(x)) {
if (is_character(x) & allow_na) {
return(invisible(NULL))
}

if (is_character(x)){
if (is_character(x)) {
if (!allow_na && any(is.na(x))) {
cli::cli_abort("`x` must not contain NA values.", call = caller_env())
abort(
sprintf("`%s` can't contain NA values.", arg),
arg = arg,
call = call
)
}

return(invisible(NULL))
}

Expand All @@ -486,7 +487,6 @@ check_character <- function(x,
x,
"a character vector",
...,
allow_na = allow_na,
allow_null = allow_null,
arg = arg,
call = call
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@
(expect_error(f(na_chr)))
Output
<error/rlang_error>
Error in `arg_match()`:
! `x` must not contain NA values.
Error in `f()`:
! `x` must be a single string, not a character `NA`.
Code
(expect_error(f(chr())))
Output
Expand Down
18 changes: 6 additions & 12 deletions tests/testthat/_snaps/standalone-types-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,18 +438,6 @@
<error/rlang_error>
Error in `checker()`:
! `foo` must be a character vector, not `NA`.
Code
err(checker(na_chr, check_character))
Output
<error/rlang_error>
Error in `checker()`:
! `x` must not contain NA values.
Code
err(checker(c("a", NA), check_character))
Output
<error/rlang_error>
Error in `checker()`:
! `x` must not contain NA values.
Code
err(checker(1, check_character))
Output
Expand All @@ -462,6 +450,12 @@
<error/rlang_error>
Error in `checker()`:
! `foo` must be a character vector or `NULL`, not a list.
Code
err(checker(c("a", NA), check_character, allow_na = FALSE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` can't contain NA values.

# `check_logical()` checks

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,20 @@ test_that("`check_environment()` checks", {

test_that("`check_character()` checks", {
expect_null(check_character(""))
expect_null(check_character(na_chr))
expect_null(check_character(c("a", NA)))
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(c("a",NA), allow_na = TRUE))

expect_snapshot({
err(checker(, check_character))
err(checker(NULL, check_character))
err(checker(NA, check_character))
err(checker(na_chr, check_character))
err(checker(c("a", NA), check_character))
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))
})
})

Expand Down

0 comments on commit e643a05

Please sign in to comment.