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_na argument to check character() #1742

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
apply changes
Marta Alcalde-Herraiz committed Aug 15, 2024
commit e5bb6c258cb8b73775886ae4668659c5871b2334
8 changes: 7 additions & 1 deletion R/standalone-types-check.R
Original file line number Diff line number Diff line change
@@ -468,14 +468,20 @@ check_character <- function(x,
if (is_character(x) & allow_na) {
return(invisible(NULL))
}
if(is.character(x) & !(TRUE %in% is.na(x)) & !allow_na){

if (is_character(x)){
if (!allow_na && any(is.na(x))) {
cli::cli_abort("`x` must not contain NA values.", call = caller_env())
}
return(invisible(NULL))
}

if (allow_null && is_null(x)) {
return(invisible(NULL))
}
}


stop_input_type(
x,
"a character vector",
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/arg.md
Original file line number Diff line number Diff line change
@@ -262,8 +262,8 @@
(expect_error(f(na_chr)))
Output
<error/rlang_error>
Error in `f()`:
! `x` must be a character vector, not a character `NA`.
Error in `arg_match()`:
! `x` must not contain NA values.
Code
(expect_error(f(chr())))
Output
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/cnd-abort.md
Original file line number Diff line number Diff line change
@@ -101,6 +101,8 @@
Code
cat_line(branch_depth_0)
Output
Warning message:
package ‘rlang’ was built under R version 4.3.3
Error:
! foo
Backtrace:
@@ -109,6 +111,8 @@
Code
cat_line(full_depth_0)
Output
Warning message:
package ‘rlang’ was built under R version 4.3.3
Error:
! foo
Backtrace:
@@ -118,6 +122,8 @@
Code
cat_line(branch_depth_1)
Output
Warning message:
package ‘rlang’ was built under R version 4.3.3
Error in `f()`:
! foo
Backtrace:
@@ -126,6 +132,8 @@
Code
cat_line(full_depth_1)
Output
Warning message:
package ‘rlang’ was built under R version 4.3.3
Error in `f()`:
! foo
Backtrace:
12 changes: 12 additions & 0 deletions tests/testthat/_snaps/standalone-types-check.md
Original file line number Diff line number Diff line change
@@ -438,6 +438,18 @@
<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
7 changes: 2 additions & 5 deletions tests/testthat/test-standalone-types-check.R
Original file line number Diff line number Diff line change
@@ -152,21 +152,18 @@ test_that("`check_environment()` checks", {

test_that("`check_character()` checks", {
expect_null(check_character(""))
expect_error(check_character(na_chr))
expect_null(check_character(chr()))
expect_null(check_character("foo"))
expect_null(check_character(letters))

expect_null(check_character(NULL, allow_null = TRUE))
expect_error(check_character(NULL, allow_null = FALSE))

expect_error(check_character(c("a",NA)))
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))
})