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

Make type.character error for NA_character_ #566

Merged
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions R/modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ type.stringr_fixed <- function(x, error_call = caller_env()) {
}
#' @export
type.character <- function(x, error_call = caller_env()) {
if (length(x) == 1) {
if(is.na(x)) {
cli::cli_abort(
tr_("{.arg pattern} must be a string, not {.obj_type_friendly {x}}."),
call = error_call
)
}
}
nash-delcamp-slp marked this conversation as resolved.
Show resolved Hide resolved

if (any(is.na(x))) {
cli::cli_abort(
tr_("{.arg pattern} must be a string that does not contain NAs."),
nash-delcamp-slp marked this conversation as resolved.
Show resolved Hide resolved
call = error_call
)
}

if (identical(x, "")) "empty" else "regex"
}

Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@
Error:
! `pattern` must be a string, not an integer vector.

# useful error message for NA_character_

Code
type(NA_character_)
Condition
Error:
! `pattern` must be a string, not a character `NA`.

# useful error message for vector that includes NAs

Code
type(c("a", "b", NA_character_, "c"))
Condition
Error:
! `pattern` must be a string that does not contain NAs.

12 changes: 12 additions & 0 deletions tests/testthat/test-modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ test_that("subsetting preserves class and options", {
x <- regex("a", multiline = TRUE)
expect_equal(x[], x)
})

test_that("useful error message for NA_character_", {
expect_snapshot(error = TRUE, {
type(NA_character_)
})
})

test_that("useful error message for vector that includes NAs", {
expect_snapshot(error = TRUE, {
type(c("a", "b", NA_character_, "c"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test!

})
})
Loading