From 618b3caebe2804b29e92ef5d5e79a88b6562ae13 Mon Sep 17 00:00:00 2001 From: b_falquet Date: Fri, 4 Oct 2024 11:25:33 +0200 Subject: [PATCH] add test for unwrap_layout --- R/unwrap.R | 4 +-- tests/testthat/test-unwrap.R | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/test-unwrap.R diff --git a/R/unwrap.R b/R/unwrap.R index 54c41769d..e4c83c596 100644 --- a/R/unwrap.R +++ b/R/unwrap.R @@ -5,9 +5,9 @@ h_unwrap_layout <- function(x, pattern) { assert_string(pattern) # If x is a list or a call, apply the function on each element - if (class(x) %in% c("list", "call", "<-", "if")) { + if (inherits(x, c("list", "call", "<-", "if"))) { lapply(x, \(x) h_unwrap_layout(x, pattern)) - } else if (class(x) == "name") { + } else if (is(x, "name")) { # Return if name match pattern. if (grepl(pattern, x)) { res <- list(x) diff --git a/tests/testthat/test-unwrap.R b/tests/testthat/test-unwrap.R new file mode 100644 index 000000000..f988aed4f --- /dev/null +++ b/tests/testthat/test-unwrap.R @@ -0,0 +1,51 @@ +test_that("unwrap_layout works as expected with standard chevron_t main function", { + res <- capture.output(unwrap_layout(aet01_main)) + expect_snapshot(cat(paste(res, collapse = "\n"))) +}) + +test_that("unwrap_layout works as expected with standard chevron_g main function", { + res <- capture.output(unwrap_layout(mng01_main)) + expect_silent(cat(paste(res, collapse = "\n"))) +}) + +test_that("unwrap_layout works as expected with standard chevron_l main function", { + res <- capture.output(unwrap_layout(ael01_nollt_main)) + expect_silent(cat(paste(res, collapse = "\n"))) +}) + +test_that("unwrap_layout works as expected with a custom function without layout function", { + foo <- function(adam_db, ...) { + lyt <- basic_table() %>% + split_cols_by("ARM") %>% + analyze("AAGE", afun = function(x) { + list( + "mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"), + "range" = diff(range(x)) + ) + }) + + tbl <- build_table(lyt, adam_db$adsl) + } + res <- capture.output(unwrap_layout(foo)) + expect_silent(cat(paste(res, collapse = "\n"))) +}) + +test_that("unwrap_layout works as expected with a custom function with layout function", { + custom_lyt <- function() { + basic_table() %>% + split_cols_by("ARM") %>% + analyze("AAGE", afun = function(x) { + list( + "mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"), + "range" = diff(range(x)) + ) + }) + } + + foo <- function(adam_db, ...) { + lyt <- custom_lyt() + tbl <- build_table(lyt, adam_db$adsl) + } + res <- capture.output(unwrap_layout(foo)) + expect_silent(cat(paste(res, collapse = "\n"))) +})