1test_that(".before and .after relocate individual cols", { 2 df <- tibble(x = 1, y = 2) 3 expect_named(relocate(df, x, .after = y), c("y", "x")) 4 expect_named(relocate(df, y, .before = x), c("y", "x")) 5}) 6 7test_that("can move blocks of variables", { 8 df <- tibble(x = 1, a = "a", y = 2, b = "a") 9 expect_named(relocate(df, where(is.character)), c("a", "b", "x", "y")) 10 expect_named(relocate(df, where(is.character), .after = where(is.numeric)), c("x", "y", "a", "b")) 11}) 12 13test_that("don't lose non-contiguous variables", { 14 df <- tibble(a = 1, b = 1, c = 1, d = 1, e = 1) 15 expect_named(relocate(df, b, .after = c(a, c, e)), c("a", "c", "d", "e", "b")) 16 expect_named(relocate(df, e, .before = c(b, d)), c("a", "e", "b", "c", "d")) 17}) 18 19test_that("no .before/.after moves to front", { 20 df <- tibble(x = 1, y = 2) 21 expect_named(relocate(df, y), c("y", "x")) 22}) 23 24test_that("can only supply one of .before and .after", { 25 df <- tibble(x = 1) 26 expect_error(relocate(df, .before = 1, .after = 1), "only one") 27}) 28 29test_that("before and after are defused with context", { 30 local_fn <- identity 31 expect_identical( 32 names(relocate(mtcars, 3, .before = local_fn(5))), 33 names(relocate(mtcars, 3, .before = 5)) 34 ) 35 expect_identical( 36 names(relocate(mtcars, 3, .after = local_fn(5))), 37 names(relocate(mtcars, 3, .after = 5)) 38 ) 39}) 40 41test_that("relocate() respects order specified by ... (#5328)", { 42 df <- tibble(a = 1, x = 1, b = 1, z = 1, y = 1) 43 44 expect_equal( 45 names(relocate(df, x, y, z, .before = x)), 46 c("a", "x", "y", "z", "b") 47 ) 48 expect_equal( 49 names(relocate(df, x, y, z, .after = last_col())), 50 c("a", "b", "x", "y", "z") 51 ) 52 expect_equal( 53 names(relocate(df, x, a, z)), 54 c("x", "a", "z", "b", "y") 55 ) 56}) 57 58test_that("relocate() can rename (#5569)", { 59 df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a") 60 expect_equal( 61 relocate(df, ffff = f), 62 tibble(ffff = "a", a = 1, b = 1, c = 1, d = "a", e = "a") 63 ) 64 expect_equal( 65 relocate(df, ffff = f, .before = c), 66 tibble(a = 1, b = 1, ffff = "a", c = 1, d = "a", e = "a") 67 ) 68 expect_equal( 69 relocate(df, ffff = f, .after = c), 70 tibble(a = 1, b = 1, c = 1, ffff = "a", d = "a", e = "a") 71 ) 72}) 73