dump: Revise whitespace usage
[jansson.git] / test / testprogs / test_dump.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #include <jansson.h>
9 #include <string.h>
10 #include "util.h"
11
12 static json_t *create_object()
13 {
14     json_t *object;
15
16     object = json_object();
17     if(!object)
18         fail("unable to create an object");
19
20     if(json_object_set_new(object, "a", json_integer(1)) ||
21        json_object_set_new(object, "b", json_integer(2)))
22         fail("unable to set object values");
23
24     return object;
25 }
26
27 static json_t *create_array()
28 {
29     json_t *array;
30
31     array = json_array();
32     if(!array)
33         fail("unable to create an array");
34
35     if(json_array_append_new(array, json_integer(1)) ||
36        json_array_append_new(array, json_integer(2)))
37         fail("unable to append array values");
38
39     return array;
40 }
41
42
43 #define NORMAL_OBJECT "{\"a\": 1, \"b\": 2}"
44 #define NORMAL_ARRAY  "[1, 2]"
45
46 static void test_normal()
47 {
48     json_t *object;
49     json_t *array;
50     char *result;
51
52     object = create_object();
53     array = create_array();
54
55     result = json_dumps(object, 0);
56     if(strcmp(result, NORMAL_OBJECT) != 0)
57         fail("unexpected encoded object");
58     free(result);
59
60     result = json_dumps(array, 0);
61     if(strcmp(result, NORMAL_ARRAY) != 0)
62         fail("unexpected encoded array");
63     free(result);
64
65     json_decref(object);
66     json_decref(array);
67 }
68
69
70 #define INDENTED_OBJECT                         \
71     "{\n"                                       \
72     "    \"a\": 1,\n"                           \
73     "    \"b\": 2\n"                            \
74     "}"
75 #define INDENTED_ARRAY                          \
76     "[\n"                                       \
77     "    1,\n"                                  \
78     "    2\n"                                   \
79     "]"
80
81 static void test_indent()
82 {
83     json_t *object;
84     json_t *array;
85     char *result;
86
87     object = create_object();
88     array = create_array();
89
90     result = json_dumps(object, JSON_INDENT(4));
91     if(strcmp(result, INDENTED_OBJECT) != 0)
92         fail("unexpected encoded object");
93     free(result);
94
95     result = json_dumps(array, JSON_INDENT(4));
96     if(strcmp(result, INDENTED_ARRAY) != 0)
97         fail("unexpected encoded array");
98     free(result);
99
100     json_decref(object);
101     json_decref(array);
102 }
103
104
105 #define COMPACT_OBJECT "{\"a\":1,\"b\":2}"
106 #define COMPACT_ARRAY  "[1,2]"
107
108 static void test_compact()
109 {
110     json_t *object;
111     json_t *array;
112     char *result;
113
114     object = create_object();
115     array = create_array();
116
117     result = json_dumps(object, JSON_COMPACT);
118     if(strcmp(result, COMPACT_OBJECT) != 0)
119         fail("unexpected encoded object");
120     free(result);
121
122     result = json_dumps(array, JSON_COMPACT);
123     if(strcmp(result, COMPACT_ARRAY) != 0)
124         fail("unexpected encoded array");
125     free(result);
126
127     json_decref(object);
128     json_decref(array);
129 }
130
131
132 #define INDENTED_COMPACT_OBJECT                 \
133     "{\n"                                       \
134     "    \"a\":1,\n"                           \
135     "    \"b\":2\n"                            \
136     "}"
137 #define INDENTED_COMPACT_ARRAY                  \
138     "[\n"                                       \
139     "    1,\n"                                  \
140     "    2\n"                                   \
141     "]"
142
143 static void test_compact_indent()
144 {
145     json_t *object;
146     json_t *array;
147     char *result;
148
149     object = create_object();
150     array = create_array();
151
152     result = json_dumps(object, JSON_INDENT(4) | JSON_COMPACT);
153     if(strcmp(result, INDENTED_COMPACT_OBJECT) != 0)
154         fail("unexpected encoded object");
155     free(result);
156
157     result = json_dumps(array, JSON_INDENT(4) | JSON_COMPACT);
158     if(strcmp(result, INDENTED_COMPACT_ARRAY) != 0)
159         fail("unexpected encoded array");
160     free(result);
161
162     json_decref(object);
163     json_decref(array);
164 }
165
166 int main(void)
167 {
168     test_normal();
169     test_indent();
170     test_compact();
171     test_compact_indent();
172
173     return 0;
174 }