Debian Codegraph @

tests/testthat/test-bootstrap.r

raw symbols SCIP
1 test_that("CSS unit validation", {
2 # On error, return NA; on success, return result
3 validateCssUnit_wrap <- function(x) {
4 tryCatch(validateCssUnit(x), error = function(e) { NA_character_ })
5 }
6
7 # Test strings and expected results
8 strings <- c("100x", "10px", "10.4px", ".4px", "1px0", "px", "5", "%", "5%", "auto", "1auto", "")
9 expected <- c(NA, "10px", "10.4px", ".4px", NA, NA, "5px", NA, "5%", "auto", NA, NA)
10 results <- vapply(strings, validateCssUnit_wrap, character(1), USE.NAMES = FALSE)
11 expect_equal(results, expected)
12
13 # Numbers should return string with "px"
14 expect_equal(validateCssUnit(100), "100px")
15 })
16
17
18 test_that("Repeated names for selectInput and radioButtons choices", {
19 # These test might be a bit too closely tied to the exact structure of the
20 # tag object, but they get the job done for now.
21
22 # Select input
23 x <- selectInput('id','label', choices = c(a='x1', a='x2', b='x3'), selectize = FALSE)
24 expect_match(
25 format(x),
26 '<select class="shiny-input-select form-control" id="id"><option value="x1" selected>a</option>\n<option value="x2">a</option>\n<option value="x3">b</option></select>',
27 fixed = TRUE
28 )
29
30 # Radio buttons using choices
31 x <- radioButtons('id','label', choices = c(a='x1', a='x2', b='x3'))
32 choices <- x$children
33
34 expect_equal(choices[[2]]$children[[1]][[1]]$children[[1]]$children[[2]]$children[[1]], HTML('a'))
35 expect_equal(choices[[2]]$children[[1]][[1]]$children[[1]]$children[[1]]$attribs$value, 'x1')
36 expect_equal(choices[[2]]$children[[1]][[1]]$children[[1]]$children[[1]]$attribs$checked, 'checked')
37
38 expect_equal(choices[[2]]$children[[1]][[2]]$children[[1]]$children[[2]]$children[[1]], HTML('a'))
39 expect_equal(choices[[2]]$children[[1]][[2]]$children[[1]]$children[[1]]$attribs$value, 'x2')
40 expect_equal(choices[[2]]$children[[1]][[2]]$children[[1]]$children[[1]]$attribs$checked, NULL)
41
42 expect_equal(choices[[2]]$children[[1]][[3]]$children[[1]]$children[[2]]$children[[1]], HTML('b'))
43 expect_equal(choices[[2]]$children[[1]][[3]]$children[[1]]$children[[1]]$attribs$value, 'x3')
44 expect_equal(choices[[2]]$children[[1]][[3]]$children[[1]]$children[[1]]$attribs$checked, NULL)
45
46 # Radio buttons using choiceNames and choiceValues
47 x <- radioButtons('id','label',
48 choiceNames = list(icon('calendar'), HTML('<p style="color:red;">Red</p>'), 'Normal'),
49 choiceValues = list('icon', 'html', 'text')
50 )
51 choices <- x$children
52
53 expect_equal(choices[[2]]$children[[1]][[1]]$children[[1]]$children[[2]]$children[[1]],
54 HTML('<i class="far fa-calendar" role="presentation" aria-label="calendar icon"></i>'))
55 expect_equal(choices[[2]]$children[[1]][[1]]$children[[1]]$children[[1]]$attribs$value, 'icon')
56 expect_equal(choices[[2]]$children[[1]][[1]]$children[[1]]$children[[1]]$attribs$checked, 'checked')
57
58 expect_equal(choices[[2]]$children[[1]][[2]]$children[[1]]$children[[2]]$children[[1]],
59 HTML('<p style="color:red;">Red</p>'))
60 expect_equal(choices[[2]]$children[[1]][[2]]$children[[1]]$children[[1]]$attribs$value, 'html')
61 expect_equal(choices[[2]]$children[[1]][[2]]$children[[1]]$children[[1]]$attribs$checked, NULL)
62
63 expect_equal(choices[[2]]$children[[1]][[3]]$children[[1]]$children[[2]]$children[[1]],
64 HTML('Normal'))
65 expect_equal(choices[[2]]$children[[1]][[3]]$children[[1]]$children[[1]]$attribs$value, 'text')
66 expect_equal(choices[[2]]$children[[1]][[3]]$children[[1]]$children[[1]]$attribs$checked, NULL)
67 })
68
69
70 test_that("Choices are correctly assigned names", {
71 # Empty non-list comes back as a list with names
72 expect_identical(
73 choicesWithNames(numeric(0)),
74 stats::setNames(list(), character(0))
75 )
76 # Empty list comes back with names
77 expect_identical(
78 choicesWithNames(list()),
79 stats::setNames(list(), character(0))
80 )
81 # NULL comes back as an empty list with names
82 expect_identical(
83 choicesWithNames(NULL),
84 stats::setNames(list(), character(0))
85 )
86 # NA is processed as a leaf, not a group
87 expect_identical(
88 choicesWithNames(NA),
89 as.list(stats::setNames(as.character(NA), NA))
90 )
91 # Empty character vector
92 # An empty character vector isn't a sensical input, but we preserved this test
93 # in the off chance that somebody relies on the existing behavior.
94 expect_identical(
95 choicesWithNames(c("")),
96 stats::setNames(list(""), "")
97 )
98 # Single-item character vector
99 expect_identical(
100 choicesWithNames(c("foob")),
101 list(foob="foob")
102 )
103 # Unnamed character vector
104 expect_identical(
105 choicesWithNames(c("a","b","3")),
106 list(a="a", b="b", "3"="3")
107 )
108 # Unnamed numeric vector
109 expect_identical(
110 choicesWithNames(c(1,2,3)),
111 list(`1`="1", `2`="2", `3`="3")
112 )
113 # Unnamed list
114 expect_identical(
115 choicesWithNames(list("a","b",3)),
116 list(a="a", b="b", "3"="3")
117 )
118 # Complex vector, with some named, some not
119 expect_identical(
120 choicesWithNames(c(A=Inf+0i, 1+0i, C=0+0i)),
121 list(A="Inf+0i", "1+0i"="1+0i", C="0+0i")
122 )
123 # List, with some named, some not
124 expect_identical(
125 choicesWithNames(list(A="a", "b", C=3, 4)),
126 list(A="a", "b"="b", C="3", "4"="4")
127 )
128 # List, named, with a sub-vector
129 expect_identical(
130 choicesWithNames(list(A="a", B="b", C=c("d", "e"))),
131 list(A="a", B="b", C=list(d="d", e="e"))
132 )
133 # List, named, with a sub-vector with numeric elements
134 expect_identical(
135 choicesWithNames(list(A="a", B="b", C=c(1, 2))),
136 list(A="a", B="b", C=list(`1`="1", `2`="2"))
137 )
138 # List, named, with sublist
139 expect_identical(
140 choicesWithNames(list(A="a", B="b", C=list("d", "e"))),
141 list(A="a", B="b", C=list(d="d", e="e"))
142 )
143 # List, named, with sublist with numeric elements
144 expect_identical(
145 choicesWithNames(list(A="a", B="b", C=list(1, 2))),
146 list(A="a", B="b", C=list(`1`="1", `2`="2"))
147 )
148 # List, named, with a named sub-vector of length 1
149 expect_identical(
150 choicesWithNames(list(A="a", B="b", C=c(D="d"))),
151 list(A="a", B="b", C=list(D="d"))
152 )
153 # List, named, with a named sub-vector of length 1 with a logical element
154 expect_identical(
155 choicesWithNames(list(A="a", B="b", C=c(D=TRUE))),
156 list(A="a", B="b", C=list(D="TRUE"))
157 )
158 # List, some named, with sublist
159 expect_identical(
160 choicesWithNames(list(A="a", "b", C=list("d", E="e"))),
161 list(A="a", b="b", C=list(d="d", E="e"))
162 )
163 # List, with a single-item unnamed group list
164 expect_identical(
165 choicesWithNames(list(C=list(123))),
166 list(C=list("123"="123"))
167 )
168 # Error when sublist is unnamed
169 expect_error(choicesWithNames(list(A="a", "b", list(1,2))))
170 # Error when list is unnamed and contains a group
171 # NULL, list(1,2), and anything of length() == 0 is considered a group.
172 # NA is NOT a group.
173 expect_error(choicesWithNames(list(NULL)), regexp = "must be named")
174 expect_error(choicesWithNames(list(list(1,2))), regexp = "must be named")
175 expect_error(choicesWithNames(list(character(0))), regexp = "must be named")
176 # Unnamed factor
177 expect_identical(
178 choicesWithNames(factor(c("a","b","3"))),
179 list(a="a", b="b", "3"="3")
180 )
181 # Named factor
182 expect_identical(
183 choicesWithNames(structure(factor(c("foo", "bar")), names = c("A", "B"))),
184 list(A="foo", B="bar")
185 )
186 # List, named, with a sub-factor
187 expect_identical(
188 choicesWithNames(list(A="a", B="b", C=factor(c("d", "e")))),
189 list(A="a", B="b", C=list(d="d", e="e"))
190 )
191 # List, named, with a named sub-factor
192 expect_identical(
193 choicesWithNames(list(A="a", B="b", C=structure(factor(c("d", "e")), names = c("d", "e")))),
194 list(A="a", B="b", C=list(d="d", e="e"))
195 )
196 # List, named, with an empty group as an unnamed empty list
197 expect_identical(
198 choicesWithNames(list(C=list())),
199 list(C=stats::setNames(list(), character()))
200 )
201 # List, named, with an empty group as an unnamed empty vector
202 expect_identical(
203 choicesWithNames(list(C=c())),
204 list(C=stats::setNames(list(), character()))
205 )
206 })
207
208
209 test_that("selectOptions returns correct HTML", {
210 # None selected
211 expect_identical(
212 selectOptions(choicesWithNames(list("a", "b")), list()),
213 HTML("<option value=\"a\">a</option>\n<option value=\"b\">b</option>")
214 )
215 # One selected
216 expect_identical(
217 selectOptions(choicesWithNames(list("a", "b")), "a"),
218 HTML("<option value=\"a\" selected>a</option>\n<option value=\"b\">b</option>")
219 )
220 # One selected, with named items
221 expect_identical(
222 selectOptions(choicesWithNames(list(A="a", B="b")), "a"),
223 HTML("<option value=\"a\" selected>A</option>\n<option value=\"b\">B</option>")
224 )
225 # Two selected, with optgroup
226 expect_identical(
227 selectOptions(choicesWithNames(list("a", B=list("c", D="d"))), c("a", "d")),
228 HTML("<option value=\"a\" selected>a</option>\n<optgroup label=\"B\">\n<option value=\"c\">c</option>\n<option value=\"d\" selected>D</option>\n</optgroup>")
229 )
230
231 # Escape HTML in strings
232 expect_identical(
233 selectOptions(choicesWithNames(list("<A>"="a", B="b")), "a"),
234 HTML("<option value=\"a\" selected>&lt;A&gt;</option>\n<option value=\"b\">B</option>")
235 )
236 })
237
238 test_that("selectInput selects items by default", {
239 # None specified as selected (defaults to first)
240 expect_true(grepl(fixed = TRUE,
241 '<option value="a" selected>',
242 selectInput('x', 'x', list("a", "b"))
243 ))
244
245 # Nested list (optgroup)
246 expect_true(grepl(fixed = TRUE,
247 '<option value="a" selected>',
248 selectInput('x', 'x', list(A=list("a", "b"), "c"))
249 ))
250
251 # Nothing selected when choices=NULL
252 expect_match(
253 format(selectInput('x', NULL, NULL, selectize = FALSE)),
254 '<select class="shiny-input-select form-control" id="x"></select>',
255 fixed = TRUE
256 )
257
258 # None specified as selected. With multiple=TRUE, none selected by default.
259 expect_true(grepl(fixed = TRUE,
260 '<option value="a">',
261 selectInput('x', 'x', list("a", "b"), multiple = TRUE)
262 ))
263 })
264
265 test_that("normalizeChoicesArgs does its job", {
266
267 # Unnamed vectors and lists
268 expected <- list(choiceNames = list("a", "b"), choiceValues = list("a", "b"))
269 expect_equal(normalizeChoicesArgs(c("a", "b"), NULL, NULL), expected)
270 expect_equal(normalizeChoicesArgs(list("a", "b"), NULL, NULL), expected)
271
272 # Named list
273 expected <- list(choiceNames = list("one", "two"), choiceValues = list("a", "b"))
274 x <- list(one = "a", two = "b")
275 expect_equal(normalizeChoicesArgs(x, NULL, NULL), expected)
276 expect_equal(normalizeChoicesArgs(NULL, names(x), unname(x)), expected)
277
278 # Using unnamed `choiceNames` and `choiceValues` vectors/lists directly
279 expect_equal(normalizeChoicesArgs(NULL, c("one", "two"), c("a", "b")), expected)
280 expect_equal(normalizeChoicesArgs(NULL, list("one", "two"), list("a", "b")), expected)
281
282 # Numbers
283 expected <- list(choiceNames = list("a", "b"), choiceValues = list("1", "2"))
284 expect_equal(normalizeChoicesArgs(c("a" = 1, "b" = 2), NULL, NULL), expected)
285 expect_equal(normalizeChoicesArgs(list("a" = 1, "b" = 2), NULL, NULL), expected)
286 expect_equal(normalizeChoicesArgs(NULL, c("a", "b"), c(1, 2)), expected)
287 expect_equal(normalizeChoicesArgs(NULL, list("a", "b"), list("1", "2")), expected)
288
289 # Using choiceNames with HTML and choiceValues
290 nms <- list(icon("calendar"), HTML("<p style='color:red;'>Red Text</p>"))
291 vals <- list("a", "b")
292 expected <- list(choiceNames = nms, choiceValues = vals)
293 expect_equal(normalizeChoicesArgs(NULL, nms, vals), expected)
294
295 # Attempt to use choices, AND choiceNames + choiceValues
296 x <- list("a", "b")
297 expect_warning(res <- normalizeChoicesArgs(x, nms, vals),
298 "Using `choices` argument; ignoring `choiceNames` and `choiceValues`.")
299 expect_equal(res, list(choiceNames = list("a", "b"), choiceValues = list("a", "b")))
300
301 # Set possibilities to character(0)
302 expected <- list(choiceNames = list(), choiceValues = list())
303 expect_equal(normalizeChoicesArgs(character(0), NULL, NULL), expected)
304 expect_equal(normalizeChoicesArgs(NULL, character(0), character(0)), expected)
305 expect_warning(res <- normalizeChoicesArgs(character(0), character(0), character(0)),
306 "Using `choices` argument; ignoring `choiceNames` and `choiceValues`.")
307 expect_equal(res, expected)
308
309 # Set possibilities to NULL in an inconsistent way
310 expected <- paste("One of `choiceNames` or `choiceValues` was set to NULL,",
311 "but either both or none should be NULL.")
312 expect_error(normalizeChoicesArgs(NULL, character(0), NULL, FALSE), expected, fixed = TRUE)
313 expect_error(normalizeChoicesArgs(NULL, NULL, character(0), FALSE), expected, fixed = TRUE)
314 expected <- paste("Please specify a non-empty vector for `choices` (or,",
315 "alternatively, for both `choiceNames` AND `choiceValues`).")
316 expect_error(normalizeChoicesArgs(NULL, character(0), NULL), expected, fixed = TRUE)
317 expect_error(normalizeChoicesArgs(NULL, NULL, character(0)), expected, fixed = TRUE)
318
319 # Set all possibilities to NULL (and mustExist = FALSE)
320 expected <- list(choiceNames = NULL, choiceValues = NULL)
321 expect_equal(normalizeChoicesArgs(NULL, NULL, NULL, FALSE), expected)
322 })
323
324 test_that("Choices need not be provided, can be NULL or c()", {
325
326 expected <- '<div id="cb" class="form-group shiny-input-checkboxgroup shiny-input-container" role="group" aria-labelledby="cb-label">\n <label class="control-label" id="cb-label" for="cb">Choose:</label>\n <div class="shiny-options-group"></div>\n</div>'
327 noChoices <- checkboxGroupInput("cb", "Choose:")
328 choicesNull <- checkboxGroupInput("cb", "Choose:", choices = NULL)
329 choicesCharacter <- checkboxGroupInput("cb", "Choose:", choices = c())
330 choicesCharacter0 <- checkboxGroupInput("cb", "Choose:", choices = character(0))
331 allChoicesNull <- checkboxGroupInput("cb", "Choose:", choices = NULL,
332 choiceNames = NULL, choiceValues = NULL)
333
334 expect_identical(noChoices, choicesNull)
335 expect_identical(noChoices, choicesCharacter)
336 expect_identical(noChoices, choicesCharacter0)
337 expect_identical(noChoices, allChoicesNull)
338
339 expect_equal(expected, format(noChoices))
340 })
341
342 # https://github.com/rstudio/shiny/pull/3187
343 test_that("radioButtons() and checkboxGroupInput() accessibility", {
344 rb <- radioButtons("foo", "bar", c("a", "b"))
345 rb_lbl <- rb$children[[1]]
346 expect_equal(rb$attribs$role, "radiogroup")
347 expect_equal(rb_lbl$name, "label")
348 expect_true(!is.null(rb_lbl$attribs$id))
349 expect_equal(
350 rb$attribs$`aria-labelledby`, rb_lbl$attribs$id
351 )
352
353 cbg <- checkboxGroupInput("foo", "bar", c("a", "b"))
354 cbg_lbl <- cbg$children[[1]]
355 expect_equal(cbg$attribs$role, "group")
356 expect_equal(cbg_lbl$name, "label")
357 expect_true(!is.null(cbg_lbl$attribs$id))
358 expect_equal(
359 cbg$attribs$`aria-labelledby`, cbg_lbl$attribs$id
360 )
361 })