From 62f1e8c86a06014273e3ecdeb3babc3d8ee73e5a Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Fri, 4 Oct 2024 18:38:13 +0200 Subject: [PATCH] tests: move to testthat mocking --- DESCRIPTION | 3 +- R/mocks.R | 10 +++ tests/testthat/test-ansi-hyperlink.R | 31 +++---- tests/testthat/test-format-conditions.R | 32 +++---- tests/testthat/test-links.R | 2 +- tests/testthat/test-num-ansi-colors.R | 83 +++++++++---------- tests/testthat/test-prettycode.R | 20 ++--- tests/testthat/test-progress-along.R | 2 +- tests/testthat/test-progress-handler-logger.R | 2 +- tests/testthat/test-progress-handler-say.R | 2 +- tests/testthat/test-progress-handlers.R | 4 +- tests/testthat/test-progress-utils.R | 2 +- tests/testthat/test-progress-variables.R | 58 ++++++------- tests/testthat/test-rlang-errors.R | 34 +++----- tests/testthat/test-utils.R | 25 +++--- 15 files changed, 140 insertions(+), 170 deletions(-) create mode 100644 R/mocks.R diff --git a/DESCRIPTION b/DESCRIPTION index c9a722517..16ec2c8ae 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,14 +33,13 @@ Suggests: htmlwidgets, knitr, methods, - mockery, processx, ps (>= 1.3.4.9000), rlang (>= 1.0.2.9003), rmarkdown, rprojroot, rstudioapi, - testthat, + testthat (>= 3.2.0), tibble, whoami, withr diff --git a/R/mocks.R b/R/mocks.R new file mode 100644 index 000000000..8a13f2756 --- /dev/null +++ b/R/mocks.R @@ -0,0 +1,10 @@ +.Call <- NULL +Sys.time <- NULL +commandArgs <- NULL +get <- NULL +getRversion <- NULL +isatty <- NULL +l10n_info <- NULL +loadedNamespaces <- NULL +system <- NULL +system2 <- NULL diff --git a/tests/testthat/test-ansi-hyperlink.R b/tests/testthat/test-ansi-hyperlink.R index ba28798a9..8e74e7850 100644 --- a/tests/testthat/test-ansi-hyperlink.R +++ b/tests/testthat/test-ansi-hyperlink.R @@ -286,38 +286,39 @@ test_that("ansi_has_hyperlink_support", { ) # if no ansi support, then no - mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L) + local_mocked_bindings(num_ansi_colors = function() 256L) expect_false(ansi_has_hyperlink_support()) # are we in rstudio with support? - mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L) - mockery::stub(ansi_has_hyperlink_support, "rstudio_detect", - list(type = "rstudio_console", hyperlink = TRUE)) + local_mocked_bindings(num_ansi_colors = function() 257L) + local_mocked_bindings( + rstudio_detect = function() list(type = "rstudio_console", hyperlink = TRUE) + ) expect_true(ansi_has_hyperlink_support()) }) test_that("ansi_has_hyperlink_support 2", { local_clean_cli_context() - mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L) + local_mocked_bindings(num_ansi_colors = function() 256L) - mockery::stub(ansi_has_hyperlink_support, "isatty", FALSE) + local_mocked_bindings(isatty = function(...) FALSE) expect_false(ansi_has_hyperlink_support()) }) test_that("ansi_has_hyperlink_support 3", { local_clean_cli_context() - mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L) + local_mocked_bindings(num_ansi_colors = function() 256L) - mockery::stub(ansi_has_hyperlink_support, "isatty", TRUE) - mockery::stub(ansi_has_hyperlink_support, "is_windows", TRUE) + local_mocked_bindings(isatty = function(...) TRUE) + local_mocked_bindings(is_windows = function() TRUE) withr::local_envvar(WT_SESSION = "4c464723-f51f-4612-83f7-31e1c75abd83") expect_true(ansi_has_hyperlink_support()) }) test_that("ansi_has_hyperlink_support 4", { local_clean_cli_context() - mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L) - mockery::stub(ansi_has_hyperlink_support, "isatty", TRUE) + local_mocked_bindings(num_ansi_colors = function() 256L) + local_mocked_bindings(isatty = function(...) TRUE) withr::local_envvar("CI" = "true") expect_false(ansi_has_hyperlink_support()) @@ -328,8 +329,8 @@ test_that("ansi_has_hyperlink_support 4", { test_that("ansi_has_hyperlink_support 5", { local_clean_cli_context() - mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L) - mockery::stub(ansi_has_hyperlink_support, "isatty", TRUE) + local_mocked_bindings(num_ansi_colors = function() 256L) + local_mocked_bindings(isatty = function(...) TRUE) withr::local_envvar( TERM_PROGRAM = "iTerm.app", @@ -340,8 +341,8 @@ test_that("ansi_has_hyperlink_support 5", { test_that("ansi_has_hyperlink_support 5", { local_clean_cli_context() - mockery::stub(ansi_has_hyperlink_support, "num_ansi_colors", 256L) - mockery::stub(ansi_has_hyperlink_support, "isatty", TRUE) + local_mocked_bindings(num_ansi_colors = function() 256L) + local_mocked_bindings(isatty = function(...) TRUE) withr::local_envvar(VTE_VERSION = "0.51.1") expect_true(ansi_has_hyperlink_support()) diff --git a/tests/testthat/test-format-conditions.R b/tests/testthat/test-format-conditions.R index 7515ceb99..c62a01786 100644 --- a/tests/testthat/test-format-conditions.R +++ b/tests/testthat/test-format-conditions.R @@ -60,41 +60,29 @@ test_that_cli("format_message", { }) test_that_cli(configs = "ansi", "color in RStudio", { - mockery::stub( - get_rstudio_fg_color0, - "rstudio_detect", - list(type = "rstudio_console", num_colors = 256) + local_mocked_bindings( + rstudio_detect = function() list(type = "rstudio_console", num_colors = 256) ) - mockery::stub( - get_rstudio_fg_color0, - "get_rstudio_theme", - list(foreground = "rgb(0, 0, 0)") + local_mocked_bindings( + get_rstudio_theme = function() list(foreground = "rgb(0, 0, 0)") ) expect_snapshot({ col <- get_rstudio_fg_color0() cat(col("this is the new color")) }) - mockery::stub( - get_rstudio_fg_color0, - "get_rstudio_theme", - list() - ) + local_mocked_bindings(get_rstudio_theme = function() list()) expect_null(get_rstudio_fg_color0()) - mockery::stub( - get_rstudio_fg_color0, - "rstudio_detect", - list(type = "rstudio_console", num_colors = 1) - ) + 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", { - mockery::stub( - update_rstudio_color, - "get_rstudio_fg_color", - function() make_ansi_style("#008800") + local_mocked_bindings( + get_rstudio_fg_color = function() make_ansi_style("#008800") ) expect_snapshot(cat(update_rstudio_color("color me interested"))) }) diff --git a/tests/testthat/test-links.R b/tests/testthat/test-links.R index 94dc92e5f..3b52fb26a 100644 --- a/tests/testthat/test-links.R +++ b/tests/testthat/test-links.R @@ -105,7 +105,7 @@ test_that_cli(configs = c("plain", "fancy"), links = c("all", "none"), cli_text("{.file {paths}}") }, transform = function(x) sanitize_home(sanitize_wd(x))) - mockery::stub(abs_path1, "is_windows", TRUE) + local_mocked_bindings(is_windows = function() TRUE) expect_equal( abs_path1("c:/foo/bar"), "file://c:/foo/bar" diff --git a/tests/testthat/test-num-ansi-colors.R b/tests/testthat/test-num-ansi-colors.R index 961e521a5..289742421 100644 --- a/tests/testthat/test-num-ansi-colors.R +++ b/tests/testthat/test-num-ansi-colors.R @@ -1,21 +1,20 @@ test_that("win10_build works for different osVersion", { - mockery::stub( - win10_build, "utils::sessionInfo", - list(running = NULL) - ) - expect_identical(win10_build(), 0L) - - mockery::stub( - win10_build, "utils::sessionInfo", - list(running = "Debian GNU/Linux 11 (bullseye)") - ) - expect_identical(win10_build(), 0L) - - mockery::stub( - win10_build, "utils::sessionInfo", - list(running = "Windows 10 x64 (build 16299)") - ) - expect_identical(win10_build(), 16299L) + 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", { @@ -49,9 +48,9 @@ test_that("cli.default_num_colors #2", { cli.default_num_colors = NULL ) - mockery::stub(num_ansi_colors, "os_type", "windows") - mockery::stub(num_ansi_colors, "commandArgs", "--ess") - mockery::stub(num_ansi_colors, "is_emacs_with_color", TRUE) + local_mocked_bindings(os_type = function() "windows") + local_mocked_bindings(commandArgs = function() "--ess") + local_mocked_bindings(is_emacs_with_color = function() TRUE) expect_equal(num_ansi_colors(), 8L) @@ -76,8 +75,8 @@ test_that("cli.default_num_colors #4", { # Unix emacs with color withr::local_envvar(COLORTERM = NA_character_) - mockery::stub(detect_tty_colors, "os_type", "unix") - mockery::stub(detect_tty_colors, "is_emacs_with_color", TRUE) + local_mocked_bindings(os_type = function() "unix") + local_mocked_bindings(is_emacs_with_color = function() TRUE) withr::local_options(cli.default_num_colors = NULL) @@ -92,14 +91,12 @@ test_that("cli.default_num_colors #5", { # rstudio terminal on Windows withr::local_envvar(COLORTERM = NA_character_) - mockery::stub(detect_tty_colors, "os_type", "windows") - mockery::stub(detect_tty_colors, "win10_build", 10586) - mockery::stub( - detect_tty_colors, - "rstudio_detect", - list(type = "rstudio_terminal") + local_mocked_bindings(os_type = function() "windows") + local_mocked_bindings(win10_build = function() 10586) + local_mocked_bindings( + rstudio_detect = function() list(type = "rstudio_terminal") ) - mockery::stub(detect_tty_colors, "system2", TRUE) + local_mocked_bindings(system2 = function(...) TRUE) withr::local_options(cli.default_num_colors = NULL) expect_equal(detect_tty_colors(), 8L) @@ -114,17 +111,15 @@ test_that("cli.default_num_colors #6", { withr::local_envvar(COLORTERM = NA_character_) withr::local_options(cli.default_num_colors = NULL) - mockery::stub(detect_tty_colors, "os_type", "windows") - mockery::stub(detect_tty_colors, "win10_build", 10586) - mockery::stub( - detect_tty_colors, - "rstudio_detect", - list(type = "not_rstudio") + local_mocked_bindings(os_type = function() "windows") + local_mocked_bindings(win10_build = function() 10586) + local_mocked_bindings( + rstudio_detect = function() list(type = "not_rstudio") ) - mockery::stub(detect_tty_colors, "system2", TRUE) + local_mocked_bindings(system2 = function(...) TRUE) expect_equal(detect_tty_colors(), 256L) - mockery::stub(detect_tty_colors, "win10_build", 14931) + local_mocked_bindings(win10_build = function() 14931) expect_equal(detect_tty_colors(), truecolor) withr::local_options(cli.default_num_colors = 123L) @@ -139,8 +134,8 @@ test_that("cli.default_num_colors #7", { ConEmuANSI = "ON" ) withr::local_options(cli.default_num_colors = NULL) - mockery::stub(detect_tty_colors, "os_type", "windows") - mockery::stub(detect_tty_colors, "win10_build", 1) + local_mocked_bindings(os_type = function() "windows") + local_mocked_bindings(win10_build = function() 1) expect_equal(detect_tty_colors(), 8L) withr::local_options(cli.default_num_colors = 123L) @@ -155,9 +150,9 @@ test_that("cli.default_num_colors #8", { TERM = "xterm" ) - mockery::stub(detect_tty_colors, "os_type", "unix") - mockery::stub(detect_tty_colors, "is_emacs_with_color", FALSE) - mockery::stub(detect_tty_colors, "system", "8") + local_mocked_bindings(os_type = function() "unix") + local_mocked_bindings(is_emacs_with_color = function() FALSE) + local_mocked_bindings(system = function(...) "8") withr::local_options(cli.default_num_colors = NULL) expect_equal(detect_tty_colors(), 256L) @@ -172,8 +167,8 @@ test_that("ESS_BACKGROUND_MODE", { ESS_BACKGROUND_MODE = NA_character_ ) - mockery::stub(detect_dark_theme, "is_iterm", FALSE) - mockery::stub(detect_dark_theme, "is_emacs", TRUE) + local_mocked_bindings(is_iterm = function() FALSE) + local_mocked_bindings(is_emacs = function() TRUE) expect_false(detect_dark_theme("auto")) diff --git a/tests/testthat/test-prettycode.R b/tests/testthat/test-prettycode.R index ed28256d8..79e2c5fb4 100644 --- a/tests/testthat/test-prettycode.R +++ b/tests/testthat/test-prettycode.R @@ -177,16 +177,14 @@ test_that("code themes", { withr::local_options(cli.code_theme_terminal = "solarized_light") expect_equal(code_theme_default()$reserved, "#859900") - mockery::stub( - code_theme_default, - "rstudio_detect", - list(type = "rstudio_console") + local_mocked_bindings( + rstudio_detect = function() list(type = "rstudio_console") ) withr::local_options(cli.code_theme_rstudio = "Xcode") expect_equal(code_theme_default()$reserved, "#C800A4") withr::local_options(cli.code_theme_rstudio = NULL) - mockery::stub(code_theme_default, "code_theme_default_rstudio", "foo") + local_mocked_bindings(code_theme_default_rstudio = function() "foo") expect_equal(code_theme_default(), "foo") }) @@ -199,17 +197,13 @@ test_that("code themes 2", { }) test_that("code_theme_default_rstudio", { - mockery::stub( - code_theme_default_rstudio, - "get_rstudio_theme", - list(editor = "Solarized Dark") + local_mocked_bindings( + get_rstudio_theme = function() list(editor = "Solarized Dark") ) expect_equal(code_theme_default_rstudio()$reserved, "#859900") - mockery::stub( - code_theme_default_rstudio, - "get_rstudio_theme", - list(editor = "Not really") + local_mocked_bindings( + get_rstudio_theme = function() list(editor = "Not really") ) expect_warning( cth <- code_theme_default_rstudio(), diff --git a/tests/testthat/test-progress-along.R b/tests/testthat/test-progress-along.R index f81ff91b6..4390b1368 100644 --- a/tests/testthat/test-progress-along.R +++ b/tests/testthat/test-progress-along.R @@ -78,7 +78,7 @@ test_that("old R is just seq_along", { # It is tricky to check that we get seq_along(), because # identical(cli_progress_along(1:10), seq_along(1:10)) holds, # so we just check that no progress bar is created. - mockery::stub(cli_progress_along, "getRversion", package_version("3.4.0")) + local_mocked_bindings(getRversion = function() package_version("3.4.0")) snapshot <- names(clienv$progress) it <- cli_progress_along(1:10) expect_identical(snapshot, names(clienv$progress)) diff --git a/tests/testthat/test-progress-handler-logger.R b/tests/testthat/test-progress-handler-logger.R index d2c5b8363..997df7188 100644 --- a/tests/testthat/test-progress-handler-logger.R +++ b/tests/testthat/test-progress-handler-logger.R @@ -4,6 +4,6 @@ test_that("loggerr_out", { bar$id <- "id" bar$current <- 13 bar$total <- 113 - mockery::stub(logger_out, "Sys.time", .POSIXct(1623325865, tz = "CET")) + local_mocked_bindings(Sys.time = function() .POSIXct(1623325865, tz = "CET")) expect_snapshot(logger_out(bar, "updated")) }) diff --git a/tests/testthat/test-progress-handler-say.R b/tests/testthat/test-progress-handler-say.R index 775823247..c2b708951 100644 --- a/tests/testthat/test-progress-handler-say.R +++ b/tests/testthat/test-progress-handler-say.R @@ -17,7 +17,7 @@ test_that("say_out", { test_that("say_update", { withr::local_options(cli.progress_say_frequency = 1e-9) - mockery::stub(say_update, "say_out", function(text) text) + local_mocked_bindings(say_out = function(text) text) bar <- new.env(parent = emptyenv()) bar$current <- 10 bar$total <- NA diff --git a/tests/testthat/test-progress-handlers.R b/tests/testthat/test-progress-handlers.R index abcf3a7ce..db88eeab0 100644 --- a/tests/testthat/test-progress-handlers.R +++ b/tests/testthat/test-progress-handlers.R @@ -32,7 +32,7 @@ test_that("cli_progress_select_handlers #2", { baz = list(), forced = list() ) - mockery::stub(cli_progress_select_handlers, "builtin_handlers", fake) + local_mocked_bindings(builtin_handlers = function() fake) expect_equal(cli_progress_select_handlers(), fake["bar"]) }) @@ -49,7 +49,7 @@ test_that("cli_progress_select_handlers #3", { baz = list(), forced = list() ) - mockery::stub(cli_progress_select_handlers, "builtin_handlers", fake) + local_mocked_bindings(builtin_handlers = function() fake) expect_equal(cli_progress_select_handlers(), fake[c("bar", "forced")]) }) diff --git a/tests/testthat/test-progress-utils.R b/tests/testthat/test-progress-utils.R index de61de3fd..b49416b8c 100644 --- a/tests/testthat/test-progress-utils.R +++ b/tests/testthat/test-progress-utils.R @@ -31,7 +31,7 @@ test_that("should_run_progress_examples", { withr::local_envvar(NOT_CRAN = "true") expect_true(should_run_progress_examples()) - mockery::stub(should_run_progress_examples, "is_rcmd_check", TRUE) + local_mocked_bindings(is_rcmd_check = function() TRUE) expect_false(should_run_progress_examples()) }) diff --git a/tests/testthat/test-progress-variables.R b/tests/testthat/test-progress-variables.R index 7d73df5ea..61bb0b6bb 100644 --- a/tests/testthat/test-progress-variables.R +++ b/tests/testthat/test-progress-variables.R @@ -51,47 +51,47 @@ test_that("pb_current_bytes", { test_that("pb_elapsed", { expect_equal(cli__pb_elapsed(NULL), "") withr::local_options(cli__pb = list(start = 0, speed_time = 1)) - mockery::stub(cli__pb_elapsed, ".Call", 1) + local_mocked_bindings(.Call = function(...) 1) expect_snapshot(cli__pb_elapsed()) - mockery::stub(cli__pb_elapsed, ".Call", 21) + local_mocked_bindings(.Call = function(...) 21) expect_snapshot(cli__pb_elapsed()) - mockery::stub(cli__pb_elapsed, ".Call", 58) + local_mocked_bindings(.Call = function(...) 58) expect_snapshot(cli__pb_elapsed()) - mockery::stub(cli__pb_elapsed, ".Call", 60 * 65) + 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)) - mockery::stub(cli__pb_elapsed_clock, ".Call", 1) + local_mocked_bindings(.Call = function(...) 1) expect_snapshot(cli__pb_elapsed_clock()) - mockery::stub(cli__pb_elapsed_clock, ".Call", 21) + local_mocked_bindings(.Call = function(...) 21) expect_snapshot(cli__pb_elapsed_clock()) - mockery::stub(cli__pb_elapsed_clock, ".Call", 58) + local_mocked_bindings(.Call = function(...) 58) expect_snapshot(cli__pb_elapsed_clock()) - mockery::stub(cli__pb_elapsed_clock, ".Call", 60 * 65) + 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)) - mockery::stub(cli__pb_elapsed_raw, ".Call", 1) + local_mocked_bindings(.Call = function(...) 1) expect_snapshot(cli__pb_elapsed_raw()) - mockery::stub(cli__pb_elapsed_raw, ".Call", 21) + local_mocked_bindings(.Call = function(...) 21) expect_snapshot(cli__pb_elapsed_raw()) - mockery::stub(cli__pb_elapsed_raw, ".Call", 58) + local_mocked_bindings(.Call = function(...) 58) expect_snapshot(cli__pb_elapsed_raw()) - mockery::stub(cli__pb_elapsed_raw, ".Call", 60 * 65) + local_mocked_bindings(.Call = function(...) 60 * 65) expect_snapshot(cli__pb_elapsed_raw()) }) test_that("pb_eta", { expect_equal(cli__pb_eta(NULL), "") - mockery::stub(cli__pb_eta, "cli__pb_eta_raw", NA_real_) + local_mocked_bindings(cli__pb_eta_raw = function(...) NA_real_) expect_snapshot(cli__pb_eta(list())) - mockery::stub(cli__pb_eta, "cli__pb_eta_raw", as.difftime(12, units = "secs")) + local_mocked_bindings(cli__pb_eta_raw = function(...) as.difftime(12, units = "secs")) expect_snapshot(cli__pb_eta(list())) }) @@ -99,7 +99,7 @@ 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_) - mockery::stub(cli__pb_eta_raw, ".Call", 0) + 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)) @@ -114,9 +114,9 @@ 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(), "") - mockery::stub(cli__pb_eta_str, "cli__pb_eta", "?") + local_mocked_bindings(cli__pb_eta = function(...) "?") expect_snapshot(cli__pb_eta_str(list())) - mockery::stub(cli__pb_eta_str, "cli__pb_eta", " 1s") + local_mocked_bindings(cli__pb_eta = function(...) " 1s") expect_snapshot(cli__pb_eta_str(list())) }) @@ -156,19 +156,19 @@ test_that("pb_pid", { test_that("pb_rate", { expect_equal(cli__pb_rate(NULL), "") - mockery::stub(cli__pb_rate, "cli__pb_rate_raw", NaN) + local_mocked_bindings(cli__pb_rate_raw = function(...) NaN) expect_snapshot(cli__pb_rate(list())) - mockery::stub(cli__pb_rate, "cli__pb_rate_raw", Inf) + local_mocked_bindings(cli__pb_rate_raw = function(...) Inf) expect_snapshot(cli__pb_rate(list())) - mockery::stub(cli__pb_rate, "cli__pb_rate_raw", 1/10) + local_mocked_bindings(cli__pb_rate_raw = function(...) 1 / 10) expect_snapshot(cli__pb_rate(list())) - mockery::stub(cli__pb_rate, "cli__pb_rate_raw", 12.4) + 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), "") - mockery::stub(cli__pb_rate_raw, "cli__pb_elapsed_raw", function(...) this) + 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) @@ -180,17 +180,17 @@ test_that("pb_rate_raw", { test_that("pb_rate_bytes", { expect_equal(cli__pb_rate_bytes(NULL), "") - mockery::stub(cli__pb_rate_bytes, "cli__pb_rate_raw", NaN) + local_mocked_bindings(cli__pb_rate_raw = function(...) NaN) expect_snapshot(cli__pb_rate_bytes(list())) - mockery::stub(cli__pb_rate_bytes, "cli__pb_rate_raw", Inf) + local_mocked_bindings(cli__pb_rate_raw = function(...) Inf) expect_snapshot(cli__pb_rate_bytes(list())) - mockery::stub(cli__pb_rate_bytes, "cli__pb_rate_raw", 0) + local_mocked_bindings(cli__pb_rate_raw = function(...) 0) expect_snapshot(cli__pb_rate_bytes(list())) - mockery::stub(cli__pb_rate_bytes, "cli__pb_rate_raw", 1024) + local_mocked_bindings(cli__pb_rate_raw = function(...) 1024) expect_snapshot(cli__pb_rate_bytes(list())) - mockery::stub(cli__pb_rate_bytes, "cli__pb_rate_raw", 1024 * 23) + local_mocked_bindings(cli__pb_rate_raw = function(...) 1024 * 23) expect_snapshot(cli__pb_rate_bytes(list())) - mockery::stub(cli__pb_rate_bytes, "cli__pb_rate_raw", 1024 * 1024 * 23) + local_mocked_bindings(cli__pb_rate_raw = function(...) 1024 * 1024 * 23) expect_snapshot(cli__pb_rate_bytes(list())) }) @@ -215,7 +215,7 @@ test_that("pb_status", { test_that("pb_timestamp", { expect_equal(cli__pb_timestamp(NULL), "") fake <- .POSIXct(1623974954, tz = "GMT") - mockery::stub(cli__pb_timestamp, "Sys.time", fake) + local_mocked_bindings(Sys.time = function() fake) expect_snapshot(cli__pb_timestamp(list())) backup <- mget(c("load_time", "speed_time"), clienv) diff --git a/tests/testthat/test-rlang-errors.R b/tests/testthat/test-rlang-errors.R index ae32d8571..761f849c9 100644 --- a/tests/testthat/test-rlang-errors.R +++ b/tests/testthat/test-rlang-errors.R @@ -78,7 +78,7 @@ test_that_cli("cli_inform", { test_that("cli_abort width in RStudio", { # this is to fix breakage with new testthat withr::local_options(cli.condition_width = getOption("cli.width")) - mockery::stub(cli_abort, "rstudio_detect", list(type = "rstudio_console")) + 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({ @@ -93,41 +93,29 @@ test_that("cli_abort width in RStudio", { }) test_that_cli(configs = "ansi", "color in RStudio", { - mockery::stub( - get_rstudio_fg_color0, - "rstudio_detect", - list(type = "rstudio_console", num_colors = 256) + local_mocked_bindings( + rstudio_detect = function() list(type = "rstudio_console", num_colors = 256) ) - mockery::stub( - get_rstudio_fg_color0, - "get_rstudio_theme", - list(foreground = "rgb(0, 0, 0)") + local_mocked_bindings( + get_rstudio_theme = function() list(foreground = "rgb(0, 0, 0)") ) expect_snapshot({ col <- get_rstudio_fg_color0() cat(col("this is the new color")) }) - mockery::stub( - get_rstudio_fg_color0, - "get_rstudio_theme", - list() - ) + local_mocked_bindings(get_rstudio_theme = function() list()) expect_null(get_rstudio_fg_color0()) - mockery::stub( - get_rstudio_fg_color0, - "rstudio_detect", - list(type = "rstudio_console", num_colors = 1) - ) + 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", { - mockery::stub( - update_rstudio_color, - "get_rstudio_fg_color", - function() make_ansi_style("#008800") + local_mocked_bindings( + get_rstudio_fg_color = function() make_ansi_style("#008800") ) expect_snapshot(cat(update_rstudio_color("color me interested"))) }) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index e6f706c1c..9e273935e 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -58,18 +58,16 @@ test_that("lpad", { test_that("is_utf8_output", { - mockery::stub( - is_utf8_output, "l10n_info", - list(MBCS = TRUE, `UTF-8` = TRUE, `Latin-1` = FALSE) + local_mocked_bindings( + l10n_info = function() list(MBCS = TRUE, `UTF-8` = TRUE, `Latin-1` = FALSE) ) withr::with_options( list(cli.unicode = NULL), expect_true(is_utf8_output()) ) - mockery::stub( - is_utf8_output, "l10n_info", - list(MBCS = FALSE, `UTF-8` = FALSE, `Latin-1` = TRUE) + local_mocked_bindings( + l10n_info = function() list(MBCS = FALSE, `UTF-8` = FALSE, `Latin-1` = TRUE) ) withr::with_options( list(cli.unicode = NULL), @@ -79,13 +77,12 @@ test_that("is_utf8_output", { test_that("is_latex_output", { - mockery::stub(is_latex_output, "loadedNamespaces", "foobar") + local_mocked_bindings(loadedNamespaces = function() "foobar") expect_false(is_latex_output()) - mockery::stub(is_latex_output, "loadedNamespaces", "knitr") - mockery::stub( - is_latex_output, "get", - function(x, ...) { + local_mocked_bindings(loadedNamespaces = function() "knitr") + local_mocked_bindings( + get = function(x, ...) { if (x == "is_latex_output") { function() TRUE } else { @@ -180,10 +177,8 @@ test_that("na.omit", { }) test_that("get_rstudio_theme", { - mockery::stub( - get_rstudio_theme, - "rstudioapi::getThemeInfo", - function(...) warning("just a word") + local_mocked_bindings( + getThemeInfo = function() function(...) warning("just a word"), .package = "rstudioapi" ) expect_silent(get_rstudio_theme()) })