tests/test_json.c

changeset 1002
1483c47063a8
parent 1000
1aecddf7e209
child 1007
81b2986d2b04
--- a/tests/test_json.c	Sat Dec 07 23:59:54 2024 +0100
+++ b/tests/test_json.c	Sun Dec 08 00:13:38 2024 +0100
@@ -59,7 +59,7 @@
     );
 
     CX_TEST_DO {
-        int result;
+        CxJsonStatus result;
 
         CxJson json;
         cxJsonInit(&json, NULL);
@@ -68,7 +68,7 @@
         // parse the big fat object
         CxJsonValue *obj;
         result = cxJsonNext(&json, &obj);
-        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
 
         // check the contents
         CX_TEST_ASSERT(cxJsonIsObject(obj));
@@ -110,7 +110,7 @@
 
         // we only have one object that already contained all the data
         result = cxJsonNext(&json, &obj);
-        CX_TEST_ASSERT(result == 0);
+        CX_TEST_ASSERT(result == CX_JSON_NO_DATA);
 
         cxJsonDestroy(&json);
     }
@@ -126,25 +126,22 @@
     }
     
     CX_TEST_DO {
-        int result;
+        CxJsonStatus result;
 
         CxJson json;
         cxJsonInit(&json, NULL);
         CxJsonValue *obj;
         
         size_t part = 0;
-        while(part < nparts) {
+        while(part < nparts - 1) {
             cxJsonFill(&json, parts[part]);
             part++;
             result = cxJsonNext(&json, &obj);
-            
-            if(result != 0) {
-                break;
-            }
+            CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA);
         }
-        
-        CX_TEST_ASSERT(result == 1);
-        CX_TEST_ASSERT(part == nparts);
+        cxJsonFill(&json, parts[nparts - 1]);
+        result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         CX_TEST_ASSERT(obj);
         
         CxJsonValue *message = cxJsonObjGet(obj, "message");
@@ -161,9 +158,9 @@
         // this recursively frees everything else
         cxJsonValueFree(obj);
 
-        // we only have one object that already contained all the data
+        // now there is everything read
         result = cxJsonNext(&json, &obj);
-        CX_TEST_ASSERT(result == 0);
+        CX_TEST_ASSERT(result == CX_JSON_NO_DATA);
 
         cxJsonDestroy(&json);
     }
@@ -187,9 +184,16 @@
     cxstring text4 = cx_str("{ \"name\": \"value\" ]");
     
     cxstring tests[] = { text0, text1, text2, text3, text4 };
+    CxJsonStatus errors[] = {
+        CX_JSON_FORMAT_ERROR_NUMBER,
+        CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN,
+        CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN,
+        CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN,
+        CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN
+    };
     
     CX_TEST_DO {
-        int result;
+        CxJsonStatus result;
         CxJson json;
         CxJsonValue *obj = NULL;
         
@@ -198,7 +202,7 @@
             cxJsonFill(&json, tests[i]);
             result = cxJsonNext(&json, &obj);
 
-            CX_TEST_ASSERT(result == -1);
+            CX_TEST_ASSERT(result == errors[i]);
             CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING);
             cxJsonDestroy(&json);
         }
@@ -257,16 +261,16 @@
     CX_TEST_DO {
         // TODO: find a better way to terminate values that are not arrays/objects
         CxJsonValue *v;
-        int result;
+        CxJsonStatus result;
         cxJsonFill(&json, "3.1415 ");
         result = cxJsonNext(&json, &v);
-        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         CX_TEST_ASSERT(cxJsonIsNumber(v));
         CX_TEST_ASSERT(cxJsonAsDouble(v) == 3.1415);
         cxJsonValueFree(v);
         cxJsonFill(&json, "-47.11e2 ");
         result = cxJsonNext(&json, &v);
-        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         CX_TEST_ASSERT(cxJsonIsNumber(v));
         CX_TEST_ASSERT(cxJsonAsDouble(v) == -4711.0);
         cxJsonValueFree(v);
@@ -279,22 +283,22 @@
     cxJsonInit(&json, NULL);
     CX_TEST_DO {
         CxJsonValue *v;
-        int result;
+        CxJsonStatus result;
         
         // read number
         cxJsonFill(&json, "10\n");
         result = cxJsonNext(&json, &v);
-        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         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);
+        CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA);
         // read string
         cxJsonFill(&json, "\"hello world\"\n");
         result = cxJsonNext(&json, &v);
-        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         CX_TEST_ASSERT(cxJsonIsString(v));
         CX_TEST_ASSERT(!cx_strcmp(cxJsonAsCxString(v), CX_STR("hello world")));
         cxJsonValueFree(v);
@@ -302,7 +306,7 @@
         // read obj
         cxJsonFill(&json, "{ \"value\": \"test\" }\n");
         result = cxJsonNext(&json, &v);
-        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         CX_TEST_ASSERT(cxJsonIsObject(v));
         CxJsonValue *value = cxJsonObjGet(v, "value");
         CX_TEST_ASSERT(cxJsonAsString(value));
@@ -310,7 +314,7 @@
         // read array
         cxJsonFill(&json, "[ 0, 1, 2, 3, 4, 5 ]\n");
         result = cxJsonNext(&json, &v);
-        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         CX_TEST_ASSERT(cxJsonIsArray(v));
         CxJsonValue *a0 = cxJsonArrGet(v, 0);
         CxJsonValue *a3 = cxJsonArrGet(v, 3);
@@ -322,14 +326,14 @@
         // read literal
         cxJsonFill(&json, "true\n");
         result = cxJsonNext(&json, &v);
-        CX_TEST_ASSERT(result == 1);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         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(result == CX_JSON_NO_ERROR);
         CX_TEST_ASSERT(cxJsonIsNull(v));
         cxJsonValueFree(v);
     }
@@ -354,8 +358,8 @@
         cxJsonFill(&json, text);
         
         CxJsonValue *obj;
-        int result = cxJsonNext(&json, &obj);
-        CX_TEST_ASSERT(result == 1);
+        CxJsonStatus result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
         CX_TEST_ASSERT(obj->allocator == allocator);
         
         // this recursively frees everything 
@@ -385,8 +389,8 @@
         cxJsonFill(&json, text);
 
         CxJsonValue *obj = NULL;
-        int result = cxJsonNext(&json, &obj);
-        CX_TEST_ASSERT(result == -1);
+        CxJsonStatus result = cxJsonNext(&json, &obj);
+        CX_TEST_ASSERT(result == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
         CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING);
 
         // clean-up any left-over memory

mercurial