Debian Codegraph @

tests/testthat/test-tbl-format.R

raw
1 test_that("print() returns output invisibly", {
2 expect_output(ret <- withVisible(print(as_tbl(trees))))
3 expect_false(ret$visible)
4 expect_identical(ret$value, as_tbl(trees))
5 })
6
7 test_that("format() reacts on cli.num_colors option", {
8 local_colors()
9
10 expect_snapshot({
11 format(as_tbl(trees))
12 options(cli.num_colors = 1)
13 format(as_tbl(trees))
14 })
15 })
16
17 test_that("print() output", {
18 expect_snapshot({
19 as_tbl(mtcars)
20
21 print(as_tbl(mtcars), n = 8, width = 30)
22
23 print(as_tbl(mtcars), n = 30)
24
25 print(as_tbl(mtcars), n = 100)
26
27 print(as_tbl(mtcars), width = 40, max_extra_cols = 1)
28
29 print(as_tbl(mtcars), width = 30, max_footer_lines = 3)
30
31 rlang::with_options(
32 tibble.print_min = 5,
33 as_tbl(mtcars)
34 )
35
36 rlang::with_options(
37 tibble.print_max = 50,
38 as_tbl(mtcars)
39 )
40
41 rlang::with_options(
42 tibble.width = 30,
43 as_tbl(mtcars)
44 )
45 })
46
47 expect_snapshot({
48 print(as_tbl(mtcars), width = 40, max_extra_cols = 1)
49
50 print(tbl_format_setup(new_tbl(trees, pillar_focus = "Volume"), width = 30))
51 })
52 })
53
54 test_that("get_n_print()", {
55 local_options(tibble.print_min = 3, tibble.print_max = 6)
56
57 expect_equal(get_n_print(5, 2), 5)
58 expect_equal(get_n_print(5, 3), 5)
59 expect_equal(get_n_print(5, 5), 5)
60 expect_equal(get_n_print(5, 6), 5)
61 expect_equal(get_n_print(5, 7), 5)
62
63 expect_equal(get_n_print(Inf, 7), Inf)
64
65 expect_equal(get_n_print(NULL, 2), 2)
66 expect_equal(get_n_print(NULL, 3), 3)
67 expect_equal(get_n_print(NULL, 5), 5)
68 expect_equal(get_n_print(NULL, 6), 6)
69 expect_equal(get_n_print(NULL, 7), 3)
70
71 expect_equal(get_n_print(NULL, NA), structure(3, max = 6))
72 })
73
74 test_that("get_width_print()", {
75 local_options(tibble.width = 120)
76
77 expect_equal(get_width_print(NULL), 120)
78 expect_equal(get_width_print(40), 40)
79 expect_equal(get_width_print(80), 80)
80 expect_equal(get_width_print(140), 140)
81 })
82
83 test_that("format() is silent if not all arguments in `...` are used", {
84 tbl <- new_tbl(trees)
85 expect_error(format(tbl, unknown_arg = TRUE), NA)
86 })
87
88 test_that("print() and format() can pass down arguments in `...`", {
89 local_methods(
90 tbl_format_setup.my_tibble = function(x, ..., known_arg = TRUE) {
91 setup <- NextMethod()
92 if (known_arg) {
93 setup$tbl_sum <- c(setup$tbl_sum, "Attribute" = "Set")
94 }
95 setup
96 }
97 )
98
99 tbl <- new_tbl(trees, class = "my_tibble")
100
101 expect_snapshot({
102 format(tbl, known_arg = TRUE)
103 print(tbl, known_arg = TRUE)
104 print(tbl, known_arg = FALSE)
105 })
106 })