1
2test_that("make_space", {
3  expect_equal(make_space(0), "")
4  expect_equal(make_space(1), " ")
5  expect_equal(make_space(5), "     ")
6})
7
8test_that("apply_style", {
9  expect_error(
10    apply_style("text", raw(0)),
11    "Not a colour name or ANSI style"
12  )
13})
14
15test_that("viapply", {
16  expect_equal(
17    viapply(c("foo", "foobar"), length),
18    vapply(c("foo", "foobar"), length, integer(1))
19  )
20
21  expect_equal(
22    viapply(character(), length),
23    vapply(character(), length, integer(1))
24  )
25})
26
27test_that("ruler", {
28  expect_snapshot(
29    ruler(20)
30  )
31})
32
33test_that("rpad", {
34  expect_equal(rpad(character()), character())
35  expect_equal(rpad("foo"), "foo")
36  expect_equal(rpad(c("foo", "foobar")), c("foo   ", "foobar"))
37})
38
39test_that("lpad", {
40  expect_equal(lpad(character()), character())
41  expect_equal(lpad("foo"), "foo")
42  expect_equal(lpad(c("foo", "foobar")), c("   foo", "foobar"))
43})
44
45test_that("is_utf8_output", {
46
47  mockery::stub(
48    is_utf8_output, "l10n_info",
49    list(MBCS = TRUE, `UTF-8` = TRUE, `Latin-1` = FALSE)
50  )
51  withr::with_options(
52    list(cli.unicode = NULL),
53    expect_true(is_utf8_output())
54  )
55
56  mockery::stub(
57    is_utf8_output, "l10n_info",
58    list(MBCS = FALSE, `UTF-8` = FALSE, `Latin-1` = TRUE)
59  )
60  withr::with_options(
61    list(cli.unicode = NULL),
62    expect_false(is_utf8_output())
63  )
64})
65
66test_that("is_latex_output", {
67
68  mockery::stub(is_latex_output, "loadedNamespaces", "foobar")
69  expect_false(is_latex_output())
70
71  mockery::stub(is_latex_output, "loadedNamespaces", "knitr")
72  mockery::stub(
73    is_latex_output, "get",
74    function(x, ...) {
75      if (x == "is_latex_output") {
76        function() TRUE
77      } else {
78        base::get(x, ...)
79      }
80    }
81  )
82  expect_true(is_latex_output())
83})
84
85test_that("dedent", {
86  cases <- list(
87    list("", 0, ""),
88    list("", 1, ""),
89    list("", 2, ""),
90    list("x", 0, "x"),
91    list("x", 1, "x"),
92    list("x", 2, "x"),
93    list("xx", 0, "xx"),
94    list("xx", 1, "xx"),
95    list("xx", 2, "xx"),
96    list("foobar", 0, "foobar"),
97    list("foobar", 1, "foobar"),
98    list("foobar", 2, "foobar"),
99
100    list(" ", 0, " "),
101    list(" ", 1, ""),
102    list(" ", 2, ""),
103    list("  ", 0, "  "),
104    list("  ", 1, " "),
105    list("  ", 2, ""),
106    list(" x", 0, " x"),
107    list(" x", 1, "x"),
108    list(" x", 2, "x"),
109    list("  x", 0, "  x"),
110    list("  x", 1, " x"),
111    list("  x", 2, "x"),
112
113    list(" x  y", 3, "x  y"),
114    list(" x  y", 4, "x  y"),
115    list(" x  y", 5, "x  y"),
116    list(" x  ", 3, "x  "),
117    list(" x  ", 4, "x  "),
118    list(" x  ", 5, "x  ")
119  )
120
121  for (c in cases) expect_identical(dedent(c[[1]], c[[2]]), ansi_string(c[[3]]))
122})
123
124test_that("tail_na", {
125  cases <- list(
126    list(1:4, 4L),
127    list(1, 1),
128    list(double(), NA_real_),
129    list(character(), NA_character_)
130  )
131
132  for (i in seq_along(cases)) {
133    c <- cases[[i]]
134    expect_identical(tail_na(c[[1]]), c[[2]], info = i)
135  }
136
137  cases2 <- list(
138    list(1:4, 2, 3:4),
139    list(1, 2, c(NA_real_, 1)),
140    list(double(), 2, c(NA_real_, NA_real_)),
141    list(character(), 2, c(NA_character_, NA_character_))
142  )
143
144  for (i in seq_along(cases2)) {
145    c <- cases2[[i]]
146    expect_identical(tail_na(c[[1]], c[[2]]), c[[3]], info = i)
147  }
148})
149