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 all 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# stringr (development version)

* `str_*` now errors if `pattern` includes any `NA`s (@nash-delcamp-slp, #546).
* `str_view()` now displays a message when called with a zero-length character
vector (@LouisMPenrod, #497).
* Adds `[[.stringr_pattern` method to go along with existing `[.stringr_pattern`
Expand Down
9 changes: 8 additions & 1 deletion R/modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ type.stringr_fixed <- function(x, error_call = caller_env()) {
}
#' @export
type.character <- function(x, error_call = caller_env()) {
if (any(is.na(x))) {
cli::cli_abort(
tr_("{.arg pattern} must be a character vector that does not contain NAs."),
call = error_call
)
}

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

Expand All @@ -213,7 +220,7 @@ type.default <- function(x, error_call = caller_env()) {
}

cli::cli_abort(
tr_("{.arg pattern} must be a string, not {.obj_type_friendly {x}}."),
tr_("{.arg pattern} must be a character vector, not {.obj_type_friendly {x}}."),
call = error_call
)
}
Expand Down
15 changes: 14 additions & 1 deletion tests/testthat/_snaps/modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,18 @@
type(1:3)
Condition
Error:
! `pattern` must be a string, not an integer vector.
! `pattern` must be a character vector, not an integer vector.

# useful errors for NAs

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

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/split.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
str_split("x", 1)
Condition
Error in `str_split()`:
! `pattern` must be a string, not a number.
! `pattern` must be a character vector, not a number.
Code
str_split("x", "x", n = 0)
Condition
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ test_that("subsetting preserves class and options", {
expect_equal(x[], x)
})

test_that("useful errors for NAs", {
expect_snapshot(error = TRUE, {
type(NA)
type(c("a", "b", NA_character_, "c"))
})
})

test_that("stringr_pattern methods", {
ex <- coll(c("foo", "bar"))
expect_true(inherits(ex[1], "stringr_pattern"))
Expand Down
Loading