Skip to content

Commit

Permalink
Use "actual vs expected" phrasing in OOB errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Oct 21, 2022
1 parent 4f2b172 commit ace0afd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
12 changes: 11 additions & 1 deletion R/subscript-loc.R
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,19 @@ cnd_body_vctrs_error_subscript_oob_location <- function(cnd, ...) {
n <- cnd$size
elt <- cnd_subscript_element_cli(n, cnd)

arg <- cnd_subscript_arg(cnd)

if (length(i) == 1) {
arg <- arg
not <- glue::glue(", not {i}")
} else {
arg <- glue::glue("Locations in {arg}")
not <- ""
}

# TODO: Switch to `format_inline()` and format bullets lazily through rlang
cli::format_error(c(
"i" = "{cli::qty(n_loc)} Location{?s} {oob_enum} do{?esn't/n't} exist.",
"x" = "{cli::qty(n_loc)} Location{?s} must be less than or equal to {n}{not}.",
"i" = "There {cli::qty(n)} {?is/are} only {elt}."
))
}
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 @@ -208,7 +208,7 @@
<error/vctrs_error_subscript_oob>
Error in `my_function()`:
! Can't subset elements past the end.
i Location 10 doesn't exist.
x Location must be less than or equal to 2, not 10.
i There are only 2 elements.

