test/json.c

changeset 44
3bac2715ccb9
parent 43
73c1ced3a620
child 46
96e139ab57f2
equal deleted inserted replaced
43:73c1ced3a620 44:3bac2715ccb9
395 CX_TEST_DO { 395 CX_TEST_DO {
396 Test5 *obj = dbuJsonToObject(test5_class, NULL, json); 396 Test5 *obj = dbuJsonToObject(test5_class, NULL, json);
397 397
398 CX_TEST_ASSERT(obj); 398 CX_TEST_ASSERT(obj);
399 CX_TEST_ASSERT(!cx_strcmp(obj->test, "hello world")); 399 CX_TEST_ASSERT(!cx_strcmp(obj->test, "hello world"));
400 CX_TEST_ASSERT(obj->test2List); 400 CX_TEST_ASSERT(obj->test2List != NULL);
401 CX_TEST_ASSERT(cxListSize(obj->test2List) == 2); 401 CX_TEST_ASSERT(cxListSize(obj->test2List) == 2);
402 402
403 Test2 *elm0 = cxListAt(obj->test2List, 0); 403 Test2 *elm0 = cxListAt(obj->test2List, 0);
404 Test2 *elm1 = cxListAt(obj->test2List, 1); 404 Test2 *elm1 = cxListAt(obj->test2List, 1);
405 405
413 CX_TEST_ASSERT(elm1->i == 1); 413 CX_TEST_ASSERT(elm1->i == 1);
414 414
415 free(obj); 415 free(obj);
416 } 416 }
417 } 417 }
418
419 CX_TEST(testJsonToObjectWithEmptyArray) {
420 const char *jsonStr =
421 "{"
422 "\"test\":\"hello world 2\","
423 "\"test2List\":[ ]"
424 "}";
425
426 CxJsonValue *json;
427 cxJsonFromString(NULL, jsonStr, &json);
428
429 CX_TEST_DO {
430 Test5 *obj = dbuJsonToObject(test5_class, NULL, json);
431
432 CX_TEST_ASSERT(obj);
433 CX_TEST_ASSERT(!cx_strcmp(obj->test, "hello world 2"));
434 CX_TEST_ASSERT(obj->test2List == NULL || cxListSize(obj->test2List) == 0);
435
436 free(obj);
437 }
438 }
439
440 CX_TEST(testJsonToObjectWithNullArray) {
441 const char *jsonStr =
442 "{"
443 "\"test\":\"hello world 3\","
444 "\"test2List\":[ null, null, null ]"
445 "}";
446
447 CxJsonValue *json;
448 cxJsonFromString(NULL, jsonStr, &json);
449
450 CX_TEST_DO {
451 Test5 *obj = dbuJsonToObject(test5_class, NULL, json);
452
453 CX_TEST_ASSERT(obj);
454 CX_TEST_ASSERT(!cx_strcmp(obj->test, "hello world 3"));
455 CX_TEST_ASSERT(obj->test2List != NULL);
456 CX_TEST_ASSERT(cxListSize(obj->test2List) == 3);
457
458 Test2 *elm0 = cxListAt(obj->test2List, 0);
459 Test2 *elm1 = cxListAt(obj->test2List, 1);
460 Test2 *elm2 = cxListAt(obj->test2List, 2);
461
462 CX_TEST_ASSERT(elm0 == NULL);
463 CX_TEST_ASSERT(elm1 == NULL);
464 CX_TEST_ASSERT(elm2 == NULL);
465
466 free(obj);
467 }
468 }

mercurial