Skip to content

Commit

Permalink
tests: migrate to testthat mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Oct 5, 2024
1 parent ad03a41 commit 2cb67e9
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 24 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Suggests:
decor,
htmlwidgets,
knitr,
mockery,
rmarkdown,
rstudioapi,
testthat (>= 3.2.0)
Expand Down
1 change: 1 addition & 0 deletions R/mocks.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
loadedNamespaces <- NULL
6 changes: 4 additions & 2 deletions tests/testthat/test-embed.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ test_that("speed", {
})

test_that("subprocess fails", {
mockery::stub(asciicast_start_process, "processx::poll", list("timeout"))
local_mocked_bindings(
poll = function(...) list("timeout"), .package = "processx"
)
expect_error(
asciicast_start_process(),
"subprocess did not connect back"
Expand Down Expand Up @@ -129,7 +131,7 @@ test_that("adjust_typing_speed", {
})

test_that("find_rem error", {
mockery::stub(find_rem, "get_embedded", "")
local_mocked_bindings(get_embedded = function() "")
expect_error(
find_rem(),
"Cannot find embedded R executable"
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-frames.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that("load_frames", {
sapply(frames$frames[[14]][[2]]$screen$lines, function(x) x[[1]][[1]])
)

mockery::stub(load_frames, "system.file", "")
local_mocked_bindings(system.file = function(...) "")
expect_error(
load_frames(cast),
"cannot find 'load-cast.js'"
Expand Down
16 changes: 7 additions & 9 deletions tests/testthat/test-gif.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,32 @@ test_that("write_gif", {
all(magick::image_info(mgif)$format == "GIF")
)

mockery::stub(write_gif, "is_rstudio", TRUE)
local_mocked_bindings(is_rstudio = function() TRUE)
rs <- NULL
mockery::stub(write_gif, "view_image_in_rstudio", function(path) rs <<- path)
local_mocked_bindings(view_image_in_rstudio = function(path) rs <<- path)
suppressMessages(
write_gif(cast, gif, show = TRUE)
)
expect_false(is.null(rs))

mockery::stub(write_gif, "is_rstudio", FALSE)
local_mocked_bindings(is_rstudio = function() FALSE)
rs <- NULL
mockery::stub(write_gif, "image_display", function(anim) rs <<- TRUE)
local_mocked_bindings(image_display = function(anim) rs <<- TRUE)
suppressMessages(
write_gif(cast, gif, show = TRUE)
)
expect_true(rs)
})

test_that("write_gif errors", {
mockery::stub(write_gif, "find_phantom", NULL)
local_mocked_bindings(find_phantom = function() NULL)
expect_error(
suppressMessages(write_gif()),
"No phantom.js, exiting"
)

mockery::stub(
write_gif,
"find_phantom",
asNamespace("processx")$get_tool("px")
local_mocked_bindings(
find_phantom = function() asNamespace("processx")$get_tool("px")
)
cast <- record(textConnection("1+1\n"))
gif <- tempfile(fileext = ".gif")
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-install-phantomjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ test_that("install_phantomjs", {
})

test_that("install_phantomjs errors", {
mockery::stub(install_phantomjs, "is_windows", FALSE)
mockery::stub(install_phantomjs, "is_macos", FALSE)
mockery::stub(install_phantomjs, "is_linux", FALSE)
local_mocked_bindings(is_windows = function() FALSE)
local_mocked_bindings(is_macos = function() FALSE)
local_mocked_bindings(is_linux = function() FALSE)
expect_message(
install_phantomjs(),
"this platform is not supported"
Expand Down
10 changes: 4 additions & 6 deletions tests/testthat/test-rstudio.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
test_that("is_rstudio", {
expect_false(is_rstudio())

mockery::stub(is_rstudio, "loadedNamespaces", "rstudioapi")
mockery::stub(is_rstudio, "rstudioapi::isAvailable", TRUE)
local_mocked_bindings(loadedNamespaces = function() "rstudioapi")
local_mocked_bindings(isAvailable = function() TRUE, .package = "rstudioapi")
expect_true(is_rstudio())
})

Expand All @@ -14,10 +14,8 @@ test_that("view_image_in_rstudio", {
path <- NULL
on.exit(unlink(path), add = TRUE)

mockery::stub(
view_image_in_rstudio,
"rstudioapi::viewer",
function(x) path <<- x
local_mocked_bindings(
viewer = function(x) path <<- x, .package = "rstudioapi"
)

view_image_in_rstudio(tempdir())
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-svg.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test_that("write_svg errors", {
"Unknown theme"
)

mockery::stub(check_svg_support, "is_svg_supported", FALSE)
local_mocked_bindings(is_svg_supported = function() FALSE)
expect_error(
check_svg_support(),
"needs a more recent Node library"
Expand All @@ -68,7 +68,7 @@ test_that("write_svg errors", {
test_that("play", {
path <- NULL
on.exit(unlink(path), add = TRUE)
mockery::stub(play, "play_svg", function(x, ...) path <<- x)
local_mocked_bindings(play_svg = function(x, ...) path <<- x)

hello <- system.file(package = "asciicast", "examples", "hello.R")
cast <- record(hello, interactive = FALSE)
Expand Down

0 comments on commit 2cb67e9

Please sign in to comment.