File: test-progress-variables.R

package info (click to toggle)
r-cran-cli 3.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,288 kB
  • sloc: ansic: 16,412; cpp: 37; sh: 13; makefile: 2
file content (246 lines) | stat: -rw-r--r-- 8,608 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

test_that("cli_progress_demo", {
  withr::local_options(cli.ansi = TRUE)
  out <- cli_progress_demo(live = FALSE, at = 50)
  out$lines <- fix_times(out$lines)
  expect_snapshot(out)

  out <- cli_progress_demo(live = FALSE, at = NULL, total = 3)
  out$lines <- fix_times(out$lines)
  expect_snapshot(out)

  out <- cli_progress_demo(live = FALSE, at = NULL, total = NA, delay = 0.001)
  out$lines <- fix_times(out$lines)
  expect_snapshot(out)

  fun <- function() {
    options(cli.progress_handlers_only = "cli")
    cli_progress_demo(live = TRUE, at = NULL, total = 3)
  }

  msgs <- capture_cli_messages(out <- fun())
  expect_snapshot(out)
  expect_snapshot(msgs)
})

test_that("pb_bar", {
  expect_equal(cli__pb_bar(NULL), "")
  withr::local_options(cli__pb = list(current = 15, total = NA))
  expect_snapshot(cli_text("-{cli::pb_bar}-"))
  withr::local_options(cli__pb = list(current = 15, total = 30))
  expect_snapshot(cli_text("-{cli::pb_bar}-"))
})

test_that("pb_current", {
  expect_equal(cli__pb_current(NULL), "")
  withr::local_options(cli__pb = list(current = 15))
  expect_equal(cli::pb_current, 15)
})

test_that("pb_current_bytes", {
  expect_equal(cli__pb_current_bytes(NULL), "")
  expect_snapshot({
    cli__pb_current_bytes(list(current = 0))
    cli__pb_current_bytes(list(current = 1))
    cli__pb_current_bytes(list(current = 1000))
    cli__pb_current_bytes(list(current = 1000 * 23))
    cli__pb_current_bytes(list(current = 1000 * 1000 * 23))
  })
})

test_that("pb_elapsed", {
  expect_equal(cli__pb_elapsed(NULL), "")
  withr::local_options(cli__pb = list(start = 0, speed_time = 1))
  local_mocked_bindings(.Call = function(...) 1)
  expect_snapshot(cli__pb_elapsed())
  local_mocked_bindings(.Call = function(...) 21)
  expect_snapshot(cli__pb_elapsed())
  local_mocked_bindings(.Call = function(...) 58)
  expect_snapshot(cli__pb_elapsed())
  local_mocked_bindings(.Call = function(...) 60 * 65)
  expect_snapshot(cli__pb_elapsed())
})

test_that("pb_elapsed_clock", {
  expect_equal(cli__pb_elapsed_clock(NULL), "")
  withr::local_options(cli__pb = list(start = 0, speed_time = 1))
  local_mocked_bindings(.Call = function(...) 1)
  expect_snapshot(cli__pb_elapsed_clock())
  local_mocked_bindings(.Call = function(...) 21)
  expect_snapshot(cli__pb_elapsed_clock())
  local_mocked_bindings(.Call = function(...) 58)
  expect_snapshot(cli__pb_elapsed_clock())
  local_mocked_bindings(.Call = function(...) 60 * 65)
  expect_snapshot(cli__pb_elapsed_clock())
})

test_that("pb_elapsed_raw", {
  expect_equal(cli__pb_elapsed_raw(NULL), "")
  withr::local_options(cli__pb = list(start = 0, speed_time = 1))
  local_mocked_bindings(.Call = function(...) 1)
  expect_snapshot(cli__pb_elapsed_raw())
  local_mocked_bindings(.Call = function(...) 21)
  expect_snapshot(cli__pb_elapsed_raw())
  local_mocked_bindings(.Call = function(...) 58)
  expect_snapshot(cli__pb_elapsed_raw())
  local_mocked_bindings(.Call = function(...) 60 * 65)
  expect_snapshot(cli__pb_elapsed_raw())
})

test_that("pb_eta", {
  expect_equal(cli__pb_eta(NULL), "")
  local_mocked_bindings(cli__pb_eta_raw = function(...) NA_real_)
  expect_snapshot(cli__pb_eta(list()))
  local_mocked_bindings(cli__pb_eta_raw = function(...) as.difftime(12, units = "secs"))
  expect_snapshot(cli__pb_eta(list()))
})

test_that("pb_eta_raw", {
  expect_equal(cli__pb_eta_raw(NULL), "")
  withr::local_options(cli__pb = list(total = NA))
  expect_identical(cli__pb_eta_raw(), NA_real_)
  local_mocked_bindings(.Call = function(...) 0)
  withr::local_options(cli__pb = list(start = -10, total = 100, current = 0))
  expect_snapshot(cli__pb_eta_raw())
  withr::local_options(cli__pb = list(start = -10, total = 100, current = 20))
  expect_snapshot(cli__pb_eta_raw())
  withr::local_options(cli__pb = list(start = -40, total = 100, current = 80))
  expect_snapshot(cli__pb_eta_raw())
  withr::local_options(cli__pb = list(start = -50, total = 100, current = 100))
  expect_snapshot(cli__pb_eta_raw())
})

test_that("pb_eta_str", {
  expect_equal(cli__pb_eta_str(NULL), "")
  withr::local_options(cli__pb = list(total = NA))
  expect_identical(cli__pb_eta_str(), "")
  local_mocked_bindings(cli__pb_eta = function(...) "?")
  expect_snapshot(cli__pb_eta_str(list()))
  local_mocked_bindings(cli__pb_eta = function(...) " 1s")
  expect_snapshot(cli__pb_eta_str(list()))
})

