Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moduleServer/testServer overhaul #2807

Merged
merged 39 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a943d95
make sure the server is only run if the example is interactive
schloerke Mar 12, 2020
3861357
get travis to pass for now
schloerke Mar 12, 2020
88374ec
Update modules.R
alandipert Mar 12, 2020
2d324c7
Add S3 class to MockShinySession
alandipert Mar 12, 2020
a577b1e
un-skip test
schloerke Mar 12, 2020
e17f416
Update roxygen level
schloerke Mar 12, 2020
f6e1718
Merge remote-tracking branch 'origin/master' into serverModule-testMo…
alandipert Mar 19, 2020
b005799
Add back many working/converted tests
alandipert Mar 23, 2020
c4852cb
Desired environment semantics are working
alandipert Mar 25, 2020
ec2c9ec
Split up and rename various tests
alandipert Mar 27, 2020
0023418
More test reorg
alandipert Mar 30, 2020
bb4aaa2
Bring back scope tests
alandipert Mar 31, 2020
dd9e034
More test progress
alandipert Mar 31, 2020
9d13cb6
test-module.R => test-server.R
alandipert Mar 31, 2020
65233cd
First passing app dir test for testServer overhaul
alandipert Mar 31, 2020
cf9ab1c
appobj coercion works
alandipert Mar 31, 2020
58b4585
Doc and test updates
alandipert Apr 1, 2020
5475ec4
document
alandipert Apr 1, 2020
7e3deb5
document
alandipert Apr 1, 2020
7f80bfd
document
alandipert Apr 1, 2020
78da4c7
Merge remote-tracking branch 'origin/master' into serverModule-testMo…
alandipert Apr 1, 2020
828567e
Add failing proxy-related and ns() related tests
alandipert Apr 1, 2020
e0ed443
WIP mock session scoped proxy
alandipert Apr 2, 2020
953de73
nested module tests pass now, many others fail %-)
alandipert Apr 2, 2020
90f5318
fix one failing test
alandipert Apr 3, 2020
70edcd6
Getting there
alandipert Apr 3, 2020
3ca8b10
Tests pass \o/
alandipert Apr 6, 2020
1d9a6ea
getEnv() => env, docs
alandipert Apr 6, 2020
a2dd97c
Merge remote-tracking branch 'origin/master' into serverModule-testMo…
alandipert Apr 6, 2020
9d8a6d0
Document new R6 methods
alandipert Apr 7, 2020
286f125
document
alandipert Apr 7, 2020
bc2aa71
Use vapply in mapNames()
alandipert Apr 8, 2020
c0c02d2
Remove unused variable
alandipert Apr 8, 2020
2f8227e
Un-inline assignment
alandipert Apr 8, 2020
214d721
Move session$env sanity check out of makeMask and into testServer
alandipert Apr 8, 2020
7e2ffab
Use base versions of a couple rlang::env_* functions
alandipert Apr 8, 2020
fc6f535
Clarify testServer lexenv assertions
alandipert Apr 8, 2020
bac7299
Remove strings from expect_error
alandipert Apr 8, 2020
f4e3e5b
server => module
alandipert Apr 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Collate:
'snapshot.R'
'tar.R'
'test-export.R'
'test-module.R'
'test-server.R'
'test.R'
'update-input.R'
RoxygenNote: 7.1.0
Expand Down
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ export(tagHasAttribute)
export(tagList)
export(tagSetChildren)
export(tags)
export(testModule)
export(testServer)
export(textAreaInput)
export(textInput)
Expand Down Expand Up @@ -354,4 +353,3 @@ importFrom(htmltools,validateCssUnit)
importFrom(htmltools,withTags)
importFrom(promises,"%...!%")
importFrom(promises,"%...>%")
importFrom(withr,with_options)
52 changes: 36 additions & 16 deletions R/mock-session.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ extract <- function(promise) {
stop("Single-bracket indexing of mockclientdata is not allowed.")
}

