
vec_as_location() checks for mix of negative and missing locations
==================================================================

> vec_as_location(-c(1L, NA), 30)
Error: Must subset elements with a valid subscript vector.
x Negative locations can't have missing values.
i The subscript has a missing value at location 2.

> vec_as_location(-c(1L, rep(NA, 10)), 30)
Error: Must subset elements with a valid subscript vector.
x Negative locations can't have missing values.
i The subscript has 10 missing values at locations 2, 3, 4, 5, 6, etc.


vec_as_location() checks for mix of negative and positive locations
===================================================================

> vec_as_location(c(-1L, 1L), 30)
Error: Must subset elements with a valid subscript vector.
x Negative locations can't be mixed with positive locations.
i The subscript has a positive value at location 2.

> vec_as_location(c(-1L, rep(1L, 10)), 30)
Error: Must subset elements with a valid subscript vector.
x Negative locations can't be mixed with positive locations.
i The subscript has 10 missing values at locations 2, 3, 4, 5, 6, etc.


num_as_location() optionally forbids negative indices
=====================================================

> num_as_location(dbl(1, -1), 2L, negative = "error")
Error: Must subset elements with a valid subscript vector.
x The subscript can't contain negative locations.


logical subscripts must match size of indexed vector
====================================================

> vec_as_location(c(TRUE, FALSE), 3)
Error: Must subset elements with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x The input has size 3 but the subscript has size 2.


character subscripts require named vectors
==========================================

> vec_as_location(letters[1], 3)
Error: Can't use character names to index an unnamed vector.


vec_as_location() requires integer, character, or logical inputs
================================================================

> vec_as_location(mtcars, 10L)
Error: Must subset elements with a valid subscript vector.
x The subscript has the wrong type `data.frame<
  mpg : double
  cyl : double
  disp: double
  hp  : double
  drat: double
  wt  : double
  qsec: double
  vs  : double
  am  : double
  gear: double
  carb: double
>`.
i It must be logical, numeric, or character.

> vec_as_location(env(), 10L)
Error: Must subset elements with a valid subscript vector.
x The subscript has the wrong type `environment`.
i It must be logical, numeric, or character.

> vec_as_location(foobar(), 10L)
Error: Must subset elements with a valid subscript vector.
x The subscript has the wrong type `vctrs_foobar`.
i It must be logical, numeric, or character.

> vec_as_location(2.5, 3L)
Error: Must subset elements with a valid subscript vector.
x Lossy cast from <double> to <integer>.

> vec_as_location(list(), 10L)
Error: Must subset elements with a valid subscript vector.
x The subscript has the wrong type `list`.
i It must be logical, numeric, or character.

> vec_as_location(function() NULL, 10L)
Error: Must subset elements with a valid subscript vector.
x The subscript has the wrong type `closure`.
i It must be logical, numeric, or character.

