Debian Codegraph @

tests/testthat/test-eval-rename.R

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