Debian Codegraph @

tests/testthat/test-ctl_colonnade.R

raw
1 test_that("output test", {
2 expect_snapshot({
3 ctl_colonnade(rep(list(paste(letters, collapse = " ")), 4), width = Inf)
4 })
5 })
6
7 test_that("tests from tibble", {
8 skip_if_not_installed("rlang", "0.4.11.9000")
9 local_options(width = 80)
10
11 expect_snapshot({
12 ctl_colonnade(mtcars[1:8, ], has_row_id = "*", width = 30)
13 ctl_colonnade(trees[1:5, ], width = 20)
14 ctl_colonnade(trees[1:5, ], width = 10)
15 ctl_colonnade(trees[1:3, ], width = 10)
16 ctl_colonnade(df_all, width = 30)
17 ctl_colonnade(df_all, width = 300)
18 options(width = 70)
19 ctl_colonnade(df_all, width = 300)
20 options(width = 60)
21 ctl_colonnade(df_all, width = 300)
22 options(width = 50)
23 ctl_colonnade(df_all, width = 300)
24 options(width = 40)
25 ctl_colonnade(df_all, width = 300)
26 options(width = 30)
27 ctl_colonnade(df_all, width = 300)
28 options(width = 20)
29 ctl_colonnade(df_all, width = 300)
30 list_with_ctl <- list(c("\n", '"'), factor(c("\n", "\n")))
31 names(list_with_ctl) <- c("\n", "\r")
32 ctl_colonnade(list_with_ctl, width = 30)
33 ctl_colonnade(list(a = c("", " ", "a ", " a")), width = 30)
34 ctl_colonnade(list("mean(x)" = 5, "var(x)" = 3), width = 30)
35 })
36 })
37
38 test_that("empty", {
39 expect_equal(
40 format(ctl_colonnade(list(a = character(), b = logical()), width = 30)$body),
41 character()
42 )
43 expect_equal(
44 format(ctl_colonnade(trees[1:5, character()], width = 30)$body),
45 character()
46 )
47 })
48
49 test_that("NA names", {
50 expect_snapshot({
51 x <- list(`NA` = 1:3, set_to_NA = 4:6)
52 names(x)[[2]] <- NA_character_
53 ctl_colonnade(x, width = 30)
54 })
55 })
56
57 test_that("sep argument", {
58 x <- list(sep = 1:3)
59 expect_snapshot({
60 ctl_colonnade(x, width = 30)
61 "dummy"
62 })
63 })
64
65 test_that("color", {
66 skip_if_not_installed("testthat", "3.1.1")
67
68 local_colors()
69 expect_equal(cli::num_ansi_colors(), 16)
70
71 if (l10n_info()$`UTF-8`) {
72 local_utf8()
73 expect_true(cli::is_utf8_output())
74 variant <- "unicode"
75 } else {
76 variant <- "ansi"
77 }
78
79 expect_snapshot(variant = variant, {
80 style_na("NA")
81 style_neg("-1")
82 })
83
84 expect_snapshot(variant = variant, {
85 xf <- function() ctl_colonnade(list(x = c((10^(-3:4)) * c(-1, 1), NA)))
86 print(xf())
87 with_options(pillar.subtle_num = TRUE, print(xf()))
88 with_options(pillar.subtle = FALSE, print(xf()))
89 with_options(pillar.neg = FALSE, print(xf()))
90 with_options(pillar.subtle = FALSE, pillar.neg = FALSE, print(xf()))
91 with_options(pillar.bold = TRUE, print(xf()))
92 })
93
94 expect_snapshot(variant = variant, {
95 ctl_colonnade(list(a_very_long_column_name = 0), width = 20)
96 })
97 })
98
99 # Run opposite test to snapshot output but not alter it
100 if (l10n_info()$`UTF-8`) {
101 test_that("color, options: UTF-8 is FALSE", {
102 skip("Symmetry")
103 })
104 }
105
106 test_that("tibble columns", {
107 x <- list(a = 1:3, b = data.frame(c = 4:6, d = 7:9))
108 expect_snapshot({
109 ctl_colonnade(x, width = 30)
110 })
111 })
112
113 test_that("tibble columns (nested)", {
114 x <- data_frame(
115 a = 1:3,
116 b = data_frame(
117 c = 4:6, d = 7:9,
118 e = data_frame(f = data_frame(g = 10:12, h = 13:15))
119 )
120 )
121 expect_snapshot({
122 ctl_colonnade(x, width = 40)
123 })
124 })
125
126 test_that("tibble columns (empty)", {
127 x <- data_frame(
128 a = 1:3,
129 b = data_frame(
130 c = 4:6, d = 7:9,
131 e = data_frame(f = 10:12)[, 0]
132 ),
133 c = 10:12
134 )
135 expect_snapshot({
136 ctl_colonnade(x, width = 40)
137 })
138 })
139
140 test_that("matrix columns (unnamed)", {
141 x <- list(a = 1:3, b = matrix(4:9, ncol = 2))
142 expect_snapshot({
143 ctl_colonnade(x, width = 30)
144 })
145 })
146
147 test_that("matrix columns (named)", {
148 x <- list(a = 1:3, b = matrix(4:9, ncol = 2, dimnames = list(NULL, c("c", "d"))))
149 expect_snapshot({
150 ctl_colonnade(x, width = 30)
151 })
152 })
153
154 test_that("matrix columns (empty)", {
155 expect_snapshot({
156 ctl_colonnade(
157 list(a = 1:3, b = matrix(4:6, ncol = 1)[, 0], c = 4:6),
158 width = 30
159 )
160 })
161 })
162
163 test_that("filling unused width (#331)", {
164 new_foo <- function(x = character()) {
165 vec_assert(x, character())
166 new_vctr(x, class = "foo")
167 }
168
169 data <- new_tbl(list(
170 month = month.name[1],
171 sentences = new_foo(paste(letters, collapse = " ")),
172 blah = paste(LETTERS, collapse = " ")
173 ))
174
175 pillar_shaft.foo <- function(x, ...) {
176 full <- format(x)
177 trunc <- format(paste0(substr(x, 1, 7), symbol$continue))
178 pillar::new_pillar_shaft(
179 list(full = full, trunc = trunc),
180 width = pillar::get_max_extent(full),
181 min_width = pillar::get_max_extent(trunc),
182 class = "pillar_shaft_foo"
183 )
184 }
185
186 format.pillar_shaft_foo <- function(x, width, ...) {
187 if (pillar::get_max_extent(x$full) <= width) {
188 ornament <- x$full
189 } else {
190 ornament <- x$trunc
191 }
192
193 pillar::new_ornament(ornament, align = "left")
194 }
195
196 local_methods(pillar_shaft.foo = pillar_shaft.foo, format.pillar_shaft_foo = format.pillar_shaft_foo)
197
198 expect_snapshot({
199 data
200 options(width = 60)
201 print(data)
202 })
203 })
204
205 test_that("focus columns", {
206 skip_if_not_installed("testthat", "3.1.1")
207
208 local_colors()
209
210 if (l10n_info()$`UTF-8`) {
211 local_utf8()
212 expect_true(cli::is_utf8_output())
213 variant <- "unicode"
214 } else {
215 variant <- "ansi"
216 }
217
218 x <- new_tbl(list(a = new_tbl(list(x = 1, y = 2)), b = "long enough"))
219
220 local_options(width = 80)
221
222 expect_snapshot(variant = variant, {
223 tbl_format_setup(x, width = 30, focus = "b")
224 tbl_format_setup(x, width = 20, focus = "b")
225 tbl_format_setup(x, width = 15, focus = "b")
226 tbl_format_setup(x, width = 10, focus = "b")
227 tbl_format_setup(x[2:1], width = 30, focus = "a")
228 tbl_format_setup(x[2:1], width = 20, focus = "a")
229 tbl_format_setup(x[2:1], width = 15, focus = "a")
230 tbl_format_setup(x[2:1], width = 10, focus = "a")
231 tbl_format_setup(x, width = 30, focus = c("a", "b"))
232 tbl_format_setup(x, width = 20, focus = c("a", "b"))
233 tbl_format_setup(x, width = 15, focus = c("a", "b"))
234 tbl_format_setup(x, width = 10, focus = c("a", "b"))
235 tbl_format_setup(x[2:1], width = 30, focus = c("a", "b"))
236 tbl_format_setup(x[2:1], width = 20, focus = c("a", "b"))
237 tbl_format_setup(x[2:1], width = 15, focus = c("a", "b"))
238 tbl_format_setup(x[2:1], width = 10, focus = c("a", "b"))
239 })
240 })