]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0019-json-writer.sh
The third batch
[thirdparty/git.git] / t / t0019-json-writer.sh
CommitLineData
75459410
JH
1#!/bin/sh
2
3test_description='test json-writer JSON generation'
1caaa858
ÆAB
4
5TEST_PASSES_SANITIZE_LEAK=true
75459410
JH
6. ./test-lib.sh
7
8test_expect_success 'unit test of json-writer routines' '
9 test-tool json-writer -u
10'
11
12test_expect_success 'trivial object' '
13 cat >expect <<-\EOF &&
14 {}
15 EOF
16 cat >input <<-\EOF &&
17 object
18 end
19 EOF
20 test-tool json-writer <input >actual &&
21 test_cmp expect actual
22'
23
24test_expect_success 'trivial array' '
25 cat >expect <<-\EOF &&
26 []
27 EOF
28 cat >input <<-\EOF &&
29 array
30 end
31 EOF
32 test-tool json-writer <input >actual &&
33 test_cmp expect actual
34'
35
36test_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
52'
53
54test_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
70'
71
72test_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
83'
84
85test_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
96'
97
98test_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}}}
101 EOF
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
112 end
113 end
114 end
115 EOF
116 test-tool json-writer <input >actual &&
117 test_cmp expect actual
118'
119
120test_expect_success 'nested inline array' '
121 cat >expect <<-\EOF &&
122 ["abc",42,[3.14,true,[false,null]]]
123 EOF
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
134 end
135 end
136 end
137 EOF
138 test-tool json-writer <input >actual &&
139 test_cmp expect actual
140'
141
142test_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]}}
145 EOF
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
156 end
157 end
158 end
159 EOF
160 test-tool json-writer <input >actual &&
161 test_cmp expect actual
162'
163
164test_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]}}
167 EOF
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
180 end
181 array-null
182 end
183 end
184 end
185 EOF
186 test-tool json-writer <input >actual &&
187 test_cmp expect actual
188'
189
190test_expect_success 'pretty nested inline object and array 2' '
191 sed -e "s/^|//" >expect <<-\EOF &&
192 |{
193 | "a": "abc",
194 | "b": 42,
195 | "sub1": {
196 | "c": 3.14,
197 | "d": true,
198 | "sub2": [
199 | false,
200 | {
201 | "g": 0,
202 | "h": 1
203 | },
204 | null
205 | ]
206 | }
207 |}
208 EOF
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
221 end
222 array-null
223 end
224 end
225 end
226 EOF
227 test-tool json-writer -p <input >actual &&
228 test_cmp expect actual
229'
230
231test_expect_success 'inline object with no members' '
232 cat >expect <<-\EOF &&
233 {"a":"abc","empty":{},"b":42}
234 EOF
235 cat >input <<-\EOF &&
236 object
237 object-string a abc
238 object-object empty
239 end
240 object-int b 42
241 end
242 EOF
243 test-tool json-writer <input >actual &&
244 test_cmp expect actual
245'
246
247test_expect_success 'inline array with no members' '
248 cat >expect <<-\EOF &&
249 {"a":"abc","empty":[],"b":42}
250 EOF
251 cat >input <<-\EOF &&
252 object
253 object-string a abc
254 object-array empty
255 end
256 object-int b 42
257 end
258 EOF
259 test-tool json-writer <input >actual &&
260 test_cmp expect actual
261'
262
263test_expect_success 'larger empty example' '
264 cat >expect <<-\EOF &&
265 {"a":"abc","empty":[{},{},{},[],{}],"b":42}
266 EOF
267 cat >input <<-\EOF &&
268 object
269 object-string a abc
270 object-array empty
271 array-object
272 end
273 array-object
274 end
275 array-object
276 end
277 array-array
278 end
279 array-object
280 end
281 end
282 object-int b 42
283 end
284 EOF
285 test-tool json-writer <input >actual &&
286 test_cmp expect actual
287'
288
289test_lazy_prereq PERLJSON '
290 perl -MJSON -e "exit 0"
291'
292
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.
296test_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
309 EOF
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
322 end
323 array-null
324 end
325 end
326 end
327 EOF
328 test-tool json-writer <input >output.json &&
329 perl "$TEST_DIRECTORY"/t0019/parse_json.perl <output.json >actual &&
330 test_cmp expect actual
331'
332
333test_done