Skip to content

Commit

Permalink
Use standard subscript error header in OOB errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Oct 21, 2022
1 parent ace0afd commit 165cb4f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 69 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ S3method(cnd_header,vctrs_error_names_cannot_be_dot_dot)
S3method(cnd_header,vctrs_error_names_cannot_be_empty)
S3method(cnd_header,vctrs_error_names_must_be_unique)
S3method(cnd_header,vctrs_error_subscript)
S3method(cnd_header,vctrs_error_subscript_oob)
S3method(diff,vctrs_vctr)
S3method(duplicated,vctrs_sclr)
S3method(duplicated,vctrs_vctr)
Expand Down
17 changes: 0 additions & 17 deletions R/subscript-loc.R
Original file line number Diff line number Diff line change
Expand Up @@ -487,23 +487,6 @@ stop_subscript_oob <- function(i,
)
}

#' @export
cnd_header.vctrs_error_subscript_oob <- function(cnd, ...) {
if (cnd_subscript_oob_non_consecutive(cnd)) {
return(cnd_header_vctrs_error_subscript_oob_non_consecutive(cnd, ...))
}

elt <- cnd_subscript_element(cnd)
action <- cnd_subscript_action(cnd)
type <- cnd_subscript_type(cnd)

if (action %in% c("rename", "relocate") || type == "character") {
glue::glue("Can't {action} {elt[[2]]} that don't exist.")
} else {
glue::glue("Can't {action} {elt[[2]]} past the end.")
}
}

#' @export
cnd_body.vctrs_error_subscript_oob <- function(cnd, ...) {
switch(cnd_subscript_type(cnd),
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 @@ -22,23 +22,23 @@
Output
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't subset elements that don't exist.
! Can't subset elements.
x Element `foo` doesn't exist.
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 that don't exist.
! Can't subset elements with `foo`.
x Element `foo` doesn't exist.
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 that don't exist.
! Can't subset elements with `foo(bar)`.
x Element `foo` doesn't exist.

# scalar type errors are informative
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/error-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
Output
<error/vctrs_error_subscript_oob>
Error in `my_function()`:
! Can't subset elements past the end.
! Can't subset elements with `10`.
x Location must be less than or equal to 2, not 10.
i There are only 2 elements.

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/slice-assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
Output
<error/vctrs_error_subscript_oob>
Error:
! Can't assign to elements past the end.
! Can't assign elements.
x Location must be less than or equal to 3, not 5.
i There are only 3 elements.
Code
Expand All @@ -52,7 +52,7 @@
Output
<error/vctrs_error_subscript_oob>
Error:
! Can't negate elements past the end.
! Can't negate elements.
x Location must be less than or equal to 26, not 100.
i There are only 26 elements.
Code
Expand All @@ -61,7 +61,7 @@
Output
<error/vctrs_error_subscript_oob>
Error:
! Can't assign to elements that don't exist.
! Can't assign elements.
x Element `foo` doesn't exist.

# must assign with proper negative locations
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/slice-chop.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
Output
<error/vctrs_error_subscript_oob>
Error:
! Can't subset elements past the end.
! Can't subset elements.
x Location must be less than or equal to 2.
i There are only 2 elements.

Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/_snaps/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
Output
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't subset elements past the end.
! Can't subset elements with `i`.
x Location must be less than or equal to 2, not 3.
i There are only 2 elements.
Code
(expect_error(vec_slice(1:2, -3L), class = "vctrs_error_subscript_oob"))
Output
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't negate elements past the end.
! Can't negate elements with `i`.
x Location must be less than or equal to 2, not 3.
i There are only 2 elements.

Expand Down Expand Up @@ -79,7 +79,7 @@
vec_slice(c(bar = 1), "foo")
Condition
Error in `vec_slice()`:
! Can't subset elements that don't exist.
! Can't subset elements with `i`.
x Element `foo` doesn't exist.

---
Expand All @@ -88,7 +88,7 @@
vec_slice(letters, c(100, 1000))
Condition
Error in `vec_slice()`:
! Can't subset elements past the end.
! Can't subset elements with `i`.
x Locations must be less than or equal to 26.
i There are only 26 elements.

Expand All @@ -98,7 +98,7 @@
vec_slice(letters, c(1, 100:103, 2, 104:110))
Condition
Error in `vec_slice()`:
! Can't subset elements past the end.
! Can't subset elements with `i`.
x Locations must be less than or equal to 26.
i There are only 26 elements.

Expand All @@ -108,7 +108,7 @@
vec_slice(set_names(letters), c("foo", "bar"))
Condition
Error in `vec_slice()`:
! Can't subset elements that don't exist.
! Can't subset elements with `i`.
x Elements `foo` and `bar` don't exist.

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

# vec_init() validates `n`
Expand Down
Loading

0 comments on commit 165cb4f

Please sign in to comment.