about summary refs log tree commit diff
path: root/t/t0019-json-writer.sh
blob: 3b0c336b38e4c7f81268227984472cb35572cdc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/bin/sh

test_description='test json-writer JSON generation'
. ./test-lib.sh

test_expect_success 'unit test of json-writer routines' '
	test-tool json-writer -u
'

test_expect_success 'trivial object' '
	cat >expect <<-\EOF &&
	{}
	EOF
	cat >input <<-\EOF &&
	object
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'trivial array' '
	cat >expect <<-\EOF &&
	[]
	EOF
	cat >input <<-\EOF &&
	array
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'simple object' '
	cat >expect <<-\EOF &&
	{"a":"abc","b":42,"c":3.14,"d":true,"e":false,"f":null}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-int b 42
		object-double c 2 3.140
		object-true d
		object-false e
		object-null f
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'simple array' '
	cat >expect <<-\EOF &&
	["abc",42,3.14,true,false,null]
	EOF
	cat >input <<-\EOF &&
	array
		array-string abc
		array-int 42
		array-double 2 3.140
		array-true
		array-false
		array-null
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'escape quoting string' '
	cat >expect <<-\EOF &&
	{"a":"abc\\def"}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc\def
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'escape quoting string 2' '
	cat >expect <<-\EOF &&
	{"a":"abc\"def"}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc"def
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'nested inline object' '
	cat >expect <<-\EOF &&
	{"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":{"e":false,"f":null}}}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-int b 42
		object-object sub1
			object-double c 2 3.140
			object-true d
			object-object sub2
				object-false e
				object-null f
			end
		end
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'nested inline array' '
	cat >expect <<-\EOF &&
	["abc",42,[3.14,true,[false,null]]]
	EOF
	cat >input <<-\EOF &&
	array
		array-string abc
		array-int 42
		array-array
			array-double 2 3.140
			array-true
			array-array
				array-false
				array-null
			end
		end
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'nested inline object and array' '
	cat >expect <<-\EOF &&
	{"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,null]}}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-int b 42
		object-object sub1
			object-double c 2 3.140
			object-true d
			object-array sub2
				array-false
				array-null
			end
		end
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'nested inline object and array 2' '
	cat >expect <<-\EOF &&
	{"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,{"g":0,"h":1},null]}}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-int b 42
		object-object sub1
			object-double c 2 3.140
			object-true d
			object-array sub2
				array-false
				array-object
					object-int g 0
					object-int h 1
				end
				array-null
			end
		end
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'pretty nested inline object and array 2' '
	sed -e "s/^|//" >expect <<-\EOF &&
	|{
	|  "a": "abc",
	|  "b": 42,
	|  "sub1": {
	|    "c": 3.14,
	|    "d": true,
	|    "sub2": [
	|      false,
	|      {
	|        "g": 0,
	|        "h": 1
	|      },
	|      null
	|    ]
	|  }
	|}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-int b 42
		object-object sub1
			object-double c 2 3.140
			object-true d
			object-array sub2
				array-false
				array-object
					object-int g 0
					object-int h 1
				end
				array-null
			end
		end
	end
	EOF
	test-tool json-writer -p <input >actual &&
	test_cmp expect actual
'

test_expect_success 'inline object with no members' '
	cat >expect <<-\EOF &&
	{"a":"abc","empty":{},"b":42}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-object empty
		end
		object-int b 42
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'inline array with no members' '
	cat >expect <<-\EOF &&
	{"a":"abc","empty":[],"b":42}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-array empty
		end
		object-int b 42
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_expect_success 'larger empty example' '
	cat >expect <<-\EOF &&
	{"a":"abc","empty":[{},{},{},[],{}],"b":42}
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-array empty
			array-object
			end
			array-object
			end
			array-object
			end
			array-array
			end
			array-object
			end
		end
		object-int b 42
	end
	EOF
	test-tool json-writer <input >actual &&
	test_cmp expect actual
'

test_lazy_prereq PERLJSON '
	perl -MJSON -e "exit 0"
'

# As a sanity check, ask Perl to parse our generated JSON and recursively
# dump the resulting data in sorted order.  Confirm that that matches our
# expectations.
test_expect_success PERLJSON 'parse JSON using Perl' '
	cat >expect <<-\EOF &&
	row[0].a abc
	row[0].b 42
	row[0].sub1 hash
	row[0].sub1.c 3.14
	row[0].sub1.d 1
	row[0].sub1.sub2 array
	row[0].sub1.sub2[0] 0
	row[0].sub1.sub2[1] hash
	row[0].sub1.sub2[1].g 0
	row[0].sub1.sub2[1].h 1
	row[0].sub1.sub2[2] null
	EOF
	cat >input <<-\EOF &&
	object
		object-string a abc
		object-int b 42
		object-object sub1
			object-double c 2 3.140
			object-true d
			object-array sub2
				array-false
				array-object
					object-int g 0
					object-int h 1
				end
				array-null
			end
		end
	end
	EOF
	test-tool json-writer <input >output.json &&
	perl "$TEST_DIRECTORY"/t0019/parse_json.perl <output.json >actual &&
	test_cmp expect actual
'

test_done