AtlatestRepositorysigil-json
sigil-json / tree / testtest-json.sgl
1
;;; Test suite for (sigil json)3
(import (sigil test)4
(sigil json))6
(test-group "json-encode - basic types"7
(test "encode string"8
(assert-equal "\"hello\"" (json-encode "hello")))10
(test "encode number"11
(assert-equal "42" (json-encode 42)))13
(test "encode float"14
(assert-equal "3.14" (json-encode 3.14)))16
(test "encode true"17
(assert-equal "true" (json-encode #t)))19
(test "encode false"20
(assert-equal "false" (json-encode #f)))22
(test "encode null"23
(assert-equal "null" (json-encode 'null))))25
(test-group "json-encode - collections"26
(test "encode empty dict"27
(assert-equal "{}" (json-encode #{})))29
(test "encode dict"30
(assert-equal "{\"name\":\"Alice\"}" (json-encode #{ name: "Alice" })))32
(test "encode empty array"33
(assert-equal "[]" (json-encode #[])))35
(test "encode array"36
(assert-equal "[1,2,3]" (json-encode #[1 2 3])))38
(test "encode empty list"39
(assert-equal "[]" (json-encode '())))41
(test "encode list"42
(assert-equal "[1,2,3]" (json-encode '(1 2 3))))44
(test "encode alist as object"45
(assert-equal "{\"a\":1}" (json-encode '((a . 1))))))47
(test-group "json-encode - nested"48
(test "encode nested dict"49
(assert-equal "{\"outer\":{\"inner\":42}}"50
(json-encode #{ outer: #{ inner: 42 } })))52
(test "encode dict with array"53
(assert-equal "{\"items\":[1,2,3]}"54
(json-encode #{ items: #[1 2 3] }))))56
(test-group "json-decode - basic types"57
(test "decode string"58
(assert-equal "hello" (json-decode "\"hello\"")))60
(test "decode integer"61
(assert-equal 42 (json-decode "42")))63
(test "decode float"64
(assert-equal 3.14 (json-decode "3.14")))66
(test "decode true"67
(assert-equal #t (json-decode "true")))69
(test "decode false"70
(assert-equal #f (json-decode "false")))72
(test "decode null"73
(assert-equal 'null (json-decode "null"))))75
(test-group "json-decode - collections"76
(test "decode empty object"77
(assert-equal #{} (json-decode "{}")))79
(test "decode object"80
(assert-equal #{ name: "Alice" } (json-decode "{\"name\": \"Alice\"}")))82
(test "decode empty array"83
(assert-equal #[] (json-decode "[]")))85
(test "decode array"86
(assert-equal #[1 2 3] (json-decode "[1, 2, 3]"))))88
(test-group "json-decode - nested"89
(test "decode nested object"90
(assert-equal #{ outer: #{ inner: 42 } }91
(json-decode "{\"outer\": {\"inner\": 42}}")))93
(test "decode object with array"94
(assert-equal #{ items: #[1 2 3] }95
(json-decode "{\"items\": [1, 2, 3]}")))97
(test "decode array of objects"98
(assert-equal #[#{ a: 1 } #{ b: 2 }]99
(json-decode "[{\"a\": 1}, {\"b\": 2}]"))))101
(test-group "json-decode - string escapes"102
(test "decode escaped quote"103
(assert-equal "say \"hello\"" (json-decode "\"say \\\"hello\\\"\"")))105
(test "decode escaped newline"106
(assert-equal "line1\nline2" (json-decode "\"line1\\nline2\"")))108
(test "decode escaped tab"109
(assert-equal "a\tb" (json-decode "\"a\\tb\"")))111
(test "decode unicode escape"112
(assert-equal "A" (json-decode "\"\\u0041\"")))114
(test "decode surrogate pair (emoji)"115
(assert-equal "\x1F600;" (json-decode "\"\\uD83D\\uDE00\"")))117
(test "decode surrogate pair (musical symbol)"118
(assert-equal "\x1D11E;" (json-decode "\"\\uD834\\uDD1E\"")))120
(test "reject bare low surrogate"121
(assert-true122
(guard (exn (else #t))123
(json-decode "\"\\uDE00\"")124
#f))))126
(test-group "json-null?"127
(test "null symbol is null"128
(assert-true (json-null? 'null)))130
(test "false is not null"131
(assert-false (json-null? #f)))133
(test "empty list is not null"134
(assert-false (json-null? '())))136
(test "decoded null is null"137
(assert-true (json-null? (json-decode "null")))))139
(test-group "json-get-in - basic access"140
(test "get top-level key"141
(assert-equal "Alice"142
(json-get-in #{ name: "Alice" } '(name:))))144
(test "get nested key"145
(assert-equal 42146
(json-get-in #{ outer: #{ inner: 42 } } '(outer: inner:))))148
(test "get array element"149
(assert-equal "b"150
(json-get-in #["a" "b" "c"] '(1))))152
(test "get from empty path"153
(assert-equal #{ a: 1 }154
(json-get-in #{ a: 1 } '()))))156
(test-group "json-get-in - mixed paths"157
(test "dict then array then dict"158
(let ((data (json-decode "{\"users\": [{\"name\": \"Alice\"}, {\"name\": \"Bob\"}]}")))159
(assert-equal "Alice" (json-get-in data '(users: 0 name:)))))161
(test "array then dict"162
(let ((data (json-decode "[{\"x\": 10}, {\"x\": 20}]")))163
(assert-equal 20 (json-get-in data '(1 x:)))))165
(test "deeply nested"166
(let ((data (json-decode "{\"a\": {\"b\": {\"c\": [1, 2, 3]}}}")))167
(assert-equal 2 (json-get-in data '(a: b: c: 1))))))169
(test-group "json-get-in - missing keys"170
(test "missing key returns false"171
(assert-false (json-get-in #{ a: 1 } '(missing:))))173
(test "missing key returns default"174
(assert-equal "default"175
(json-get-in #{ a: 1 } '(missing:) "default")))177
(test "missing nested key returns false"178
(assert-false (json-get-in #{ a: #{ b: 1 } } '(a: c:))))180
(test "array index out of bounds returns false"181
(assert-false (json-get-in #[1 2 3] '(10))))183
(test "array index out of bounds returns default"184
(assert-equal "oob"185
(json-get-in #[1 2 3] '(10) "oob")))187
(test "negative index returns false"188
(assert-false (json-get-in #[1 2 3] '(-1)))))190
(test-group "json-get-in - type mismatches"191
(test "array access on dict returns false"192
(assert-false (json-get-in #{ a: 1 } '(0))))194
(test "dict access on array returns false"195
(assert-false (json-get-in #[1 2 3] '(name:))))197
(test "access on primitive returns false"198
(assert-false (json-get-in "hello" '(name:)))))200
(test-group "json-get-in - string and symbol keys"201
(test "string key works"202
(assert-equal 42 (json-get-in #{ name: 42 } '("name"))))204
(test "symbol key works"205
(assert-equal 42 (json-get-in #{ name: 42 } '(name)))))