| 1 |
test_that("Private randomness works at startup", { |
| 2 |
|
| 3 |
if (exists(".Random.seed", envir = .GlobalEnv)) |
| 4 |
rm(".Random.seed", envir = .GlobalEnv) |
| 5 |
.globals$ownSeed <- NULL |
| 6 |
# Just make sure this doesn't blow up |
| 7 |
expect_no_error(createUniqueId(4)) |
| 8 |
}) |
| 9 |
|
| 10 |
test_that("Setting process-wide seed doesn't affect private randomness", { |
| 11 |
set.seed(0) |
| 12 |
id1 <- createUniqueId(4) |
| 13 |
set.seed(0) |
| 14 |
id2 <- createUniqueId(4) |
| 15 |
|
| 16 |
expect_false(identical(id1, id2)) |
| 17 |
}) |
| 18 |
|
| 19 |
test_that("Resetting private seed doesn't result in dupes", { |
| 20 |
.globals$ownSeed <- NULL |
| 21 |
id3 <- createUniqueId(4) |
| 22 |
# Make sure we let enough time pass that reinitializing the seed is |
| 23 |
# going to result in a different value. This is especially required |
| 24 |
# on Windows. |
| 25 |
Sys.sleep(1) |
| 26 |
set.seed(0) |
| 27 |
.globals$ownSeed <- NULL |
| 28 |
id4 <- createUniqueId(4) |
| 29 |
|
| 30 |
expect_false(identical(id3, id4)) |
| 31 |
}) |
| 32 |
|
| 33 |
test_that("Clearing process-wide seed doesn't affect private randomness", { |
| 34 |
set.seed(NULL) |
| 35 |
id5 <- createUniqueId(4) |
| 36 |
set.seed(NULL) |
| 37 |
id6 <- createUniqueId(4) |
| 38 |
|
| 39 |
expect_false(identical(id5, id6)) |
| 40 |
}) |
| 41 |
|
| 42 |
test_that("Setting the private seed explicitly results in identical values", { |
| 43 |
set.seed(0) |
| 44 |
.globals$ownSeed <- .Random.seed |
| 45 |
id7 <- createUniqueId(4) |
| 46 |
set.seed(0) |
| 47 |
.globals$ownSeed <- .Random.seed |
| 48 |
id8 <- createUniqueId(4) |
| 49 |
|
| 50 |
expect_identical(id7, id8) |
| 51 |
}) |
| 52 |
|
| 53 |
test_that("Private and 'public' random streams are independent and work the same", { |
| 54 |
set.seed(0) |
| 55 |
public <- c(runif(1), runif(1), runif(1)) |
| 56 |
withPrivateSeed(set.seed(0)) |
| 57 |
private <- c(withPrivateSeed(runif(1)), withPrivateSeed(runif(1)), withPrivateSeed(runif(1))) |
| 58 |
expect_identical(public, private) |
| 59 |
|
| 60 |
# Interleaved calls to runif() with private and public streams |
| 61 |
set.seed(0) |
| 62 |
withPrivateSeed(set.seed(0)) |
| 63 |
public <- numeric() |
| 64 |
private <- numeric() |
| 65 |
public[1] <- runif(1) |
| 66 |
private[1] <- withPrivateSeed(runif(1)) |
| 67 |
private[2] <- withPrivateSeed(runif(1)) |
| 68 |
public[2] <- runif(1) |
| 69 |
public[3] <- runif(1) |
| 70 |
private[3] <- withPrivateSeed(runif(1)) |
| 71 |
expect_identical(public, private) |
| 72 |
}) |
| 73 |
|
| 74 |
test_that("need() works as expected", { |
| 75 |
|
| 76 |
# These are all falsy |
| 77 |
|
| 78 |
expect_false(need(FALSE, FALSE)) |
| 79 |
expect_false(need(NULL, FALSE)) |
| 80 |
expect_false(need("", FALSE)) |
| 81 |
|
| 82 |
expect_false(need(character(0), FALSE)) |
| 83 |
expect_false(need(logical(0), FALSE)) |
| 84 |
expect_false(need(numeric(0), FALSE)) |
| 85 |
expect_false(need(integer(0), FALSE)) |
| 86 |
expect_false(need(complex(0), FALSE)) |
| 87 |
expect_false(need(matrix(), FALSE)) |
| 88 |
|
| 89 |
expect_false(need(NA, FALSE)) |
| 90 |
expect_false(need(NA_integer_, FALSE)) |
| 91 |
expect_false(need(NA_real_, FALSE)) |
| 92 |
expect_false(need(NA_complex_, FALSE)) |
| 93 |
expect_false(need(NA_character_, FALSE)) |
| 94 |
|
| 95 |
expect_false(need(c(NA, NA, FALSE), FALSE)) |
| 96 |
expect_false(need(c(FALSE), FALSE)) |
| 97 |
|
| 98 |
expect_false(need(try(stop("boom"), silent = TRUE), FALSE)) |
| 99 |
|
| 100 |
# These are all truthy |
| 101 |
|
| 102 |
expect_null(need(0, FALSE)) |
| 103 |
expect_null(need(1:10, FALSE)) |
| 104 |
expect_null(need(LETTERS, FALSE)) |
| 105 |
expect_null(need("NA", FALSE)) |
| 106 |
expect_null(need(TRUE, FALSE)) |
| 107 |
expect_null(need(c(NA, NA, TRUE), FALSE)) |
| 108 |
expect_null(need(c(FALSE, FALSE, TRUE), FALSE)) |
| 109 |
}) |
| 110 |
|
| 111 |
test_that("req works", { |
| 112 |
expect_error(req(TRUE, FALSE)) |
| 113 |
expect_error(req(TRUE, stop("boom"))) |
| 114 |
expect_equal(req(1, TRUE), 1) |
| 115 |
|
| 116 |
# req arguments short circuit when a falsy value is found |
| 117 |
value <- 0 |
| 118 |
expect_error(req(NULL, value <- 1)) |
| 119 |
expect_equal(value, 0) |
| 120 |
|
| 121 |
# first argument is evaluated exactly once |
| 122 |
value <- 0 |
| 123 |
req(value <- value + 1) |
| 124 |
expect_equal(value, 1) |
| 125 |
|
| 126 |
# correct environment is used |
| 127 |
req2 <- function(...) { |
| 128 |
req(...) |
| 129 |
} |
| 130 |
value <- 0 |
| 131 |
req2(value <- value + 1) |
| 132 |
expect_equal(value, 1) |
| 133 |
|
| 134 |
# Lots of args are supported |
| 135 |
expect_error(do.call(req, c(as.list(1:1000), list(NULL)))) |
| 136 |
expect_equal(1, do.call(req, as.list(1:1000))) |
| 137 |
}) |
| 138 |
|
| 139 |
test_that("any_unnamed works as expected", { |
| 140 |
expect_false(any_unnamed(list())) |
| 141 |
expect_true(any_unnamed(list(1,2,3))) |
| 142 |
expect_true(any_unnamed(list(A = 1,2,3))) |
| 143 |
expect_false(any_unnamed(list(A = 1,B = 2,C = 3))) |
| 144 |
|
| 145 |
# List with named elements removed |
| 146 |
x <- list(A = 1, B = 2, 3, 4) |
| 147 |
x <- x[3:4] |
| 148 |
expect_true(any_unnamed(x)) |
| 149 |
}) |
| 150 |
|
| 151 |
test_that("sortByName works as expected", { |
| 152 |
# Error if any unnamed elements |
| 153 |
expect_error(sortByName(c("a", "b"))) |
| 154 |
expect_error(sortByName(list(a=1, 2))) |
| 155 |
|
| 156 |
expect_identical(sortByName(NULL), NULL) |
| 157 |
expect_identical(sortByName(numeric(0)), numeric(0)) |
| 158 |
expect_identical(sortByName(character(0)), character(0)) |
| 159 |
# Empty unnamed list |
| 160 |
expect_identical(sortByName(list()), list()) |
| 161 |
# Empty named list |
| 162 |
expect_identical(sortByName(list(a=1)[0]), list(a=1)[0]) |
| 163 |
|
| 164 |
expect_identical(sortByName(list(b=1, a=2)), list(a=2, b=1)) |
| 165 |
expect_identical(sortByName(list(b=1)), list(b=1)) |
| 166 |
|
| 167 |
# Ties are resolved by using original order |
| 168 |
expect_identical(sortByName(list(b=1, a=2, b=3)), list(a=2, b=1, b=3)) |
| 169 |
expect_identical(sortByName(list(b=3, a=2, b=1)), list(a=2, b=3, b=1)) |
| 170 |
|
| 171 |
# Make sure atomic vectors work |
| 172 |
expect_identical(sortByName(c(b=1, a=2)), c(a=2, b=1)) |
| 173 |
expect_identical(sortByName(c(b=1, a=2, b=3)), c(a=2, b=1, b=3)) |
| 174 |
}) |
| 175 |
|
| 176 |
test_that("sortByName gives expected sort order using `radix` method", { |
| 177 |
skip_on_cran() |
| 178 |
|
| 179 |
# without UTF-8 |
| 180 |
items <- list("aa"=1, "bb"=2, "AA"=5, "BB"=6, "a_"=7, "b_"=8, "_A"=9, "_B"=10) |
| 181 |
items_expected <- list(AA=5, BB=6, "_A"=9, "_B"=10, a_=7, aa=1, b_=8, bb=2) |
| 182 |
# sort(names(items), method = "radix") |
| 183 |
# #> [1] "AA" "BB" "_A" "_B" "a_" "aa" "b_" "bb" |
| 184 |
# sort(names(items), method = "shell") |
| 185 |
# #> [1] "_A" "_B" "a_" "aa" "AA" "b_" "bb" "BB" |
| 186 |
expect_identical(sortByName(items, method = "radix"), items_expected) |
| 187 |
|
| 188 |
skip_on_os("windows") # windows can't handle UTF-8 |
| 189 |
|
| 190 |
# with UTF-8 |
| 191 |
items <- list("aa"=1, "bb"=2, "åå"=3, "∫∫"=4, "AA"=5, "BB"=6, "a_"=7, "b_"=8, "_A"=9, "_B"=10) |
| 192 |
items_expected <- list(AA=5, BB=6, "_A"=9, "_B"=10, a_=7, aa=1, b_=8, bb=2, "åå"=3, "∫∫"=4) |
| 193 |
# sort(name(items), method = "radix") |
| 194 |
# #> [1] "AA" "BB" "_A" "_B" "a_" "aa" "b_" "bb" "åå" "∫∫" |
| 195 |
# sort(items, method = "shell") |
| 196 |
# #> [1] "_A" "_B" "∫∫" "a_" "aa" "AA" "åå" "b_" "bb" "BB" |
| 197 |
expect_identical(sortByName(items, method = "radix"), items_expected) |
| 198 |
}) |
| 199 |
|
| 200 |
test_that("Callbacks fire in predictable order", { |
| 201 |
cb <- Callbacks$new() |
| 202 |
|
| 203 |
x <- numeric(0) |
| 204 |
cb$register(function() { |
| 205 |
x <<- c(x, 1) |
| 206 |
}) |
| 207 |
cb$register(function() { |
| 208 |
x <<- c(x, 2) |
| 209 |
}) |
| 210 |
cb$register(function() { |
| 211 |
x <<- c(x, 3) |
| 212 |
}) |
| 213 |
cb$invoke() |
| 214 |
expect_equal(x, c(1, 2, 3)) |
| 215 |
}) |
| 216 |
|
| 217 |
test_that("Application directories are identified", { |
| 218 |
tests <- test_path("..", "test-modules", "12_counter", "tests") |
| 219 |
expect_false(isAppDir(tests), "tests directory not an app") |
| 220 |
expect_true(isAppDir(dirname(tests)), "tests parent directory is an app") |
| 221 |
expect_equal( |
| 222 |
findEnclosingApp(tests), |
| 223 |
normalizePath(dirname(tests), winslash = "/") |
| 224 |
) |
| 225 |
expect_equal( |
| 226 |
findEnclosingApp(dirname(tests)), |
| 227 |
normalizePath(dirname(tests), winslash = "/") |
| 228 |
) |
| 229 |
}) |
| 230 |
|
| 231 |
test_that("dateYMD works", { |
| 232 |
expect_identical(dateYMD("2020-01-14"),"2020-01-14") |
| 233 |
expect_identical(dateYMD("2020/01/14"),"2020-01-14") |
| 234 |
expect_identical( |
| 235 |
dateYMD(c("2020-01-14", "2019-11-05")), |
| 236 |
c("2020-01-14", "2019-11-05") |
| 237 |
) |
| 238 |
expect_identical( |
| 239 |
dateYMD(c("2020/01/14", "2019/11/05")), |
| 240 |
c("2020-01-14", "2019-11-05") |
| 241 |
) |
| 242 |
expect_identical( |
| 243 |
dateYMD(as.POSIXct("2025-11-09 00:01:59", tz = "Asia/Tokyo")), |
| 244 |
"2025-11-09" |
| 245 |
) |
| 246 |
|
| 247 |
expect_warning(val <- dateYMD("")) |
| 248 |
expect_identical(val, "") |
| 249 |
|
| 250 |
expect_warning(val <- dateYMD(c(NA))) |
| 251 |
expect_identical(val, NA) |
| 252 |
|
| 253 |
expect_warning(val <- dateYMD(c("", NA))) |
| 254 |
expect_identical(val, c("", NA)) |
| 255 |
|
| 256 |
# If there are any bad values, the entire thing goes through unchanged |
| 257 |
expect_warning(val <- dateYMD(c("2019/11/05", NA))) |
| 258 |
expect_identical(val, c("2019/11/05", NA)) |
| 259 |
|
| 260 |
expect_warning(val <- dateYMD(c("2019/11/05", ""))) |
| 261 |
expect_identical(val, c("2019/11/05", "")) |
| 262 |
}) |
| 263 |
|
| 264 |
test_that("quoToFunction handles nested quosures", { |
| 265 |
quo_inner <- local({ |
| 266 |
x <- 1 |
| 267 |
rlang::quo(x) |
| 268 |
}) |
| 269 |
|
| 270 |
quo_outer <- rlang::quo(!!quo_inner + 1) |
| 271 |
|
| 272 |
func <- quoToFunction(quo_outer, "foo") |
| 273 |
expect_identical(func(), 2) |
| 274 |
}) |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
test_that("toJSON can set digits using options - default", { |
| 279 |
withr::local_options(list(shiny.json.digits = NULL)) |
| 280 |
expect_equal( |
| 281 |
as.character(toJSON(pi)), |
| 282 |
"[3.141592653589793]" |
| 283 |
) |
| 284 |
}) |
| 285 |
test_that("toJSON can set digits using options - number", { |
| 286 |
withr::local_options(list(shiny.json.digits = 4)) |
| 287 |
expect_equal( |
| 288 |
as.character(toJSON(pi)), |
| 289 |
"[3.1416]" |
| 290 |
) |
| 291 |
}) |
| 292 |
test_that("toJSON can set digits using options - asis number", { |
| 293 |
withr::local_options(list(shiny.json.digits = I(4))) |
| 294 |
expect_equal( |
| 295 |
as.character(toJSON(pi)), |
| 296 |
"[3.142]" |
| 297 |
) |
| 298 |
}) |