Debian Codegraph @

tests/testthat/test_tidyverse.R

raw
1 test_that("pillar methods are available for units objects", {
2 skip_if_not_installed("pillar")
3
4 x = set_units(1:3, km)
5 m = c(x, set_units(4:6, g), allow_mixed = TRUE)
6
7 expect_equal(unclass(pillar::type_sum(x)), "[km]")
8 expect_s3_class(pillar::type_sum(x), "type_sum_units")
9 expect_equal(pillar::type_sum(m), "mixed_units")
10
11 expect_snapshot({
12 pillar::pillar(x)
13 pillar::pillar(m)
14 })
15 })
16
17 test_that("units have coercion methods", {
18 skip_if_not_installed("vctrs", "0.3.1")
19
20 x = set_units(1:3, "cm")
21 y = set_units(4.0, "m")
22 z = set_units(10, "celsius")
23
24 expect_error(vctrs::vec_ptype_common(y, x, z), class = "vctrs_error_incompatible_type")
25 expect_error(vctrs::vec_cast_common(y, x, z), class = "vctrs_error_incompatible_type")
26
27 expect_identical(vctrs::vec_ptype_common(x, y, x), set_units(double(), "cm"))
28 expect_identical(vctrs::vec_ptype_common(x, x), set_units(integer(), "cm"))
29 expect_identical(vctrs::vec_ptype_common(y, x, x), set_units(double(), "m"))
30
31 expect_identical(
32 vctrs::vec_cast_common(x, y),
33 list(set_units(c(1, 2, 3), "cm"), set_units(400, "cm"))
34 )
35 expect_identical(
36 vctrs::vec_cast_common(y, x),
37 list(set_units(4, "m"), set_units(c(0.01, 0.02, 0.03), "m"))
38 )
39 })
40
41 test_that("can combine units vectors", {
42 skip_if_not_installed("vctrs", "0.3.1")
43 skip_if_not_installed("dplyr", "1.0.0")
44
45 x <- set_units(1:3, "cm")
46 y <- set_units(4, "m")
47
48 exp = set_units(c(1, 2, 3, 400), "cm")
49 expect_identical(vctrs::vec_c(x, y), exp)
50
51 # Recursive case
52 df1 = dplyr::tibble(x = dplyr::tibble(x = x))
53 df2 = dplyr::tibble(x = dplyr::tibble(x = y))
54 df_exp = dplyr::tibble(x = dplyr::tibble(x = exp))
55 expect_identical(vctrs::vec_c(df1, df2), df_exp)
56 })
57
58 test_that("can slice units vectors", {
59 skip_if_not_installed("vctrs", "0.3.1")
60 skip_if_not_installed("dplyr", "1.0.0")
61
62 x = set_units(1:3, "cm")
63 exp = list(set_units(1L, "cm"), set_units(2L, "cm"), set_units(3L, "cm"))
64 expect_identical(vctrs::vec_chop(x), exp)
65
66 # Recursive case
67 df = dplyr::tibble(dplyr::tibble(x = x))
68 exp = list(
69 dplyr::tibble(x = set_units(1L, "cm")),
70 dplyr::tibble(x = set_units(2L, "cm")),
71 dplyr::tibble(x = set_units(3L, "cm"))
72 )
73 expect_identical(vctrs::vec_chop(df), exp)
74 })
75
76 test_that("split-apply-combine with dplyr and base agree", {
77 skip_if_not_installed("vctrs", "0.3.1")
78 skip_if_not_installed("dplyr", "1.0.0")
79
80 `%>%` <- dplyr::`%>%`
81 iris2 <- iris
82 for (i in 1:4)
83 units(iris2[,i]) <- "cm"
84
85 out <- iris2 %>%
86 dplyr::group_by(Species) %>%
87 dplyr::summarise(dplyr::across(where(is.numeric), mean))
88
89 # Transform to list of lists
90 out <- vctrs::vec_chop(out[2:5]) %>%
91 stats::setNames(out$Species) %>%
92 lapply(as.list)
93
94 exp <- lapply(split(iris2[1:4], iris2$Species), lapply, mean)
95 expect_equal(out, exp)
96 })
97
98 test_that("storage mode is uniformly set to double (see #324)", {
99 skip_if_not_installed("vctrs", "0.3.1")
100
101 x <- set_units(1:10, cm)
102 br <- set_units(2:4, `in`)
103 expect_silent(vctrs::vec_cast_common(x, br))
104 })