| 1 |
|
| 2 |
test_that("sample JSON strings can be read", { |
| 3 |
|
| 4 |
expect_identical( |
| 5 |
renv_json_read(text = '[true, false, null]'), |
| 6 |
list(TRUE, FALSE, NULL) |
| 7 |
) |
| 8 |
|
| 9 |
expect_identical( |
| 10 |
renv_json_read(text = '{"{}": "::"}'), |
| 11 |
list("{}" = "::") |
| 12 |
) |
| 13 |
|
| 14 |
expect_identical( |
| 15 |
renv_json_read(text = '[{"a": 1.0}, {"b": -1e5}]'), |
| 16 |
list(list(a = 1.0), list(b = -1E5)) |
| 17 |
) |
| 18 |
|
| 19 |
expect_identical( |
| 20 |
renv_json_read(text = '[{}, [], {}]'), |
| 21 |
list(named(list()), list(), named(list())) |
| 22 |
) |
| 23 |
|
| 24 |
expect_identical( |
| 25 |
renv_json_read(text = '[{"]": "["}]'), |
| 26 |
list(list("]" = "[")) |
| 27 |
) |
| 28 |
|
| 29 |
}) |
| 30 |
|
| 31 |
test_that("sample R objects can be converted to JSON", { |
| 32 |
|
| 33 |
before <- list(alpha = 1, beta = 2) |
| 34 |
json <- renv_json_convert(before) |
| 35 |
after <- renv_json_read(text = json) |
| 36 |
expect_equal(before, after) |
| 37 |
|
| 38 |
}) |
| 39 |
|
| 40 |
test_that("empty R lists are converted as expected", { |
| 41 |
|
| 42 |
data <- list() |
| 43 |
json <- renv_json_convert(data) |
| 44 |
expect_equal(json, "[]") |
| 45 |
|
| 46 |
names(data) <- character() |
| 47 |
json <- renv_json_convert(data) |
| 48 |
expect_equal(json, "{}") |
| 49 |
|
| 50 |
}) |
| 51 |
|
| 52 |
test_that("we can parse a GitHub remotes specification", { |
| 53 |
|
| 54 |
skip_slow() |
| 55 |
|
| 56 |
data <- renv_remotes_resolve("rstudio/renv") |
| 57 |
expect_true(data$Source == "GitHub") |
| 58 |
expect_true(data$RemoteUsername == "rstudio") |
| 59 |
expect_true(data$RemoteRepo == "renv") |
| 60 |
|
| 61 |
}) |
| 62 |
|
| 63 |
test_that("we can parse a GitHub remotes specification with 'wininet'", { |
| 64 |
skip_on_cran() |
| 65 |
skip_on_os("windows") |
| 66 |
skip_if_not(renv_platform_windows()) |
| 67 |
|
| 68 |
renv_scope_envvars(RENV_DOWNLOAD_FILE_METHOD = "wininet") |
| 69 |
data <- renv_remotes_resolve("rstudio/renv") |
| 70 |
expect_true(data$Source == "GitHub") |
| 71 |
expect_true(data$RemoteUsername == "rstudio") |
| 72 |
expect_true(data$RemoteRepo == "renv") |
| 73 |
|
| 74 |
}) |
| 75 |
|
| 76 |
test_that("we can read json containing escape characters", { |
| 77 |
actual <- list(data = "\\\"") |
| 78 |
json <- renv_json_convert(actual) |
| 79 |
expected <- renv_json_read(text = json) |
| 80 |
expect_equal(actual, expected) |
| 81 |
}) |
| 82 |
|
| 83 |
test_that("JSON null is read as R NULL", { |
| 84 |
json <- "{\"NULL\": null}" |
| 85 |
actual <- renv_json_read(text = json) |
| 86 |
expected <- list("NULL" = NULL) |
| 87 |
expect_equal(actual, expected) |
| 88 |
}) |
| 89 |
|
| 90 |
test_that("some common control characters are escaped", { |
| 91 |
json <- renv_json_convert("\b\f\n\r\t") |
| 92 |
expect_equal(json, "\"\\b\\f\\n\\r\\t\"") |
| 93 |
}) |
| 94 |
|
| 95 |
test_that("scalar values are boxed if requested", { |
| 96 |
|
| 97 |
json <- renv_json_convert( |
| 98 |
object = list(A = "hello", B = "world"), |
| 99 |
config = list(box = "B") |
| 100 |
) |
| 101 |
|
| 102 |
value <- renv_json_read(text = json) |
| 103 |
expect_equal(value, list(A = "hello", B = list("world"))) |
| 104 |
|
| 105 |
}) |
| 106 |
|
| 107 |
test_that("the renv + jsonlite JSON readers are compatible", { |
| 108 |
|
| 109 |
skip_if_not_installed("jsonlite") |
| 110 |
|
| 111 |
renv_tests_scope("breakfast") |
| 112 |
init() |
| 113 |
|
| 114 |
lhs <- renv_json_read_default("renv.lock") |
| 115 |
rhs <- renv_json_read_jsonlite("renv.lock") |
| 116 |
expect_equal(lhs, rhs) |
| 117 |
|
| 118 |
}) |
| 119 |
|
| 120 |
test_that("NA values are properly serialized", { |
| 121 |
|
| 122 |
expect_equal(renv_json_write(NA, file = NULL), "null") |
| 123 |
expect_equal(renv_json_write(NA_character_, file = NULL), "null") |
| 124 |
|
| 125 |
expect_equal(renv_json_write(NA_real_, file = NULL), "null") |
| 126 |
expect_equal(renv_json_write(NA_integer_, file = NULL), "null") |
| 127 |
expect_equal(renv_json_write(NA_complex_, file = NULL), "null") |
| 128 |
expect_equal(renv_json_write(NaN, file = NULL), "null") |
| 129 |
|
| 130 |
}) |
| 131 |
|
| 132 |
test_that("we fall back to the internal JSON reader if jsonlite fails", { |
| 133 |
|
| 134 |
skip_if_not_installed("jsonlite") |
| 135 |
json <- renv_json_read(text = "[\"key\": NA]") |
| 136 |
expect_equal(json, list(key = NA)) |
| 137 |
|
| 138 |
}) |
| 139 |
|
| 140 |
test_that("json-read.R can function standalone", { |
| 141 |
|
| 142 |
renv_tests_scope() |
| 143 |
|
| 144 |
renv <- asNamespace("renv") |
| 145 |
keys <- ls(envir = renv, pattern = "^renv_json_read", all.names = TRUE) |
| 146 |
vals <- mget(c("%||%", keys), envir = renv) |
| 147 |
|
| 148 |
# put those into a separate environment inheriting only from base, and |
| 149 |
# re-mark those as inheriting from base (so they only 'see' each-other) |
| 150 |
envir <- new.env(parent = baseenv()) |
| 151 |
list2env(vals, envir = envir) |
| 152 |
|
| 153 |
# for each function, check that it only uses functions from base |
| 154 |
ok <- list() |
| 155 |
for (val in vals) { |
| 156 |
recurse(body(val), function(node) { |
| 157 |
if (is.call(node) && is.symbol(node[[1L]])) { |
| 158 |
lhs <- as.character(node[[1L]]) |
| 159 |
ok[[lhs]] <<- exists(lhs, envir = envir) |
| 160 |
} |
| 161 |
}) |
| 162 |
} |
| 163 |
|
| 164 |
# convert to logical vector |
| 165 |
ok <- convert(ok, "logical") |
| 166 |
bad <- names(ok)[!ok] |
| 167 |
if (length(bad) != 0L) { |
| 168 |
fmt <- "json-read.R is not standalone: %s" |
| 169 |
stopf(fmt, paste(bad, collapse = ", ")) |
| 170 |
} |
| 171 |
|
| 172 |
}) |
| 173 |
|
| 174 |
test_that("we can parse long JSON strings containing unicode newlines", { |
| 175 |
|
| 176 |
ns <- 10^(1:5) |
| 177 |
|
| 178 |
ch <- "\\u000a" |
| 179 |
for (n in ns) { |
| 180 |
chs <- paste(rep.int(ch, n), collapse = "") |
| 181 |
text <- sprintf("\"%s\"", chs) |
| 182 |
actual <- renv_json_read_default(text = text) |
| 183 |
expected <- paste(rep.int("\n", n), collapse = "") |
| 184 |
expect_equal(actual, expected) |
| 185 |
} |
| 186 |
|
| 187 |
}) |