Merge branch 'sb/diff-color-moved-config-option-fixup'
[git/raj.git] / t / t0019-json-writer.sh
blob3b0c336b38e4c7f81268227984472cb35572cdc2
1 #!/bin/sh
3 test_description='test json-writer JSON generation'
4 . ./test-lib.sh
6 test_expect_success 'unit test of json-writer routines' '
7 test-tool json-writer -u
10 test_expect_success 'trivial object' '
11 cat >expect <<-\EOF &&
13 EOF
14 cat >input <<-\EOF &&
15 object
16 end
17 EOF
18 test-tool json-writer <input >actual &&
19 test_cmp expect actual
22 test_expect_success 'trivial array' '
23 cat >expect <<-\EOF &&
25 EOF
26 cat >input <<-\EOF &&
27 array
28 end
29 EOF
30 test-tool json-writer <input >actual &&
31 test_cmp expect actual
34 test_expect_success 'simple object' '
35 cat >expect <<-\EOF &&
36 {"a":"abc","b":42,"c":3.14,"d":true,"e":false,"f":null}
37 EOF
38 cat >input <<-\EOF &&
39 object
40 object-string a abc
41 object-int b 42
42 object-double c 2 3.140
43 object-true d
44 object-false e
45 object-null f
46 end
47 EOF
48 test-tool json-writer <input >actual &&
49 test_cmp expect actual
52 test_expect_success 'simple array' '
53 cat >expect <<-\EOF &&
54 ["abc",42,3.14,true,false,null]
55 EOF
56 cat >input <<-\EOF &&
57 array
58 array-string abc
59 array-int 42
60 array-double 2 3.140
61 array-true
62 array-false
63 array-null
64 end
65 EOF
66 test-tool json-writer <input >actual &&
67 test_cmp expect actual
70 test_expect_success 'escape quoting string' '
71 cat >expect <<-\EOF &&
72 {"a":"abc\\def"}
73 EOF
74 cat >input <<-\EOF &&
75 object
76 object-string a abc\def
77 end
78 EOF
79 test-tool json-writer <input >actual &&
80 test_cmp expect actual
83 test_expect_success 'escape quoting string 2' '
84 cat >expect <<-\EOF &&
85 {"a":"abc\"def"}
86 EOF
87 cat >input <<-\EOF &&
88 object
89 object-string a abc"def
90 end
91 EOF
92 test-tool json-writer <input >actual &&
93 test_cmp expect actual
96 test_expect_success 'nested inline object' '
97 cat >expect <<-\EOF &&
98 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":{"e":false,"f":null}}}
99 EOF
100 cat >input <<-\EOF &&
101 object
102 object-string a abc
103 object-int b 42
104 object-object sub1
105 object-double c 2 3.140
106 object-true d
107 object-object sub2
108 object-false e
109 object-null f
114 test-tool json-writer <input >actual &&
115 test_cmp expect actual
118 test_expect_success 'nested inline array' '
119 cat >expect <<-\EOF &&
120 ["abc",42,[3.14,true,[false,null]]]
122 cat >input <<-\EOF &&
123 array
124 array-string abc
125 array-int 42
126 array-array
127 array-double 2 3.140
128 array-true
129 array-array
130 array-false
131 array-null
136 test-tool json-writer <input >actual &&
137 test_cmp expect actual
140 test_expect_success 'nested inline object and array' '
141 cat >expect <<-\EOF &&
142 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,null]}}
144 cat >input <<-\EOF &&
145 object
146 object-string a abc
147 object-int b 42
148 object-object sub1
149 object-double c 2 3.140
150 object-true d
151 object-array sub2
152 array-false
153 array-null
158 test-tool json-writer <input >actual &&
159 test_cmp expect actual
162 test_expect_success 'nested inline object and array 2' '
163 cat >expect <<-\EOF &&
164 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,{"g":0,"h":1},null]}}
166 cat >input <<-\EOF &&
167 object
168 object-string a abc
169 object-int b 42
170 object-object sub1
171 object-double c 2 3.140
172 object-true d
173 object-array sub2
174 array-false
175 array-object
176 object-int g 0
177 object-int h 1
179 array-null
184 test-tool json-writer <input >actual &&
185 test_cmp expect actual
188 test_expect_success 'pretty nested inline object and array 2' '
189 sed -e "s/^|//" >expect <<-\EOF &&
191 | "a": "abc",
192 | "b": 42,
193 | "sub1": {
194 | "c": 3.14,
195 | "d": true,
196 | "sub2": [
197 | false,
199 | "g": 0,
200 | "h": 1
201 | },
202 | null
207 cat >input <<-\EOF &&
208 object
209 object-string a abc
210 object-int b 42
211 object-object sub1
212 object-double c 2 3.140
213 object-true d
214 object-array sub2
215 array-false
216 array-object
217 object-int g 0
218 object-int h 1
220 array-null
225 test-tool json-writer -p <input >actual &&
226 test_cmp expect actual
229 test_expect_success 'inline object with no members' '
230 cat >expect <<-\EOF &&
231 {"a":"abc","empty":{},"b":42}
233 cat >input <<-\EOF &&
234 object
235 object-string a abc
236 object-object empty
238 object-int b 42
241 test-tool json-writer <input >actual &&
242 test_cmp expect actual
245 test_expect_success 'inline array with no members' '
246 cat >expect <<-\EOF &&
247 {"a":"abc","empty":[],"b":42}
249 cat >input <<-\EOF &&
250 object
251 object-string a abc
252 object-array empty
254 object-int b 42
257 test-tool json-writer <input >actual &&
258 test_cmp expect actual
261 test_expect_success 'larger empty example' '
262 cat >expect <<-\EOF &&
263 {"a":"abc","empty":[{},{},{},[],{}],"b":42}
265 cat >input <<-\EOF &&
266 object
267 object-string a abc
268 object-array empty
269 array-object
271 array-object
273 array-object
275 array-array
277 array-object
280 object-int b 42
283 test-tool json-writer <input >actual &&
284 test_cmp expect actual
287 test_lazy_prereq PERLJSON '
288 perl -MJSON -e "exit 0"
291 # As a sanity check, ask Perl to parse our generated JSON and recursively
292 # dump the resulting data in sorted order. Confirm that that matches our
293 # expectations.
294 test_expect_success PERLJSON 'parse JSON using Perl' '
295 cat >expect <<-\EOF &&
296 row[0].a abc
297 row[0].b 42
298 row[0].sub1 hash
299 row[0].sub1.c 3.14
300 row[0].sub1.d 1
301 row[0].sub1.sub2 array
302 row[0].sub1.sub2[0] 0
303 row[0].sub1.sub2[1] hash
304 row[0].sub1.sub2[1].g 0
305 row[0].sub1.sub2[1].h 1
306 row[0].sub1.sub2[2] null
308 cat >input <<-\EOF &&
309 object
310 object-string a abc
311 object-int b 42
312 object-object sub1
313 object-double c 2 3.140
314 object-true d
315 object-array sub2
316 array-false
317 array-object
318 object-int g 0
319 object-int h 1
321 array-null
326 test-tool json-writer <input >output.json &&
327 perl "$TEST_DIRECTORY"/t0019/parse_json.perl <output.json >actual &&
328 test_cmp expect actual
331 test_done