File: test-progress-types.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 (153 lines) | stat: -rw-r--r-- 4,291 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

start_app()
on.exit(stop_app(), add = TRUE)

test_that("iterator", {
  withr::local_options(
    cli.dynamic = TRUE,
    cli.ansi = TRUE,
    cli.spinner = NULL,
    cli.spinner_unicode = NULL,
    cli.progress_format_iterator = NULL,
    cli.progress_format_iterator_nototal= NULL,
    cli.width = Inf
  )

  fun <- function() {
    cli_progress_bar(type = "iterator", total = 100)
    cli_progress_update(set = 50, force = TRUE)
  }
  out <- fix_times(capture_cli_messages(fun()))
  expect_snapshot(out)

  fun <- function() {
    cli_progress_bar(type = "iterator", total = NA)
    cli_progress_update(set = 50, force = TRUE)
  }
  out <- fix_times(capture_cli_messages(fun()))
  expect_snapshot(out)
})

test_that("tasks", {
  skip_on_cran()
  withr::local_options(
    cli.dynamic = TRUE,
    cli.ansi = TRUE,
    cli.spinner = NULL,
    cli.spinner_unicode = NULL,
    cli.progress_format_tasks = NULL,
    cli.progress_format_tasks_nototal= NULL,
    cli.width = Inf
  )

  fun <- function() {
    cli_progress_bar(type = "tasks", total = 100)
    cli_progress_update(set = 50, force = TRUE)
  }
  out <- fix_times(capture_cli_messages(fun()))
  expect_snapshot(out)

  fun <- function() {
    cli_progress_bar(type = "tasks", total = NA)
    cli_progress_update(set = 50, force = TRUE)
  }
  out <- fix_times(capture_cli_messages(fun()))
  expect_snapshot(out)
})

test_that("download", {
  withr::local_options(
    cli.dynamic = TRUE,
    cli.ansi = TRUE,
    cli.spinner = NULL,
    cli.spinner_unicode = NULL,
    cli.progress_format_download = NULL,
    cli.progress_format_download_nototal= NULL
  )

  fun <- function() {
    cli_progress_bar(type = "download", total = 1024 * 1024)
    cli_progress_update(set = 52 * 1024, force = TRUE)
  }
  out <- fix_times(capture_cli_messages(fun()))
  expect_snapshot(out)

  fun <- function() {
    cli_progress_bar(type = "download", total = NA)
    cli_progress_update(set = 52 * 1024, force = TRUE)
  }
  out <- fix_times(capture_cli_messages(fun()))
  expect_snapshot(out)
})

test_that("customize with options, iterator", {
  withr::local_options(
    cli.dynamic = TRUE,
    cli.ansi = TRUE,
    cli.progress_format_iterator = "new format {cli::pb_current}",
    cli.progress_format_iterator_nototal = NULL
  )

  fun <- function(type, total, set = 50) {
    cli_progress_bar(type = type, total = total)
    cli_progress_update(set = set, force = TRUE)
  }

  expect_snapshot(capture_cli_messages(fun("iterator", 100)))
  expect_snapshot(capture_cli_messages(fun("iterator", NA)))

  withr::local_options(
    cli.progress_format_iterator_nototal = "new too {cli::pb_current}"
  )

  expect_snapshot(capture_cli_messages(fun("iterator", 100)))
  expect_snapshot(capture_cli_messages(fun("iterator", NA)))
})

test_that("customize with options, tasks", {
  withr::local_options(
    cli.dynamic = TRUE,
    cli.ansi = TRUE,
    cli.progress_format_tasks = "new format {cli::pb_current}",
    cli.progress_format_tasks_nototal = NULL
  )

  fun <- function(type, total, set = 50) {
    cli_progress_bar(type = type, total = total)
    cli_progress_update(set = set, force = TRUE)
  }

  expect_snapshot(capture_cli_messages(fun("tasks", 100)))
  expect_snapshot(capture_cli_messages(fun("tasks", NA)))

  withr::local_options(
    cli.progress_format_tasks_nototal = "new too {cli::pb_current}"
  )

  expect_snapshot(capture_cli_messages(fun("tasks", 100)))
  expect_snapshot(capture_cli_messages(fun("tasks", NA)))
})

test_that("customize with options, download", {
  withr::local_options(
    cli.dynamic = TRUE,
    cli.ansi = TRUE,
    cli.progress_format_download = "new format {cli::pb_current_bytes}",
    cli.progress_format_download_nototal = NULL
  )

  fun <- function(type, total, set = 50) {
    cli_progress_bar(type = type, total = total)
    cli_progress_update(set = set, force = TRUE)
  }

  expect_snapshot(capture_cli_messages(fun("download", 1024 * 1024, 512 * 1024)))
  expect_snapshot(capture_cli_messages(fun("download", NA, 512 * 1024)))

  withr::local_options(
    cli.progress_format_download_nototal = "new too {cli::pb_current_bytes}"
  )

  expect_snapshot(capture_cli_messages(fun("download", 1024 * 1024, 512 * 1024)))
  expect_snapshot(capture_cli_messages(fun("download", NA, 512 * 1024)))
})