| 1 |
test_that("c() interpolates union and setdiff operations (#130)", { |
| 2 |
expect_identical(select_loc(mtcars, c(mpg:disp, -(mpg:cyl))), c(disp = 3L)) |
| 3 |
|
| 4 |
expect_identical(select_loc(mtcars, c(mpg, -mpg)), set_names(int(), chr())) |
| 5 |
expect_identical(select_loc(mtcars, c(mpg, -mpg, mpg)), c(mpg = 1L)) |
| 6 |
expect_identical(select_loc(mtcars, c(mpg, -mpg, mpg, -mpg)), set_names(int(), chr())) |
| 7 |
|
| 8 |
expect_identical(select_loc(mtcars, c(mpg, cyl, -mpg)), c(cyl = 2L)) |
| 9 |
expect_identical(select_loc(mtcars, c(mpg, cyl, -mpg, -cyl)), set_names(int(), chr())) |
| 10 |
expect_identical(select_loc(mtcars, c(mpg, cyl, -mpg, mpg, -cyl)), c(mpg = 1L)) |
| 11 |
}) |
| 12 |
|
| 13 |
test_that("c() expands dots", { |
| 14 |
fn <- function(...) select_loc(mtcars, c(...)) |
| 15 |
expect_identical(fn(), set_names(int(), chr())) |
| 16 |
expect_identical(fn(mpg), c(mpg = 1L)) |
| 17 |
expect_identical(fn(mpg, cyl), c(mpg = 1L, cyl = 2L)) |
| 18 |
expect_identical(fn(mpg, cyl, disp), c(mpg = 1L, cyl = 2L, disp = 3L)) |
| 19 |
}) |
| 20 |
|
| 21 |
test_that("c() combines names tidily", { |
| 22 |
expect_identical(select_loc(mtcars, c(foo = mpg)), set_names(1L, "foo")) |
| 23 |
expect_identical(select_loc(mtcars, c(foo = c(bar = mpg))), set_names(1L, "foo...bar")) |
| 24 |
|
| 25 |
expect_identical(select_loc(mtcars, c(foo = mpg:cyl)), set_names(1:2, c("foo1", "foo2"))) |
| 26 |
expect_identical(select_loc(mtcars, c(foo = c(bar = mpg:cyl))), set_names(1:2, c("foo...bar1", "foo...bar2"))) |
| 27 |
}) |
| 28 |
|
| 29 |
test_that("c() renames duplicates", { |
| 30 |
x <- list(a = 1L, b = 2L, a = 3L) |
| 31 |
expect_identical(select(x, foo = a, bar = b), list(foo = 1L, foo = 3L, bar = 2L)) |
| 32 |
}) |
| 33 |
|
| 34 |
test_that("allow named negative selections for consistency even if it has no effect", { |
| 35 |
expect_identical(select_loc(iris, c(foo = -!Species)), c(Species = 5L)) |
| 36 |
}) |
| 37 |
|
| 38 |
test_that("c() handles names consistently", { |
| 39 |
x <- list(a = 1L, b = 2L) |
| 40 |
expect_identical(select(x, a, foo = a), list(foo = 1L)) |
| 41 |
expect_identical(select(x, a, foo = a, bar = a), list(foo = 1L, bar = 1L)) |
| 42 |
expect_identical(select(x, foo = a, -a), named(list())) |
| 43 |
expect_identical(select(x, foo = a, -c(bar = a)), list(foo = 1L)) |
| 44 |
}) |
| 45 |
|
| 46 |
test_that("with uniquely-named inputs names are propagated with disambiguation", { |
| 47 |
expect_identical(select_loc(mtcars, c(foo = c(mpg, cyl))), c(foo1 = 1L, foo2 = 2L)) |
| 48 |
expect_identical(select_loc(mtcars, c(bar = c(foo = c(mpg, cyl)))), c(bar...foo1 = 1L, bar...foo2 = 2L)) |
| 49 |
}) |
| 50 |
|
| 51 |
test_that("with minimally-named inputs names are propagated without disambiguation", { |
| 52 |
expect_identical(select_loc(unclass(mtcars), c(foo = c(mpg, cyl))), c(foo = 1L, foo = 2L)) |
| 53 |
expect_identical(select_loc(unclass(mtcars), c(bar = c(foo = c(mpg, cyl)))), c(bar...foo = 1L, bar...foo = 2L)) |
| 54 |
}) |
| 55 |
|
| 56 |
test_that("uniquely-named inputs can't rename duplicates", { |
| 57 |
df <- vctrs::new_data_frame(list(a = 1, b = 2, a = 3)) |
| 58 |
|
| 59 |
expect_error(select_loc(df, c(foo = a)), "rename duplicate") |
| 60 |
expect_identical(select_loc(unclass(df), c(foo = a)), c(foo = 1L, foo = 3L)) |
| 61 |
|
| 62 |
expect_snapshot(error = TRUE, { |
| 63 |
names(df) |
| 64 |
select_loc(df, c(foo = a)) |
| 65 |
}) |
| 66 |
}) |
| 67 |
|
| 68 |
test_that("can select with c() (#2685)", { |
| 69 |
expect_identical(select_loc(letters2, c(a, z)), c(a = 1L, z = 26L)) |
| 70 |
}) |
| 71 |
|
| 72 |
test_that("unnegate() flattens quosures properly", { |
| 73 |
expr <- quo(-!!local(quo(am))) |
| 74 |
expect_identical( |
| 75 |
select_loc(mtcars, c(!!expr)), |
| 76 |
select_loc(mtcars, -am) |
| 77 |
) |
| 78 |
}) |
| 79 |
|
| 80 |
test_that("`-x:-y` is syntax for `-(x:y)` for compatibility", { |
| 81 |
expect_identical( |
| 82 |
select_loc(iris, c(-1, -2:-3)), |
| 83 |
select_loc(iris, c(-1, -(2:3))) |
| 84 |
) |
| 85 |
expect_identical( |
| 86 |
select_loc(iris, c(-Sepal.Length, -Sepal.Width:-Petal.Length)), |
| 87 |
select_loc(iris, c(-Sepal.Length, -(Sepal.Width:Petal.Length))) |
| 88 |
) |
| 89 |
expect_identical( |
| 90 |
select_loc(iris, c(-1:-2)), |
| 91 |
select_loc(iris, -(1:2)) |
| 92 |
) |
| 93 |
}) |
| 94 |
|
| 95 |
test_that("`c()` compacts missing arguments (#147)", { |
| 96 |
expect_identical(eval_select(quote(c(NULL, NULL)), mtcars), named(int())) |
| 97 |
expect_identical(eval_select(quote(c(, )), mtcars), named(int())) |
| 98 |
expect_identical(eval_select(quote(c(, 2, , )), mtcars), c(cyl = 2L)) |
| 99 |
}) |