#' @noRd
mapNames <- function(func, ...) {
vals <- list(...)
names(vals) <- vapply(names(vals), func, character(1))
vals
}

#' Mock Shiny Session
#'
#' @description
Expand All @@ -83,6 +90,8 @@ MockShinySession <- R6Class(
public = list(
#' @field env The environment associated with the session.
env = NULL,
#' @field returned The value returned by the module.
returned = NULL,
#' @field singletons Hardcoded as empty. Needed for rendering HTML (i.e. renderUI)
singletons = character(0),
#' @field clientData Mock client data that always returns a size for plots
Expand Down Expand Up @@ -371,10 +380,10 @@ MockShinySession <- R6Class(
#' @param export Not used
#' @param format Not used
getTestSnapshotUrl = function(input=TRUE, output=TRUE, export=TRUE, format="json") {},
#' @description Returns the given id prefixed by `mock-session-`.
#' @description Returns the given id prefixed by this namespace's id.
#' @param id The id to modify.
ns = function(id) {
paste0("mock-session-", id) # TODO: does this need to be more complex/intelligent?
NS(private$nsPrefix, id)
},
#' @description Trigger a reactive flush right now.
flushReact = function(){
Expand All @@ -388,8 +397,30 @@ MockShinySession <- R6Class(
self,
input = .createReactiveValues(private$.input, readonly = TRUE, ns = ns),
output = structure(.createOutputWriter(self, ns = ns), class = "shinyoutput"),
makeScope = function(namespace) self$makeScope(ns(namespace))
makeScope = function(namespace) self$makeScope(ns(namespace)),
ns = function(namespace) ns(namespace),
setInputs = function(...) do.call(self$setInputs, mapNames(ns, ...))
)
},
#' @description Set the environment associated with a testServer() call.
#' @param env The environment to retain.
setEnv = function(env) {
self$env <- env
},
#' @description Set the value returned by the module call and proactively flush.
#' @param value The value returned from the module
setReturned = function(value) {
self$returned <- value
private$flush()
value
},
#' @description Get the value returned by the module call.
getReturned = function() self$returned,
#' @description Return a distinct character identifier for use as a proxy
#' namespace.
genId = function() {
private$idCounter <- private$idCounter + 1
paste0("proxy", private$idCounter)
}
),
private = list(
Expand All @@ -400,7 +431,8 @@ MockShinySession <- R6Class(
timer = NULL,
closed = FALSE,
outs = list(),
returnedVal = NULL,
nsPrefix = "mock-session",
idCounter = 0,

flush = function(){
isolate(private$flushCBs$invoke(..stacktraceon = TRUE))
Expand All @@ -410,18 +442,6 @@ MockShinySession <- R6Class(
}
),
active = list(
# If assigning to `returned`, proactively flush
#' @field returned The value returned from the module
returned = function(value){
if(missing(value)){
return(private$returnedVal)
}
# When you assign to returned, that implies that you just ran
# the module. So we should proactively flush. We have to do this
# here since flush is private.
private$returnedVal <- value
private$flush()
},
#' @field request An empty environment where the request should be. The request isn't meaningfully mocked currently.
request = function(value) {
if (!missing(value)){
Expand Down
7 changes: 6 additions & 1 deletion R/modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ createSessionProxy <- function(parentSession, ...) {

`[[<-.session_proxy` <- `$<-.session_proxy`


#' Shiny modules
#'
#' Shiny's module feature lets you break complicated UI and server logic into
Expand Down Expand Up @@ -132,6 +131,12 @@ createSessionProxy <- function(parentSession, ...) {
#'
#' @export
moduleServer <- function(id, module, session = getDefaultReactiveDomain()) {
if (inherits(session, "MockShinySession")) {
body(module) <- rlang::expr({
session$setEnv(base::environment())
session$setReturned({ !!!body(module) })
})
}
callModule(module, id, session = session)
}

Expand Down
169 changes: 0 additions & 169 deletions R/test-module.R

This file was deleted.

Loading