Skip to content

Commit

Permalink
fix: adds one more test that executes the code inside teal data
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Oct 28, 2024
1 parent b06054d commit 1078e02
Showing 1 changed file with 81 additions and 47 deletions.
128 changes: 81 additions & 47 deletions tests/testthat/test-module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -2323,54 +2323,88 @@ testthat::describe("Datanames with special symbols", {
})
})

testthat::test_that("Show R Code is reproducible with a function defined in teal_data", {
shiny::testServer(
app = srv_teal,
args = list(
id = "test",
data = reactive(within(teal.data::teal_data(), {
fun <- function(x) {
y <- x + 1
y + 3
}
})),
modules = modules(module("module_1", server = function(id, data) data))
), ,
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
session$flushReact()

# Need to evaluate characters to preserve indentation
fun_env <- new.env(parent = .GlobalEnv)
eval(
parse(
text = paste(
sep = "\n",
testthat::describe("teal.data code with a function defined", {
testthat::it("is fully reproducible", {
shiny::testServer(
app = srv_teal,
args = list(
id = "test",
data = reactive(within(teal.data::teal_data(), {
fun <- function(x) {
y <- x + 1
y + 3
}
})),
modules = modules(module("module_1", server = function(id, data) data))
), ,
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
session$flushReact()

# Need to evaluate characters to preserve indentation
local_env <- new.env(parent = .GlobalEnv)
dat <- modules_output$module_1()()

eval(
parse(text = teal.code::get_code(dat)),
envir = local_env
)

testthat::expect_identical(local_env$fun(1), 5)
testthat::expect_identical(local_env$fun(1), dat[["fun"]](1))
}
)
})

testthat::it("has the correct code (with hash)", {
shiny::testServer(
app = srv_teal,
args = list(
id = "test",
data = reactive(within(teal.data::teal_data(), {
fun <- function(x) {
y <- x + 1
y + 3
}
})),
modules = modules(module("module_1", server = function(id, data) data))
), ,
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
session$flushReact()

# Need to evaluate characters to preserve indentation
local_env <- new.env(parent = .GlobalEnv)
eval(
parse(
text = paste(
sep = "\n",
"fun <- function(x) {",
" y <- x + 1",
" y + 3",
"}"
)
),
envir = local_env
)
local(hash <- rlang::hash(deparse1(fun)), envir = local_env)

testthat::expect_setequal(
trimws(strsplit(
x = teal.code::get_code(modules_output$module_1()()),
split = "\n"
)[[1]]),
c(
"fun <- function(x) {",
" y <- x + 1",
" y + 3",
"}"
"y <- x + 1",
"y + 3",
"}",
sprintf("stopifnot(rlang::hash(deparse1(fun)) == \"%s\")", local_env$hash),
".raw_data <- list2env(list(fun = fun))",
"lockEnvironment(.raw_data)"
)
),
envir = fun_env
)
local(hash <- rlang::hash(deparse1(fun_env$fun)), envir = fun_env)

testthat::expect_setequal(
trimws(strsplit(
x = teal.code::get_code(modules_output$module_1()()),
split = "\n"
)[[1]]),
c(
"fun <- function(x) {",
"y <- x + 1",
"y + 3",
"}",
sprintf("stopifnot(rlang::hash(deparse1(fun)) == \"%s\")", fun_env$hash),
".raw_data <- list2env(list(fun = fun))",
"lockEnvironment(.raw_data)"
)
)
}
)
}
)
})
})

0 comments on commit 1078e02

Please sign in to comment.