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
|
test_that_cli("cli_abort", {
withr::local_options(cli.theme_dark = FALSE)
expect_snapshot(error = TRUE, local({
n <- "boo"
cli_abort(c(
"{.var n} must be a numeric vector",
"x" = "You've supplied a {.cls {class(n)}} vector."
))
}))
expect_snapshot(error = TRUE, local({
len <- 26
idx <- 100
cli_abort(c(
"Must index an existing element:",
"i" = "There {?is/are} {len} element{?s}.",
"x" = "You've tried to subset element {idx}."
))
}))
n <- "boo"
err <- tryCatch(
cli_abort(c(
"{.var n} must be a numeric vector",
"x" = "You've supplied a {.cls {class(n)}} vector."
)),
error = function(e) e
)
expect_snapshot(c(err$message, err$body))
})
test_that_cli("cli_warn", {
skip_if_not_installed("rlang", "1.0.0")
withr::local_options(cli.theme_dark = FALSE)
expect_snapshot({
n <- "boo"
cli_warn(c(
"{.var n} must be a numeric vector",
"x" = "You've supplied a {.cls {class(n)}} vector."
))
})
expect_snapshot(local({
len <- 26
idx <- 100
cli_warn(c(
"Must index an existing element:",
"i" = "There {?is/are} {len} element{?s}.",
"x" = "You've tried to subset element {idx}."
))
}))
})
test_that_cli("cli_inform", {
skip_if_not_installed("rlang", "1.0.0")
withr::local_options(cli.ansi = FALSE)
expect_snapshot({
n <- "boo"
cli_inform(c(
"{.var n} must be a numeric vector",
"x" = "You've supplied a {.cls {class(n)}} vector."
))
})
expect_snapshot(local({
len <- 26
idx <- 100
cli_inform(c(
"Must index an existing element:",
"i" = "There {?is/are} {len} element{?s}.",
"x" = "You've tried to subset element {idx}."
))
}))
})
test_that("cli_abort width in RStudio", {
# this is to fix breakage with new testthat
withr::local_options(cli.condition_width = getOption("cli.width"))
local_mocked_bindings(rstudio_detect = function() list(type = "rstudio_console"))
withr::local_rng_version("3.5.0")
set.seed(42)
expect_snapshot(error = TRUE, local({
len <- 26
idx <- 100
cli_abort(c(
lorem_ipsum(1, 3),
"i" = lorem_ipsum(1, 3),
"x" = lorem_ipsum(1, 3)
))
}))
})
test_that_cli(configs = "ansi", "color in RStudio", {
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_console", num_colors = 256),
get_rstudio_theme = function() list(foreground = "rgb(0, 0, 0)")
)
expect_snapshot({
col <- get_rstudio_fg_color0()
cat(col("this is the new color"))
})
local_mocked_bindings(get_rstudio_theme = function() list())
expect_null(get_rstudio_fg_color0())
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_console", num_colors = 1)
)
expect_null(get_rstudio_fg_color0())
})
test_that_cli(configs = "ansi", "update_rstudio_color", {
local_mocked_bindings(
get_rstudio_fg_color = function() make_ansi_style("#008800")
)
expect_snapshot(cat(update_rstudio_color("color me interested")))
})
test_that("cli_abort() captures correct call and backtrace", {
skip_on_cran()
rlang::local_options(
rlang_trace_top_env = environment(),
rlang_trace_format_srcrefs = FALSE
)
f <- function() g()
g <- function() h()
h <- function() cli::cli_abort("foo")
expect_snapshot({
print(expect_error(f()))
}, variant = paste0("rlang-", packageVersion("rlang")))
classed_stop <- function(message, env = parent.frame()) {
cli::cli_abort(
message,
.envir = env,
class = "cli_my_class"
)
}
h <- function(x) {
if (!length(x)) {
classed_stop("{.arg x} can't be empty.")
}
}
f <- function(x) g(x)
g <- function(x) h(x)
expect_snapshot({
print(expect_error(f(list())))
}, variant = paste0("rlang-", packageVersion("rlang")))
})
test_that("cli_abort(.internal = TRUE) reports the correct function (r-lib/rlang#1386)", {
skip_on_cran()
fn <- function() {
cli::cli_abort("Message.", .internal = TRUE)
}
environment(fn) <- rlang::ns_env("base")
# Should mention an internal error in the `base` package
expect_snapshot({
(expect_error(fn()))
}, variant = paste0("rlang-", packageVersion("rlang")))
})
|