Make json_pack/json_unpack() recursive
[jansson.git] / test / suites / api / test_pack.c
index f1ca388..8968dc7 100644 (file)
@@ -15,13 +15,14 @@ int main()
 {
     json_t *value;
     int i;
+    json_error_t error;
 
     /*
      * Simple, valid json_pack cases
      */
 
     /* true */
-    value = json_pack(NULL, "b", 1);
+    value = json_pack(&error, "b", 1);
     if(!json_is_true(value))
             fail("json_pack boolean failed");
     if(value->refcount != (ssize_t)-1)
@@ -29,7 +30,7 @@ int main()
     json_decref(value);
 
     /* false */
-    value = json_pack(NULL, "b", 0);
+    value = json_pack(&error, "b", 0);
     if(!json_is_false(value))
             fail("json_pack boolean failed");
     if(value->refcount != (ssize_t)-1)
@@ -37,7 +38,7 @@ int main()
     json_decref(value);
 
     /* null */
-    value = json_pack(NULL, "n");
+    value = json_pack(&error, "n");
     if(!json_is_null(value))
             fail("json_pack null failed");
     if(value->refcount != (ssize_t)-1)
@@ -45,7 +46,7 @@ int main()
     json_decref(value);
 
     /* integer */
-    value = json_pack(NULL, "i", 1);
+    value = json_pack(&error, "i", 1);
     if(!json_is_integer(value) || json_integer_value(value) != 1)
             fail("json_pack integer failed");
     if(value->refcount != (ssize_t)1)
@@ -54,7 +55,7 @@ int main()
 
 
     /* real */
-    value = json_pack(NULL, "f", 1.0);
+    value = json_pack(&error, "f", 1.0);
     if(!json_is_real(value) || json_real_value(value) != 1.0)
             fail("json_pack real failed");
     if(value->refcount != (ssize_t)1)
@@ -62,7 +63,7 @@ int main()
     json_decref(value);
 
     /* string */
-    value = json_pack(NULL, "s", "test");
+    value = json_pack(&error, "s", "test");
     if(!json_is_string(value) || strcmp("test", json_string_value(value)))
             fail("json_pack string failed");
     if(value->refcount != (ssize_t)1)
@@ -70,7 +71,7 @@ int main()
     json_decref(value);
 
     /* empty object */
-    value = json_pack(NULL, "{}", 1.0);
+    value = json_pack(&error, "{}", 1.0);
     if(!json_is_object(value) || json_object_size(value) != 0)
             fail("json_pack empty object failed");
     if(value->refcount != (ssize_t)1)
@@ -78,7 +79,7 @@ int main()
     json_decref(value);
 
     /* empty list */
-    value = json_pack(NULL, "[]", 1.0);
+    value = json_pack(&error, "[]", 1.0);
     if(!json_is_array(value) || json_array_size(value) != 0)
             fail("json_pack empty list failed");
     if(value->refcount != (ssize_t)1)
@@ -86,7 +87,7 @@ int main()
     json_decref(value);
 
     /* non-incref'd object */
-    value = json_pack(NULL, "o", json_integer(1));
+    value = json_pack(&error, "o", json_integer(1));
     if(!json_is_integer(value) || json_integer_value(value) != 1)
             fail("json_pack object failed");
     if(value->refcount != (ssize_t)1)
@@ -94,7 +95,7 @@ int main()
     json_decref(value);
 
     /* incref'd object */
-    value = json_pack(NULL, "O", json_integer(1));
+    value = json_pack(&error, "O", json_integer(1));
     if(!json_is_integer(value) || json_integer_value(value) != 1)
             fail("json_pack object failed");
     if(value->refcount != (ssize_t)2)
@@ -103,17 +104,17 @@ int main()
     json_decref(value);
 
     /* simple object */
-    value = json_pack(NULL, "{s:[]}", "foo");
+    value = json_pack(&error, "{s:[]}", "foo");
     if(!json_is_object(value) || json_object_size(value) != 1)
-            fail("json_pack object failed");
+            fail("json_pack array failed");
     if(!json_is_array(json_object_get(value, "foo")))
-            fail("json_pack object failed");
+            fail("json_pack array failed");
     if(json_object_get(value, "foo")->refcount != (ssize_t)1)
             fail("json_pack object refcount failed");
     json_decref(value);
 
     /* simple array */
-    value = json_pack(NULL, "[i,i,i]", 0, 1, 2);
+    value = json_pack(&error, "[i,i,i]", 0, 1, 2);
     if(!json_is_array(value) || json_array_size(value) != 3)
             fail("json_pack object failed");
     for(i=0; i<3; i++)
@@ -125,30 +126,82 @@ int main()
     }
     json_decref(value);
 
