tests/test_json.c

changeset 1573
cd2e974410ad
parent 1567
f60f23b362e9
equal deleted inserted replaced
1572:0499bf03aef3 1573:cd2e974410ad
1247 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1247 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1248 } 1248 }
1249 cx_testing_allocator_destroy(&talloc); 1249 cx_testing_allocator_destroy(&talloc);
1250 } 1250 }
1251 1251
1252 CX_TEST_SUBROUTINE(test_json_write_sub, 1252 static CxJsonValue *test_json_write_create_test_object(const CxAllocator *allocator) {
1253 const CxAllocator *allocator,
1254 cxstring expected,
1255 const CxJsonWriter *writer
1256 ) {
1257 // create the value
1258 CxJsonValue *obj = cxJsonCreateObj(allocator); 1253 CxJsonValue *obj = cxJsonCreateObj(allocator);
1259 cxJsonObjPutLiteral(obj, "bool", CX_JSON_FALSE); 1254 cxJsonObjPutLiteral(obj, "bool", CX_JSON_FALSE);
1260 cxJsonObjPutNumber(obj, "int", 47); // purposely use PutNumber to put an int 1255 cxJsonObjPutNumber(obj, "int", 47); // purposely use PutNumber to put an int
1261 CxJsonValue *strings = cxJsonObjPutArr(obj, "strings"); 1256 CxJsonValue *strings = cxJsonObjPutArr(obj, "strings");
1262 cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); 1257 cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2);
1276 cxJsonArrAddIntegers(ints, (int64_t[]){4, 8, 15}, 3); 1271 cxJsonArrAddIntegers(ints, (int64_t[]){4, 8, 15}, 3);
1277 CxJsonValue *nested_array = cxJsonCreateArr(allocator); 1272 CxJsonValue *nested_array = cxJsonCreateArr(allocator);
1278 cxJsonArrAddValues(ints, &nested_array, 1); 1273 cxJsonArrAddValues(ints, &nested_array, 1);
1279 cxJsonArrAddIntegers(nested_array, (int64_t[]){16, 23}, 2); 1274 cxJsonArrAddIntegers(nested_array, (int64_t[]){16, 23}, 2);
1280 cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1); 1275 cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1);
1276 return obj;
1277 }
1278
1279 CX_TEST_SUBROUTINE(test_json_write_sub,
1280 const CxAllocator *allocator,
1281 cxstring expected,
1282 const CxJsonWriter *writer
1283 ) {
1284 // create the value
1285 CxJsonValue *obj = test_json_write_create_test_object(allocator);
1281 1286
1282 // write it to a buffer 1287 // write it to a buffer
1283 CxBuffer buf; 1288 CxBuffer buf;
1284 cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT); 1289 cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT);
1285 int result = cxJsonWrite(&buf, obj, cxBufferWriteFunc, writer); 1290 int result = cxJsonWrite(&buf, obj, cxBufferWriteFunc, writer);
1563 nothing.type = CX_JSON_NOTHING; 1568 nothing.type = CX_JSON_NOTHING;
1564 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, &nothing, cxBufferWriteFunc, NULL)); 1569 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, &nothing, cxBufferWriteFunc, NULL));
1565 CX_TEST_ASSERT(buf.size == 0); 1570 CX_TEST_ASSERT(buf.size == 0);
1566 } 1571 }
1567 cxBufferDestroy(&buf); 1572 cxBufferDestroy(&buf);
1573 }
1574
1575 CX_TEST(test_json_to_string) {
1576 CxTestingAllocator talloc;
1577 cx_testing_allocator_init(&talloc);
1578 CxAllocator *allocator = &talloc.base;
1579 CX_TEST_DO {
1580 // expected value
1581 cxstring expected = cx_str(
1582 "{\"bool\":false,"
1583 "\"int\":47,"
1584 "\"strings\":[\"hello\",\"world\"],"
1585 "\"nested\":{"
1586 "\"objects\":[{"
1587 "\"name1\":1,"
1588 "\"name2\":3"
1589 "},{"
1590 "\"name2\":7,"
1591 "\"name1\":3"
1592 "}],"
1593 "\"floats\":[3.1415,47.11,8.15],"
1594 "\"literals\":[true,null,false],"
1595 "\"ints\":[4,8,15,[16,23],42]"
1596 "}"
1597 "}"
1598 );
1599
1600 CxJsonValue *obj = test_json_write_create_test_object(allocator);
1601 cxmutstr result = cxJsonToString(obj, allocator);
1602 CX_TEST_ASSERT(0 == cx_strcmp(result, expected));
1603 CX_TEST_ASSERT(result.ptr[result.length] == '\0');
1604
1605 cx_strfree_a(allocator, &result);
1606 cxJsonValueFree(obj);
1607
1608 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1609 }
1610 cx_testing_allocator_destroy(&talloc);
1611 }
1612
1613 CX_TEST(test_json_to_pretty_string) {
1614 CxTestingAllocator talloc;
1615 cx_testing_allocator_init(&talloc);
1616 CxAllocator *allocator = &talloc.base;
1617 CX_TEST_DO {
1618 cxstring expected = cx_str(
1619 "{\n"
1620 " \"bool\": false,\n"
1621 " \"int\": 47,\n"
1622 " \"strings\": [\"hello\", \"world\"],\n"
1623 " \"nested\": {\n"
1624 " \"objects\": [{\n"
1625 " \"name1\": 1,\n"
1626 " \"name2\": 3\n"
1627 " }, {\n"
1628 " \"name2\": 7,\n"
1629 " \"name1\": 3\n"
1630 " }],\n"
1631 " \"floats\": [3.1415, 47.11, 8.15],\n"
1632 " \"literals\": [true, null, false],\n"
1633 " \"ints\": [4, 8, 15, [16, 23], 42]\n"
1634 " }\n"
1635 "}"
1636 );
1637
1638 CxJsonValue *obj = test_json_write_create_test_object(allocator);
1639 cxmutstr result = cxJsonToPrettyString(obj, allocator);
1640 CX_TEST_ASSERT(0 == cx_strcmp(result, expected));
1641 CX_TEST_ASSERT(result.ptr[result.length] == '\0');
1642
1643 cx_strfree_a(allocator, &result);
1644 cxJsonValueFree(obj);
1645
1646 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1647 }
1648 cx_testing_allocator_destroy(&talloc);
1568 } 1649 }
1569 1650
1570 CxTestSuite *cx_test_suite_json(void) { 1651 CxTestSuite *cx_test_suite_json(void) {
1571 CxTestSuite *suite = cx_test_suite_new("json"); 1652 CxTestSuite *suite = cx_test_suite_new("json");
1572 1653
1607 cx_test_register(suite, test_json_write_frac_max_digits); 1688 cx_test_register(suite, test_json_write_frac_max_digits);
1608 cx_test_register(suite, test_json_write_string_escape); 1689 cx_test_register(suite, test_json_write_string_escape);
1609 cx_test_register(suite, test_json_write_name_escape); 1690 cx_test_register(suite, test_json_write_name_escape);
1610 cx_test_register(suite, test_json_write_solidus); 1691 cx_test_register(suite, test_json_write_solidus);
1611 cx_test_register(suite, test_json_write_nothing); 1692 cx_test_register(suite, test_json_write_nothing);
1693 cx_test_register(suite, test_json_to_string);
1694 cx_test_register(suite, test_json_to_pretty_string);
1612 1695
1613 return suite; 1696 return suite;
1614 } 1697 }
1615 1698

mercurial