Fri, 03 Jan 2025 17:12:28 +0100
add test case for pretty printing with tabs - relates to #526
tests/test_json.c | file | annotate | diff | comparison | revisions |
--- a/tests/test_json.c Thu Jan 02 21:00:11 2025 +0100 +++ b/tests/test_json.c Fri Jan 03 17:12:28 2025 +0100 @@ -754,6 +754,53 @@ cx_testing_allocator_destroy(&talloc); } + +CX_TEST_SUBROUTINE(test_json_write_pretty_default_sub, + const CxAllocator *allocator, + cxstring expected, + bool use_spaces +) { + // create the value + CxJsonValue *obj = cxJsonCreateObj(allocator); + cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); + cxJsonObjPutNumber(obj, CX_STR("int"), 47); // purposely use PutNumber to put an int + CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); + cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); + CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); + CxJsonValue *objects = cxJsonObjPutArr(nested, CX_STR("objects")); + CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; + cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); + cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); + cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); + cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); + cxJsonArrAddValues(objects, obj_in_arr, 2); + cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), + (double[]){3.1415, 47.11, 8.15}, 3); + cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), + (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); + CxJsonValue *ints = cxJsonObjPutArr(nested, CX_STR("ints")); + cxJsonArrAddIntegers(ints, (int64_t[]){4, 8, 15}, 3); + CxJsonValue *nested_array = cxJsonCreateArr(allocator); + cxJsonArrAddValues(ints, &nested_array, 1); + cxJsonArrAddIntegers(nested_array, (int64_t[]){16, 23}, 2); + cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1); + + // write it to a buffer + CxBuffer buf; + cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT); + CxJsonWriter writer = cxJsonWriterPretty(use_spaces); + int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, &writer); + cxBufferTerminate(&buf); // makes debugging easier + CX_TEST_ASSERT(result == 0); + + // compare the string + CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); + + // destroy everything + cxBufferDestroy(&buf); + cxJsonValueFree(obj); +} + CX_TEST(test_json_write_pretty_default_spaces) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); @@ -780,45 +827,39 @@ "}" ); - // create the value - CxJsonValue *obj = cxJsonCreateObj(allocator); - cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); - cxJsonObjPutNumber(obj, CX_STR("int"), 47); // purposely use PutNumber to put an int - CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); - cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); - CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); - CxJsonValue *objects = cxJsonObjPutArr(nested, CX_STR("objects")); - CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; - cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); - cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); - cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); - cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); - cxJsonArrAddValues(objects, obj_in_arr, 2); - cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), - (double[]){3.1415, 47.11, 8.15}, 3); - cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), - (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); - CxJsonValue *ints = cxJsonObjPutArr(nested, CX_STR("ints")); - cxJsonArrAddIntegers(ints, (int64_t[]){4, 8, 15}, 3); - CxJsonValue *nested_array = cxJsonCreateArr(allocator); - cxJsonArrAddValues(ints, &nested_array, 1); - cxJsonArrAddIntegers(nested_array, (int64_t[]){16, 23}, 2); - cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1); + CX_TEST_CALL_SUBROUTINE(test_json_write_pretty_default_sub, allocator, expected, true); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} - // write it to a buffer - CxBuffer buf; - cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT); - CxJsonWriter writer = cxJsonWriterPretty(true); - int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, &writer); - cxBufferTerminate(&buf); // makes debugging easier - CX_TEST_ASSERT(result == 0); - - // compare the string - CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); - - // destroy everything - cxBufferDestroy(&buf); - cxJsonValueFree(obj); +CX_TEST(test_json_write_pretty_default_tabs) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + // expected value + cxstring expected = CX_STR( +"{\n" +"\t\"bool\": false,\n" +"\t\"int\": 47,\n" +"\t\"nested\": {\n" +"\t\t\"floats\": [3.1415, 47.11, 8.15],\n" +"\t\t\"ints\": [4, 8, 15, [16, 23], 42],\n" +"\t\t\"literals\": [true, null, false],\n" +"\t\t\"objects\": [{\n" +"\t\t\t\"name1\": 1,\n" +"\t\t\t\"name2\": 3\n" +"\t\t}, {\n" +"\t\t\t\"name1\": 3,\n" +"\t\t\t\"name2\": 7\n" +"\t\t}]\n" +"\t},\n" +"\t\"strings\": [\"hello\", \"world\"]\n" +"}" + ); + CX_TEST_CALL_SUBROUTINE(test_json_write_pretty_default_sub, allocator, expected, false); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); @@ -844,6 +885,7 @@ cx_test_register(suite, test_json_create_value); cx_test_register(suite, test_json_write_default_format); cx_test_register(suite, test_json_write_pretty_default_spaces); + cx_test_register(suite, test_json_write_pretty_default_tabs); return suite; }