File: test-num-ansi-colors.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 (203 lines) | stat: -rw-r--r-- 5,235 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
test_that("win10_build works for different osVersion", {
  local_mocked_bindings(
    sessionInfo = function() list(running = NULL), .package = "utils"
  )
  expect_identical(win10_build(), 0L)

  local_mocked_bindings(
    sessionInfo = function() list(running = "Debian GNU/Linux 11 (bullseye)"),
    .package = "utils"
  )
  expect_identical(win10_build(), 0L)

  local_mocked_bindings(
    sessionInfo = function() list(running = "Windows 10 x64 (build 16299)"),
    .package = "utils"
  )
  expect_identical(win10_build(), 16299L)
})

test_that("cli.default_num_colors #1", {

  # crayon.enabled
  withr::local_envvar(R_CLI_NUM_COLORS = NA_character_)
  withr::local_options(
    cli.num_colors = NULL,
    crayon.enabled = TRUE,
    crayon.colors = NULL,
    cli.default_num_colors = NULL
  )

  expect_equal(num_ansi_colors(), 8L)

  withr::local_options(cli.default_num_colors = 123L)
  expect_equal(num_ansi_colors(), 123L)
})

test_that("cli.default_num_colors #2", {

  # Windows emacs
  withr::local_envvar(
    R_CLI_NUM_COLORS = NA_character_,
    RSTUDIO = NA_character_
  )
  withr::local_options(
    cli.num_colors = NULL,
    crayon.enabled = NULL,
    crayon.colors = NULL,
    cli.default_num_colors = NULL
  )

  local_mocked_bindings(
    os_type = function() "windows",
    commandArgs = function() "--ess",
    is_emacs_with_color = function() TRUE
  )

  expect_equal(num_ansi_colors(), 8L)

  withr::local_options(cli.default_num_colors = 123L)
  expect_equal(num_ansi_colors(), 123L)
})

test_that("cli.default_num_colors #3", {

  # non-truecolor COLORMAP
  withr::local_envvar(COLORTERM = "other")
  withr::local_options(cli.default_num_colors = NULL)

  expect_equal(detect_tty_colors(), 8L)

  withr::local_options(cli.default_num_colors = 123L)
  expect_equal(detect_tty_colors(), 123L)
})

test_that("cli.default_num_colors #4", {

  # Unix emacs with color
  withr::local_envvar(COLORTERM = NA_character_)

  local_mocked_bindings(
    os_type = function() "unix",
    is_emacs_with_color = function() TRUE
  )

  withr::local_options(cli.default_num_colors = NULL)

  expect_equal(detect_tty_colors(), 8L)

  withr::local_options(cli.default_num_colors = 123L)
  expect_equal(detect_tty_colors(), 123L)
})

test_that("cli.default_num_colors #5", {

  # rstudio terminal on Windows
  withr::local_envvar(COLORTERM = NA_character_)

  local_mocked_bindings(
    os_type = function() "windows",
    win10_build = function() 10586,
    rstudio_detect = function() list(type = "rstudio_terminal"),
    system2 = function(...) TRUE
  )

  withr::local_options(cli.default_num_colors = NULL)
  expect_equal(detect_tty_colors(), 8L)

  withr::local_options(cli.default_num_colors = 123L)
  expect_equal(detect_tty_colors(), 123L)
})

test_that("cli.default_num_colors #6", {

  # Windows 10 terminal
  withr::local_envvar(COLORTERM = NA_character_)
  withr::local_options(cli.default_num_colors = NULL)

  local_mocked_bindings(
    os_type = function() "windows",
    win10_build = function() 10586,
    rstudio_detect = function() list(type = "not_rstudio"),
    system2 = function(...) TRUE
  )
  expect_equal(detect_tty_colors(), 256L)

  local_mocked_bindings(win10_build = function() 14931)
  expect_equal(detect_tty_colors(), truecolor)

  withr::local_options(cli.default_num_colors = 123L)
  expect_equal(detect_tty_colors(), 123L)
})

test_that("cli.default_num_colors #7", {

  # conemu or cmder
  withr::local_envvar(
    COLORTERM = NA_character_,
    ConEmuANSI = "ON"
  )
  withr::local_options(cli.default_num_colors = NULL)
  local_mocked_bindings(
    os_type = function() "windows",
    win10_build = function() 1
  )

  expect_equal(detect_tty_colors(), 8L)
  withr::local_options(cli.default_num_colors = 123L)
  expect_equal(detect_tty_colors(), 123L)
})

test_that("cli.default_num_colors #8", {

  # unix terminal, xterm
  withr::local_envvar(
    COLORTERM = NA_character_,
    TERM = "xterm"
  )

  local_mocked_bindings(
    os_type = function() "unix",
    is_emacs_with_color = function() FALSE,
    system = function(...) "8"
  )

  withr::local_options(cli.default_num_colors = NULL)
  expect_equal(detect_tty_colors(), 256L)

  withr::local_options(cli.default_num_colors = 123L)
  expect_equal(detect_tty_colors(), 123L)
})

test_that("ESS_BACKGROUND_MODE", {
  withr::local_envvar(
    RSTUDIO = NA_character_,
    ESS_BACKGROUND_MODE = NA_character_
  )

  local_mocked_bindings(
    is_iterm = function() FALSE,
    is_emacs = function() TRUE
  )

  expect_false(detect_dark_theme("auto"))

  withr::local_envvar(ESS_BACKGROUND_MODE = "dark")
  expect_true(detect_dark_theme("auto"))
})

test_that("emacs_version", {
  withr::local_envvar(INSIDE_EMACS = "")
  expect_true(is.na(emacs_version()))
  withr::local_envvar(INSIDE_EMACS = "foobar")
  expect_true(is.na(emacs_version()))

  withr::local_envvar(INSIDE_EMACS = "23.2.3")
  expect_equal(emacs_version(), c(23, 2, 3))
  withr::local_envvar(INSIDE_EMACS = "23.2.3,foobar")
  expect_equal(emacs_version(), c(23, 2, 3))
  withr::local_envvar(INSIDE_EMACS = "'23.2.3'")
  expect_equal(emacs_version(), c(23, 2, 3))
  withr::local_envvar(INSIDE_EMACS = "'23.2.3',foobar")
  expect_equal(emacs_version(), c(23, 2, 3))
})