tests/test_json.c

changeset 1007
81b2986d2b04
parent 1002
1483c47063a8
child 1015
a0922b925d2a
--- a/tests/test_json.c	Tue Dec 10 00:09:55 2024 +0100
+++ b/tests/test_json.c	Tue Dec 10 00:19:45 2024 +0100
@@ -142,7 +142,7 @@
         cxJsonFill(&json, parts[nparts - 1]);
         result = cxJsonNext(&json, &obj);
         CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
-        CX_TEST_ASSERT(obj);
+        CX_TEST_ASSERT(cxJsonIsObject(obj));
         
         CxJsonValue *message = cxJsonObjGet(obj, "message");
         CX_TEST_ASSERT(cxJsonIsString(message));
@@ -166,6 +166,43 @@
     }
 }
 
+CX_TEST(test_json_subsequent_fill) {
+    cxstring text = cx_str(
+            "{\"message\":\"success\"  ,     \"__timestamp\":1729348561}");
+
+    cxstring part1 = cx_strsubsl(text, 0, 25);
+    cxstring part2 = cx_strsubs(text, 25);
+
+    CX_TEST_DO {
+        CxJson json;
+        cxJsonInit(&json, NULL);
+        CxJsonValue *obj;
+
+        cxJsonFill(&json, part1);
+        cxJsonFill(&json, part2);
+        CxJsonStatus result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
+        CX_TEST_ASSERT(cxJsonIsObject(obj));
+
+        CxJsonValue *message = cxJsonObjGet(obj, "message");
+        CX_TEST_ASSERT(cxJsonIsString(message));
+        CX_TEST_ASSERT(0 == cx_strcmp(
+                cxJsonAsCxString(message),
+                cx_str("success"))
+        );
+        CxJsonValue *timestamp = cxJsonObjGet(obj, "__timestamp");
+        CX_TEST_ASSERT(message->type == CX_JSON_STRING);
+        CX_TEST_ASSERT(cxJsonIsInteger(timestamp));
+        CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561);
+
+        cxJsonValueFree(obj);
+        result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_DATA);
+
+        cxJsonDestroy(&json);
+    }
+}
+
 CX_TEST(test_json_object_error) {
     cxstring text0 = cx_str(
             "{\n"
@@ -408,6 +445,7 @@
     cx_test_register(suite, test_json_simple_object);
     cx_test_register(suite, test_json_object_incomplete_token);
     cx_test_register(suite, test_json_object_error);
+    cx_test_register(suite, test_json_subsequent_fill);
     cx_test_register(suite, test_json_large_nesting_depth);
     cx_test_register(suite, test_json_number);
     cx_test_register(suite, test_json_multiple_values);

mercurial