Debian Codegraph @

tests/testthat/test-test-server-scope.R

raw
1 test_that("Variables outside of the module are inaccessible", {
2 module <- local({
3 outside <- 123
4 function(id, x) {
5 y <- x+1
6 moduleServer(id, function(input, output, session) {
7 z <- y+1
8 })
9 }
10 }, envir = new.env(parent = globalenv()))
11
12 testServer(module, args = list(x = 0), {
13 expect_equal(x, 0)
14 expect_equal(y, 1)
15 expect_equal(z, 2)
16 expect_equal(exists("outside"), FALSE)
17 })
18 })
19
20 test_that("Variables outside the testServer() have correct visibility", {
21 module <- local({
22 function(id, x) {
23 moduleServer(id, function(input, output, session) {
24 y <- 1
25 })
26 }
27 }, envir = new.env(parent = globalenv()))
28
29 x <- 99
30 z <- 123
31
32 testServer(module, args = list(x = 0), {
33 expect_equal(x, 0)
34 expect_equal(y, 1)
35 expect_equal(z, 123)
36 })
37 })
38
39 test_that("testServer allows lexical environment access through session$env", {
40 module <- local({
41 a_var <- 123
42 function(id) {
43 moduleServer(id, function(input, output, session) {
44 b_var <- 321
45 })
46 }
47 })
48
49 expect_false(exists("a_var", inherits = FALSE))
50
51 testServer(module, {
52 expect_equal(b_var, 321)
53 expect_equal(get("a_var", session$env, inherits = TRUE), 123)
54 expect_false(exists("a_var", inherits = FALSE))
55 })
56 })
57
58 test_that("Shadowing can be mitigated with unquote", {
59 i <- 0
60 inc <- function() i <<- i+1
61
62 module <- local({
63 function(id) {
64 moduleServer(id, function(input, output, session) {
65 inc <- function() stop("I should never be called")
66 })
67 }
68 }, envir = globalenv())
69
70 testServer(module, {
71 expect_type(inc, "closure")
72 expect_false(identical(inc, !!inc))
73 !!inc()
74 })
75
76 expect_equal(i, 1)
77 })