Skip to content

Commit

Permalink
add test for unwrap_layout
Browse files Browse the repository at this point in the history
  • Loading branch information
BFalquet committed Oct 4, 2024
1 parent c9dc9f9 commit 618b3ca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/unwrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions tests/testthat/test-unwrap.R
Original file line number Diff line number Diff line change
@@ -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")))
})

0 comments on commit 618b3ca

Please sign in to comment.