test_that("pb_extra", {
  expect_equal(cli__pb_extra(NULL), "")
  expect_equal(cli__pb_extra(list(extra = list(a = 1))), list(a = 1))
})

test_that("pb_id", {
  expect_equal(cli__pb_id(NULL), "")
  expect_equal(cli__pb_id(list(id = 123)), 123)
})

test_that("pb_name", {
  expect_equal(cli__pb_name(NULL), "")
  expect_equal(cli__pb_name(list(name = NULL)), "")
  expect_equal(cli__pb_name(list(name = "foo")), "foo ")
})

test_that("pb_percent", {
  expect_equal(cli__pb_percent(NULL), "")
  expect_snapshot({
    cli__pb_percent(list(current = 0, total = 99))
    cli__pb_percent(list(current = 5, total = 99))
    cli__pb_percent(list(current = 10, total = 99))
    cli__pb_percent(list(current = 25, total = 99))
    cli__pb_percent(list(current = 99, total = 99))
    cli__pb_percent(list(current = 100, total = 99))
  })
})

test_that("pb_pid", {
  expect_equal(cli__pb_pid(NULL), "")
  expect_equal(cli__pb_pid(list()), Sys.getpid())
  expect_equal(cli__pb_pid(list(pid = 100)), 100)
})

test_that("pb_rate", {
  expect_equal(cli__pb_rate(NULL), "")
  local_mocked_bindings(cli__pb_rate_raw = function(...) NaN)
  expect_snapshot(cli__pb_rate(list()))
  local_mocked_bindings(cli__pb_rate_raw = function(...) Inf)
  expect_snapshot(cli__pb_rate(list()))
  local_mocked_bindings(cli__pb_rate_raw = function(...) 1 / 10)
  expect_snapshot(cli__pb_rate(list()))
  local_mocked_bindings(cli__pb_rate_raw = function(...) 12.4)
  expect_snapshot(cli__pb_rate(list()))
})

test_that("pb_rate_raw", {
  expect_equal(cli__pb_rate_raw(NULL), "")
  local_mocked_bindings(cli__pb_elapsed_raw = function(...) this)
  this <- 0
  expect_equal(cli__pb_rate_raw(list(current = 0)), NaN)
  expect_equal(cli__pb_rate_raw(list(current = 23)), Inf)
  this <- 1
  expect_equal(cli__pb_rate_raw(list(current = 23)), 23)
  this <- 10
  expect_equal(cli__pb_rate_raw(list(current = 1)), 1/10)
})

test_that("pb_rate_bytes", {
  expect_equal(cli__pb_rate_bytes(NULL), "")
  local_mocked_bindings(cli__pb_rate_raw = function(...) NaN)
  expect_snapshot(cli__pb_rate_bytes(list()))
  local_mocked_bindings(cli__pb_rate_raw = function(...) Inf)
  expect_snapshot(cli__pb_rate_bytes(list()))
  local_mocked_bindings(cli__pb_rate_raw = function(...) 0)
  expect_snapshot(cli__pb_rate_bytes(list()))
  local_mocked_bindings(cli__pb_rate_raw = function(...) 1024)
  expect_snapshot(cli__pb_rate_bytes(list()))
  local_mocked_bindings(cli__pb_rate_raw = function(...) 1024 * 23)
  expect_snapshot(cli__pb_rate_bytes(list()))
  local_mocked_bindings(cli__pb_rate_raw = function(...) 1024 * 1024 * 23)
  expect_snapshot(cli__pb_rate_bytes(list()))
})

test_that("pb_spin", {
  expect_equal(cli__pb_spin(NULL), "")
  withr::local_options(cli.spinner = "line")
  withr::local_options(cli__pb = list(tick = 1))
  expect_snapshot(cli_text("-{cli::pb_spin}-{cli::pb_spin}-"))
  withr::local_options(cli__pb = list(tick = 2))
  expect_snapshot(cli_text("-{cli::pb_spin}-{cli::pb_spin}-"))
  withr::local_options(cli__pb = list(tick = 10))
  expect_snapshot(cli_text("-{cli::pb_spin}-{cli::pb_spin}-"))
})

test_that("pb_status", {
  expect_equal(cli__pb_status(NULL), "")
  expect_equal(cli__pb_status(NULL), "")
  expect_equal(cli__pb_status(list(status = NULL)), "")
  expect_equal(cli__pb_status(list(status = "foo")), "foo ")
})

test_that("pb_timestamp", {
  expect_equal(cli__pb_timestamp(NULL), "")
  fake <- .POSIXct(1623974954, tz = "GMT")
  local_mocked_bindings(Sys.time = function() fake)
  expect_snapshot(cli__pb_timestamp(list()))

  backup <- mget(c("load_time", "speed_time"), clienv)
  on.exit({
    clienv$load_time <- backup$load_time
    clienv$speed_time <- backup$speed_time
  }, add = TRUE)

  clienv$load_time <- fake - 10
  clienv$speed_time <- 3.0
  expect_snapshot(cli__pb_timestamp(list()))
})

test_that("pb_total", {
  expect_equal(cli__pb_total(NULL), "")
  expect_equal(cli__pb_total(list(total = 101)), 101)
})

test_that("pb_total_bytes", {
  expect_equal(cli__pb_total_bytes(NULL), "")
  expect_snapshot({
    cli__pb_total_bytes(list(total = 0))
    cli__pb_total_bytes(list(total = 1))
    cli__pb_total_bytes(list(total = 1000))
    cli__pb_total_bytes(list(total = 1000 * 23))
    cli__pb_total_bytes(list(total = 1000 * 1000 * 23))
  })
})