File: test-ansi.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 (104 lines) | stat: -rw-r--r-- 3,416 bytes parent folder | download | duplicates (2)
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

test_that("Classes", {
  expect_equal(class(style_underline("foo")), c("cli_ansi_string", "ansi_string", "character"))
})

test_that("Coloring and highlighting works", {
  local_reproducible_output(crayon = TRUE)
  expect_equal(c(style_underline("foo")), "\u001b[4mfoo\u001b[24m")
  expect_equal(c(col_red("foo")), "\u001b[31mfoo\u001b[39m")
  expect_equal(c(bg_red("foo")), "\u001b[41mfoo\u001b[49m")
})

test_that("Applying multiple styles at once works", {
  local_reproducible_output(crayon = TRUE)
  st <- combine_ansi_styles(col_red, bg_green, "underline")
  expect_equal(
    c(st("foo")),
    "\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m\u001b[39m")
  st <- combine_ansi_styles(style_underline, "red", bg_green)
  expect_equal(
    c(st("foo")),
    "\u001b[4m\u001b[31m\u001b[42mfoo\u001b[49m\u001b[39m\u001b[24m")
})

test_that("Nested styles are supported", {
  local_reproducible_output(crayon = TRUE)
  st <- combine_ansi_styles(style_underline, bg_blue)
  expect_equal(
    c(col_red("foo", st("bar"), "!")),
    "\u001b[31mfoo\u001b[4m\u001b[44mbar\u001b[49m\u001b[24m!\u001b[39m")
})

test_that("Nested styles of the same type are supported", {
  local_reproducible_output(crayon = TRUE)
  expect_equal(
    c(col_red("a", col_blue("b", col_green("c"), "b"), "c")),
    "\u001b[31ma\u001b[34mb\u001b[32mc\u001b[34mb\u001b[31mc\u001b[39m")
})

test_that("Reset all styles", {
  local_reproducible_output(crayon = TRUE)
  st <- combine_ansi_styles("red", bg_green, "underline")
  ok <- c(
    paste0(
      "\033[0m\033[31m\033[42m\033[4mfoo\033[24m\033[49m\033[39m",
      "foo\033[0m\033[22m\033[23m\033[24m\033[27m\033[28m",
      "\033[29m\033[39m\033[49m"),
    paste0("\u001b[0m\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m",
           "\u001b[39mfoo\u001b[0m")
  )
  expect_true(style_reset(st("foo"), "foo") %in% ok)
})

test_that("Variable number of arguments", {
  local_reproducible_output(crayon = TRUE)
  expect_equal(c(col_red("foo", "bar")), "\u001b[31mfoobar\u001b[39m")
})

test_that("print.cli_ansi_style", {
  expect_snapshot(
    print(col_red)
  )
})

test_that("print.cli_ansi_string", {
  withr::local_options(cli.num_colors = 256)
  expect_snapshot(
    print(col_red("red"))
  )
})

test_that("ansi-scale", {
  expect_snapshot({
    ansi_scale(c(0,0,0))
    ansi_scale(c(255,100,0))
    ansi_scale(c(255,100,0), round = FALSE)
  })
})

test_that("zero length vectors", {
  withr::local_options(cli.num_colors = 1)
  expect_equal(length(col_cyan(character())), 0)
  expect_equal(length(bg_cyan(character())), 0)
  expect_equal(length(col_br_cyan(character())), 0)
  expect_equal(length(bg_br_cyan(character())), 0)

  withr::local_options(cli.num_colors = 8)
  expect_equal(length(col_cyan(character())), 0)
  expect_equal(length(bg_cyan(character())), 0)
  expect_equal(length(col_br_cyan(character())), 0)
  expect_equal(length(bg_br_cyan(character())), 0)

  withr::local_options(cli.num_colors = 256)
  expect_equal(length(col_cyan(character())), 0)
  expect_equal(length(bg_cyan(character())), 0)
  expect_equal(length(col_br_cyan(character())), 0)
  expect_equal(length(bg_br_cyan(character())), 0)

  withr::local_options(cli.num_colors = truecolor)
  expect_equal(length(col_cyan(character())), 0)
  expect_equal(length(bg_cyan(character())), 0)
  expect_equal(length(col_br_cyan(character())), 0)
  expect_equal(length(bg_br_cyan(character())), 0)
})