summaryrefslogtreecommitdiffstats
path: root/t/helper/test-json-writer.c
blob: 8c3edacc0007df7ddb52f2f65d00e44b95edeb5a (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "test-tool.h"
#include "cache.h"
#include "json-writer.h"

static const char *expect_obj1 = "{\"a\":\"abc\",\"b\":42,\"c\":true}";
static const char *expect_obj2 = "{\"a\":-1,\"b\":2147483647,\"c\":0}";
static const char *expect_obj3 = "{\"a\":0,\"b\":4294967295,\"c\":9223372036854775807}";
static const char *expect_obj4 = "{\"t\":true,\"f\":false,\"n\":null}";
static const char *expect_obj5 = "{\"abc\\tdef\":\"abc\\\\def\"}";
static const char *expect_obj6 = "{\"a\":3.14}";

static const char *pretty_obj1 = ("{\n"
				  "  \"a\": \"abc\",\n"
				  "  \"b\": 42,\n"
				  "  \"c\": true\n"
				  "}");
static const char *pretty_obj2 = ("{\n"
				  "  \"a\": -1,\n"
				  "  \"b\": 2147483647,\n"
				  "  \"c\": 0\n"
				  "}");
static const char *pretty_obj3 = ("{\n"
				  "  \"a\": 0,\n"
				  "  \"b\": 4294967295,\n"
				  "  \"c\": 9223372036854775807\n"
				  "}");
static const char *pretty_obj4 = ("{\n"
				  "  \"t\": true,\n"
				  "  \"f\": false,\n"
				  "  \"n\": null\n"
				  "}");

static struct json_writer obj1 = JSON_WRITER_INIT;
static struct json_writer obj2 = JSON_WRITER_INIT;
static struct json_writer obj3 = JSON_WRITER_INIT;
static struct json_writer obj4 = JSON_WRITER_INIT;
static struct json_writer obj5 = JSON_WRITER_INIT;
static struct json_writer obj6 = JSON_WRITER_INIT;

static void make_obj1(int pretty)
{
	jw_object_begin(&obj1, pretty);
	{
		jw_object_string(&obj1, "a", "abc");
		jw_object_intmax(&obj1, "b", 42);
		jw_object_true(&obj1, "c");
	}
	jw_end(&obj1);
}

static void make_obj2(int pretty)
{
	jw_object_begin(&obj2, pretty);
	{
		jw_object_intmax(&obj2, "a", -1);
		jw_object_intmax(&obj2, "b", 0x7fffffff);
		jw_object_intmax(&obj2, "c", 0);
	}
	jw_end(&obj2);
}

static void make_obj3(int pretty)
{
	jw_object_begin(&obj3, pretty);
	{
		jw_object_intmax(&obj3, "a", 0);
		jw_object_intmax(&obj3, "b", 0xffffffff);
		jw_object_intmax(&obj3, "c", 0x7fffffffffffffffULL);
	}
	jw_end(&obj3);
}

static void make_obj4(int pretty)
{
	jw_object_begin(&obj4, pretty);
	{
		jw_object_true(&obj4, "t");
		jw_object_false(&obj4, "f");
		jw_object_null(&obj4, "n");
	}
	jw_end(&obj4);
}

static void make_obj5(int pretty)
{
	jw_object_begin(&obj5, pretty);
	{
		jw_object_string(&obj5, "abc" "\x09" "def", "abc" "\\" "def");
	}
	jw_end(&obj5);
}

static void make_obj6(int pretty)
{
	jw_object_begin(&obj6, pretty);
	{
		jw_object_double(&obj6, "a", 2, 3.14159);
	}
	jw_end(&obj6);
}

static const char *expect_arr1 = "[\"abc\",42,true]";
static const char *expect_arr2 = "[-1,2147483647,0]";
static const char *expect_arr3 = "[0,4294967295,9223372036854775807]";
static const char *expect_arr4 = "[true,false,null]";

static const char *pretty_arr1 = ("[\n"
				  "  \"abc\",\n"
				  "  42,\n"
				  "  true\n"
				  "]");
static const char *pretty_arr2 = ("[\n"
				  "  -1,\n"
				  "  2147483647,\n"
				  "  0\n"
				  "]");
static const char *pretty_arr3 = ("[\n"
				  "  0,\n"
				  "  4294967295,\n"
				  "  9223372036854775807\n"
				  "]");
static const char *pretty_arr4 = ("[\n"
				  "  true,\n"
				  "  false,\n"
				  "  null\n"
				  "]");

static struct json_writer arr1 = JSON_WRITER_INIT;
static struct json_writer arr2 = JSON_WRITER_INIT;
static struct json_writer arr3 = JSON_WRITER_INIT;
static struct json_writer arr4 = JSON_WRITER_INIT;

static void make_arr1(int pretty)
{
	jw_array_begin(&arr1, pretty);
	{
		jw_array_string(&arr1, "abc");
		jw_array_intmax(&arr1, 42);
		jw_array_true(&arr1);
	}
	jw_end(&arr1);
}

static void make_arr2(int pretty)
{
	jw_array_begin(&arr2, pretty);
	{
		jw_array_intmax(&arr2, -1);
		jw_array_intmax(&arr2, 0x7fffffff);
		jw_array_intmax(&arr2, 0);
	}
	jw_end(&arr2);
}

static void make_arr3(int pretty)
{
	jw_array_begin(&arr3, pretty);
	{
		jw_array_intmax(&arr3, 0);
		jw_array_intmax(&arr3, 0xffffffff);
		jw_array_intmax(&arr3, 0x7fffffffffffffffULL);
	}
	jw_end(&arr3);
}

static void make_arr4(int pretty)
{
	jw_array_begin(&arr4, pretty);
	{
		jw_array_true(&arr4);
		jw_array_false(&arr4);
		jw_array_null(&arr4);
	}
	jw_end(&arr4);
}

static char *expect_nest1 =
	"{\"obj1\":{\"a\":\"abc\",\"b\":42,\"c\":true},\"arr1\":[\"abc\",42,true]}";

static struct json_writer nest1 = JSON_WRITER_INIT;

static void make_nest1(int pretty)
{
	make_obj1(0);
	make_arr1(0);

	jw_object_begin(&nest1, pretty);
	{
		jw_object_sub_jw(&nest1, "obj1", &obj1);
		jw_object_sub_jw(&nest1, "arr1", &arr1);
	}
	jw_end(&nest1);

	jw_release(&obj1);
	jw_release(&arr1);
}

static char *expect_inline1 =
	"{\"obj1\":{\"a\":\"abc\",\"b\":42,\"c\":true},\"arr1\":[\"abc\",42,true]}";

static char *pretty_inline1 =
	("{\n"
	 "  \"obj1\": {\n"
	 "    \"a\": \"abc\",\n"
	 "    \"b\": 42,\n"
	 "    \"c\": true\n"
	 "  },\n"
	 "  \"arr1\": [\n"
	 "    \"abc\",\n"
	 "    42,\n"
	 "    true\n"
	 "  ]\n"
	 "}");

static struct json_writer inline1 = JSON_WRITER_INIT;

static void make_inline1(int pretty)
{
	jw_object_begin(&inline1, pretty);
	{
		jw_object_inline_begin_object(&inline1, "obj1");
		{
			jw_object_string(&inline1, "a", "abc");
			jw_object_intmax(&inline1, "b", 42);
			jw_object_true(&inline1, "c");
		}
		jw_end(&inline1);
		jw_object_inline_begin_array(&inline1, "arr1");
		{
			jw_array_string(&inline1, "abc");
			jw_array_intmax(&inline1, 42);
			jw_array_true(&inline1);
		}
		jw_end(&inline1);
	}
	jw_end(&inline1);
}

static char *expect_inline2 =
	"[[1,2],[3,4],{\"a\":\"abc\"}]";

static char *pretty_inline2 =
	("[\n"
	 "  [\n"
	 "    1,\n"
	 "    2\n"
	 "  ],\n"
	 "  [\n"
	 "    3,\n"
	 "    4\n"
	 "  ],\n"
	 "  {\n"
	 "    \"a\": \"abc\"\n"
	 "  }\n"
	 "]");

static struct json_writer inline2 = JSON_WRITER_INIT;

static void make_inline2(int pretty)
{
	jw_array_begin(&inline2, pretty);
	{
		jw_array_inline_begin_array(&inline2);
		{
			jw_array_intmax(&inline2, 1);
			jw_array_intmax(&inline2, 2);
		}
		jw_end(&inline2);
		jw_array_inline_begin_array(&inline2);
		{
			jw_array_intmax(&inline2, 3);
			jw_array_intmax(&inline2, 4);
		}
		jw_end(&inline2);
		jw_array_inline_begin_object(&inline2);
		{
			jw_object_string(&inline2, "a", "abc");
		}
		jw_end(&inline2);
	}
	jw_end(&inline2);
}

/*
 * When super is compact, we expect subs to be compacted (even if originally
 * pretty).
 */
static const char *expect_mixed1 =
	("{\"obj1\":{\"a\":\"abc\",\"b\":42,\"c\":true},"
	 "\"arr1\":[\"abc\",42,true]}");

/*
 * When super is pretty, a compact sub (obj1) is kept compact and a pretty
 * sub (arr1) is re-indented.
 */
static const char *pretty_mixed1 =
	("{\n"
	 "  \"obj1\": {\"a\":\"abc\",\"b\":42,\"c\":true},\n"
	 "  \"arr1\": [\n"
	 "    \"abc\",\n"
	 "    42,\n"
	 "    true\n"
	 "  ]\n"
	 "}");

static struct json_writer mixed1 = JSON_WRITER_INIT;

static void make_mixed1(int pretty)
{
	jw_init(&obj1);
	jw_init(&arr1);

	make_obj1(0); /* obj1 is compact */
	make_arr1(1); /* arr1 is pretty */

	jw_object_begin(&mixed1, pretty);
	{
		jw_object_sub_jw(&mixed1, "obj1", &obj1);
		jw_object_sub_jw(&mixed1, "arr1", &arr1);
	}
	jw_end(&mixed1);

	jw_release(&obj1);
	jw_release(&arr1);
}

static void cmp(const char *test, const struct json_writer *jw, const char *exp)
{
	if (!strcmp(jw->json.buf, exp))
		return;

	printf("error[%s]: observed '%s' expected '%s'\n",
	       test, jw->json.buf, exp);
	exit(1);
}

#define t(v) do { make_##v(0); cmp(#v, &v, expect_##v); jw_release(&v); } while (0)
#define p(v) do { make_##v(1); cmp(#v, &v, pretty_##v); jw_release(&v); } while (0)

/*
 * Run some basic regression tests with some known patterns.
 * These tests also demonstrate how to use the jw_ API.
 */
static int unit_tests(void)
{
	/* comptact (canonical) forms */
	t(obj1);
	t(obj2);
	t(obj3);
	t(obj4);
	t(obj5);
	t(obj6);

	t(arr1);
	t(arr2);
	t(arr3);
	t(arr4);

	t(nest1);

	t(inline1);
	t(inline2);

	jw_init(&obj1);
	jw_init(&obj2);
	jw_init(&obj3);
	jw_init(&obj4);

	jw_init(&arr1);
	jw_init(&arr2);
	jw_init(&arr3);
	jw_init(&arr4);

	jw_init(&inline1);
	jw_init(&inline2);

	/* pretty forms */
	p(obj1);
	p(obj2);
	p(obj3);
	p(obj4);

	p(arr1);
	p(arr2);
	p(arr3);
	p(arr4);

	p(inline1);
	p(inline2);

	/* mixed forms */
	t(mixed1);
	p(mixed1);

	return 0;
}

static void get_s(int line_nr, char **s_in)
{
	*s_in = strtok(NULL, " ");
	if (!*s_in)
		die("line[%d]: expected: <s>", line_nr);
}

static void get_i(int line_nr, intmax_t *s_in)
{
	char *s;
	char *endptr;

	get_s(line_nr, &s);

	*s_in = strtol(s, &endptr, 10);
	if (*endptr || errno == ERANGE)
		die("line[%d]: invalid integer value", line_nr);
}

static void get_d(int line_nr, double *s_in)
{
	char *s;
	char *endptr;

	get_s(line_nr, &s);

	*s_in = strtod(s, &endptr);
	if (*endptr || errno == ERANGE)
		die("line[%d]: invalid float value", line_nr);
}

static int pretty;

#define MAX_LINE_LENGTH (64 * 1024)

static char *get_trimmed_line(char *buf, int buf_size)
{
	int len;

	if (!fgets(buf, buf_size, stdin))
		return NULL;

	len = strlen(buf);
	while (len > 0) {
		char c = buf[len - 1];
		if (c == '\n' || c == '\r' || c == ' ' || c == '\t')
			buf[--len] = 0;
		else
			break;
	}

	while (*buf == ' ' || *buf == '\t')
		buf++;

	return buf;
}

static int scripted(void)
{
	struct json_writer jw = JSON_WRITER_INIT;
	char buf[MAX_LINE_LENGTH];
	char *line;
	int line_nr = 0;

	line = get_trimmed_line(buf, MAX_LINE_LENGTH);
	if (!line)
		return 0;

	if (!strcmp(line, "object"))
		jw_object_begin(&jw, pretty);
	else if (!strcmp(line, "array"))
		jw_array_begin(&jw, pretty);
	else
		die("expected first line to be 'object' or 'array'");

	while ((line = get_trimmed_line(buf, MAX_LINE_LENGTH)) != NULL) {
		char *verb;
		char *key;
		char *s_value;
		intmax_t i_value;
		double d_value;

		line_nr++;

		verb = strtok(line, " ");

		if (!strcmp(verb, "end")) {
			jw_end(&jw);
		}
		else if (!strcmp(verb, "object-string")) {
			get_s(line_nr, &key);
			get_s(line_nr, &s_value);
			jw_object_string(&jw, key, s_value);
		}
		else if (!strcmp(verb, "object-int")) {
			get_s(line_nr, &key);
			get_i(line_nr, &i_value);
			jw_object_intmax(&jw, key, i_value);
		}
		else if (!strcmp(verb, "object-double")) {
			get_s(line_nr, &key);
			get_i(line_nr, &i_value);
			get_d(line_nr, &d_value);
			jw_object_double(&jw, key, i_value, d_value);
		}
		else if (!strcmp(verb, "object-true")) {
			get_s(line_nr, &key);
			jw_object_true(&jw, key);
		}
		else if (!strcmp(verb, "object-false")) {
			get_s(line_nr, &key);
			jw_object_false(&jw, key);
		}
		else if (!strcmp(verb, "object-null")) {
			get_s(line_nr, &key);
			jw_object_null(&jw, key);
		}
		else if (!strcmp(verb, "object-object")) {
			get_s(line_nr, &key);
			jw_object_inline_begin_object(&jw, key);
		}
		else if (!strcmp(verb, "object-array")) {
			get_s(line_nr, &key);
			jw_object_inline_begin_array(&jw, key);
		}
		else if (!strcmp(verb, "array-string")) {
			get_s(line_nr, &s_value);
			jw_array_string(&jw, s_value);
		}
		else if (!strcmp(verb, "array-int")) {
			get_i(line_nr, &i_value);
			jw_array_intmax(&jw, i_value);
		}
		else if (!strcmp(verb, "array-double")) {
			get_i(line_nr, &i_value);
			get_d(line_nr, &d_value);
			jw_array_double(&jw, i_value, d_value);
		}
		else if (!strcmp(verb, "array-true"))
			jw_array_true(&jw);
		else if (!strcmp(verb, "array-false"))
			jw_array_false(&jw);
		else if (!strcmp(verb, "array-null"))
			jw_array_null(&jw);
		else if (!strcmp(verb, "array-object"))
			jw_array_inline_begin_object(&jw);
		else if (!strcmp(verb, "array-array"))
			jw_array_inline_begin_array(&jw);
		else
			die("unrecognized token: '%s'", verb);
	}

	if (!jw_is_terminated(&jw))
		die("json not terminated: '%s'", jw.json.buf);

	printf("%s\n", jw.json.buf);

	jw_release(&jw);
	return 0;
}

int cmd__json_writer(int argc, const char **argv)
{
	argc--; /* skip over "json-writer" arg */
	argv++;

	if (argc > 0 && argv[0][0] == '-') {
		if (!strcmp(argv[0], "-u") || !strcmp(argv[0], "--unit"))
			return unit_tests();

		if (!strcmp(argv[0], "-p") || !strcmp(argv[0], "--pretty"))
			pretty = 1;
	}

	return scripted();
}