+    /* Whitespace; regular string */
+    value = json_pack(&error, " s ", "test");
+    if(!json_is_string(value) || strcmp("test", json_string_value(value)))
+            fail("json_pack string (with whitespace) failed");
+    json_decref(value);
+
+    /* Whitespace; empty array */
+    value = json_pack(&error, "[ ]");
+    if(!json_is_array(value) || json_array_size(value) != 0)
+            fail("json_pack empty array (with whitespace) failed");
+    json_decref(value);
+
+    /* Whitespace; array */
+    value = json_pack(&error, "[ i , i,  i ] ", 1, 2, 3);
+    if(!json_is_array(value) || json_array_size(value) != 3)
+            fail("json_pack array (with whitespace) failed");
+    json_decref(value);
+
     /*
      * Invalid cases
      */
-    
+
     /* mismatched open/close array/object */
-    if(json_pack(NULL, "[}"))
+    if(json_pack(&error, "[}"))
         fail("json_pack failed to catch mismatched '}'");
+    if(error.line != 1 || error.column != 2)
+        fail("json_pack didn't get the error coordinates right!");
 
-    if(json_pack(NULL, "{]"))
+    if(json_pack(&error, "{]"))
         fail("json_pack failed to catch mismatched ']'");
+    if(error.line != 1 || error.column != 2)
+        fail("json_pack didn't get the error coordinates right!");
 
     /* missing close array */
-    if(json_pack(NULL, "["))
+    if(json_pack(&error, "["))
         fail("json_pack failed to catch missing ']'");
+    if(error.line != 1 || error.column != 2)
+        fail("json_pack didn't get the error coordinates right!");
 
     /* missing close object */
-    if(json_pack(NULL, "{"))
+    if(json_pack(&error, "{"))
         fail("json_pack failed to catch missing '}'");
+    if(error.line != 1 || error.column != 2)
+        fail("json_pack didn't get the error coordinates right!");
 
     /* NULL string */
-    if(json_pack(NULL, "s", NULL))
-        fail("json_pack failed to catch null string");
+    if(json_pack(&error, "s", NULL))
+        fail("json_pack failed to catch null argument string");
+    if(error.line != 1 || error.column != 1)
+        fail("json_pack didn't get the error coordinates right!");
+
+    /* NULL format */
+    if(json_pack(&error, NULL))
+        fail("json_pack failed to catch NULL format string");
+    if(error.line != 1 || error.column != 1)
+        fail("json_pack didn't get the error coordinates right!");
+
+    /* More complicated checks for row/columns */
+    if(json_pack(&error, "{ {}: s }", "foo"))
+        fail("json_pack failed to catch object as key");
+    if(error.line != 1 || error.column != 3)
+        fail("json_pack didn't get the error coordinates right!");
+
+    if(json_pack(&error, "{ s: {},  s:[ii{} }", "foo", "bar", 12, 13))
+        fail("json_pack failed to catch missing ]");
+    if(error.line != 1 || error.column != 13)
+        fail("json_pack didn't get the error coordinates right!");
+
+    if(json_pack(&error, "[[[[[   [[[[[  [[[[ }]]]] ]]]] ]]]]]"))
+        fail("json_pack failed to catch missing ]");
+    if(error.line != 1 || error.column != 21)
+        fail("json_pack didn't get the error coordinates right!");
 
     return(0);
+
+    //fprintf(stderr, "%i/%i: %s %s\n", error.line, error.column, error.source, error.text);
 }
 
 /* vim: ts=4:expandtab:sw=4