tests/test_json.c

changeset 1338
cc153bffea28
parent 1337
6dfa1eb58ce3
--- a/tests/test_json.c	Thu Aug 14 23:03:01 2025 +0200
+++ b/tests/test_json.c	Fri Aug 15 17:42:01 2025 +0200
@@ -446,7 +446,7 @@
             "\t\"data\":{\n"
             "\t\t\"obj\":{\n"
             "\t\t\t\"array\": [1, 2, 3, ?syntaxerror? ]\n"
-            "\t\t\"}\n"
+            "\t\t}\n"
             "\t},\n"
             "\t\"timestamp\":1729348561,\n"
             "}"
@@ -482,6 +482,46 @@
     }
 }
 
+CX_TEST(test_json_object_remove_member) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    const CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        CxJson json;
+        cxJsonInit(&json, alloc);
+        cxJsonFill(&json, cx_str(
+            "{\n"
+            "\t\"message\":\"success\",\n"
+            "\t\"data\":{\n"
+            "\t\t\"obj\":{\n"
+            "\t\t\t\"array\": [1, 2, 3]\n"
+            "\t\t}\n"
+            "\t},\n"
+            "\t\"timestamp\":1729348561\n"
+            "}"
+        ));
+        CxJsonValue *obj;
+        CX_TEST_ASSERT(CX_JSON_NO_ERROR == cxJsonNext(&json, &obj));
+        cxJsonDestroy(&json);
+
+        CX_TEST_ASSERT(cxJsonIsObject(cxJsonObjGet(obj, "data")));
+        CxJsonValue *data = cxJsonObjRemove(obj, "data");
+        CX_TEST_ASSERT(cxJsonIsObject(data));
+        CX_TEST_ASSERT(!cxJsonIsObject(cxJsonObjGet(obj, "data")));
+        CX_TEST_ASSERT(cxJsonIsObject(cxJsonObjGet(data, "obj")));
+
+        CX_TEST_ASSERT(NULL == cxJsonObjRemove(obj, "data"));
+
+        // does not verify, yet, because we extracted an object
+        cxJsonValueFree(obj);
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+
+        cxJsonValueFree(data);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CX_TEST(test_json_large_nesting_depth) {
     CxJson json;
     CxJsonValue *d1;
@@ -737,6 +777,9 @@
         CX_TEST_ASSERT(cxJsonIsNumber(removed));
         CX_TEST_ASSERT(cxJsonAsInteger(removed) == 6);
         CX_TEST_ASSERT(cxJsonArrSize(arr) == 5);
+        e = cxJsonArrGet(arr, 3);
+        CX_TEST_ASSERT(cxJsonIsNumber(e));
+        CX_TEST_ASSERT(cxJsonAsInteger(e) == 12);
         e = cxJsonArrRemove(arr, 5);
         CX_TEST_ASSERT(e == NULL);
         cxJsonValueFree(arr);
@@ -1227,6 +1270,7 @@
     cx_test_register(suite, test_json_object_incomplete_token);
     cx_test_register(suite, test_json_token_wrongly_completed);
     cx_test_register(suite, test_json_object_error);
+    cx_test_register(suite, test_json_object_remove_member);
     cx_test_register(suite, test_json_subsequent_fill);
     cx_test_register(suite, test_json_large_nesting_depth);
     cx_test_register(suite, test_json_number);

mercurial