File: test-utils-ui.R

package info (click to toggle)
r-cran-usethis 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,228 kB
  • sloc: sh: 26; makefile: 17; cpp: 6; ansic: 3
file content (231 lines) | stat: -rw-r--r-- 6,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
cli::test_that_cli("ui_bullets() look as expected", {
  # suppress test silencing
  withr::local_options(list(usethis.quiet = FALSE))

  expect_snapshot(
    ui_bullets(c(
      # relate to legacy functions
      "_" = "todo", # ui_todo()
      "v" = "done", # ui_done()
      "x" = "oops", # ui_oops()
      "i" = "info", # ui_info()
            "noindent", # ui_line()

      # other cli bullets that have no special connection to usethis history
      " " = "indent",
      "*" = "bullet",
      ">" = "arrow",
      "!" = "warning"
    ))
  )
})

test_that("ui_bullets() respect usethis.quiet = TRUE", {
  withr::local_options(list(usethis.quiet = TRUE))

  expect_no_message(
    ui_bullets(c(
      # relate to legacy functions
      "_" = "todo", # ui_todo()
      "v" = "done", # ui_done()
      "x" = "oops", # ui_oops()
      "i" = "info", # ui_info()
            "noindent", # ui_line()

      # other cli bullets that have no special connection to usethis history
      " " = "indent",
      "*" = "bullet",
      ">" = "arrow",
      "!" = "warning"
    ))
  )
})

cli::test_that_cli("ui_bullets() does glue interpolation and inline markup", {
  # suppress test silencing
  withr::local_options(list(usethis.quiet = FALSE))

  x <- "world"

  expect_snapshot(
    ui_bullets(c(
      "i" = "Hello, {x}!",
      "v" = "Updated the {.field BugReports} field",
      "x" = "Scary {.code code} or {.fun function}"
    ))
  )
})

test_that("trailing slash behaviour of ui_path_impl()", {
  # target doesn't exist so no empirical evidence that it's a directory
  expect_match(ui_path_impl("abc"), "abc$")

  # path suggests it's a directory
  expect_match(ui_path_impl("abc/"), "abc/$")
  expect_match(ui_path_impl("abc//"), "abc/$")

  # path is known to be a directory
  tmpdir <- withr::local_tempdir(pattern = "ui_path_impl")

  expect_match(ui_path_impl(tmpdir), "/$")
  expect_match(ui_path_impl(paste0(tmpdir, "/")), "[^/]/$")
  expect_match(ui_path_impl(paste0(tmpdir, "//")), "[^/]/$")
})

test_that("ui_abort() works", {
  expect_usethis_error(ui_abort("spatula"), "spatula")

  # usethis.quiet should have no effect on this
  withr::local_options(list(usethis.quiet = TRUE))
  expect_usethis_error(ui_abort("whisk"), "whisk")
})

test_that("ui_abort() defaults to 'x' for first bullet", {
  expect_snapshot(error = TRUE, ui_abort("no explicit bullet"))
})

test_that("ui_abort() can take explicit first bullet", {
  expect_snapshot(error = TRUE, ui_abort(c("v" = "success bullet")))
})

test_that("ui_abort() defaults to 'i' for non-first bullet", {
  expect_snapshot(
    error = TRUE,
    ui_abort(c(
      "oops",
      " " = "space bullet",
      "info bullet",
      "v" = "success bullet"
    ))
  )
})

