| 1 |
test_that("select() is generic", { |
| 2 |
expect_identical( |
| 3 |
select(set_names(letters), b:c), |
| 4 |
c(b = "b", c = "c") |
| 5 |
) |
| 6 |
expect_identical( |
| 7 |
select(as.list(set_names(letters)), b:c), |
| 8 |
list(b = "b", c = "c") |
| 9 |
) |
| 10 |
}) |
| 11 |
|
| 12 |
test_that("select() supports existing duplicates", { |
| 13 |
x <- list(a = 1, b = 2, a = 3) |
| 14 |
expect_identical(select(x, A = a), list(A = 1, A = 3)) |
| 15 |
}) |
| 16 |
|
| 17 |
test_that("absent `sel` leads to empty selection", { |
| 18 |
expect_identical(select_loc(mtcars), named(int())) |
| 19 |
expect_identical(select_loc(data.frame()), named(int())) |
| 20 |
}) |
| 21 |
|
| 22 |
test_that("zero-length vector `sel` leads to empty selection (#214)", { |
| 23 |
expect_identical(select_loc(mtcars, character()), named(int())) |
| 24 |
expect_identical(select_loc(data.frame(), character()), named(int())) |
| 25 |
}) |
| 26 |
|
| 27 |
test_that("can specify inclusion and exclusion", { |
| 28 |
x <- list(a = 1, b = 2, c = 3) |
| 29 |
expect_identical(select_loc(x, int(), include = "b"), c(b = 2L)) |
| 30 |
expect_identical(select_loc(x, -int(), exclude = c("a", "c")), c(b = 2L)) |
| 31 |
|
| 32 |
# Can exclude variables that don't exist |
| 33 |
expect_identical(select_loc(x, 2, exclude = "d"), c(b = 2L)) |
| 34 |
}) |
| 35 |
|
| 36 |
test_that("included variables added to front", { |
| 37 |
x <- list(a = 1, b = 2, c = 3) |
| 38 |
expect_named(select_loc(x, "a", include = "b"), c("b", "a")) |
| 39 |
expect_named(select_loc(x, "c", include = "b"), c("b", "c")) |
| 40 |
|
| 41 |
# but only if not already present |
| 42 |
expect_named(select_loc(x, c("a", "b"), include = "b"), c("a", "b")) |
| 43 |
expect_named(select_loc(x, c("b", "a"), include = "b"), c("b", "a")) |
| 44 |
|
| 45 |
state <- 0L |
| 46 |
fn <- fn <- function() { |
| 47 |
state <<- state + 1L |
| 48 |
"b" |
| 49 |
} |
| 50 |
|
| 51 |
select_loc(x, c("a", fn()), include = "b") |
| 52 |
expect_equal(state, 1) |
| 53 |
}) |
| 54 |
|
| 55 |
test_that("include and exclude validate their inputs", { |
| 56 |
expect_snapshot({ |
| 57 |
x <- list(a = 1, b = 2, c = 3) |
| 58 |
(expect_error(select_loc(x, "a", include = 1))) |
| 59 |
(expect_error(select_loc(x, "a", include = "d"))) |
| 60 |
(expect_error(select_loc(x, "a", exclude = 1))) |
| 61 |
}) |
| 62 |
}) |
| 63 |
|
| 64 |
test_that("variables are excluded with non-strict `any_of()`", { |
| 65 |
expect_identical( |
| 66 |
select_loc(iris, 1:3, exclude = "foo"), |
| 67 |
select_loc(iris, 1:3) |
| 68 |
) |
| 69 |
}) |
| 70 |
|
| 71 |
test_that("select_loc() checks inputs", { |
| 72 |
expect_error(select_loc(function() NULL), class = "vctrs_error_scalar_type") |
| 73 |
}) |
| 74 |
|
| 75 |
test_that("select_loc() accepts name spec", { |
| 76 |
expect_identical( |
| 77 |
select_loc(mtcars, c(foo = c(mpg, cyl)), name_spec = "{outer}_{inner}"), |
| 78 |
c(foo_1 = 1L, foo_2 = 2L) |
| 79 |
) |
| 80 |
}) |
| 81 |
|
| 82 |
test_that("result is named even with constant inputs (#173)", { |
| 83 |
expect_identical( |
| 84 |
eval_select("Sepal.Width", iris), |
| 85 |
c(Sepal.Width = 2L) |
| 86 |
) |
| 87 |
}) |
| 88 |
|
| 89 |
test_that("can forbid rename syntax (#178)", { |
| 90 |
expect_snapshot({ |
| 91 |
(expect_error(select_loc(mtcars, c(foo = cyl), allow_rename = FALSE))) |
| 92 |
(expect_error(select_loc(mtcars, c(cyl, foo = cyl), allow_rename = FALSE))) |
| 93 |
(expect_error(select_loc(mtcars, c(cyl, foo = mpg), allow_rename = FALSE))) |
| 94 |
(expect_error(select_loc(mtcars, c(foo = mpg, cyl), allow_rename = FALSE))) |
| 95 |
}) |
| 96 |
|
| 97 |
expect_named(select_loc(mtcars, starts_with("c") | all_of("am"), allow_rename = FALSE), c("cyl", "carb", "am")) |
| 98 |
}) |
| 99 |
|
| 100 |
test_that("can forbid empty selections", { |
| 101 |
expect_snapshot(error = TRUE, { |
| 102 |
select_loc(mtcars, allow_empty = FALSE) |
| 103 |
select_loc(mtcars, integer(), allow_empty = FALSE) |
| 104 |
select_loc(mtcars, starts_with("z"), allow_empty = FALSE) |
| 105 |
}) |
| 106 |
}) |
| 107 |
|
| 108 |
test_that("eval_select() errors mention correct calls", { |
| 109 |
f <- function() stop("foo") |
| 110 |
expect_snapshot((expect_error(select_loc(mtcars, f())))) |
| 111 |
}) |
| 112 |
|
| 113 |
test_that("predicate outputs are type-checked", { |
| 114 |
expect_snapshot({ |
| 115 |
(expect_error(select_loc(mtcars, function(x) ""))) |
| 116 |
}) |
| 117 |
}) |
| 118 |
|
| 119 |
test_that("eval_select() produces correct backtraces", { |
| 120 |
f <- function(base) g(base) |
| 121 |
g <- function(base) h(base) |
| 122 |
h <- function(base) if (base) stop("foo") else abort("foo") |
| 123 |
|
| 124 |
local_options( |
| 125 |
rlang_trace_trop_env = current_env(), |
| 126 |
rlang_trace_format_srcrefs = FALSE |
| 127 |
) |
| 128 |
|
| 129 |
expect_snapshot({ |
| 130 |
print(expect_error(select_loc(mtcars, f(base = TRUE)))) |
| 131 |
print(expect_error(select_loc(mtcars, f(base = FALSE)))) |
| 132 |
}) |
| 133 |
}) |
| 134 |
|
| 135 |
test_that("eval_select() produces correct chained errors", { |
| 136 |
expect_snapshot({ |
| 137 |
(expect_error(select_loc(mtcars, 1 + ""))) |
| 138 |
|
| 139 |
f <- function() 1 + "" |
| 140 |
(expect_error(select_loc(mtcars, f()))) |
| 141 |
}) |
| 142 |
}) |
| 143 |
|
| 144 |
test_that("can select with predicate when `allow_rename` is `FALSE` (#225)", { |
| 145 |
sel <- eval_select( |
| 146 |
expr(where(is.numeric)), |
| 147 |
mtcars, |
| 148 |
allow_rename = FALSE |
| 149 |
) |
| 150 |
expect_equal(sel, set_names(seq_along(mtcars), names(mtcars))) |
| 151 |
}) |
| 152 |
|
| 153 |
test_that("location must resolve to numeric variables throws error", { |
| 154 |
expect_error( |
| 155 |
select_loc(letters2, !!list()), |
| 156 |
class = "vctrs_error_subscript_type" |
| 157 |
) |
| 158 |
expect_error( |
| 159 |
select_loc(letters2, !!env()), |
| 160 |
class = "vctrs_error_subscript_type" |
| 161 |
) |
| 162 |
}) |
| 163 |
|
| 164 |
test_that("order is determined from inputs (#53)", { |
| 165 |
expect_identical( |
| 166 |
select_loc(mtcars, c(starts_with("c"), starts_with("d"))), |
| 167 |
c(cyl = 2L, carb = 11L, disp = 3L, drat = 5L) |
| 168 |
) |
| 169 |
expect_identical( |
| 170 |
select_loc(mtcars, one_of(c("carb", "mpg"))), |
| 171 |
c(carb = 11L, mpg = 1L) |
| 172 |
) |
| 173 |
}) |
| 174 |
|
| 175 |
|
| 176 |
test_that("middle (no-match) selector should not clear previous selectors (issue #2275)", { |
| 177 |
cn <- setNames(nm = c("x", "y", "z")) |
| 178 |
|
| 179 |
expect_equal( |
| 180 |
select_loc(cn, c(contains("x"), contains("foo"), contains("z"))), |
| 181 |
c(x = 1L, z = 3L) |
| 182 |
) |
| 183 |
expect_equal( |
| 184 |
select_loc(cn, c(contains("x"), -contains("foo"), contains("z"))), |
| 185 |
c(x = 1L, z = 3L) |
| 186 |
) |
| 187 |
}) |
| 188 |
|
| 189 |
test_that("selecting with predicate + rename doesn't duplicate", { |
| 190 |
x <- list(a = "x", b = "y", c = 1, d = 2) |
| 191 |
expect_equal(select_loc(x, c(where(is.numeric), C = c)), c(C = 3, d = 4)) |
| 192 |
expect_equal(select_loc(x, c(where(is.numeric), A = a)), c(c = 3, d = 4, A = 1)) |
| 193 |
}) |