Skip to content

Commit

Permalink
tests: combine consecutive mocking calls
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Oct 7, 2024
1 parent 18e2858 commit fa19e8f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
36 changes: 22 additions & 14 deletions tests/testthat/test-ansi-hyperlink.R
Original file line number Diff line number Diff line change
Expand Up @@ -290,35 +290,39 @@ test_that("ansi_has_hyperlink_support", {
expect_false(ansi_has_hyperlink_support())

# are we in rstudio with support?
local_mocked_bindings(num_ansi_colors = function() 257L)
local_mocked_bindings(
num_ansi_colors = function() 257L,
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()
local_mocked_bindings(num_ansi_colors = function() 256L)

local_mocked_bindings(isatty = function(...) FALSE)
local_mocked_bindings(
num_ansi_colors = function() 256L,
isatty = function(...) FALSE
)
expect_false(ansi_has_hyperlink_support())
})

test_that("ansi_has_hyperlink_support 3", {
local_clean_cli_context()
local_mocked_bindings(num_ansi_colors = function() 256L)

local_mocked_bindings(isatty = function(...) TRUE)
local_mocked_bindings(is_windows = function() TRUE)
local_mocked_bindings(
num_ansi_colors = function() 256L,
isatty = function(...) TRUE,
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()
local_mocked_bindings(num_ansi_colors = function() 256L)
local_mocked_bindings(isatty = function(...) TRUE)
local_mocked_bindings(
num_ansi_colors = function() 256L,
isatty = function(...) TRUE
)

withr::local_envvar("CI" = "true")
expect_false(ansi_has_hyperlink_support())
Expand All @@ -329,8 +333,10 @@ test_that("ansi_has_hyperlink_support 4", {

test_that("ansi_has_hyperlink_support 5", {
local_clean_cli_context()
local_mocked_bindings(num_ansi_colors = function() 256L)
local_mocked_bindings(isatty = function(...) TRUE)
local_mocked_bindings(
num_ansi_colors = function() 256L,
isatty = function(...) TRUE
)

withr::local_envvar(
TERM_PROGRAM = "iTerm.app",
Expand All @@ -341,8 +347,10 @@ test_that("ansi_has_hyperlink_support 5", {

test_that("ansi_has_hyperlink_support 5", {
local_clean_cli_context()
local_mocked_bindings(num_ansi_colors = function() 256L)
local_mocked_bindings(isatty = function(...) TRUE)
local_mocked_bindings(
num_ansi_colors = function() 256L,
isatty = function(...) TRUE
)

withr::local_envvar(VTE_VERSION = "0.51.1")
expect_true(ansi_has_hyperlink_support())
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-format-conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ test_that_cli("format_message", {

test_that_cli(configs = "ansi", "color in RStudio", {
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_console", num_colors = 256)
)
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({
Expand Down
50 changes: 30 additions & 20 deletions tests/testthat/test-num-ansi-colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ test_that("cli.default_num_colors #2", {
cli.default_num_colors = NULL
)

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

expect_equal(num_ansi_colors(), 8L)

Expand All @@ -75,8 +77,10 @@ test_that("cli.default_num_colors #4", {
# Unix emacs with color
withr::local_envvar(COLORTERM = NA_character_)

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

withr::local_options(cli.default_num_colors = NULL)

Expand All @@ -91,12 +95,12 @@ test_that("cli.default_num_colors #5", {
# rstudio terminal on Windows
withr::local_envvar(COLORTERM = NA_character_)

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

withr::local_options(cli.default_num_colors = NULL)
expect_equal(detect_tty_colors(), 8L)
Expand All @@ -111,12 +115,12 @@ test_that("cli.default_num_colors #6", {
withr::local_envvar(COLORTERM = NA_character_)
withr::local_options(cli.default_num_colors = NULL)

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

local_mocked_bindings(win10_build = function() 14931)
Expand All @@ -134,8 +138,10 @@ test_that("cli.default_num_colors #7", {
ConEmuANSI = "ON"
)
withr::local_options(cli.default_num_colors = NULL)
local_mocked_bindings(os_type = function() "windows")
local_mocked_bindings(win10_build = function() 1)
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)
Expand All @@ -150,9 +156,11 @@ test_that("cli.default_num_colors #8", {
TERM = "xterm"
)

local_mocked_bindings(os_type = function() "unix")
local_mocked_bindings(is_emacs_with_color = function() FALSE)
local_mocked_bindings(system = function(...) "8")
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)
Expand All @@ -167,8 +175,10 @@ test_that("ESS_BACKGROUND_MODE", {
ESS_BACKGROUND_MODE = NA_character_
)

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

expect_false(detect_dark_theme("auto"))

Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-rlang-errors.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ test_that("cli_abort width in RStudio", {

test_that_cli(configs = "ansi", "color in RStudio", {
local_mocked_bindings(
rstudio_detect = function() list(type = "rstudio_console", num_colors = 256)
)
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({
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ test_that("is_latex_output", {
local_mocked_bindings(loadedNamespaces = function() "foobar")
expect_false(is_latex_output())

local_mocked_bindings(loadedNamespaces = function() "knitr")
local_mocked_bindings(
loadedNamespaces = function() "knitr",
get = function(x, ...) {
if (x == "is_latex_output") {
function() TRUE
Expand Down

0 comments on commit fa19e8f

Please sign in to comment.