1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
test_that("rename_loc() requires named vectors", {
expect_error(
rename_loc(letters, c(foo = a)),
"unnamed vector"
)
})
test_that("rename_loc() partially renames", {
expect_identical(
rename_loc(mtcars, c(foo = cyl, bar = disp)),
int(foo = 2, bar = 3)
)
})
test_that("rename_loc() allows renaming to self", {
expect_identical(
rename_loc(mtcars, c(mpg = mpg, cyl = cyl)),
int(mpg = 1, cyl = 2)
)
})
test_that("rename() always preserves order", {
expect_identical(
rename(mtcars, c(disp = disp, cyl = cyl, mpg = mpg)),
mtcars
)
})
test_that("rename_loc() partially renames", {
expect_identical(
rename_loc(mtcars, c(foo = cyl, bar = disp)),
int(foo = 2, bar = 3)
)
})
test_that("rename_loc() requires unique names", {
expect_error(
rename_loc(mtcars, c(foo = cyl, foo = disp)),
class = "vctrs_error_names_must_be_unique"
)
expect_error(
rename_loc(mtcars, c(cyl = mpg, foo = disp)),
class = "vctrs_error_names_must_be_unique"
)
})
test_that("rename_loc() disambiguates if necessary", {
expect_identical(
rename_loc(mtcars, c(foo = starts_with("d"))),
int(foo1 = 3, foo2 = 5)
)
expect_identical(
rename_loc(unclass(mtcars), c(foo = starts_with("d"))),
int(foo = 3, foo = 5)
)
})
test_that("rename_loc() allows renaming to existing variable that is also renamed", {
expect_identical(
rename_loc(mtcars, c(cyl = mpg, foo = cyl)),
int(cyl = 1, foo = 2)
)
})
test_that("rename_loc() allows fixing duplicates by locations", {
dups <- vctrs::new_data_frame(list(x = 1, x = 2))
expect_identical(
rename_loc(dups, c(foo = 2L)),
int(foo = 2)
)
})
test_that("rename_loc() works with predicate functions", {
x <- data.frame(a = 1, b = 2, c = "x", stringsAsFactors = FALSE)
expect_equal(rename_loc(x, c(var = where(is.numeric))), c(var1 = 1, var2 = 2))
expect_snapshot(rename_loc(x, where(is.numeric)), error = TRUE)
})
test_that("rename_loc() requires named inputs", {
expect_error(rename_loc(iris, Species), "named")
expect_error(rename_loc(iris, c(contains("Width"))), "named")
})
test_that("rename_loc() uses names inside c()", {
expect_identical(rename_loc(iris, c(foo = Species)), c(foo = 5L))
})
test_that("rename_loc() throws helpful errors", {
expect_snapshot(error = TRUE, {
"Unnamed vector"
rename_loc(letters, c(foo = a))
"Duplicate names (FIXME)"
rename_loc(mtcars, c(foo = cyl, foo = disp))
"Unnamed inputs"
rename_loc(iris, Species)
})
})
|