Debian Codegraph @

tests/testthat/test-reactlog.R

raw
1 keyValList <- function(key, value) {
2 ret <- list()
3 ret[[key]] <- value
4 ret
5 }
6 withOption <- function(key, value, oldVal = NULL, expr) {
7 oldVal <- getOption(key, oldVal)
8 do.call("options", keyValList(key, value))
9 on.exit({
10 do.call("options", keyValList(key, oldVal))
11 })
12 force(expr)
13 }
14
15 withLogging <- function(expr) {
16
17 rLog$reset()
18
19 # reset ctx counter
20 reactiveEnvr <- .getReactiveEnvironment()
21 reactiveEnvr$.nextId <- 0L
22
23 withOption("shiny.reactlog", TRUE, FALSE, {
24 withOption("shiny.reactlog.console", TRUE, FALSE, {
25 withOption("shiny.suppressMissingContextError", TRUE, FALSE, {
26 force(expr)
27 })
28 })
29 })
30 }
31
32 expect_logs <- function(expr, ...) {
33 expected_messages <- unlist(list(...))
34 captured_messages <- capture_messages(expr)
35 captured_messages <- sub("\n$", "", captured_messages)
36 if (length(captured_messages) != length(expected_messages)) {
37 cat("\nCaptured: \n"); print(captured_messages)
38 cat("Expected: \n"); print(expected_messages)
39 }
40 expect_equal(
41 captured_messages,
42 expected_messages
43 )
44 }
45
46 test_that("rLog resets when options are FALSE", {
47
48 withOption("shiny.reactlog", FALSE, FALSE, {
49 withOption("shiny.reactlog.console", FALSE, FALSE, {
50 rLog$reset()
51
52 # check for dummy and no reactid information
53 expect_true(!is.null(rLog$noReactId))
54 expect_true(!is.null(rLog$dummyReactId))
55 expect_equal(rLog$msg$getReact(rLog$noReactId, force = TRUE)$reactId, rLog$noReactId)
56 expect_equal(rLog$msg$getReact(rLog$dummyReactId, force = TRUE)$reactId, rLog$dummyReactId)
57 expect_equal(length(rLog$msg$reactCache), 2)
58 })
59 })
60
61 })
62
63 test_that("message logger appears", {
64
65 withLogging({
66
67 expect_logs(
68 {
69 val <- reactiveVal(1, label = "val")
70 },
71 "- define: r1:'val' - reactiveVal ' num 1'"
72 )
73 expect_silent(
74 {
75 values <- reactiveValues(a = 2, b = 3)
76 local({
77 values_obj <- .subset2(values, 'impl')
78 values_obj$.label <- "values"
79 })
80 }
81 )
82 expect_logs(
83 {
84 react <- reactive(val() + values$a)
85 },
86 "- define: r3:'reactive({\\n val() + values$a\\n})' - observable ' NULL'"
87 )
88
89 expect_logs(
90 {
91 react()
92 },
93 "- createContext: ctxDummy - isolate",
94 "- dependsOn: rDummyReactId:'DummyReactId' on r3:'reactive({\\n val() + values$a\\n})' in ctxDummy",
95 "- createContext: ctx1 - observable",
96 "- enter: r3:'reactive({\\n val() + values$a\\n})' in ctx1 - observable",
97 "= - dependsOn: r3:'reactive({\\n val() + values$a\\n})' on r1:'val' in ctx1",
98 "= - define: r2$a:'values$a' - reactiveValuesKey ' num 2'",
99 "= - dependsOn: r3:'reactive({\\n val() + values$a\\n})' on r2$a:'values$a' in ctx1",
100 "- exit: r3:'reactive({\\n val() + values$a\\n})' in ctx1 - observable"
101 )
102
103 expect_logs(
104 {
105 val(4)
106 },
107 "- valueChange: r1:'val' ' num 4'",
108 "- invalidateStart: r1:'val'",
109 "= - invalidateStart: r3:'reactive({\\n val() + values$a\\n})' in ctx1 - observable",
110 "= = - isolateInvalidateStart: rDummyReactId:'DummyReactId' in ctxDummy",
111 "= = = - dependsOnRemove: rDummyReactId:'DummyReactId' on r3:'reactive({\\n val() + values$a\\n})' in ctxDummy",
112 "= = - isolateInvalidateEnd: rDummyReactId:'DummyReactId' in ctxDummy",
113 "= = - dependsOnRemove: r3:'reactive({\\n val() + values$a\\n})' on r1:'val' in ctx1",
114 "= = - dependsOnRemove: r3:'reactive({\\n val() + values$a\\n})' on r2$a:'values$a' in ctx1",
115 "= - invalidateEnd: r3:'reactive({\\n val() + values$a\\n})' in ctx1 - observable",
116 "- invalidateEnd: r1:'val'"
117 )
118
119 expect_logs(
120 {values$a <- 5},
121 "- valueChange: r2$a:'values$a' ' num 5'",
122 "- invalidateStart: r2$a:'values$a'",
123 "- invalidateEnd: r2$a:'values$a'"
124 )
125
126 })
127
128 })
129
130
131 test_that("reactlog_version is as expected", {
132 expect_match(
133 packageDescription("shiny")$Suggests,
134 # The space between reactlog and the version number can include \n
135 sprintf("\\breactlog\\s+\\Q(>= %s)\\E", reactlog_min_version)
136 )
137 })