Fri, 03 Jan 2025 17:16:49 +0100
remove code duplication from writer tests - relates to #526
tests/test_json.c | file | annotate | diff | comparison | revisions |
--- a/tests/test_json.c Fri Jan 03 17:12:28 2025 +0100 +++ b/tests/test_json.c Fri Jan 03 17:16:49 2025 +0100 @@ -713,52 +713,11 @@ cx_testing_allocator_destroy(&talloc); } -CX_TEST(test_json_write_default_format) { - CxTestingAllocator talloc; - cx_testing_allocator_init(&talloc); - CxAllocator *allocator = &talloc.base; - CX_TEST_DO { - // expected value - cxstring expected = CX_STR("{\"bool\":false,\"nested\":{\"floats\":[3.1415,47.11,8.15],\"ints\":[4,8,15,16,23,42],\"literals\":[true,null,false],\"string\":\"test\"},\"num\":47.11,\"strings\":[\"hello\",\"world\"]}"); - // create the value - CxJsonValue *obj = cxJsonCreateObj(allocator); - cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); - cxJsonObjPutNumber(obj, CX_STR("num"), 47.11); - CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); - cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); - CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); - cxJsonObjPutString(nested, CX_STR("string"), "test"); - 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); - cxJsonArrAddIntegers(cxJsonObjPutArr(nested, CX_STR("ints")), - (int64_t[]){4, 8, 15, 16, 23, 42}, 6); - - // write it to a buffer - CxBuffer buf; - cxBufferInit(&buf, NULL, 256, NULL, CX_BUFFER_DEFAULT); - int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, NULL); - 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_ASSERT(cx_testing_allocator_verify(&talloc)); - } - cx_testing_allocator_destroy(&talloc); -} - - -CX_TEST_SUBROUTINE(test_json_write_pretty_default_sub, +CX_TEST_SUBROUTINE(test_json_write_sub, const CxAllocator *allocator, cxstring expected, - bool use_spaces + const CxJsonWriter *writer ) { // create the value CxJsonValue *obj = cxJsonCreateObj(allocator); @@ -788,8 +747,7 @@ // 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); + int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, writer); cxBufferTerminate(&buf); // makes debugging easier CX_TEST_ASSERT(result == 0); @@ -801,12 +759,44 @@ cxJsonValueFree(obj); } +CX_TEST(test_json_write_default_format) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + // expected value + cxstring expected = CX_STR( +"{\"bool\":false," +"\"int\":47," +"\"nested\":{" +"\"floats\":[3.1415,47.11,8.15]," +"\"ints\":[4,8,15,[16,23],42]," +"\"literals\":[true,null,false]," +"\"objects\":[{" +"\"name1\":1," +"\"name2\":3" +"},{" +"\"name1\":3," +"\"name2\":7" +"}]" +"}," +"\"strings\":[\"hello\",\"world\"]" +"}" + ); + + CxJsonWriter writer = cxJsonWriterCompact(); + CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + CX_TEST(test_json_write_pretty_default_spaces) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); CxAllocator *allocator = &talloc.base; CX_TEST_DO { - // expected value cxstring expected = CX_STR( "{\n" " \"bool\": false,\n" @@ -827,7 +817,8 @@ "}" ); - CX_TEST_CALL_SUBROUTINE(test_json_write_pretty_default_sub, allocator, expected, true); + CxJsonWriter writer = cxJsonWriterPretty(true); + CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } @@ -839,7 +830,6 @@ cx_testing_allocator_init(&talloc); CxAllocator *allocator = &talloc.base; CX_TEST_DO { - // expected value cxstring expected = CX_STR( "{\n" "\t\"bool\": false,\n" @@ -859,7 +849,8 @@ "\t\"strings\": [\"hello\", \"world\"]\n" "}" ); - CX_TEST_CALL_SUBROUTINE(test_json_write_pretty_default_sub, allocator, expected, false); + CxJsonWriter writer = cxJsonWriterPretty(false); + CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc);