Skip to content

Commit

Permalink
Use "can't" phrasing in unknown named location errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Oct 21, 2022
1 parent a1e7bbf commit ad93a63
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
7 changes: 4 additions & 3 deletions R/subscript-loc.R
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 +541,16 @@ cnd_body_vctrs_error_subscript_oob_location <- function(cnd, ...) {
))
}
cnd_body_vctrs_error_subscript_oob_name <- function(cnd, ...) {
elt <- cnd_subscript_element(cnd, capital = TRUE)
elt <- cnd_subscript_element(cnd, capital = FALSE)
elt_cap <- cnd_subscript_element(cnd, capital = TRUE)
oob <- cnd$i[!cnd$i %in% cnd$names]
oob_enum <- enumerate(glue::backtick(oob))

format_error_bullets(c(
x = glue::glue(ngettext(
length(oob),
"{elt[[1]]} {oob_enum} doesn't exist.",
"{elt[[2]]} {oob_enum} don't exist."
"Can't find {elt[[1]]} {oob_enum}.",
"Can't find {elt[[2]]} {oob_enum}.",
))
))
}
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't subset elements.
x Element `foo` doesn't exist.
x Can't find element `foo`.
Code
(expect_error(with_subscript_data(vec_slice(set_names(letters), "foo"), quote(
foo)), class = "vctrs_error_subscript_oob"))
Output
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't subset elements with `foo`.
x Element `foo` doesn't exist.
x Can't find element `foo`.
Code
(expect_error(with_subscript_data(vec_slice(set_names(letters), "foo"), quote(
foo(bar))), class = "vctrs_error_subscript_oob"))
Output
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't subset elements with `foo(bar)`.
x Element `foo` doesn't exist.
x Can't find element `foo`.

# scalar type errors are informative

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/slice-assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<error/vctrs_error_subscript_oob>
Error:
! Can't assign elements.
x Element `foo` doesn't exist.
x Can't find element `foo`.

# must assign with proper negative locations

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
Condition
Error in `vec_slice()`:
! Can't subset elements with `i`.
x Element `foo` doesn't exist.
x Can't find element `foo`.

---

Expand Down Expand Up @@ -111,7 +111,7 @@
Condition
Error in `vec_slice()`:
! Can't subset elements with `i`.
x Elements `foo` and `bar` don't exist.
x Can't find elements `foo` and `bar`.

---

Expand All @@ -120,7 +120,7 @@
Condition
Error in `vec_slice()`:
! Can't subset elements with `i`.
x Elements `A`, `B`, `C`, `D`, `E`, etc. don't exist.
x Can't find elements `A`, `B`, `C`, `D`, `E`, etc..

# vec_init() validates `n`

Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/_snaps/subscript-loc.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,23 @@
<error/vctrs_error_subscript_oob>
Error:
! Can't subset elements with `"foo"`.
x Element `foo` doesn't exist.
x Can't find element `foo`.
Code
(expect_error(vec_as_location2("foo", 1L, names = "bar"), class = "vctrs_error_subscript_oob")
)
Output
<error/vctrs_error_subscript_oob>
Error:
! Can't extract element with `"foo"`.
x Element `foo` doesn't exist.
x Can't find element `foo`.
Code
(expect_error(vec_as_location2("foo", 1L, names = "bar", call = call("baz")),
class = "vctrs_error_subscript_oob"))
Output
<error/vctrs_error_subscript_oob>
Error in `baz()`:
! Can't extract element with `"foo"`.
x Element `foo` doesn't exist.
x Can't find element `foo`.

# vec_as_location2() requires length 1 inputs

Expand Down Expand Up @@ -897,7 +897,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't subset elements with `i`.
x Element `foo` doesn't exist.
x Can't find element `foo`.
Code
# With custom `arg`
(expect_error(vec_as_location(30, length(letters), arg = "foo", call = call(
Expand All @@ -915,7 +915,7 @@
<error/vctrs_error_subscript_oob>
Error in `my_function()`:
! Can't subset elements with `foo`.
x Element `foo` doesn't exist.
x Can't find element `foo`.
Code
# With tibble columns
(expect_error(with_tibble_cols(vec_slice(set_names(letters), "foo")), class = "vctrs_error_subscript_oob")
Expand All @@ -924,7 +924,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't rename columns with `foo(bar)`.
x Column `foo` doesn't exist.
x Can't find column `foo`.
Code
(expect_error(with_tibble_cols(vec_slice(set_names(letters), 30)), class = "vctrs_error_subscript_oob")
)
Expand All @@ -951,7 +951,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't remove rows with `foo(bar)`.
x Rows `foo` and `bar` don't exist.
x Can't find rows `foo` and `bar`.
Code
(expect_error(with_tibble_rows(vec_slice(set_names(letters), 1:30)), class = "vctrs_error_subscript_oob")
)
Expand Down Expand Up @@ -980,7 +980,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't select columns with `foo(bar)`.
x Columns `foo` and `bar` don't exist.
x Can't find columns `foo` and `bar`.
Code
(expect_error(with_tidyselect_select(vec_slice(set_names(letters), 30)), class = "vctrs_error_subscript_oob")
)
Expand Down Expand Up @@ -1008,7 +1008,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't relocate columns with `foo(bar)`.
x Columns `foo` and `bar` don't exist.
x Can't find columns `foo` and `bar`.
Code
(expect_error(with_tidyselect_relocate(vec_slice(set_names(letters), 30)),
class = "vctrs_error_subscript_oob"))
Expand Down

0 comments on commit ad93a63

Please sign in to comment.