cli::test_that_cli("ui_code_snippet() with scalar input", {
  withr::local_options(list(usethis.quiet = FALSE))

  expect_snapshot(
    ui_code_snippet("
      options(
        warnPartialMatchArgs = TRUE,
        warnPartialMatchDollar = TRUE,
        warnPartialMatchAttr = TRUE
      )")
  )
}, configs = c("plain", "ansi"))

cli::test_that_cli("ui_code_snippet() with vector input", {
  withr::local_options(list(usethis.quiet = FALSE))

  expect_snapshot(
    ui_code_snippet(c(
      "options(",
      "  warnPartialMatchArgs = TRUE,",
      "  warnPartialMatchDollar = TRUE,",
      "  warnPartialMatchAttr = TRUE",
      ")"
    ))
  )
}, configs = c("plain", "ansi"))

cli::test_that_cli("ui_code_snippet() when language is not R", {
  withr::local_options(list(usethis.quiet = FALSE))
  h <- "blah.h"
  expect_snapshot(
    ui_code_snippet("#include <{h}>", language = "")
  )
}, configs = c("plain", "ansi"))

cli::test_that_cli("ui_code_snippet() can interpolate", {
  withr::local_options(list(usethis.quiet = FALSE))

  true_val <- "TRUE"
  false_val <- "'FALSE'"

  expect_snapshot(
    ui_code_snippet("if (1) {true_val} else {false_val}")
  )
}, configs = c("plain", "ansi"))

cli::test_that_cli("ui_code_snippet() can NOT interpolate", {
  withr::local_options(list(usethis.quiet = FALSE))
  expect_snapshot({
    ui_code_snippet(
      "foo <- function(x){x}",
      interpolate = FALSE
    )
    ui_code_snippet(
      "foo <- function(x){{x}}",
      interpolate = TRUE
    )
  })
}, configs = c("plain", "ansi"))

test_that("bulletize() works", {
  withr::local_options(list(usethis.quiet = FALSE))
  expect_snapshot(ui_bullets(bulletize(letters)))
  expect_snapshot(ui_bullets(bulletize(letters, bullet = "x")))
  expect_snapshot(ui_bullets(bulletize(letters, n_show = 2)))
  expect_snapshot(ui_bullets(bulletize(letters[1:6])))
  expect_snapshot(ui_bullets(bulletize(letters[1:7])))
  expect_snapshot(ui_bullets(bulletize(letters[1:8])))
  expect_snapshot(ui_bullets(bulletize(letters[1:6], n_fudge = 0)))
  expect_snapshot(ui_bullets(bulletize(letters[1:8], n_fudge = 3)))
})

test_that("usethis_map_cli() works", {
  x <- c("aaa", "bbb", "ccc")
  expect_equal(
    usethis_map_cli(x, template = "{.file <<x>>}"),
    c("{.file aaa}", "{.file bbb}", "{.file ccc}")
  )
})

cli::test_that_cli("ui_special() works", {
  expect_snapshot(cli::cli_text(ui_special()))
  expect_snapshot(cli::cli_text(ui_special("whatever")))
}, configs = c("plain", "ansi"))

cli::test_that_cli("kv_line() looks as expected in basic use", {
  withr::local_options(list(usethis.quiet = FALSE))

  expect_snapshot({
    kv_line("CHARACTER", "VALUE")
    kv_line("NUMBER", 1)
    kv_line("LOGICAL", TRUE)
  })
}, configs = c("plain", "fancy"))

cli::test_that_cli("kv_line() can interpolate and style inline in key", {
  withr::local_options(list(usethis.quiet = FALSE))

  field <- "SOME_FIELD"
  expect_snapshot(
    kv_line("Let's reveal {.field {field}}", "whatever")
  )
}, configs = c("plain", "fancy"))

cli::test_that_cli("kv_line() can treat value in different ways", {
  withr::local_options(list(usethis.quiet = FALSE))

  value <- "some value"
  adjective <- "great"
  url <- "https://usethis.r-lib.org/"

  expect_snapshot({
    # evaluation in .envir
    kv_line("Key", value)

    # NULL is special
    kv_line("Something we don't have", NULL)
    # explicit special
    kv_line("Key", ui_special("discovered"))

    # value taken at face value
    kv_line("Key", "something {.emph important}")

    # I() indicates value has markup
    kv_line("Key", I("something {.emph important}"))
    kv_line("Key", I("something {.emph {adjective}}"))
    kv_line("Interesting file", I("{.url {url}}"))
  })
}, configs = c("plain", "fancy"))