From ace0afd4d083a5b92536c2095c9d8a6b381f7dc7 Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Fri, 21 Oct 2022 10:46:14 +0200 Subject: [PATCH] Use "actual vs expected" phrasing in OOB errors --- R/subscript-loc.R | 12 +++++++- tests/testthat/_snaps/error-call.md | 2 +- tests/testthat/_snaps/slice-assign.md | 4 +-- tests/testthat/_snaps/slice-chop.md | 2 +- tests/testthat/_snaps/slice.md | 8 +++--- tests/testthat/_snaps/subscript-loc.md | 38 +++++++++++++------------- 6 files changed, 38 insertions(+), 28 deletions(-) diff --git a/R/subscript-loc.R b/R/subscript-loc.R index 53d7989c3..41f385b95 100644 --- a/R/subscript-loc.R +++ b/R/subscript-loc.R @@ -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}." )) } diff --git a/tests/testthat/_snaps/error-call.md b/tests/testthat/_snaps/error-call.md index 9fa9ddc08..515a54178 100644 --- a/tests/testthat/_snaps/error-call.md +++ b/tests/testthat/_snaps/error-call.md @@ -208,7 +208,7 @@ 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. --- diff --git a/tests/testthat/_snaps/slice-assign.md b/tests/testthat/_snaps/slice-assign.md index 93eebb30e..b8e0f35a4 100644 --- a/tests/testthat/_snaps/slice-assign.md +++ b/tests/testthat/_snaps/slice-assign.md @@ -38,7 +38,7 @@ 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")) @@ -53,7 +53,7 @@ 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") diff --git a/tests/testthat/_snaps/slice-chop.md b/tests/testthat/_snaps/slice-chop.md index f79e057c2..80a67a613 100644 --- a/tests/testthat/_snaps/slice-chop.md +++ b/tests/testthat/_snaps/slice-chop.md @@ -228,7 +228,7 @@ 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. --- diff --git a/tests/testthat/_snaps/slice.md b/tests/testthat/_snaps/slice.md index ef493a9a2..c8640b310 100644 --- a/tests/testthat/_snaps/slice.md +++ b/tests/testthat/_snaps/slice.md @@ -24,7 +24,7 @@ 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")) @@ -32,7 +32,7 @@ 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 @@ -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. --- @@ -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. --- diff --git a/tests/testthat/_snaps/subscript-loc.md b/tests/testthat/_snaps/subscript-loc.md index e1f40b9f0..0eff34209 100644 --- a/tests/testthat/_snaps/subscript-loc.md +++ b/tests/testthat/_snaps/subscript-loc.md @@ -172,7 +172,7 @@ 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")) @@ -180,7 +180,7 @@ 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")) @@ -188,7 +188,7 @@ 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 @@ -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. --- @@ -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) @@ -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. --- @@ -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. --- @@ -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 @@ -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 @@ -544,7 +544,7 @@ 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") @@ -893,7 +893,7 @@ 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( @@ -919,7 +919,7 @@ 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") @@ -928,7 +928,7 @@ 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 @@ -946,7 +946,7 @@ 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") @@ -955,7 +955,7 @@ 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 @@ -973,7 +973,7 @@ 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))), @@ -982,7 +982,7 @@ 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 @@ -1000,7 +1000,7 @@ 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))), @@ -1009,7 +1009,7 @@ 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