From 4372a0680853f4cc24424b72a285772801aea89d Mon Sep 17 00:00:00 2001 From: Alan Dipert Date: Wed, 15 Jan 2020 00:02:42 +0000 Subject: [PATCH 1/6] 4 apps --- 001-hello/app.R | 19 +++++--------- 001-hello/tests/testthat.R | 9 +++++++ 005-sliders/tests/testthat.R | 26 +++++++++++++++++++ 119-namespaced-conditionalpanel-demo/app.R | 8 +++--- .../tests/testthat.R | 15 +++++++++++ 168-supporting-r-dir/tests/testthat.R | 25 ++++++++++++++++++ 6 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 001-hello/tests/testthat.R create mode 100644 005-sliders/tests/testthat.R create mode 100644 119-namespaced-conditionalpanel-demo/tests/testthat.R create mode 100644 168-supporting-r-dir/tests/testthat.R diff --git a/001-hello/app.R b/001-hello/app.R index 887c50576d..542141eac1 100644 --- a/001-hello/app.R +++ b/001-hello/app.R @@ -34,20 +34,15 @@ ui <- fluidPage( # Define server logic required to draw a histogram ---- server <- function(input, output) { - # Histogram of the Old Faithful Geyser Data ---- - # with requested number of bins - # This expression that generates a histogram is wrapped in a call - # to renderPlot to indicate that: - # - # 1. It is "reactive" and therefore should be automatically - # re-executed when inputs (input$bins) change - # 2. Its output type is a plot - output$distPlot <- renderPlot({ + x <- faithful$waiting + + bins <- reactive({ + seq(min(x), max(x), length.out = input$bins + 1) + }) - x <- faithful$waiting - bins <- seq(min(x), max(x), length.out = input$bins + 1) + output$distPlot <- renderPlot({ - hist(x, breaks = bins, col = "#75AADB", border = "white", + hist(x, breaks = bins(), col = "#75AADB", border = "white", xlab = "Waiting time to next eruption (in mins)", main = "Histogram of waiting times") diff --git a/001-hello/tests/testthat.R b/001-hello/tests/testthat.R new file mode 100644 index 0000000000..e70bfcf6c3 --- /dev/null +++ b/001-hello/tests/testthat.R @@ -0,0 +1,9 @@ +library(testthat) +library(shiny) + +test_that("reactives update", { + testServer({ + session$setInputs(bins = 5) + expect_equal(bins(), seq(43, 96, length.out = 6)) + }) +}) diff --git a/005-sliders/tests/testthat.R b/005-sliders/tests/testthat.R new file mode 100644 index 0000000000..fda9342e38 --- /dev/null +++ b/005-sliders/tests/testthat.R @@ -0,0 +1,26 @@ +library(testthat) +library(shiny) + +test_that("inputs are stored", { + testServer({ + session$setInputs( + integer = 593, + decimal = 0.6, + range = c(100,300), + format = 5000, + animation = 100 + ) + + # The data frame should have these properties + df <- sliderValues() + expect_equal(nrow(df), 5) + expect_equal(ncol(df), 2) + expect_is(df$Value, "character") + + # Updating an input should result in a new plot + plot1 <- output$values + session$setInputs(integer = 594) + plot2 <- output$values + expect_true(plot1 != plot2) + }) +}) diff --git a/119-namespaced-conditionalpanel-demo/app.R b/119-namespaced-conditionalpanel-demo/app.R index 0039644516..f6cec88966 100644 --- a/119-namespaced-conditionalpanel-demo/app.R +++ b/119-namespaced-conditionalpanel-demo/app.R @@ -33,11 +33,9 @@ myPlotUI <- function(id, label = "My Plot") { } myPlot <- function(input, output, session, deviates) { - output$scatterPlot <- renderPlot({ - x <- rnorm(input$n) - y <- rnorm(input$n) - plot(x, y) - }) + x <- reactive(rnorm(input$n)) + y <- reactive(rnorm(input$n)) + output$scatterPlot <- renderPlot(plot(x(), y())) } server <- function(input, output) { diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat.R b/119-namespaced-conditionalpanel-demo/tests/testthat.R new file mode 100644 index 0000000000..e043d14ddb --- /dev/null +++ b/119-namespaced-conditionalpanel-demo/tests/testthat.R @@ -0,0 +1,15 @@ +library(testthat) +library(shiny) +library(withr) + +# TODO Determine the ideal way for module-reliant test code to load an app +with_dir(shiny:::findApp(), { + local({ + source("app.R") + test_that("module works", { + testModule(myPlot, { + expect_true(TRUE) + }) + }) + }) +}) diff --git a/168-supporting-r-dir/tests/testthat.R b/168-supporting-r-dir/tests/testthat.R new file mode 100644 index 0000000000..9b4833766f --- /dev/null +++ b/168-supporting-r-dir/tests/testthat.R @@ -0,0 +1,25 @@ +library(testthat) +library(shiny) +library(withr) + +inc <- function(x) if (is.null(x)) 0 else x+1 + +# TODO Determine the ideal way for module-reliant test code to load an app +with_dir(shiny:::findApp(), { + local({ + source("R/counter.R") + test_that("counter works", { + testModule(counter, { + expect_equal(count(), 0) + # Simulate button press. Action buttons are counters, but their initial + # value is NULL so that they don't cause reactivity without being + # pressed at least once. + # TODO Consider adding session$click() or similar to mock session + session$setInputs(button = inc(input$button)) + expect_equal(count(), 1) + expect_equal(output$out, "1") + expect_equal(session$returned(), 1) + }) + }) + }) +}) From b0a1069194fdbc0bd01faac68b364a04045d35d2 Mon Sep 17 00:00:00 2001 From: Alan Dipert Date: Wed, 15 Jan 2020 22:43:09 +0000 Subject: [PATCH 2/6] And the fifth app was tested --- 050-kmeans-example/tests/testthat.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 050-kmeans-example/tests/testthat.R diff --git a/050-kmeans-example/tests/testthat.R b/050-kmeans-example/tests/testthat.R new file mode 100644 index 0000000000..78c95d1ed4 --- /dev/null +++ b/050-kmeans-example/tests/testthat.R @@ -0,0 +1,16 @@ +library(testthat) +library(shiny) + +testServer({ + test_that("it works", { + session$setInputs( + xcol = "Sepal.Length", + ycol = "Petal.Length", + clusters = 4 + ) + expect_equal(colnames(selectedData()), c("Sepal.Length", "Petal.Length")) + expect_equal(nrow(clusters()$centers), 4) + session$setInputs(clusters = 3) + expect_equal(nrow(clusters()$centers), 3) + }) +}) From c90065e2ab8f93e03f4f13cfc30be8150cca5e77 Mon Sep 17 00:00:00 2001 From: Alan Dipert Date: Thu, 16 Jan 2020 23:58:00 +0000 Subject: [PATCH 3/6] Tweak testthat test arrangement --- 001-hello/tests/testthat.R | 7 +---- 001-hello/tests/testthat/tests.R | 9 +++++++ 005-sliders/tests/testthat.R | 24 +---------------- 005-sliders/tests/testthat/tests.R | 26 +++++++++++++++++++ 050-kmeans-example/tests/testthat.R | 14 +--------- 050-kmeans-example/tests/testthat/tests.R | 16 ++++++++++++ .../tests/testthat.R | 13 +--------- .../tests/testthat/tests.R | 15 +++++++++++ 168-supporting-r-dir/tests/testthat.R | 23 +--------------- 168-supporting-r-dir/tests/testthat/tests.R | 13 ++++++++++ 10 files changed, 84 insertions(+), 76 deletions(-) create mode 100644 001-hello/tests/testthat/tests.R create mode 100644 005-sliders/tests/testthat/tests.R create mode 100644 050-kmeans-example/tests/testthat/tests.R create mode 100644 119-namespaced-conditionalpanel-demo/tests/testthat/tests.R create mode 100644 168-supporting-r-dir/tests/testthat/tests.R diff --git a/001-hello/tests/testthat.R b/001-hello/tests/testthat.R index e70bfcf6c3..edc089d427 100644 --- a/001-hello/tests/testthat.R +++ b/001-hello/tests/testthat.R @@ -1,9 +1,4 @@ library(testthat) library(shiny) -test_that("reactives update", { - testServer({ - session$setInputs(bins = 5) - expect_equal(bins(), seq(43, 96, length.out = 6)) - }) -}) +testthat::test_file("testthat/tests.R") diff --git a/001-hello/tests/testthat/tests.R b/001-hello/tests/testthat/tests.R new file mode 100644 index 0000000000..e70bfcf6c3 --- /dev/null +++ b/001-hello/tests/testthat/tests.R @@ -0,0 +1,9 @@ +library(testthat) +library(shiny) + +test_that("reactives update", { + testServer({ + session$setInputs(bins = 5) + expect_equal(bins(), seq(43, 96, length.out = 6)) + }) +}) diff --git a/005-sliders/tests/testthat.R b/005-sliders/tests/testthat.R index fda9342e38..edc089d427 100644 --- a/005-sliders/tests/testthat.R +++ b/005-sliders/tests/testthat.R @@ -1,26 +1,4 @@ library(testthat) library(shiny) -test_that("inputs are stored", { - testServer({ - session$setInputs( - integer = 593, - decimal = 0.6, - range = c(100,300), - format = 5000, - animation = 100 - ) - - # The data frame should have these properties - df <- sliderValues() - expect_equal(nrow(df), 5) - expect_equal(ncol(df), 2) - expect_is(df$Value, "character") - - # Updating an input should result in a new plot - plot1 <- output$values - session$setInputs(integer = 594) - plot2 <- output$values - expect_true(plot1 != plot2) - }) -}) +testthat::test_file("testthat/tests.R") diff --git a/005-sliders/tests/testthat/tests.R b/005-sliders/tests/testthat/tests.R new file mode 100644 index 0000000000..fda9342e38 --- /dev/null +++ b/005-sliders/tests/testthat/tests.R @@ -0,0 +1,26 @@ +library(testthat) +library(shiny) + +test_that("inputs are stored", { + testServer({ + session$setInputs( + integer = 593, + decimal = 0.6, + range = c(100,300), + format = 5000, + animation = 100 + ) + + # The data frame should have these properties + df <- sliderValues() + expect_equal(nrow(df), 5) + expect_equal(ncol(df), 2) + expect_is(df$Value, "character") + + # Updating an input should result in a new plot + plot1 <- output$values + session$setInputs(integer = 594) + plot2 <- output$values + expect_true(plot1 != plot2) + }) +}) diff --git a/050-kmeans-example/tests/testthat.R b/050-kmeans-example/tests/testthat.R index 78c95d1ed4..edc089d427 100644 --- a/050-kmeans-example/tests/testthat.R +++ b/050-kmeans-example/tests/testthat.R @@ -1,16 +1,4 @@ library(testthat) library(shiny) -testServer({ - test_that("it works", { - session$setInputs( - xcol = "Sepal.Length", - ycol = "Petal.Length", - clusters = 4 - ) - expect_equal(colnames(selectedData()), c("Sepal.Length", "Petal.Length")) - expect_equal(nrow(clusters()$centers), 4) - session$setInputs(clusters = 3) - expect_equal(nrow(clusters()$centers), 3) - }) -}) +testthat::test_file("testthat/tests.R") diff --git a/050-kmeans-example/tests/testthat/tests.R b/050-kmeans-example/tests/testthat/tests.R new file mode 100644 index 0000000000..78c95d1ed4 --- /dev/null +++ b/050-kmeans-example/tests/testthat/tests.R @@ -0,0 +1,16 @@ +library(testthat) +library(shiny) + +testServer({ + test_that("it works", { + session$setInputs( + xcol = "Sepal.Length", + ycol = "Petal.Length", + clusters = 4 + ) + expect_equal(colnames(selectedData()), c("Sepal.Length", "Petal.Length")) + expect_equal(nrow(clusters()$centers), 4) + session$setInputs(clusters = 3) + expect_equal(nrow(clusters()$centers), 3) + }) +}) diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat.R b/119-namespaced-conditionalpanel-demo/tests/testthat.R index e043d14ddb..edc089d427 100644 --- a/119-namespaced-conditionalpanel-demo/tests/testthat.R +++ b/119-namespaced-conditionalpanel-demo/tests/testthat.R @@ -1,15 +1,4 @@ library(testthat) library(shiny) -library(withr) -# TODO Determine the ideal way for module-reliant test code to load an app -with_dir(shiny:::findApp(), { - local({ - source("app.R") - test_that("module works", { - testModule(myPlot, { - expect_true(TRUE) - }) - }) - }) -}) +testthat::test_file("testthat/tests.R") diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R b/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R new file mode 100644 index 0000000000..e043d14ddb --- /dev/null +++ b/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R @@ -0,0 +1,15 @@ +library(testthat) +library(shiny) +library(withr) + +# TODO Determine the ideal way for module-reliant test code to load an app +with_dir(shiny:::findApp(), { + local({ + source("app.R") + test_that("module works", { + testModule(myPlot, { + expect_true(TRUE) + }) + }) + }) +}) diff --git a/168-supporting-r-dir/tests/testthat.R b/168-supporting-r-dir/tests/testthat.R index 9b4833766f..edc089d427 100644 --- a/168-supporting-r-dir/tests/testthat.R +++ b/168-supporting-r-dir/tests/testthat.R @@ -1,25 +1,4 @@ library(testthat) library(shiny) -library(withr) -inc <- function(x) if (is.null(x)) 0 else x+1 - -# TODO Determine the ideal way for module-reliant test code to load an app -with_dir(shiny:::findApp(), { - local({ - source("R/counter.R") - test_that("counter works", { - testModule(counter, { - expect_equal(count(), 0) - # Simulate button press. Action buttons are counters, but their initial - # value is NULL so that they don't cause reactivity without being - # pressed at least once. - # TODO Consider adding session$click() or similar to mock session - session$setInputs(button = inc(input$button)) - expect_equal(count(), 1) - expect_equal(output$out, "1") - expect_equal(session$returned(), 1) - }) - }) - }) -}) +testthat::test_file("testthat/tests.R") diff --git a/168-supporting-r-dir/tests/testthat/tests.R b/168-supporting-r-dir/tests/testthat/tests.R new file mode 100644 index 0000000000..360ea936e6 --- /dev/null +++ b/168-supporting-r-dir/tests/testthat/tests.R @@ -0,0 +1,13 @@ +library(testthat) +library(shiny) + +test_that("counter works", { + testModule(counter, { + inc <- function(x) if (is.null(x)) 0 else x+1 + expect_equal(count(), 0) + session$setInputs(button = inc(input$button)) + expect_equal(count(), 1) + expect_equal(output$out, "1") + expect_equal(session$returned(), 1) + }) +}) From bacfee3a91761e8a8b50a8c9c74d34706d822730 Mon Sep 17 00:00:00 2001 From: Alan Dipert Date: Sat, 18 Jan 2020 01:59:53 +0000 Subject: [PATCH 4/6] testthat action: Fix 168 --- 168-supporting-r-dir/tests/testthat.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/168-supporting-r-dir/tests/testthat.R b/168-supporting-r-dir/tests/testthat.R index edc089d427..9974530cae 100644 --- a/168-supporting-r-dir/tests/testthat.R +++ b/168-supporting-r-dir/tests/testthat.R @@ -1,4 +1,8 @@ library(testthat) library(shiny) -testthat::test_file("testthat/tests.R") +# We explicitly pass environment() here because shiny::runTests() has sourced +# the files in R/, and the module we're testing was defined in those files. If +# we don't specify env, testthat uses an environment that doesn't include the +# module, and the tests fail. +testthat::test_file("testthat/tests.R", env = environment()) From b584fc3314e97f89f50e9deb06295a4934c7df3a Mon Sep 17 00:00:00 2001 From: Alan Dipert Date: Tue, 21 Jan 2020 21:07:23 +0000 Subject: [PATCH 5/6] Remove unnecessary withr use --- .../tests/testthat.R | 2 +- .../tests/testthat/tests.R | 13 +++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat.R b/119-namespaced-conditionalpanel-demo/tests/testthat.R index edc089d427..9cbf743cbe 100644 --- a/119-namespaced-conditionalpanel-demo/tests/testthat.R +++ b/119-namespaced-conditionalpanel-demo/tests/testthat.R @@ -1,4 +1,4 @@ library(testthat) library(shiny) -testthat::test_file("testthat/tests.R") +testthat::test_file("testthat/tests.R", env = environment()) diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R b/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R index e043d14ddb..9014d00184 100644 --- a/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R +++ b/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R @@ -1,15 +1,8 @@ library(testthat) library(shiny) -library(withr) -# TODO Determine the ideal way for module-reliant test code to load an app -with_dir(shiny:::findApp(), { - local({ - source("app.R") - test_that("module works", { - testModule(myPlot, { - expect_true(TRUE) - }) - }) +test_that("module works", { + testModule(myPlot, { + expect_true(TRUE) }) }) From 3da69c8277c389f376b22ee406e3c97fcbddbf44 Mon Sep 17 00:00:00 2001 From: Alan Dipert Date: Wed, 22 Jan 2020 21:20:38 +0000 Subject: [PATCH 6/6] Revert "Remove unnecessary withr use" This reverts commit b584fc3314e97f89f50e9deb06295a4934c7df3a. --- .../tests/testthat.R | 2 +- .../tests/testthat/tests.R | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat.R b/119-namespaced-conditionalpanel-demo/tests/testthat.R index 9cbf743cbe..edc089d427 100644 --- a/119-namespaced-conditionalpanel-demo/tests/testthat.R +++ b/119-namespaced-conditionalpanel-demo/tests/testthat.R @@ -1,4 +1,4 @@ library(testthat) library(shiny) -testthat::test_file("testthat/tests.R", env = environment()) +testthat::test_file("testthat/tests.R") diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R b/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R index 9014d00184..e043d14ddb 100644 --- a/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R +++ b/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R @@ -1,8 +1,15 @@ library(testthat) library(shiny) +library(withr) -test_that("module works", { - testModule(myPlot, { - expect_true(TRUE) +# TODO Determine the ideal way for module-reliant test code to load an app +with_dir(shiny:::findApp(), { + local({ + source("app.R") + test_that("module works", { + testModule(myPlot, { + expect_true(TRUE) + }) + }) }) })