From f3455dd11978fb9a19d2b530755086d7776db49c Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Fri, 22 Nov 2024 15:35:26 -0600 Subject: [PATCH] test `.init_addin()` --- R/init-addin.R | 2 +- tests/testthat/_snaps/init-addin.md | 23 +++++++++++++++++++ tests/testthat/test-init-addin.R | 34 +++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/_snaps/init-addin.md create mode 100644 tests/testthat/test-init-addin.R diff --git a/R/init-addin.R b/R/init-addin.R index ae382bb..fecc440 100644 --- a/R/init-addin.R +++ b/R/init-addin.R @@ -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) } ) diff --git a/tests/testthat/_snaps/init-addin.md b/tests/testthat/_snaps/init-addin.md new file mode 100644 index 0000000..6039f6d --- /dev/null +++ b/tests/testthat/_snaps/init-addin.md @@ -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! + diff --git a/tests/testthat/test-init-addin.R b/tests/testthat/test-init-addin.R new file mode 100644 index 0000000..9e4a748 --- /dev/null +++ b/tests/testthat/test-init-addin.R @@ -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) +})