test/json.c

changeset 29
b8c826c720f3
parent 28
e46f9f254fcd
child 30
d33eaaec15da
equal deleted inserted replaced
28:e46f9f254fcd 29:b8c826c720f3
28 28
29 #include "json.h" 29 #include "json.h"
30 30
31 #include <stdbool.h> 31 #include <stdbool.h>
32 #include <cx/buffer.h> 32 #include <cx/buffer.h>
33 33 #include <cx/linked_list.h>
34 34
35 typedef struct Test1 { 35 typedef struct Test1 {
36 char *str; 36 char *str;
37 cxmutstr str2; 37 cxmutstr str2;
38 int8_t i8; 38 int8_t i8;
62 int id; 62 int id;
63 Test3 *test3; 63 Test3 *test3;
64 Test2 *test2; 64 Test2 *test2;
65 } Test4; 65 } Test4;
66 66
67 typedef struct Test5 {
68 char *test;
69 CxList *test2List;
70 } Test5;
71
67 static DBUContext *ctx; 72 static DBUContext *ctx;
68 static DBUClass *test1_class; 73 static DBUClass *test1_class;
69 static DBUClass *test2_class; 74 static DBUClass *test2_class;
70 static DBUClass *test3_class; 75 static DBUClass *test3_class;
71 static DBUClass *test4_class; 76 static DBUClass *test4_class;
77 static DBUClass *test5_class;
72 78
73 int init_json_tests(void) { 79 int init_json_tests(void) {
74 ctx = dbuContextCreate(); 80 ctx = dbuContextCreate();
75 81
76 test1_class = dbuRegisterClassWithoutPK(ctx, "test1", sizeof(Test1)); 82 test1_class = dbuRegisterClassWithoutPK(ctx, "test1", sizeof(Test1));
98 104
99 test4_class = dbuRegisterClassWithoutPK(ctx, "test4", sizeof(Test4)); 105 test4_class = dbuRegisterClassWithoutPK(ctx, "test4", sizeof(Test4));
100 dbuClassAdd(test4_class, Test4, id); 106 dbuClassAdd(test4_class, Test4, id);
101 dbuClassAddObj(test4_class, "test2", offsetof(Test4, test2), test2_class); 107 dbuClassAddObj(test4_class, "test2", offsetof(Test4, test2), test2_class);
102 dbuClassAddObj(test4_class, "test3", offsetof(Test4, test3), test3_class); 108 dbuClassAddObj(test4_class, "test3", offsetof(Test4, test3), test3_class);
109
110 test5_class = dbuRegisterClassWithoutPK(ctx, "test5", sizeof(Test5));
111 dbuClassAdd(test5_class, Test5, test);
112 dbuClassAddCxLinkedList(test5_class, "test2List", offsetof(Test5, test2List), test2_class);
103 } 113 }
104 114
105 void cleanup_json_tests(void) { 115 void cleanup_json_tests(void) {
106 dbuContextFree(ctx); 116 dbuContextFree(ctx);
107 } 117 }
237 CX_TEST_ASSERT(!cx_strcmp(x->value.string, t2_1.name)); 247 CX_TEST_ASSERT(!cx_strcmp(x->value.string, t2_1.name));
238 248
239 cxJsonValueFree(value); 249 cxJsonValueFree(value);
240 } 250 }
241 } 251 }
252
253 CX_TEST(testObjectToJsonChildList) {
254 Test5 test5;
255 test5.test = "hello";
256 test5.test2List = cxLinkedListCreateSimple(CX_STORE_POINTERS);
257
258 Test2 c1;
259 c1.i = 1;
260 c1.name = "c1";
261
262 Test2 c2;
263 c2.i = 2;
264 c2.name = "c2";
265
266 Test2 c3;
267 c3.i = 3;
268 c3.name = "c3";
269
270 cxListAdd(test5.test2List, &c1);
271 cxListAdd(test5.test2List, &c2);
272 cxListAdd(test5.test2List, &c3);
273
274 CX_TEST_DO {
275 CxJsonValue *value = dbuObjectToJson(test5_class, &test5, NULL);
276 CX_TEST_ASSERT(value);
277 CX_TEST_ASSERT(cxJsonIsObject(value));
278
279 CxJsonValue *v = cxJsonObjGet(value, "test");
280 CX_TEST_ASSERT(v);
281 CX_TEST_ASSERT(cxJsonIsString(v));
282 CX_TEST_ASSERT(!cx_strcmp(v->value.string, test5.test));
283
284 v = cxJsonObjGet(value, "test2List");
285 CX_TEST_ASSERT(v);
286 CX_TEST_ASSERT(cxJsonIsArray(v));
287
288 CxJsonValue *x = cxJsonArrGet(v, 0);
289 CX_TEST_ASSERT(cxJsonIsObject(x));
290 CxJsonValue *z = cxJsonObjGet(x, "i");
291 CX_TEST_ASSERT(cxJsonIsInteger(z));
292 CX_TEST_ASSERT(z->value.integer == c1.i);
293 z = cxJsonObjGet(x, "name");
294 CX_TEST_ASSERT(cxJsonIsString(z));
295 CX_TEST_ASSERT(!cx_strcmp(z->value.string, c1.name));
296
297 x = cxJsonArrGet(v, 1);
298 CX_TEST_ASSERT(cxJsonIsObject(x));
299 z = cxJsonObjGet(x, "i");
300 CX_TEST_ASSERT(cxJsonIsInteger(z));
301 CX_TEST_ASSERT(z->value.integer == c2.i);
302 z = cxJsonObjGet(x, "name");
303 CX_TEST_ASSERT(cxJsonIsString(z));
304 CX_TEST_ASSERT(!cx_strcmp(z->value.string, c2.name));
305
306 x = cxJsonArrGet(v, 2);
307 CX_TEST_ASSERT(cxJsonIsObject(x));
308 z = cxJsonObjGet(x, "i");
309 CX_TEST_ASSERT(cxJsonIsInteger(z));
310 CX_TEST_ASSERT(z->value.integer == c3.i);
311 z = cxJsonObjGet(x, "name");
312 CX_TEST_ASSERT(cxJsonIsString(z));
313 CX_TEST_ASSERT(!cx_strcmp(z->value.string, c3.name));
314
315 cxJsonValueFree(value);
316 }
317 }

mercurial