> # Idem with custom `arg`
> vec_as_location(env(), 10L, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x The subscript `foo` has the wrong type `environment`.
i It must be logical, numeric, or character.

> vec_as_location(foobar(), 10L, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x The subscript `foo` has the wrong type `vctrs_foobar`.
i It must be logical, numeric, or character.

> vec_as_location(2.5, 3L, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Lossy cast from `foo` <double> to <integer>.


vec_as_location2() requires integer or character inputs
=======================================================

> vec_as_location2(TRUE, 10L)
Error: Must extract element with a single subscript.
x The subscript has the wrong type `logical`.
i It must be numeric or character.

> vec_as_location2(mtcars, 10L)
Error: Must extract element with a single subscript.
x The subscript has the wrong type `data.frame<
  mpg : double
  cyl : double
  disp: double
  hp  : double
  drat: double
  wt  : double
  qsec: double
  vs  : double
  am  : double
  gear: double
  carb: double
>`.
i It must be numeric or character.

> vec_as_location2(env(), 10L)
Error: Must extract element with a single subscript.
x The subscript has the wrong type `environment`.
i It must be numeric or character.

> vec_as_location2(foobar(), 10L)
Error: Must extract element with a single subscript.
x The subscript has the wrong type `vctrs_foobar`.
i It must be numeric or character.

> vec_as_location2(2.5, 3L)
Error: Must extract element with a single subscript.
x Lossy cast from <double> to <integer>.

> # Idem with custom `arg`
> vec_as_location2(foobar(), 10L, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` has the wrong type `vctrs_foobar`.
i It must be numeric or character.

> vec_as_location2(2.5, 3L, arg = "foo")
Error: Must extract element with a single subscript.
x Lossy cast from `foo` <double> to <integer>.


vec_as_location2() requires length 1 inputs
===========================================

> vec_as_location2(1:2, 2L)
Error: Must extract element with a single subscript.
x The subscript has size 2 but must be size 1.

> vec_as_location2(mtcars, 10L)
Error: Must extract element with a single subscript.
x The subscript has the wrong type `data.frame<
  mpg : double
  cyl : double
  disp: double
  hp  : double
  drat: double
  wt  : double
  qsec: double
  vs  : double
  am  : double
  gear: double
  carb: double
>`.
i It must be numeric or character.

> # Idem with custom `arg`
> vec_as_location2(1:2, 2L, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` has size 2 but must be size 1.

> vec_as_location2(mtcars, 10L, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` has the wrong type `data.frame<
  mpg : double
  cyl : double
  disp: double
  hp  : double
  drat: double
  wt  : double
  qsec: double
  vs  : double
  am  : double
  gear: double
  carb: double
>`.
i It must be numeric or character.

> vec_as_location2(1:2, 2L, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` has size 2 but must be size 1.


vec_as_location2() requires positive integers
=============================================

> vec_as_location2(0, 2L)
Error: Must extract element with a single subscript.
x The subscript has value 0 but must be a positive location.

> vec_as_location2(-1, 2L)
Error: Must extract element with a single subscript.
x The subscript has value -1 but must be a positive location.

> # Idem with custom `arg`
> vec_as_location2(0, 2L, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` has value 0 but must be a positive location.

> # vec_as_location2() fails with NA
> vec_as_location2(na_int, 2L)
Error: Must extract element with a single subscript.
x The subscript can't be `NA`.

> vec_as_location2(na_chr, 1L, names = "foo")
Error: Must extract element with a single subscript.
x The subscript can't be `NA`.

> # Idem with custom `arg`
> vec_as_location2(na_int, 2L, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` can't be `NA`.


vec_as_location() and variants check for OOB elements
=====================================================

> # Numeric subscripts
> vec_as_location(10L, 2L)
Error: Can't subset elements that don't exist.
x The location 10 doesn't exist.
i There are only 2 elements.

> vec_as_location(-10L, 2L)
Error: Can't negate elements that don't exist.
x The location 10 doesn't exist.
i There are only 2 elements.

> vec_as_location2(10L, 2L)
Error: Can't subset elements that don't exist.
x The location 10 doesn't exist.
i There are only 2 elements.

> # Character subscripts
> vec_as_location("foo", 1L, names = "bar")
Error: Can't subset elements that don't exist.
x The element `foo` doesn't exist.

> vec_as_location2("foo", 1L, names = "bar")
Error: Can't subset elements that don't exist.
x The element `foo` doesn't exist.


can optionally extend beyond the end
====================================

> num_as_location(c(1, 3), 1, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i The input has size 1.
x The subscript contains non-consecutive location 3.

> num_as_location(c(1:5, 7), 3, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i The input has size 3.
x The subscript contains non-consecutive locations 4 and 7.

> num_as_location(c(1:5, 7, 1), 3, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i The input has size 3.
x The subscript contains non-consecutive locations 4 and 7.

> num_as_location(c(1:5, 7, 1, 10), 3, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i The input has size 3.
x The subscript contains non-consecutive locations 4, 7, and 10.


missing values are supported in error formatters
================================================

> num_as_location(c(1, NA, 2, 3), 1)
Error: Can't subset elements that don't exist.
x The locations 2 and 3 don't exist.
i There are only 1 element.

> num_as_location(c(1, NA, 3), 1, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i The input has size 1.
x The subscript contains non-consecutive location 3.


can customise subscript type errors
===================================

> # With custom `arg`
> num_as_location(-1, 2, negative = "error", arg = "foo")
Error: Must subset elements with a valid subscript vector.
x The subscript `foo` can't contain negative locations.

> num_as_location2(-1, 2, negative = "error", arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` has value -1 but must be a positive location.

> vec_as_location2(0, 2, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` has value 0 but must be a positive location.

> vec_as_location2(na_dbl, 2, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` can't be `NA`.

> vec_as_location2(c(1, 2), 2, arg = "foo")
Error: Must extract element with a single subscript.
x The subscript `foo` has size 2 but must be size 1.

> vec_as_location(c(TRUE, FALSE), 3, arg = "foo")
Error: Must subset elements with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x The input has size 3 but the subscript `foo` has size 2.

> vec_as_location(c(-1, NA), 3, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Negative locations can't have missing values.
i The subscript `foo` has a missing value at location 2.

> vec_as_location(c(-1, 1), 3, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Negative locations can't be mixed with positive locations.
i The subscript `foo` has a positive value at location 2.

> num_as_location(c(1, 4), 2, oob = "extend", arg = "foo")
Error: Can't subset elements beyond the end with non-consecutive locations.
i The input has size 2.
x The subscript `foo` contains non-consecutive location 4.

> # With tibble columns
> with_tibble_cols(num_as_location(-1, 2, negative = "error"))
Error: Must rename columns with a valid subscript vector.
x The subscript `foo(bar)` can't contain negative locations.

> with_tibble_cols(num_as_location2(-1, 2, negative = "error"))
Error: Must rename column with a single subscript.
x The subscript `foo(bar)` has value -1 but must be a positive location.

> with_tibble_cols(vec_as_location2(0, 2))
Error: Must rename column with a single subscript.
x The subscript `foo(bar)` has value 0 but must be a positive location.

> with_tibble_cols(vec_as_location2(na_dbl, 2))
Error: Must rename column with a single subscript.
x The subscript `foo(bar)` can't be `NA`.

> with_tibble_cols(vec_as_location2(c(1, 2), 2))
Error: Must rename column with a single subscript.
x The subscript `foo(bar)` has size 2 but must be size 1.

> with_tibble_cols(vec_as_location(c(TRUE, FALSE), 3))
Error: Must rename columns with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x The input has size 3 but the subscript `foo(bar)` has size 2.

> with_tibble_cols(vec_as_location(c(-1, NA), 3))
Error: Must rename columns with a valid subscript vector.
x Negative locations can't have missing values.
i The subscript `foo(bar)` has a missing value at location 2.

> with_tibble_cols(vec_as_location(c(-1, 1), 3))
Error: Must rename columns with a valid subscript vector.
x Negative locations can't be mixed with positive locations.
i The subscript `foo(bar)` has a positive value at location 2.

> with_tibble_cols(num_as_location(c(1, 4), 2, oob = "extend"))
Error: Can't rename columns beyond the end with non-consecutive locations.
i The input has size 2.
x The subscript `foo(bar)` contains non-consecutive location 4.


can customise OOB errors
========================

> vec_slice(set_names(letters), "foo")
Error: Can't subset elements that don't exist.
x The element `foo` doesn't exist.

> # With custom `arg`
> vec_as_location(30, length(letters), arg = "foo")
Error: Can't subset elements that don't exist.
x The location 30 doesn't exist.
i There are only 26 elements.

> vec_as_location("foo", NULL, letters, arg = "foo")
Error: Can't subset elements that don't exist.
x The element `foo` doesn't exist.

> # With tibble columns
> with_tibble_cols(vec_slice(set_names(letters), "foo"))
Error: Can't rename columns that don't exist.
x The column `foo` doesn't exist.

> with_tibble_cols(vec_slice(set_names(letters), 30))
Error: Can't rename columns that don't exist.
x The location 30 doesn't exist.
i There are only 26 columns.

> with_tibble_cols(vec_slice(set_names(letters), -30))
Error: Can't rename columns that don't exist.
x The location 30 doesn't exist.
i There are only 26 columns.

> # With tibble rows
> with_tibble_rows(vec_slice(set_names(letters), c("foo", "bar")))
Error: Can't remove rows that don't exist.
x The rows `foo` and `bar` don't exist.

> with_tibble_rows(vec_slice(set_names(letters), 1:30))
Error: Can't remove rows that don't exist.
x The locations 27, 28, 29, and 30 don't exist.
i There are only 26 rows.

> with_tibble_rows(vec_slice(set_names(letters), -(1:30)))
Error: Can't remove rows that don't exist.
x The locations 27, 28, 29, and 30 don't exist.
i There are only 26 rows.


can disallow missing values
===========================

> vec_as_location(c(1, NA), 2, missing = "error")
Error: Must subset elements with a valid subscript vector.
x The subscript can't contain missing values.
x It has a missing value at location 2.

> vec_as_location(c(1, NA, 2, NA), 2, missing = "error", arg = "foo")
Error: Must subset elements with a valid subscript vector.
x The subscript can't contain missing values.
x It has missing values at locations 2 and 4.

> with_tibble_cols(vec_as_location(c(1, NA, 2, NA), 2, missing = "error"))
Error: Must rename columns with a valid subscript vector.
x The subscript `foo(bar)` can't contain missing values.
x It has missing values at locations 2 and 4.

