File: test-json.R

package info (click to toggle)
r-cran-xfun 0.37%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 792 kB
  • sloc: ansic: 242; sh: 22; makefile: 2
file content (27 lines) | stat: -rw-r--r-- 845 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
library(testit)

assert("tojson() works", {
  (tojson(NULL) %==% "null")
  (tojson(list()) %==% "{}")
  (has_error(tojson(Sys.Date())))
  (has_error(tojson(NA)))
  (tojson(NA_character_) %==% '"NA"')
  (tojson(1:10) %==% "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]")
  (tojson(TRUE) %==% "true")
  (tojson(FALSE) %==% "false")

  x = list(a = 1, b = list(c = 1:3, d = "abc"))
  out = '{\n"a": 1,\n"b": {\n"c": [1, 2, 3],\n"d": "abc"\n}\n}'
  (tojson(x) %==% out)

  x = list(c("a", "b"), 1:5, TRUE)
  out = '[["a", "b"], [1, 2, 3, 4, 5], true]'
  (tojson(x) %==% out)

  (tojson(list('"a b"' = 'quotes "\'')) %==% '{\n"\\"a b\\"": "quotes \\"\'"\n}')

  JS = function(x) structure(x, class = "JS_EVAL")
  x = list(a = 1:5, b = JS("function() {return true;}"))
  out = '{\n"a": [1, 2, 3, 4, 5],\n"b": function() {return true;}\n}'
  (tojson(x) %==% out)
})