Debian Codegraph @

tests/testthat/test-params.R

raw
1 context("params")
2
3 test_that("setting of params works", {
4
5 params_sample <- '---\ntitle: "test"\noutput: html_document\nparams:\n field1:\n value: "defaulthere"\n field2: null\n field3: \n field4: NULL\n---'
6
7 # No overrides
8 params <- knit_params_get(params_sample, NULL)
9 expect_equal(params$field1, "defaulthere")
10 expect_null(params$field2)
11 expect_null(params$field3)
12 expect_null(params$field4)
13
14 # With overrides
15 params <- knit_params_get(params_sample, list(field1 = "new value"))
16 expect_equal(params$field1, "new value")
17
18 # With overrides for NULL/null/empty params
19 params <- knit_params_get(params_sample, list(field2 = NULL,field4 = "value"))
20 expect_null(params$field2)
21 expect_equal(params$field4, "value")
22
23 # Invalid
24 expect_error(knit_params_get(params_sample, "new value"))
25
26 # Params not declared in YAML
27 expect_error(knit_params_get(params_sample, list(field5 = "a",field6 = "b",field7 = NULL)), regexp = "field5, field6, field7$")
28 })
29
30 test_that("params render their UI", {
31
32 # file input is always NULL
33 ui <- params_value_to_ui(shiny::fileInput, "anything", TRUE)
34 expect_null(ui)
35
36 # text input is a pass-through
37 ui <- params_value_to_ui(shiny::textInput, "some char", TRUE)
38 expect_equal(ui, "some char")
39
40 # Date gets converted to char
41 dat <- Sys.time()
42 ui <- params_value_to_ui(shiny::textInput, dat, TRUE)
43 expect_equal(ui, as.character(dat))
44
45 # NULLs get massaged for numeric/slider
46 ui <- params_value_to_ui(shiny::numericInput, NULL, TRUE)
47 expect_equal(ui, 0)
48 ui <- params_value_to_ui(shiny::sliderInput, NULL, TRUE)
49 expect_equal(ui, 0)
50
51 # Everything else is a passthrough
52 myobj <- list(a = 123, b = NULL, c = "huh")
53 ui <- params_value_to_ui(NULL, myobj, TRUE)
54 expect_equal(ui, myobj)
55 })
56
57 test_that("parameters are configurable", {
58 # Unknown input types are not configurable.
59 expect_error(params_configurable(list(
60 input = "unsupported")))
61
62 # Numeric (and most other controls) do not support multiple values.
63 expect_true(params_configurable(list(
64 input = "numeric",
65 value = 42)))
66 expect_false(params_configurable(list(
67 input = "numeric",
68 value = c(13, 42))))
69
70 # Selectors permit multiple values if explicitly enabled.
71 expect_true(params_configurable(list(
72 input = "select",
73 choices = c(1, 2, 3, 4),
74 value = 2)))
75 expect_true(params_configurable(list(
76 input = "select",
77 multiple = TRUE,
78 choices = c(1, 2, 3, 4),
79 value = c(2, 4))))
80 expect_false(params_configurable(list(
81 input = "select",
82 multiple = FALSE,
83 choices = c(1, 2, 3, 4),
84 value = c(2, 4))))
85 expect_false(params_configurable(list(
86 input = "select",
87 choices = c(1, 2, 3, 4),
88 value = c(2, 4))))
89
90 # Sliders permit either one or two values (singular or double-ended slider).
91 expect_true(params_configurable(list(
92 input = "slider",
93 min = 1,
94 max = 100,
95 value = 50)))
96 expect_true(params_configurable(list(
97 input = "slider",
98 min = 1,
99 max = 100,
100 value = c(45, 55))))
101 expect_false(params_configurable(list(
102 input = "slider",
103 min = 1,
104 max = 100,
105 value = c(10, 20, 30))))
106 })
107
108 test_that("params_get_input", {
109 # input derived from value type.
110 expect_equal(params_get_input(list(
111 value = TRUE
112 )), "checkbox")
113 expect_equal(params_get_input(list(
114 value = as.integer(42)
115 )), "numeric")
116 expect_equal(params_get_input(list(
117 value = 7.5
118 )), "numeric")
119 expect_equal(params_get_input(list(
120 value = "character"
121 )), "text")
122 expect_equal(params_get_input(list(
123 value = Sys.Date()
124 )), "date")
125 expect_equal(params_get_input(list(
126 value = Sys.time()
127 )), "datetime")
128
129 # numeric becomes a slider with both min and max
130 expect_equal(params_get_input(list(
131 value = as.integer(42),
132 min = 0
133 )), "numeric")
134 expect_equal(params_get_input(list(
135 value = as.integer(42),
136 max = 100
137 )), "numeric")
138 expect_equal(params_get_input(list(
139 value = as.integer(42),
140 min = 0,
141 max = 100
142 )), "slider")
143 expect_equal(params_get_input(list(
144 value = 7.5,
145 min = 0
146 )), "numeric")
147 expect_equal(params_get_input(list(
148 value = 7.5,
149 max = 10
150 )), "numeric")
151 expect_equal(params_get_input(list(
152 value = 7.5,
153 min = 0,
154 max = 10
155 )), "slider")
156
157 # choices implies radio or select
158 expect_equal(params_get_input(list(
159 value = "red",
160 choices = c("red", "green", "blue")
161 )), "radio")
162 expect_equal(params_get_input(list(
163 value = "red",
164 multiple = TRUE,
165 choices = c("red", "green", "blue")
166 )), "select")
167 expect_equal(params_get_input(list(
168 value = "red",
169 choices = c("red", "green", "blue", "yellow", "black", "white")
170 )), "select")
171 expect_equal(params_get_input(list(
172 value = "red",
173 multiple = TRUE,
174 choices = c("red", "green", "blue", "yellow", "black", "white")
175 )), "select")
176
177 # explicit input overrides inference
178 expect_equal(params_get_input(list(
179 input = "slider",
180 value = 42
181 )), "slider")
182 expect_equal(params_get_input(list(
183 input = "file",
184 value = "data.csv"
185 )), "file")
186 })
187
188 test_that("params hidden w/o show_default", {
189
190 # file input is always NULL
191 ui <- params_value_to_ui(shiny::fileInput, "anything", FALSE)
192 expect_null(ui)
193
194 # text input suppressed
195 ui <- params_value_to_ui(shiny::textInput, "some char", FALSE)
196 expect_equal(ui, NULL)
197
198 # Date gets scrubbed
199 ui <- params_value_to_ui(shiny::textInput, dat, FALSE)
200 expect_equal(ui, NULL)
201
202 # Numeric -> 0
203 ui <- params_value_to_ui(shiny::numericInput, 13, FALSE)
204 expect_equal(ui, 0)
205
206 # Slider -> 0
207 # TODO: should this be MIN?
208 ui <- params_value_to_ui(shiny::sliderInput, 13, FALSE)
209 expect_equal(ui, 0)
210
211 # Everything else is scrubbed
212 myobj <- list(a = 123, b = NULL, c = "huh")
213 ui <- params_value_to_ui(function(){}, myobj, FALSE)
214 expect_equal(ui, NULL)
215 })
216
217 test_that("ui value conversion", {
218 emptydt <- Sys.time()[-1]
219
220 # Sys.time() -> as.character loses sub-second precision
221 nows <- as.character(Sys.time())
222 now <- as.POSIXct(nows)
223 expect_null(params_value_from_ui(shiny::textInput, now, "not-a-datetime"),
224 "produce NULL when datetime cannot be parsed")
225 expect_identical(params_value_from_ui(shiny::textInput, now, nows), now,
226 "produce datetime from valid UI textual value")
227 expect_identical(params_value_from_ui(shiny::textInput, now, ""), emptydt,
228 "produce empty POSIXct on empty input")
229 expect_identical(params_value_from_ui(shiny::textInput, "not-a-datetime", "uivalue"), "uivalue",
230 "textInput unmodified when datetime not involved")
231 expect_identical(params_value_from_ui(shiny::numericInput, 13, 42), 42,
232 "numericInput values pass through")
233 })