Debian Codegraph @

parsers/hjson/hjson_test.go

raw
1 package hjson
2
3 import (
4 "github.com/stretchr/testify/assert"
5 "testing"
6 )
7
8 func TestHJSON_Unmarshal(t *testing.T) {
9 testCases := []struct {
10 name string
11 input []byte
12 keys []string
13 values []any
14 isErr bool
15 }{
16 {
17 name: "Empty HJSON",
18 input: []byte(`{}`),
19 },
20 {
21 name: "Valid HJSON",
22 input: []byte(`{
23 key: val
24 name: test
25 number: 2
26 }`),
27 keys: []string{"key", "name", "number"},
28 values: []any{"val", "test", 2.0},
29 },
30 {
31 name: "Commented HJSON",
32 input: []byte(`{
33 # hash style comments
34 # (because it's just one character)
35
36 // line style comments
37 // (because it's like C/JavaScript/...)
38
39 /* block style comments because
40 it allows you to comment out a block */
41
42 key: v1
43 name: Comments
44 number: 3
45 }`),
46 keys: []string{"key", "name", "number"},
47 values: []any{"v1", "Comments", 3.0},
48 },
49 {
50 name: "Quoted strings HJSON",
51 input: []byte(`{
52 JSON: "a string"
53
54 HJSON: a string
55
56 #notice, no escape necessary:
57 RegEx: \s+
58 }`),
59 keys: []string{"JSON", "HJSON", "RegEx"},
60 values: []any{"a string", "a string", `\s+`},
61 },
62 {
63 name: "Multiline strings HJSON",
64 input: []byte(`{
65 md:
66 '''
67 First line.
68 Second line.
69 This line is indented by two spaces.
70 '''
71 }`),
72 keys: []string{"md"},
73 values: []any{"First line.\nSecond line.\n This line is indented by two spaces."},
74 },
75 {
76 name: "Punctuators HJSON",
77 input: []byte(`{
78 "key name": "{ sample }"
79 "()": " sample at the start/end "
80 this: is OK though: {}[],:
81 }`),
82 keys: []string{"key name", "()", "this"},
83 values: []any{"{ sample }", " sample at the start/end ", "is OK though: {}[],:"},
84 },
85 {
86 name: "Invalid HJSON - missing curly brace",
87 input: []byte(`{
88 key: val,`),
89 isErr: true,
90 },
91 {
92 name: "Comma HJSON",
93 input: []byte(`{
94 key: a,
95 key_n: 3
96 key3: b,
97 }`),
98 keys: []string{"key", "key_n", "key3"},
99 values: []any{"a,", 3.0, "b,"},
100 },
101 {
102 name: "One quote in key - HJSON",
103 input: []byte(`{
104 "key: a
105 "key2": b
106 }`),
107 isErr: true,
108 },
109 {
110 name: "Comment without marks HJSON",
111 input: []byte(`{
112 Wrong commentary without #, // or /**/
113
114 key: 1
115 }`),
116 isErr: true,
117 },
118 {
119 name: "Wrong comment mark HJSON",
120 input: []byte(`{
121 $ Wrong comment mark
122
123 key: a
124 }`),
125 isErr: true,
126 },
127 {
128 name: "Wrong line style comment HJSON",
129 input: []byte(`{
130 / Wrong comment - / instead of //
131
132 key: a
133 }`),
134 isErr: true,
135 },
136 {
137 name: "Wrong multiline style comment HJSON",
138 input: []byte(`{
139 /* Multiline comment without second mark
140
141 key: b
142 }`),
143 isErr: true,
144 },
145 {
146 name: "Whitespace in the key - HJSON",
147 input: []byte(`{
148 key whitespace: a
149 }`),
150 isErr: true,
151 },
152 {
153 name: "Punctuator (comma) in the key - HJSON",
154 input: []byte(`{
155 key, comma: a
156 }`),
157 isErr: true,
158 },
159 {
160 name: "Complex HJSON - all types",
161 input: []byte(`{
162 # All types
163
164 // and comments
165
166 /* including
167 multiline style
168 comments */
169
170 array: [
171 1,
172 2,
173 3
174 ]
175
176 boolean: true,
177 color: gold
178 null: null
179 number: 123
180 object: {
181 "a": "b",
182 "c": "d"
183 },
184 string: "Hello World"
185 }`),
186 keys: []string{"array", "boolean", "color", "null", "number", "object", "string"},
187 values: []any{[]any{1.0, 2.0, 3.0},
188 true,
189 "gold",
190 nil,
191 123.0,
192 map[string]any{"a": "b", "c": "d"},
193 "Hello World"},
194 },
195 }
196 h := Parser()
197
198 for _, tc := range testCases {
199 t.Run(tc.name, func(t *testing.T) {
200 out, err := h.Unmarshal(tc.input)
201 if tc.isErr {
202 assert.NotNil(t, err)
203 } else {
204 assert.Nil(t, err)
205 for i, k := range tc.keys {
206 v := out[k]
207 assert.Equal(t, tc.values[i], v)
208 }
209 }
210 })
211 }
212 }
213
214 func TestHJSON_Marshal(t *testing.T) {
215 testCases := []struct {
216 name string
217 input map[string]any
218 output map[string]any
219 }{
220 {
221 name: "Empty HJSON",
222 input: map[string]any{},
223 output: map[string]any{},
224 },
225 {
226 name: "Valid HJSON",
227 input: map[string]any{
228 "key": "val",
229 "name": "test",
230 "number": 2.0,
231 },
232 output: map[string]any{
233 "key": "val",
234 "name": "test",
235 "number": 2.0,
236 },
237 },
238 {
239 name: "Multiline value HJSON",
240 input: map[string]any{
241 "md": `
242 '''
243 First line.
244 Second line.
245 This line is indented by two spaces.
246 '''`,
247 },
248 output: map[string]any{
249 "md": `
250 '''
251 First line.
252 Second line.
253 This line is indented by two spaces.
254 '''`,
255 },
256 },
257 {
258 name: "Complex HJSON - All types",
259 input: map[string]any{
260 "array": []any{1, 2, 3, 4, 5},
261 "boolean": true,
262 "color": "red",
263 "null": nil,
264 "number": 123,
265 "object": map[string]any{"a": "b", "c": "d"},
266 "string": "Hello HJSON",
267 },
268 output: map[string]any{
269 "array": []any{1.0, 2.0, 3.0, 4.0, 5.0},
270 "boolean": true,
271 "color": "red",
272 "null": nil,
273 "number": 123.0,
274 "object": map[string]any{"a": "b", "c": "d"},
275 "string": "Hello HJSON",
276 },
277 },
278 }
279
280 h := Parser()
281
282 for _, tc := range testCases {
283 t.Run(tc.name, func(t *testing.T) {
284 buf, err := h.Marshal(tc.input)
285 assert.Nil(t, err)
286 out, err := h.Unmarshal(buf)
287 assert.Nil(t, err)
288 for key, val := range out {
289 assert.Equal(t, tc.output[key], val)
290 }
291 })
292 }
293 }