add test for reading multiple json values

Sun, 03 Nov 2024 09:56:33 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 03 Nov 2024 09:56:33 +0100
changeset 977
a941b01b684f
parent 976
d9063c14d3fb
child 978
1da52af18ac3

add test for reading multiple json values

tests/test_json.c file | annotate | diff | comparison | revisions
--- a/tests/test_json.c	Sun Nov 03 09:30:10 2024 +0100
+++ b/tests/test_json.c	Sun Nov 03 09:56:33 2024 +0100
@@ -241,7 +241,7 @@
 
         CX_TEST_ASSERT(json.states != json.states_internal);
         CX_TEST_ASSERT(json.states_alloc > cx_nmemb(json.states_internal));
-
+        
         cxJsonDestroy(&json);
     }
 }
@@ -267,6 +267,68 @@
     cxJsonDestroy(&json);
 }
 
+CX_TEST(test_json_multiple_values) {
+    CxJson json;
+    cxJsonInit(&json);
+    CX_TEST_DO {
+        CxJsonValue *v;
+        int result;
+        
+        // read number
+        cxJsonFill(&json, "10\n");
+        result = cxJsonNext(&json, &v);
+        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(cxJsonIsNumber(v));
+        CX_TEST_ASSERT(cxJsonAsInteger(v) == 10);
+        cxJsonValueFree(v);
+        // read remaining '\n'
+        result = cxJsonNext(&json, &v);
+        CX_TEST_ASSERT(result == 0);
+        // read string
+        cxJsonFill(&json, "\"hello world\"\n");
+        result = cxJsonNext(&json, &v);
+        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(cxJsonIsString(v));
+        CX_TEST_ASSERT(!cx_strcmp(cxJsonAsCxString(v), CX_STR("hello world")));
+        cxJsonValueFree(v);
+        // don't process the remaining newline this time
+        // read obj
+        cxJsonFill(&json, "{ \"value\": \"test\" }\n");
+        result = cxJsonNext(&json, &v);
+        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(cxJsonIsObject(v));
+        CxJsonValue *value = cxJsonObjGet(v, "value");
+        CX_TEST_ASSERT(cxJsonAsString(value));
+        cxJsonValueFree(v);
+        // read array
+        cxJsonFill(&json, "[ 0, 1, 2, 3, 4, 5 ]\n");
+        result = cxJsonNext(&json, &v);
+        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(cxJsonIsArray(v));
+        CxJsonValue *a0 = cxJsonArrGet(v, 0);
+        CxJsonValue *a3 = cxJsonArrGet(v, 3);
+        CX_TEST_ASSERT(cxJsonIsNumber(a0));
+        CX_TEST_ASSERT(cxJsonAsInteger(a0) == 0);
+        CX_TEST_ASSERT(cxJsonIsNumber(a3));
+        CX_TEST_ASSERT(cxJsonAsInteger(a3) == 3);
+        cxJsonValueFree(v);
+        // read literal
+        cxJsonFill(&json, "true\n");
+        result = cxJsonNext(&json, &v);
+        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(cxJsonIsLiteral(v));
+        CX_TEST_ASSERT(cxJsonAsBool(v));
+        cxJsonValueFree(v);
+        // read null
+        cxJsonFill(&json, "null\n");
+        result = cxJsonNext(&json, &v);
+        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(cxJsonIsNull(v));
+        cxJsonValueFree(v);
+    }
+    cxJsonDestroy(&json);
+}
+
 CxTestSuite *cx_test_suite_json(void) {
     CxTestSuite *suite = cx_test_suite_new("json");
 
@@ -276,7 +338,8 @@
     cx_test_register(suite, test_json_object_error);
     cx_test_register(suite, test_json_large_nesting_depth);
     cx_test_register(suite, test_json_number);
-
+    cx_test_register(suite, test_json_multiple_values);
+    
     return suite;
 }
 

mercurial