tests/test_json.c

changeset 1078
ffa8bb4e9288
parent 1075
0cc4b63a0ae0
child 1080
e16f4f336e3c
--- a/tests/test_json.c	Thu Jan 02 19:07:56 2025 +0100
+++ b/tests/test_json.c	Thu Jan 02 20:58:32 2025 +0100
@@ -740,6 +740,77 @@
         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(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"
+"    \"int\": 47,\n"
+"    \"nested\": {\n"
+"        \"floats\": [3.1415, 47.11, 8.15],\n"
+"        \"ints\": [4, 8, 15, [16, 23], 42],\n"
+"        \"literals\": [true, null, false],\n"
+"        \"objects\": [{\n"
+"            \"name1\": 1,\n"
+"            \"name2\": 3\n"
+"        }, {\n"
+"            \"name1\": 3,\n"
+"            \"name2\": 7\n"
+"        }]\n"
+"    },\n"
+"    \"strings\": [\"hello\", \"world\"]\n"
+"}"
+        );
+
+        // 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(true);
+        int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, &writer);
+        cxBufferTerminate(&buf); // makes debugging easier
         CX_TEST_ASSERT(result == 0);
 
         // compare the string
@@ -772,6 +843,7 @@
     cx_test_register(suite, test_json_allocator_parse_error);
     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);
     
     return suite;
 }

mercurial