| 1 |
test_that("leaves of data expression tree are evaluated in the context", { |
| 2 |
wrapper <- function(x, var) select_loc(x, {{ var }}:length(x)) |
| 3 |
expect_identical(wrapper(letters2, x), select_loc(letters2, x:26)) |
| 4 |
|
| 5 |
wrapper <- function(x, var) select_loc(x, -({{ var }}:length(x))) |
| 6 |
expect_identical(wrapper(letters2, x), select_loc(letters2, -(x:26))) |
| 7 |
|
| 8 |
wrapper <- function(x, var1, var2) select_loc(x, c(-{{ var1 }}, -({{ var2 }}:length(x)))) |
| 9 |
expect_identical(wrapper(letters2, a, c), select_loc(letters2, c(-a, -(c:26)))) |
| 10 |
}) |
| 11 |
|
| 12 |
test_that("dots passed to `c()` are evaluated in their context", { |
| 13 |
wrapper <- function(x, ...) { |
| 14 |
select_loc(x, c(x, length(x), ...)) |
| 15 |
} |
| 16 |
f <- function(x, ...) { |
| 17 |
a <- 13 |
| 18 |
g(x, ..., identity(a)) |
| 19 |
} |
| 20 |
g <- function(x, ...) { |
| 21 |
a <- 15 |
| 22 |
wrapper(x, ..., identity(a)) |
| 23 |
} |
| 24 |
expect_identical(f(letters2, e, 10), select_loc(letters2, c(x, 26, e, 10, 13, 15))) |
| 25 |
}) |
| 26 |
|
| 27 |
test_that("quosures can be used in data expressions", { |
| 28 |
expect_identical(select_loc(letters2, !!quo(a)), select_loc(letters2, a)) |
| 29 |
expect_identical(select_loc(letters2, !!quo(a:!!quo(c))), select_loc(letters2, a:c)) |
| 30 |
expect_identical(select_loc(letters2, !!quo(c(!!quo(a)))), select_loc(letters2, a)) |
| 31 |
}) |
| 32 |
|
| 33 |
test_that("quosures update the current context", { |
| 34 |
quo <- local({ |
| 35 |
`_foo` <- 24 |
| 36 |
quo(a:identity(`_foo`)) |
| 37 |
}) |
| 38 |
expect_identical(select_loc(letters2, !!quo(c(-(!!quo)))), select_loc(letters2, -(a:24))) |
| 39 |
}) |
| 40 |
|
| 41 |
test_that("data expressions support character vectors (#78)", { |
| 42 |
expect_identical(select_loc(letters2, -identity(letters2[2:5])), select_loc(letters2, -(2:5))) |
| 43 |
expect_identical(select_loc(letters2, identity("a"):identity("c")), select_loc(letters2, a:c)) |
| 44 |
expect_identical(select_loc(letters2, (identity(letters2[[1]]))), select_loc(letters2, a)) |
| 45 |
expect_identical(select_loc(letters2, c(identity(letters2[[1]]))), select_loc(letters2, a)) |
| 46 |
}) |
| 47 |
|
| 48 |
test_that("boolean operators are overloaded", { |
| 49 |
expect_identical( |
| 50 |
select_loc(letters2, starts_with("a") & ends_with("a")), |
| 51 |
select_loc(letters2, intersect(starts_with("a"), ends_with("a"))), |
| 52 |
) |
| 53 |
|
| 54 |
expect_identical( |
| 55 |
select_loc(letters2, starts_with("a") | ends_with("c")), |
| 56 |
select_loc(letters2, c(starts_with("a"), ends_with("c"))) |
| 57 |
) |
| 58 |
|
| 59 |
expect_identical( |
| 60 |
select_loc(letters2, starts_with("a") | ends_with("c") | contains("z")), |
| 61 |
select_loc(letters2, c(starts_with("a"), ends_with("c"), contains("z"))) |
| 62 |
) |
| 63 |
|
| 64 |
expect_identical( |
| 65 |
select_loc(letters2, (starts_with("a") | ends_with("c")) & contains("a")), |
| 66 |
select_loc(letters2, intersect(c(starts_with("a"), ends_with("c")), contains("a"))) |
| 67 |
) |
| 68 |
|
| 69 |
expect_identical( |
| 70 |
select_loc(letters2, !(starts_with("a") | ends_with("c"))), |
| 71 |
select_loc(letters2, -(starts_with("a") | ends_with("c"))), |
| 72 |
) |
| 73 |
|
| 74 |
# This pattern is not possible with `intersect()` because its |
| 75 |
# arguments are evaluated in non-data context |
| 76 |
expect_error( |
| 77 |
select_loc(letters2, intersect(c(starts_with("a"), ends_with("c")), b:d)), |
| 78 |
"not found" |
| 79 |
) |
| 80 |
expect_identical( |
| 81 |
select_loc(letters2, (starts_with("a") | ends_with("c")) & b:d), |
| 82 |
select_loc(letters2, c) |
| 83 |
) |
| 84 |
|
| 85 |
expect_identical( |
| 86 |
select_loc(letters2, (starts_with("a") | ends_with("c")) | i:k), |
| 87 |
select_loc(letters2, c(c(starts_with("a"), ends_with("c")), i:k)), |
| 88 |
) |
| 89 |
}) |
| 90 |
|
| 91 |
test_that("scalar boolean operators fail informatively", { |
| 92 |
expect_snapshot(error = TRUE, { |
| 93 |
select_loc(letters2, starts_with("a") || ends_with("b")) |
| 94 |
select_loc(letters2, starts_with("a") && ends_with("b")) |
| 95 |
}) |
| 96 |
}) |
| 97 |
|
| 98 |
test_that("can use `+` in env context", { |
| 99 |
foo <- 1 |
| 100 |
expect_identical(select_loc(letters2, foo + 2), c(c = 3L)) |
| 101 |
expect_error(select_loc(letters2, a + 2), "not found") |
| 102 |
}) |
| 103 |
|
| 104 |
test_that("can use `-` in env context", { |
| 105 |
expect_identical( |
| 106 |
select_loc(iris, 1:(ncol(iris) - 2)), |
| 107 |
select_loc(iris, 1:3) |
| 108 |
) |
| 109 |
}) |
| 110 |
|
| 111 |
test_that("can't use `*` and `^` in data context", { |
| 112 |
expect_error(select_loc(letters2, a * 2), "arithmetic") |
| 113 |
expect_error(select_loc(letters2, a^2), "arithmetic") |
| 114 |
|
| 115 |
expect_snapshot(error = TRUE, { |
| 116 |
select_loc(letters2, a * 2) |
| 117 |
select_loc(letters2, a^2) |
| 118 |
}) |
| 119 |
}) |
| 120 |
|
| 121 |
test_that("can use arithmetic operators in non-data context", { |
| 122 |
expect_identical(select_loc(letters2, identity(2 * 2 + 2^2 / 2)), c(f = 6L)) |
| 123 |
}) |
| 124 |
|
| 125 |
test_that("symbol lookup outside data informs caller about better practice", { |
| 126 |
expect_snapshot({ |
| 127 |
vars <- c("a", "b") |
| 128 |
select_loc(letters2, vars) |
| 129 |
}) |
| 130 |
}) |
| 131 |
|
| 132 |
test_that("selection helpers are in the context mask", { |
| 133 |
out <- local(envir = env(base_env()), { |
| 134 |
letters2 <- rlang::set_names(letters) |
| 135 |
tidyselect::eval_select(quote(all_of("a")), letters2) |
| 136 |
}) |
| 137 |
expect_identical(out, c(a = 1L)) |
| 138 |
}) |
| 139 |
|
| 140 |
test_that("non-strict evaluation allows unknown variables", { |
| 141 |
expect_identical( |
| 142 |
select_loc(letters2, identity("foo"), strict = FALSE), |
| 143 |
select_loc(letters2, int()) |
| 144 |
) |
| 145 |
expect_identical( |
| 146 |
select_loc(letters2, identity(100), strict = FALSE), |
| 147 |
select_loc(letters2, int()) |
| 148 |
) |
| 149 |
expect_identical( |
| 150 |
select_loc(letters2, -identity("foo"), strict = FALSE), |
| 151 |
select_loc(letters2, -int()) |
| 152 |
) |
| 153 |
expect_identical( |
| 154 |
select_loc(letters2, -identity(100), strict = FALSE), |
| 155 |
select_loc(letters2, -int()) |
| 156 |
) |
| 157 |
}) |
| 158 |
|
| 159 |
test_that("can use predicates in selections", { |
| 160 |
expect_identical(select_loc(iris, where(is.factor)), c(Species = 5L)) |
| 161 |
expect_identical(select_loc(iris, where(is.numeric)), set_names(1:4, names(iris)[1:4])) |
| 162 |
expect_identical(select_loc(iris, where(is.numeric) & where(is.factor)), set_names(int(), chr())) |
| 163 |
expect_identical(select_loc(iris, where(is.numeric) | where(is.factor)), set_names(1:5, names(iris))) |
| 164 |
}) |
| 165 |
|
| 166 |
test_that("can forbid use of predicates", { |
| 167 |
expect_snapshot( |
| 168 |
select_loc(iris, where(is.factor), allow_predicates = FALSE), |
| 169 |
error = TRUE |
| 170 |
) |
| 171 |
}) |
| 172 |
|
| 173 |
test_that("inline functions are allowed", { |
| 174 |
expect_identical( |
| 175 |
select_loc(iris, !!is.numeric), |
| 176 |
select_loc(iris, where(is.numeric)), |
| 177 |
) |
| 178 |
expect_identical( |
| 179 |
select_loc(iris, function(x) is.numeric(x)), |
| 180 |
select_loc(iris, where(is.numeric)), |
| 181 |
) |
| 182 |
}) |
| 183 |
|
| 184 |
test_that("predicates have access to the full data", { |
| 185 |
p <- function(x) is.numeric(x) && mean(x) > 5 |
| 186 |
expect_identical(select_loc(iris, where(p)), c(Sepal.Length = 1L)) |
| 187 |
}) |
| 188 |
|
| 189 |
test_that("unary `-` is alias for `!`", { |
| 190 |
expect_identical(select_loc(mtcars, -(cyl:carb)), c(mpg = 1L)) |
| 191 |
}) |
| 192 |
|
| 193 |
test_that("empty inputs return empty indices", { |
| 194 |
expect_identical(select_loc(mtcars, int()), named(int())) |
| 195 |
expect_identical(select_loc(mtcars, !!int()), named(int())) |
| 196 |
}) |
| 197 |
|
| 198 |
test_that("indices are returned in order of evaluation", { |
| 199 |
expect_identical(select_loc(mtcars, cyl | mpg), c(cyl = 2L, mpg = 1L)) |
| 200 |
expect_identical(select_loc(mtcars, c(cyl | mpg)), c(cyl = 2L, mpg = 1L)) |
| 201 |
}) |
| 202 |
|
| 203 |
test_that("0 is ignored", { |
| 204 |
expect_identical(select_loc(mtcars, 0), named(int())) |
| 205 |
expect_identical(select_loc(mtcars, identity(0)), named(int())) |
| 206 |
expect_identical(select_loc(mtcars, 0L | 0L), named(int())) |
| 207 |
expect_identical(select_loc(mtcars, c(0L, -1L)), named(int())) |
| 208 |
}) |
| 209 |
|
| 210 |
test_that("negative indices are disallowed", { |
| 211 |
expect_error(select_loc(mtcars, identity(c(-1, 1))), "negative") |
| 212 |
expect_error(select_loc(mtcars, !!c(-1, 1)), "negative") |
| 213 |
expect_error(select_loc(mtcars, cyl | !!c(-1, 1)), "negative") |
| 214 |
}) |
| 215 |
|
| 216 |
test_that("unique elements are returned", { |
| 217 |
x <- list(a = 1L, b = 2L) |
| 218 |
expect_identical(select_loc(x, !!c(1L, 1L)), c(a = 1L)) |
| 219 |
expect_identical(select_loc(x, !!c(1L, foo = 1L)), c(foo = 1L)) |
| 220 |
expect_identical(select_loc(x, !!c(foo = 1L, 1L)), c(foo = 1L)) |
| 221 |
expect_identical(select_loc(x, !!c(foo = 1L, 1L, bar = 1L)), c(foo = 1L, bar = 1L)) |
| 222 |
}) |
| 223 |
|
| 224 |
test_that("selections provide informative errors", { |
| 225 |
expect_snapshot(error = TRUE, { |
| 226 |
"Foreign errors during evaluation" |
| 227 |
select_loc(iris, eval_tidy(foobar)) |
| 228 |
}) |
| 229 |
}) |
| 230 |
|
| 231 |
test_that("can select with .data pronoun (#2715)", { |
| 232 |
withr::local_options(lifecycle_verbosity = "quiet") |
| 233 |
|
| 234 |
expect_identical(select_loc(c(foo = "foo"), .data$foo), c(foo = 1L)) |
| 235 |
expect_identical(select_loc(c(foo = "foo"), .data[["foo"]]), c(foo = 1L)) |
| 236 |
expect_identical(select_loc(letters2, .data$a:.data$b), c(a = 1L, b = 2L)) |
| 237 |
expect_identical(select_loc(letters2, .data[["a"]]:.data[["b"]]), c(a = 1L, b = 2L)) |
| 238 |
}) |
| 239 |
|
| 240 |
test_that("use of .data is deprecated", { |
| 241 |
x <- list(a = 1, b = 2, c = 3) |
| 242 |
var <- "a" |
| 243 |
expect_snapshot(x <- select_loc(x, .data$a)) |
| 244 |
expect_snapshot(x <- select_loc(x, .data[[var]])) |
| 245 |
}) |
| 246 |
|
| 247 |
test_that(".data in env-expression has the lexical definition", { |
| 248 |
quo <- local({ |
| 249 |
.data <- mtcars |
| 250 |
quo({ stopifnot(identical(.data, mtcars)); NULL}) |
| 251 |
}) |
| 252 |
expect_error(select_loc(mtcars, !!quo), regexp = NA) |
| 253 |
}) |
| 254 |
|
| 255 |
test_that("binary `/` is short for set difference", { |
| 256 |
expect_identical( |
| 257 |
select_loc(iris, starts_with("Sepal") / ends_with("Width")), |
| 258 |
select_loc(iris, c(starts_with("Sepal"), -ends_with("Width"))) |
| 259 |
) |
| 260 |
}) |
| 261 |
|
| 262 |
test_that("can select names with unrepresentable characters", { |
| 263 |
skip_if_not_installed("rlang", "0.4.2.9000") |
| 264 |
|
| 265 |
# R now emits a warning when converting to symbol. Since Windows |
| 266 |
# gained UTF-8 support, supporting unrepresentable characters is no |
| 267 |
# longer necessary. |
| 268 |
suppressWarnings( |
| 269 |
withr::with_locale(c(LC_CTYPE = "C"), { |
| 270 |
name <- "\u4e2d" |
| 271 |
tbl <- setNames(data.frame(a = 1), name) |
| 272 |
expect_identical( |
| 273 |
select_loc(tbl, !!sym(name)), |
| 274 |
set_names(1L, name) |
| 275 |
) |
| 276 |
}) |
| 277 |
) |
| 278 |
}) |
| 279 |
|
| 280 |
test_that("`-1:-2` is syntax for `-(1:2)` for compatibility", { |
| 281 |
expect_identical( |
| 282 |
select_loc(iris, -1:-2), |
| 283 |
select_loc(iris, -(1:2)) |
| 284 |
) |
| 285 |
expect_identical( |
| 286 |
select_loc(iris, -Sepal.Length:-Sepal.Width), |
| 287 |
select_loc(iris, -(Sepal.Length:Sepal.Width)) |
| 288 |
) |
| 289 |
}) |
| 290 |
|
| 291 |
test_that("eval_sym() doesn't look for functions in the context", { |
| 292 |
foo <- is.numeric |
| 293 |
expect_error(select_loc(iris, foo), class = "vctrs_error_subscript_oob") |
| 294 |
expect_error(select_loc(iris, data), class = "vctrs_error_subscript_oob") |
| 295 |
}) |
| 296 |
|
| 297 |
test_that("eval_sym() still supports predicate functions starting with `is`", { |
| 298 |
local_options(lifecycle_verbosity = "quiet") |
| 299 |
expect_identical(select_loc(iris, is_integer), select_loc(iris, where(is_integer))) |
| 300 |
expect_identical(select_loc(iris, is.numeric), select_loc(iris, where(is.numeric))) |
| 301 |
expect_identical(select_loc(iris, isTRUE), select_loc(iris, where(isTRUE))) |
| 302 |
}) |
| 303 |
|
| 304 |
test_that("eval_walk() has informative messages", { |
| 305 |
expect_snapshot({ |
| 306 |
"Using a predicate without where() warns" |
| 307 |
invisible(select_loc(iris, is_integer)) |
| 308 |
invisible(select_loc(iris, is.numeric)) |
| 309 |
invisible(select_loc(iris, isTRUE)) |
| 310 |
|
| 311 |
"Warning is not repeated" |
| 312 |
invisible(select_loc(iris, is_integer)) |
| 313 |
|
| 314 |
"formula shorthand must be wrapped" |
| 315 |
(expect_error(select_loc(mtcars, ~ is.numeric(.x)))) |
| 316 |
(expect_error(select_loc(mtcars, ~ is.numeric(.x) || is.factor(.x) || is.character(.x)))) |
| 317 |
(expect_error(select_loc(mtcars, ~ is.numeric(.x) || is.factor(.x) || is.character(.x) || |
| 318 |
is.numeric(.x) || is.factor(.x) || is.character(.x)))) |
| 319 |
|
| 320 |
(expect_error(select_loc(mtcars, .data$"foo"))) |
| 321 |
}) |
| 322 |
}) |
| 323 |
|
| 324 |
test_that("can forbid empty selection", { |
| 325 |
expect_snapshot(error = TRUE, { |
| 326 |
ensure_named(integer(), allow_empty = FALSE) |
| 327 |
ensure_named(integer(), allow_empty = FALSE, allow_rename = FALSE) |
| 328 |
}) |
| 329 |
}) |
| 330 |
|
| 331 |
test_that("can make empty selection with allow_rename = FALSE", { |
| 332 |
expect_equal( |
| 333 |
select_loc(mtcars, character(), allow_rename = FALSE), |
| 334 |
set_names(integer(0)) |
| 335 |
) |
| 336 |
expect_equal( |
| 337 |
select_loc(mtcars, c(cyl, am), allow_rename = FALSE), |
| 338 |
c(cyl = 2L, am = 9L) |
| 339 |
) |
| 340 |
}) |