Debian Codegraph @

tests/test-cran/test-json.R

raw
1 library(testit)
2
3 assert("tojson() works", {
4 (.tojson(NULL) %==% "null")
5 (.tojson(list()) %==% "{}")
6 (.tojson(NA) %==% 'null')
7 (.tojson(NA_character_) %==% 'null')
8 (.tojson(1:10) %==% "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]")
9 (.tojson(TRUE) %==% "true")
10 (.tojson(FALSE) %==% "false")
11
12 x = list(a = 1, b = list(c = 1:3, d = "abc"))
13 out = '{\n "a": 1,\n "b": {\n "c": [1, 2, 3],\n "d": "abc"\n }\n}'
14 (.tojson(x) %==% out)
15
16 x = list(c("a", "b"), 1:5, TRUE)
17 out = '[\n ["a", "b"],\n [1, 2, 3, 4, 5],\n true\n]'
18 (.tojson(x) %==% out)
19
20 (.tojson(list('"a b"' = 'quotes "\'')) %==% '{\n "\\"a b\\"": "quotes \\"\'"\n}')
21
22 # data frames
23 (.tojson(data.frame()) %==% '{}')
24 # by column for named data frames
25 x = data.frame(a = 1:3, b = c('x', 'y', 'z'))
26 (.tojson(x) %==% '{\n "a": [1, 2, 3],\n "b": ["x", "y", "z"]\n}')
27 # length 1 columns must be arrays, too
28 x = data.frame(a = 1, b = 'x')
29 (.tojson(x) %==% '{\n "a": [1],\n "b": ["x"]\n}')
30 # by row for unnamed data frames
31 x = unname(data.frame(a = 1:3, b = c('x', 'y', 'z')))
32 (.tojson(x) %==% '[\n [1, "x"],\n [2, "y"],\n [3, "z"]\n]')
33
34 x = list(a = 1:5, b = js("function() {return true;}"))
35 out = '{\n "a": [1, 2, 3, 4, 5],\n "b": function() {return true;}\n}'
36 (.tojson(x) %==% out)
37
38 res = tojson(list(NULL, 1:10, TRUE, FALSE))
39 # passing a json object through tojson() should return it unchanged
40 (tojson(res) %==% res)
41 })
42
43 assert('json_vector() converts atomic vectors to JSON', {
44 (json_vector(c('a', 'b'), to_array = TRUE) %==% '["a", "b"]')
45 (json_vector(c('a', 'b'), to_array = FALSE) %==% c('"a"', '"b"'))
46 (json_vector(1:3, to_array = TRUE, quote = FALSE) %==% '[1, 2, 3]')
47 # NA becomes null (character and numeric)
48 (json_vector(c('a', NA_character_), to_array = TRUE) %==% '["a", null]')
49 (json_vector(c(1, NA, 3), to_array = TRUE, quote = FALSE) %==% '[1, null, 3]')
50 # Inf/-Inf become Infinity/-Infinity (valid JS)
51 (json_vector(c(1, Inf, -Inf), to_array = TRUE, quote = FALSE) %==% '[1, Infinity, -Infinity]')
52 (json_vector(c(Inf, NA), to_array = TRUE, quote = FALSE) %==% '[Infinity, null]')
53 })
54
55 assert('json_atomic() handles Date, POSIXct, and factor', {
56 d = as.Date('2024-01-15')
57 (json_atomic(d) %==% 'new Date("2024-01-15")')
58 # Date/POSIXt in data.frames (I() must not trigger format.AsIs truncation)
59 t = as.POSIXct(c('2024-01-15 09:30:00', '2024-01-15 17:45:00'), tz = 'UTC')
60 (.tojson(data.frame(t = t)) %==%
61 '{\n "t": [new Date("2024-01-15 09:30:00"), new Date("2024-01-15 17:45:00")]\n}')
62 # single-row Date data.frame still produces an array
63 (.tojson(data.frame(d = d)) %==% '{\n "d": [new Date("2024-01-15")]\n}')
64 f = factor(c('a', 'b', 'a'))
65 (json_atomic(f) %==% '["a", "b", "a"]')
66 })
67
68 assert('.tojson() handles arrays', {
69 m = matrix(1:4, nrow = 2)
70 out = .tojson(m)
71 (grepl('\\[', out)) # should produce nested arrays
72 })
73
74 assert('json_vector() escapes control characters in strings', {
75 # newline, backspace, formfeed, carriage return, tab must all be escaped
76 (json_vector('\n', to_array = FALSE) %==% '"\\n"')
77 (json_vector('\b', to_array = FALSE) %==% '"\\b"')
78 (json_vector('\f', to_array = FALSE) %==% '"\\f"')
79 (json_vector('\r', to_array = FALSE) %==% '"\\r"')
80 (json_vector('\t', to_array = FALSE) %==% '"\\t"')
81 # backslash and double-quote must be escaped (via quote_string)
82 (json_vector('a\\b', to_array = FALSE) %==% '"a\\\\b"')
83 (json_vector('say "hi"', to_array = FALSE) %==% '"say \\"hi\\""')
84 })
85
86 assert('quote_string() returns character(0) for empty input', {
87 (quote_string(character(0)) %==% character(0))
88 })