From 03dc9a011a17299ea0824ca39ef95b9a29b98b26 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 15 Nov 2024 23:02:51 +0000 Subject: [PATCH] feat: #466 inform function skeleton and tests --- R/lifecycle_admiral.R | 23 +++++++++++ tests/testthat/_snaps/lifecycle_admiral.md | 10 +++++ tests/testthat/test-lifecycle_admiral.R | 44 ++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 R/lifecycle_admiral.R create mode 100644 tests/testthat/_snaps/lifecycle_admiral.md create mode 100644 tests/testthat/test-lifecycle_admiral.R diff --git a/R/lifecycle_admiral.R b/R/lifecycle_admiral.R new file mode 100644 index 00000000..5e208041 --- /dev/null +++ b/R/lifecycle_admiral.R @@ -0,0 +1,23 @@ +#' Deprecation with Soft Message +#' +#' Wrapper around `lifecycle::deprecate_soft()` +#' +#' @param ... +#' +#' @return Return value of the expression +#' +#' @keywords messages +#' @family messages +#' +#' @details +#' +#' @export +deprecate_inform <- function(...) { + tryCatch( + lifecycle::deprecate_soft(...), + warning = \(w) { + message(conditionMessage(w)) + tryInvokeRestart("muffleWarning") + } + ) +} diff --git a/tests/testthat/_snaps/lifecycle_admiral.md b/tests/testthat/_snaps/lifecycle_admiral.md new file mode 100644 index 00000000..71ef7b8f --- /dev/null +++ b/tests/testthat/_snaps/lifecycle_admiral.md @@ -0,0 +1,10 @@ +# lifecycle_admiral Test 1: Message is sent to user + + Code + example_fun(data) + +# lifecycle_admiral Test 2: Nicer message is sent to user + + Code + example_fun(data) + diff --git a/tests/testthat/test-lifecycle_admiral.R b/tests/testthat/test-lifecycle_admiral.R new file mode 100644 index 00000000..dcd42899 --- /dev/null +++ b/tests/testthat/test-lifecycle_admiral.R @@ -0,0 +1,44 @@ +## Test 1: Message is sent to user ---- +test_that("lifecycle_admiral Test 1: Message is sent to user", { + example_fun <- function(dataset) { + deprecate_inform("1.0.0", "example_fun()", "example_fun2()") + assert_data_frame(dataset, required_vars = exprs(STUDYID, USUBJID)) + } + + data <- dplyr::tribble( + ~STUDYID, ~USUBJID, + "xyz", 123, + "xyz", 456 + ) + + withr::local_options(lifecycle_verbosity = "quiet") + expect_snapshot( + example_fun(data), + ) + +}) + +## Test 2: Nicer message is sent to user ---- +test_that("lifecycle_admiral Test 2: Nicer message is sent to user", { + example_fun <- function(dataset) { + deprecate_inform("1.0.0", "example_fun()", "example_fun2()", + details = c( + x = "Highly recommended to move to the suggested function", + i = "See admiral's deprecation guidance: + https://pharmaverse.github.io/admiraldev/dev/articles/programming_strategy.html#deprecation") #nolint + ) + assert_data_frame(dataset, required_vars = exprs(STUDYID, USUBJID)) + } + + data <- dplyr::tribble( + ~STUDYID, ~USUBJID, + "xyz", 123, + "xyz", 456 + ) + + withr::local_options(lifecycle_verbosity = "quiet") + expect_snapshot( + example_fun(data), + ) + +})