| 1 |
test_that("`before` and `after` relocates the selection", { |
| 2 |
x <- list(x = 1, a = "a", y = 2, b = "a") |
| 3 |
|
| 4 |
expect_identical( |
| 5 |
relocate_loc(x, x, after = y), |
| 6 |
c(a = 2L, y = 3L, x = 1L, b = 4L) |
| 7 |
) |
| 8 |
expect_identical( |
| 9 |
relocate_loc(x, y, before = x), |
| 10 |
c(y = 3L, x = 1L, a = 2L, b = 4L) |
| 11 |
) |
| 12 |
expect_identical( |
| 13 |
relocate_loc(x, where(is.character), before = x), |
| 14 |
c(a = 2L, b = 4L, x = 1L, y = 3L) |
| 15 |
) |
| 16 |
}) |
| 17 |
|
| 18 |
test_that("works with `before` and `after` `everything()`", { |
| 19 |
x <- c(w = 1, x = 2, y = 3, z = 4) |
| 20 |
|
| 21 |
expect_identical( |
| 22 |
relocate_loc(x, c(y, z), before = everything()), |
| 23 |
c(y = 3L, z = 4L, w = 1L, x = 2L) |
| 24 |
) |
| 25 |
expect_identical( |
| 26 |
relocate_loc(x, c(y, z), after = everything()), |
| 27 |
c(w = 1L, x = 2L, y = 3L, z = 4L) |
| 28 |
) |
| 29 |
}) |
| 30 |
|
| 31 |
test_that("moves columns to the front when neither `before` nor `after` are specified", { |
| 32 |
x <- c(x = 1, y = 2, z = 3) |
| 33 |
|
| 34 |
expect_identical( |
| 35 |
relocate_loc(x, c(z, y)), |
| 36 |
c(z = 3L, y = 2L, x = 1L) |
| 37 |
) |
| 38 |
}) |
| 39 |
|
| 40 |
test_that("empty `before` selection moves columns to front", { |
| 41 |
x <- c(x = 1, y = 2, z = 3) |
| 42 |
|
| 43 |
expect_identical( |
| 44 |
relocate_loc(x, y, before = where(is.character)), |
| 45 |
c(y = 2L, x = 1L, z = 3L) |
| 46 |
) |
| 47 |
}) |
| 48 |
|
| 49 |
test_that("empty `after` selection moves columns to end", { |
| 50 |
x <- c(x = 1, y = 2, z = 3) |
| 51 |
|
| 52 |
expect_identical( |
| 53 |
relocate_loc(x, y, after = where(is.character)), |
| 54 |
c(x = 1L, z = 3L, y = 2L) |
| 55 |
) |
| 56 |
}) |
| 57 |
|
| 58 |
test_that("minimum of the `before` selection is used", { |
| 59 |
x <- c(a = 1, b = 1, c = 1, d = 1, e = 1) |
| 60 |
expect_named(relocate_loc(x, e, before = c(b, d)), c("a", "e", "b", "c", "d")) |
| 61 |
}) |
| 62 |
|
| 63 |
test_that("maximum of the `after` selection is used", { |
| 64 |
x <- c(a = 1, b = 1, c = 1, d = 1, e = 1) |
| 65 |
expect_named(relocate_loc(x, b, after = c(a, c, e)), c("a", "c", "d", "e", "b")) |
| 66 |
}) |
| 67 |
|
| 68 |
test_that("works with zero column data frames (tidyverse/dplyr#6167)", { |
| 69 |
expect_identical(relocate_loc(data.frame(), any_of("b")), named(int())) |
| 70 |
expect_identical(relocate_loc(data.frame(), any_of("b"), before = where(is.character)), named(int())) |
| 71 |
expect_identical(relocate_loc(data.frame(), any_of("b"), after = where(is.character)), named(int())) |
| 72 |
}) |
| 73 |
|
| 74 |
test_that("retains the last duplicate when renaming while moving (tidyverse/dplyr#6209)", { |
| 75 |
# To enforce the invariant that relocating can't change the number of columns |
| 76 |
x <- c(x = 1) |
| 77 |
expect_identical( |
| 78 |
relocate_loc(x, c(a = x, b = x)), |
| 79 |
c(b = 1L) |
| 80 |
) |
| 81 |
|
| 82 |
x <- c(x = 1, y = 2) |
| 83 |
expect_identical( |
| 84 |
relocate_loc(x, c(a = x, b = y, c = x)), |
| 85 |
c(b = 2L, c = 1L) |
| 86 |
) |
| 87 |
}) |
| 88 |
|
| 89 |
test_that("respects order specified by `...` (tidyverse/dplyr#5328)", { |
| 90 |
x <- c(a = 1, x = 1, b = 1, z = 1, y = 1) |
| 91 |
|
| 92 |
expect_named( |
| 93 |
relocate_loc(x, c(x, y, z), before = x), |
| 94 |
c("a", "x", "y", "z", "b") |
| 95 |
) |
| 96 |
expect_named( |
| 97 |
relocate_loc(x, c(x, y, z), after = last_col()), |
| 98 |
c("a", "b", "x", "y", "z") |
| 99 |
) |
| 100 |
expect_named( |
| 101 |
relocate_loc(x, c(x, a, z)), |
| 102 |
c("x", "a", "z", "b", "y") |
| 103 |
) |
| 104 |
}) |
| 105 |
|
| 106 |
test_that("allows for renaming (tidyverse/dplyr#5569)", { |
| 107 |
x <- c(a = 1, b = 1, c = 1) |
| 108 |
|
| 109 |
expect_named( |
| 110 |
relocate_loc(x, c(new = b)), |
| 111 |
c("new", "a", "c") |
| 112 |
) |
| 113 |
expect_named( |
| 114 |
relocate_loc(x, c(new = b), after = c), |
| 115 |
c("a", "c", "new") |
| 116 |
) |
| 117 |
}) |
| 118 |
|
| 119 |
test_that("can't supply both `before` and `after`", { |
| 120 |
expect_snapshot(error = TRUE, { |
| 121 |
relocate_loc(c(x = 1), before = 1, after = 1) |
| 122 |
}) |
| 123 |
expect_snapshot(error = TRUE, { |
| 124 |
relocate_loc(c(x = 1), before = 1, after = 1, before_arg = ".before", after_arg = ".after") |
| 125 |
}) |
| 126 |
}) |
| 127 |
|
| 128 |
test_that("can't relocate with out-of-bounds variables by default", { |
| 129 |
x <- c(a = 1, b = 2) |
| 130 |
|
| 131 |
expect_snapshot({ |
| 132 |
(expect_error(relocate_loc(x, c))) |
| 133 |
(expect_error(relocate_loc(x, c(1, 3)))) |
| 134 |
(expect_error(relocate_loc(x, a, before = c))) |
| 135 |
(expect_error(relocate_loc(x, a, after = c))) |
| 136 |
}) |
| 137 |
}) |
| 138 |
|
| 139 |
test_that("can relocate with out-of-bounds variables in `expr` if `strict = FALSE`", { |
| 140 |
x <- c(a = 1, b = 2) |
| 141 |
|
| 142 |
expect_identical(relocate_loc(x, c, strict = FALSE), c(a = 1L, b = 2L)) |
| 143 |
expect_identical(relocate_loc(x, c(d = b, e = c), strict = FALSE), c(d = 2L, a = 1L)) |
| 144 |
|
| 145 |
# But still not with OOB variables in `before` or `after` |
| 146 |
expect_snapshot({ |
| 147 |
(expect_error(relocate_loc(x, a, before = c, strict = FALSE))) |
| 148 |
(expect_error(relocate_loc(x, a, after = c, strict = FALSE))) |
| 149 |
}) |
| 150 |
}) |
| 151 |
|
| 152 |
test_that("accepts name spec", { |
| 153 |
x <- c(a = 1, b = 2, c = 3) |
| 154 |
|
| 155 |
expect_identical( |
| 156 |
relocate_loc(x, c(foo = c(c, a)), name_spec = "{outer}_{inner}"), |
| 157 |
c(foo_1 = 3L, foo_2 = 1L, b = 2L) |
| 158 |
) |
| 159 |
expect_identical( |
| 160 |
relocate_loc(x, c(foo = c(bar = c, baz = a)), name_spec = "{outer}_{inner}"), |
| 161 |
c(foo_bar = 3L, foo_baz = 1L, b = 2L) |
| 162 |
) |
| 163 |
}) |
| 164 |
|
| 165 |
test_that("can forbid rename syntax", { |
| 166 |
x <- c(a = 1, b = 2, c = 3) |
| 167 |
|
| 168 |
expect_snapshot({ |
| 169 |
(expect_error(relocate_loc(x, c(foo = b), allow_rename = FALSE))) |
| 170 |
(expect_error(relocate_loc(x, c(b, foo = b), allow_rename = FALSE))) |
| 171 |
}) |
| 172 |
|
| 173 |
expect_named(relocate_loc(x, c(c, b), allow_rename = FALSE), c("c", "b", "a")) |
| 174 |
}) |
| 175 |
|
| 176 |
test_that("can forbid empty selections", { |
| 177 |
x <- c(a = 1, b = 2, c = 3) |
| 178 |
|
| 179 |
expect_snapshot({ |
| 180 |
(expect_error(relocate_loc(x, allow_empty = FALSE))) |
| 181 |
(expect_error(relocate_loc(mtcars, integer(), allow_empty = FALSE))) |
| 182 |
(expect_error(relocate_loc(mtcars, starts_with("z"), allow_empty = FALSE))) |
| 183 |
}) |
| 184 |
}) |
| 185 |
|
| 186 |
test_that("`before` and `after` forbid renaming", { |
| 187 |
x <- c(a = 1, b = 2, c = 3) |
| 188 |
|
| 189 |
expect_snapshot({ |
| 190 |
(expect_error(relocate_loc(x, b, before = c(new = c)))) |
| 191 |
(expect_error(relocate_loc(x, b, before = c(new = c), before_arg = ".before"))) |
| 192 |
|
| 193 |
(expect_error(relocate_loc(x, b, after = c(new = c)))) |
| 194 |
(expect_error(relocate_loc(x, b, after = c(new = c), after_arg = ".after"))) |
| 195 |
}) |
| 196 |
}) |