Skip to content

Commit

Permalink
Make type.character error for NA_character_ (#566)
Browse files Browse the repository at this point in the history
Fixes #546

---------

Co-authored-by: Hadley Wickham <[email protected]>
  • Loading branch information
nash-delcamp-slp and hadley authored Aug 20, 2024
1 parent 1562054 commit b511603
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
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

0 comments on commit b511603

Please sign in to comment.