| 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 } |