Skip to content

Commit

Permalink
Add mock-based test of RStudio API
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Jan 22, 2024
1 parent a9287be commit 3ee887c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/testthat/test-interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@ test_that("does not unnecessarily suggest changing working directory", {
))
})

test_that("rstudio API is not called when unavailable", {
testthat::skip_if_not_installed("mockery")
mock_rstudio_available <- mockery::mock(FALSE, cycle=TRUE)
mock_rstudio_context <- mockery::mock()
mockery::stub(
rstudio_get_current_active_editor_path,
"is_testing",
mockery::mock(FALSE))
mockery::stub(
rstudio_get_current_active_editor_path,
"rstudioapi::isAvailable",
mock_rstudio_available)
mockery::stub(
rstudio_get_current_active_editor_path,
"rstudioapi::getSourceEditorContext",
mockery::mock(FALSE))
expect_null(rstudio_get_current_active_editor_path())
mockery::expect_called(mock_rstudio_available, 1)
mockery::expect_called(mock_rstudio_context, 0)
})

test_that("rstudio API is used to find current editor path", {
mockery::stub(
rstudio_get_current_active_editor_path,
"is_testing",
mockery::mock(FALSE))
mockery::stub(
rstudio_get_current_active_editor_path,
"rstudioapi::isAvailable",
mockery::mock(TRUE))
mockery::stub(
rstudio_get_current_active_editor_path,
"rstudioapi::getSourceEditorContext",
mockery::mock(list(path="/path/to/file")))
expect_equal(rstudio_get_current_active_editor_path(), "/path/to/file")
})

test_that("can validate interactive parameters", {
mock_readline <- mockery::mock("TRUE", "100", "1.23", '"string"')
mockery::stub(get_parameter_interactive, "readline", mock_readline)
Expand Down

0 comments on commit 3ee887c

Please sign in to comment.