Merge branch 'en/rebase-signoff-fix'
[git] / t / t0019-json-writer.sh
1 #!/bin/sh
2
3 test_description='test json-writer JSON generation'
4 . ./test-lib.sh
5
6 test_expect_success 'unit test of json-writer routines' '
7         test-tool json-writer -u
8 '
9
10 test_expect_success 'trivial object' '
11         cat >expect <<-\EOF &&
12         {}
13         EOF
14         cat >input <<-\EOF &&
15         object
16         end
17         EOF
18         test-tool json-writer <input >actual &&
19         test_cmp expect actual
20 '
21
22 test_expect_success 'trivial array' '
23         cat >expect <<-\EOF &&
24         []
25         EOF
26         cat >input <<-\EOF &&
27         array
28         end
29         EOF
30         test-tool json-writer <input >actual &&
31         test_cmp expect actual
32 '
33
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
50 '
51
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
68 '
69
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
81 '
82
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
94 '
95
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
110                         end
111                 end
112         end
113         EOF
114         test-tool json-writer <input >actual &&
115         test_cmp expect actual
116 '
117
118 test_expect_success 'nested inline array' '
119         cat >expect <<-\EOF &&
120         ["abc",42,[3.14,true,[false,null]]]
121         EOF
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
132                         end
133                 end
134         end
135         EOF
136         test-tool json-writer <input >actual &&
137         test_cmp expect actual
138 '
139
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]}}
143         EOF
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
154                         end
155                 end
156         end
157         EOF
158         test-tool json-writer <input >actual &&
159         test_cmp expect actual
160 '
161
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]}}
165         EOF
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
178                                 end
179                                 array-null
180                         end
181                 end
182         end
183         EOF
184         test-tool json-writer <input >actual &&
185         test_cmp expect actual
186 '
187
188 test_expect_success 'pretty nested inline object and array 2' '
189         sed -e "s/^|//" >expect <<-\EOF &&
190         |{
191         |  "a": "abc",
192         |  "b": 42,
193         |  "sub1": {
194         |    "c": 3.14,
195         |    "d": true,
196         |    "sub2": [
197         |      false,
198         |      {
199         |        "g": 0,
200         |        "h": 1
201         |      },
202         |      null
203         |    ]
204         |  }
205         |}
206         EOF
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
219                                 end
220                                 array-null
221                         end
222                 end
223         end
224         EOF
225         test-tool json-writer -p <input >actual &&
226         test_cmp expect actual
227 '
228
229 test_expect_success 'inline object with no members' '
230         cat >expect <<-\EOF &&
231         {"a":"abc","empty":{},"b":42}
232         EOF
233         cat >input <<-\EOF &&
234         object
235                 object-string a abc
236                 object-object empty
237                 end
238                 object-int b 42
239         end
240         EOF
241         test-tool json-writer <input >actual &&
242         test_cmp expect actual
243 '
244
245 test_expect_success 'inline array with no members' '
246         cat >expect <<-\EOF &&
247         {"a":"abc","empty":[],"b":42}
248         EOF
249         cat >input <<-\EOF &&
250         object
251                 object-string a abc
252                 object-array empty
253                 end
254                 object-int b 42
255         end
256         EOF
257         test-tool json-writer <input >actual &&
258         test_cmp expect actual
259 '
260
261 test_expect_success 'larger empty example' '
262         cat >expect <<-\EOF &&
263         {"a":"abc","empty":[{},{},{},[],{}],"b":42}
264         EOF
265         cat >input <<-\EOF &&
266         object
267                 object-string a abc
268                 object-array empty
269                         array-object
270                         end
271                         array-object
272                         end
273                         array-object
274                         end
275                         array-array
276                         end
277                         array-object
278                         end
279                 end
280                 object-int b 42
281         end
282         EOF
283         test-tool json-writer <input >actual &&
284         test_cmp expect actual
285 '
286
287 test_lazy_prereq PERLJSON '
288         perl -MJSON -e "exit 0"
289 '
290
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
307         EOF
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
320                                 end
321                                 array-null
322                         end
323                 end
324         end
325         EOF
326         test-tool json-writer <input >output.json &&
327         perl "$TEST_DIRECTORY"/t0019/parse_json.perl <output.json >actual &&
328         test_cmp expect actual
329 '
330
331 test_done