Skip to content

Commit

Permalink
test .init_addin()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Nov 22, 2024
1 parent 4734dc8 commit f3455dd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/init-addin.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
try_fetch(
pal_fn <- env_get(pal_env(), pal_fn_name),
error = function(e) {
cli::cli_abort("Unable to locate the requested pal.")
cli::cli_abort("Unable to locate the requested pal.", call = NULL)
}
)

Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/_snaps/init-addin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# errors informatively on .pal_app error

Code
.init_addin()
Condition
Error:
! some error

# errors informatively with missing binding

Code
.init_addin()
Condition
Error:
! Unable to locate the requested pal.

# .init_addin executes found binding

Code
res <- .init_addin()
Message
howdy!

34 changes: 34 additions & 0 deletions tests/testthat/test-init-addin.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
test_that(".init_addin exits early with missing selection", {
testthat::local_mocked_bindings(.pal_app = function() NULL)
expect_null(.init_addin())
})

test_that(".init_addin exits early with empty selection", {
testthat::local_mocked_bindings(.pal_app = function() ".pal_rs_")
expect_null(.init_addin())
})

test_that("errors informatively on .pal_app error", {
testthat::local_mocked_bindings(
.pal_app = function() cli::cli_abort("some error")
)
expect_snapshot(.init_addin(), error = TRUE)
})

test_that("errors informatively with missing binding", {
testthat::local_mocked_bindings(
.pal_app = function() ".pal_rs_some_fn",
env_get = function(...) cli::cli_abort("nope.")
)
expect_snapshot(.init_addin(), error = TRUE)
})

test_that(".init_addin executes found binding", {
mock_fn <- function() cli::cli_inform("howdy!")
testthat::local_mocked_bindings(
.pal_app = function() ".pal_rs_mock",
env_get = function(...) mock_fn
)
expect_snapshot(res <- .init_addin())
expect_null(res)
})

0 comments on commit f3455dd

Please sign in to comment.