MyFirstContribution: mention contrib/contacts/git-contacts
[git/gitster.git] / t / t0019-json-writer.sh
blob19a730c29ed8f791d7885c31ff2a908cb1b5dfb3
1 #!/bin/sh
3 test_description='test json-writer JSON generation'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 test_expect_success 'unit test of json-writer routines' '
9 test-tool json-writer -u
12 test_expect_success 'trivial object' '
13 cat >expect <<-\EOF &&
15 EOF
16 cat >input <<-\EOF &&
17 object
18 end
19 EOF
20 test-tool json-writer <input >actual &&
21 test_cmp expect actual
24 test_expect_success 'trivial array' '
25 cat >expect <<-\EOF &&
27 EOF
28 cat >input <<-\EOF &&
29 array
30 end
31 EOF
32 test-tool json-writer <input >actual &&
33 test_cmp expect actual
36 test_expect_success 'simple object' '
37 cat >expect <<-\EOF &&
38 {"a":"abc","b":42,"c":3.14,"d":true,"e":false,"f":null}
39 EOF
40 cat >input <<-\EOF &&
41 object
42 object-string a abc
43 object-int b 42
44 object-double c 2 3.140
45 object-true d
46 object-false e
47 object-null f
48 end
49 EOF
50 test-tool json-writer <input >actual &&
51 test_cmp expect actual
54 test_expect_success 'simple array' '
55 cat >expect <<-\EOF &&
56 ["abc",42,3.14,true,false,null]
57 EOF
58 cat >input <<-\EOF &&
59 array
60 array-string abc
61 array-int 42
62 array-double 2 3.140
63 array-true
64 array-false
65 array-null
66 end
67 EOF
68 test-tool json-writer <input >actual &&
69 test_cmp expect actual
72 test_expect_success 'escape quoting string' '
73 cat >expect <<-\EOF &&
74 {"a":"abc\\def"}
75 EOF
76 cat >input <<-\EOF &&
77 object
78 object-string a abc\def
79 end
80 EOF
81 test-tool json-writer <input >actual &&
82 test_cmp expect actual
85 test_expect_success 'escape quoting string 2' '
86 cat >expect <<-\EOF &&
87 {"a":"abc\"def"}
88 EOF
89 cat >input <<-\EOF &&
90 object
91 object-string a abc"def
92 end
93 EOF
94 test-tool json-writer <input >actual &&
95 test_cmp expect actual
98 test_expect_success 'nested inline object' '
99 cat >expect <<-\EOF &&
100 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":{"e":false,"f":null}}}
102 cat >input <<-\EOF &&
103 object
104 object-string a abc
105 object-int b 42
106 object-object sub1
107 object-double c 2 3.140
108 object-true d
109 object-object sub2
110 object-false e
111 object-null f
116 test-tool json-writer <input >actual &&
117 test_cmp expect actual
120 test_expect_success 'nested inline array' '
121 cat >expect <<-\EOF &&
122 ["abc",42,[3.14,true,[false,null]]]
124 cat >input <<-\EOF &&
125 array
126 array-string abc
127 array-int 42
128 array-array
129 array-double 2 3.140
130 array-true
131 array-array
132 array-false
133 array-null
138 test-tool json-writer <input >actual &&
139 test_cmp expect actual
142 test_expect_success 'nested inline object and array' '
143 cat >expect <<-\EOF &&
144 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,null]}}
146 cat >input <<-\EOF &&
147 object
148 object-string a abc
149 object-int b 42
150 object-object sub1
151 object-double c 2 3.140
152 object-true d
153 object-array sub2
154 array-false
155 array-null
160 test-tool json-writer <input >actual &&
161 test_cmp expect actual
164 test_expect_success 'nested inline object and array 2' '
165 cat >expect <<-\EOF &&
166 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,{"g":0,"h":1},null]}}
168 cat >input <<-\EOF &&
169 object
170 object-string a abc
171 object-int b 42
172 object-object sub1
173 object-double c 2 3.140
174 object-true d
175 object-array sub2
176 array-false
177 array-object
178 object-int g 0
179 object-int h 1
181 array-null
186 test-tool json-writer <input >actual &&
187 test_cmp expect actual
190 test_expect_success 'pretty nested inline object and array 2' '
191 sed -e "s/^|//" >expect <<-\EOF &&
193 | "a": "abc",
194 | "b": 42,
195 | "sub1": {
196 | "c": 3.14,
197 | "d": true,
198 | "sub2": [
199 | false,
201 | "g": 0,
202 | "h": 1
203 | },
204 | null
209 cat >input <<-\EOF &&
210 object
211 object-string a abc
212 object-int b 42
213 object-object sub1
214 object-double c 2 3.140
215 object-true d
216 object-array sub2
217 array-false
218 array-object
219 object-int g 0
220 object-int h 1
222 array-null
227 test-tool json-writer -p <input >actual &&
228 test_cmp expect actual
231 test_expect_success 'inline object with no members' '
232 cat >expect <<-\EOF &&
233 {"a":"abc","empty":{},"b":42}
235 cat >input <<-\EOF &&
236 object
237 object-string a abc
238 object-object empty
240 object-int b 42
243 test-tool json-writer <input >actual &&
244 test_cmp expect actual
247 test_expect_success 'inline array with no members' '
248 cat >expect <<-\EOF &&
249 {"a":"abc","empty":[],"b":42}
251 cat >input <<-\EOF &&
252 object
253 object-string a abc
254 object-array empty
256 object-int b 42
259 test-tool json-writer <input >actual &&
260 test_cmp expect actual
263 test_expect_success 'larger empty example' '
264 cat >expect <<-\EOF &&
265 {"a":"abc","empty":[{},{},{},[],{}],"b":42}
267 cat >input <<-\EOF &&
268 object
269 object-string a abc
270 object-array empty
271 array-object
273 array-object
275 array-object
277 array-array
279 array-object
282 object-int b 42
285 test-tool json-writer <input >actual &&
286 test_cmp expect actual
289 test_lazy_prereq PERLJSON '
290 perl -MJSON -e "exit 0"
293 # As a sanity check, ask Perl to parse our generated JSON and recursively
294 # dump the resulting data in sorted order. Confirm that that matches our
295 # expectations.
296 test_expect_success PERLJSON 'parse JSON using Perl' '
297 cat >expect <<-\EOF &&
298 row[0].a abc
299 row[0].b 42
300 row[0].sub1 hash
301 row[0].sub1.c 3.14
302 row[0].sub1.d 1
303 row[0].sub1.sub2 array
304 row[0].sub1.sub2[0] 0
305 row[0].sub1.sub2[1] hash
306 row[0].sub1.sub2[1].g 0
307 row[0].sub1.sub2[1].h 1
308 row[0].sub1.sub2[2] null
310 cat >input <<-\EOF &&
311 object
312 object-string a abc
313 object-int b 42
314 object-object sub1
315 object-double c 2 3.140
316 object-true d
317 object-array sub2
318 array-false
319 array-object
320 object-int g 0
321 object-int h 1
323 array-null
328 test-tool json-writer <input >output.json &&
329 perl "$TEST_DIRECTORY"/t0019/parse_json.perl <output.json >actual &&
330 test_cmp expect actual
333 test_done