---
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/slice-assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<error/vctrs_error_subscript_oob>
Error:
! Can't assign to elements past the end.
i Location 5 doesn't exist.
x Location must be less than or equal to 3, not 5.
i There are only 3 elements.
Code
(expect_error(vec_assign(1:3, "foo", 10), "unnamed vector"))
Expand All @@ -53,7 +53,7 @@
<error/vctrs_error_subscript_oob>
Error:
! Can't negate elements past the end.
i Location 100 doesn't exist.
x Location must be less than or equal to 26, not 100.
i There are only 26 elements.
Code
(expect_error(vec_assign(set_names(letters), "foo", "bar"), class = "vctrs_error_subscript_oob")
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 @@ -228,7 +228,7 @@
<error/vctrs_error_subscript_oob>
Error:
! Can't subset elements past the end.
i Location 3 doesn't exist.
x Location must be less than or equal to 2.
i There are only 2 elements.

---
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't subset elements past the end.
i Location 3 doesn't exist.
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.
i Location 3 doesn't exist.
x Location must be less than or equal to 2, not 3.
i There are only 2 elements.

# can slice with double indices
Expand Down Expand Up @@ -89,7 +89,7 @@
Condition
Error in `vec_slice()`:
! Can't subset elements past the end.
i Locations 100 and 1000 don't exist.
x Locations must be less than or equal to 26.
i There are only 26 elements.

---
Expand All @@ -99,7 +99,7 @@
Condition
Error in `vec_slice()`:
! Can't subset elements past the end.
i Locations 100, 101, 102, ..., 109, and 110 don't exist.
x Locations must be less than or equal to 26.
i There are only 26 elements.

---
Expand Down
38 changes: 19 additions & 19 deletions tests/testthat/_snaps/subscript-loc.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,23 @@
<error/vctrs_error_subscript_oob>
Error:
! Can't subset elements past the end.
i Location 10 doesn't exist.
x Location must be less than or equal to 2, not 10.
i There are only 2 elements.
Code
(expect_error(vec_as_location(-10L, 2L), class = "vctrs_error_subscript_oob"))
Output
<error/vctrs_error_subscript_oob>
Error:
! Can't negate elements past the end.
i Location 10 doesn't exist.
x Location must be less than or equal to 2, not 10.
i There are only 2 elements.
Code
(expect_error(vec_as_location2(10L, 2L), class = "vctrs_error_subscript_oob"))
Output
<error/vctrs_error_subscript_oob>
Error:
! Can't extract elements past the end.
i Location 10 doesn't exist.
x Location must be less than or equal to 2, not 10.
i There are only 2 elements.
Code
# Character indexing
Expand Down Expand Up @@ -462,7 +462,7 @@
Condition
Error:
! Can't negate elements past the end.
i Location 4 doesn't exist.
x Location must be less than or equal to 3, not 4.
i There are only 3 elements.

---
Expand All @@ -472,7 +472,7 @@
Condition
Error:
! Can't negate elements past the end.
i Location 4 doesn't exist.
x Location must be less than or equal to 3, not 4.
i There are only 3 elements.

# num_as_location() errors on disallowed zeros when inverting negatives (#1612)
Expand Down Expand Up @@ -502,7 +502,7 @@
Condition
Error:
! Can't negate elements past the end.
i Location 6 doesn't exist.
x Location must be less than or equal to 5, not 6.
i There are only 5 elements.

---
Expand All @@ -512,7 +512,7 @@
Condition
Error:
! Can't negate elements past the end.
i Location 7 doesn't exist.
x Location must be less than or equal to 5, not 7.
i There are only 5 elements.

---
Expand All @@ -522,7 +522,7 @@
Condition
Error:
! Can't negate elements past the end.
i Location 7 doesn't exist.
x Location must be less than or equal to 5, not 7.
i There are only 5 elements.

# num_as_location() with `oob = 'error'` reports negative and positive oob values
Expand All @@ -532,7 +532,7 @@
Condition
Error:
! Can't subset elements past the end.
i Locations 6 and 7 don't exist.
x Locations must be less than or equal to 5.
i There are only 5 elements.

# missing values are supported in error formatters
Expand All @@ -544,7 +544,7 @@
<error/vctrs_error_subscript_oob>
Error:
! Can't subset elements past the end.
i Locations 2 and 3 don't exist.
x Locations must be less than or equal to 1.
i There is only 1 element.
Code
(expect_error(num_as_location(c(1, NA, 3), 1, oob = "extend"), class = "vctrs_error_subscript_oob")
Expand Down Expand Up @@ -893,7 +893,7 @@
<error/vctrs_error_subscript_oob>
Error in `my_function()`:
! Can't subset elements past the end.
i Location 30 doesn't exist.
x Location must be less than or equal to 26, not 30.
i There are only 26 elements.
Code
(expect_error(vec_as_location("foo", NULL, letters, arg = "foo", call = call(
Expand All @@ -919,7 +919,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't rename columns that don't exist.
i Location 30 doesn't exist.
x Location must be less than or equal to 26, not 30.
i There are only 26 columns.
Code
(expect_error(with_tibble_cols(vec_slice(set_names(letters), -30)), class = "vctrs_error_subscript_oob")
Expand All @@ -928,7 +928,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't rename columns that don't exist.
i Location 30 doesn't exist.
x Location must be less than or equal to 26, not 30.
i There are only 26 columns.
Code
# With tibble rows
Expand All @@ -946,7 +946,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't remove rows past the end.
i Locations 27, 28, 29, and 30 don't exist.
x Locations must be less than or equal to 26.
i There are only 26 rows.
Code
(expect_error(with_tibble_rows(vec_slice(set_names(letters), -(1:30))), class = "vctrs_error_subscript_oob")
Expand All @@ -955,7 +955,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't remove rows past the end.
i Locations 27, 28, 29, and 30 don't exist.
x Locations must be less than or equal to 26.
i There are only 26 rows.
Code
# With tidyselect select
Expand All @@ -973,7 +973,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't select columns past the end.
i Location 30 doesn't exist.
x Location must be less than or equal to 26, not 30.
i There are only 26 columns.
Code
(expect_error(with_tidyselect_select(vec_slice(set_names(letters), -(1:30))),
Expand All @@ -982,7 +982,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't select columns past the end.
i Locations 27, 28, 29, and 30 don't exist.
x Locations must be less than or equal to 26.
i There are only 26 columns.
Code
# With tidyselect relocate
Expand All @@ -1000,7 +1000,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't relocate columns that don't exist.
i Location 30 doesn't exist.
x Location must be less than or equal to 26, not 30.
i There are only 26 columns.
Code
(expect_error(with_tidyselect_relocate(vec_slice(set_names(letters), -(1:30))),
Expand All @@ -1009,7 +1009,7 @@
<error/vctrs_error_subscript_oob>
Error in `vec_slice()`:
! Can't relocate columns that don't exist.
i Locations 27, 28, 29, and 30 don't exist.
x Locations must be less than or equal to 26.
i There are only 26 columns.

# vec_as_location() checks dimensionality
Expand Down

0 comments on commit ace0afd

Please sign in to comment.