Debian Codegraph @

tests/testthat/test-glimpse.R

raw
1 test_that("format_glimpse() output test", {
2 expect_snapshot({
3 "# Atomic numbers"
4 format_glimpse(1)
5 format_glimpse(1:3)
6 format_glimpse(NA)
7 format_glimpse(TRUE)
8 format_glimpse(logical())
9
10 "# Strings"
11 format_glimpse("1")
12 format_glimpse(letters)
13 format_glimpse(NA_character_)
14 format_glimpse(character())
15
16 "# Factors"
17 format_glimpse(factor(c("1", "a")))
18 format_glimpse(factor(c("foo", '"bar"')))
19 format_glimpse(factor())
20
21 "Add quotes around factor levels with comma"
22 "so they don't appear as if they were two observations (#384)"
23 format_glimpse(factor(c("foo, bar", "foo", '"bar"')))
24
25 "# Lists"
26 format_glimpse(list(1:3))
27 format_glimpse(as.list(1:3))
28 format_glimpse(list(1:3, 4))
29 format_glimpse(list(1:3, 4:5))
30 format_glimpse(list())
31 format_glimpse(list(list()))
32 format_glimpse(list(character()))
33 format_glimpse(list(1:3, list(4)))
34 format_glimpse(list(1:3, list(4:5)))
35 })
36 })
37
38 test_that("glimpse(width = Inf) raises legible error", {
39 expect_error(
40 glimpse(mtcars, width = Inf)
41 )
42 })
43
44 test_that("glimpse calls tbl_sum() (#550)", {
45 skip_if(!l10n_info()$`UTF-8`) # capture_output_lines() forces native encoding
46
47 local_override_tbl_sum()
48
49 trees2 <- as_override_tbl_sum(trees)
50
51 expect_output(
52 glimpse(trees2),
53 "Overridden: tbl_sum",
54 fixed = TRUE
55 )
56 })
57
58 test_that("output test for glimpse()", {
59 skip_if_not_installed("tibble")
60 local_unknown_rows()
61
62 expect_snapshot({
63 glimpse(as_tbl(mtcars), width = 70L)
64
65 glimpse(as_tbl(trees), width = 70L)
66
67 "No columns"
68 glimpse(as_tbl(trees[integer()]), width = 70L)
69
70 "Non-syntactic names"
71 df <- tibble::tibble(!!!set_names(c(5, 3), c("mean(x)", "var(x)")))
72 glimpse(df, width = 28)
73
74 glimpse(as_tbl(df_all), width = 70L)
75
76 "options(tibble.width = 50)"
77 withr::with_options(
78 list(tibble.width = 50),
79 glimpse(as_tbl(df_all))
80 )
81
82 "options(tibble.width = 35)"
83 withr::with_options(
84 list(tibble.width = 35),
85 glimpse(as_tbl(df_all))
86 )
87
88 "non-tibble"
89 glimpse(5)
90
91 trees2 <- as_unknown_rows(trees)
92 glimpse(trees2, width = 70L)
93
94 cyl <- unique(mtcars$cyl)
95 data <- unname(split(mtcars, mtcars$cyl))
96 nested_mtcars_df <- tibble::tibble(cyl, data)
97 glimpse(nested_mtcars_df, width = 70L)
98
99 data <- map(data, as_tbl)
100 nested_mtcars_tbl <- tibble::tibble(cyl, data)
101 glimpse(nested_mtcars_tbl, width = 70L)
102 })
103 })
104
105 test_that("format_glimpse_1() handles commas correctly", {
106 local_colors()
107
108 expect_snapshot({
109 "# Vector with commas"
110 format_glimpse_1(1:3)
111 format_glimpse_1(c("a", "b", "c"))
112 format_glimpse_1(c(1, NA, 3))
113
114 "# Empty and single-element vectors"
115 format_glimpse_1(integer())
116 format_glimpse_1(1)
117
118 "# Mixed content with commas"
119 format_glimpse_1(list(1:3, "a, b", NA))
120
121 # format_glimpse_1 with .inner = TRUE
122 "# Simple list elements"
123 format_glimpse_1(1:3, .inner = TRUE)
124 format_glimpse_1(letters[1:3], .inner = TRUE)
125
126 "# Nested lists"
127 format_glimpse_1(list(1:3), .inner = TRUE)
128 format_glimpse_1(list(list(1:3), list(4:6)), .inner = TRUE)
129
130 "# Mixed content"
131 format_glimpse_1(list(1:3, "a,b,c"), .inner = TRUE)
132 format_glimpse_1(list(list(1, 2, 3), letters[1:3]), .inner = TRUE)
133
134 "# Empty and NULL"
135 format_glimpse_1(list(), .inner = TRUE)
136 format_glimpse_1(NULL, .inner = TRUE)
137 })
138 })
139
140 test_that("color test for missing values", {
141 local_colors()
142
143 expect_snapshot({
144 "individual data types"
145 format_glimpse(df_all$a)
146 format_glimpse(df_all$b)
147 format_glimpse(df_all$c)
148 format_glimpse(df_all$d)
149 format_glimpse(df_all$e)
150 format_glimpse(df_all$f)
151 format_glimpse(df_all$g)
152 format_glimpse(df_all$h)
153 format_glimpse(df_all$i)
154
155 "tables"
156 glimpse(df_all)
157 glimpse(as.data.frame(df_all))
158 })
159 })