From dd9ab7fb60a768a7ae4f2a261566d39be4f6b12c Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakob.olsson@iopsys.eu>
Date: Wed, 2 Oct 2019 14:56:51 +0200
Subject: [PATCH] add test.json, make relative path

---
 api.c           |  2 +-
 test.json       | 21 +++++++++++
 test/api_test.c | 93 ++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 90 insertions(+), 26 deletions(-)
 create mode 100644 test.json

diff --git a/api.c b/api.c
index 34b1633..5b02ef1 100644
--- a/api.c
+++ b/api.c
@@ -399,7 +399,7 @@ struct json_object *get(struct json_object *src, char *fmt)
 
 int main()
 {
-	struct json_object *obj = path_to_obj("/home/jakob/git/json-editor/test.json");
+	struct json_object *obj = path_to_obj("test.json");
 	struct json_object *ptr;
 
 	printf("%s %d\n", __func__, __LINE__);
diff --git a/test.json b/test.json
new file mode 100644
index 0000000..b9f9184
--- /dev/null
+++ b/test.json
@@ -0,0 +1,21 @@
+{
+	"test":"success",
+	"nested": {
+		"api":"test2"
+	},
+	"integer": 1,
+	"array": [[1,2,3], [4,5,6]],
+	"complex_nested": {
+		"dummy": {},
+		"array": [
+			{
+				"test":"1",
+				"test2":2
+			 },
+			{
+				"test": "3",
+				"test2": 4
+			}
+		]
+	}
+}
diff --git a/test/api_test.c b/test/api_test.c
index 5fdc6a4..5d3d054 100644
--- a/test/api_test.c
+++ b/test/api_test.c
@@ -13,7 +13,7 @@ static void test_cfg_parse_success(void **state)
 {
 	(void) state; /* unused */
 
-	struct json_object *file = path_to_obj("/home/jakob/git/json-editor-api/test.json");
+	struct json_object *file = path_to_obj("test.json");
 
     struct json_object *obj = json_object_new_object();
     struct json_object *tot = json_object_new_object();
@@ -35,15 +35,58 @@ static void test_cfg_parse_fail(void **state)
 	if (obj)
 		json_object_put(obj);
 }
-
+/*
+{
+    "test":"success",
+    "nested": {
+        "api":"test2"
+    },
+    "integer": 1,
+    "array": [[1, 2, 3],[3, 4, 5]],
+    "complex_nested": {
+        "dummy": {},
+        "array": [
+                {
+                        "test":"1",
+                        "test2":2
+                    },
+                {
+                        "test": "3",
+                        "test2": 4
+                }
+        ]
+    }
+}
+*/
 static void test_build_from_scratch(void **state)
 {
 	(void) state; /* unused */
 
-	struct json_object *file = path_to_obj("/home/jakob/git/json-editor-api/test.json");
+	struct json_object *file = path_to_obj("test.json");
     struct json_object *jobj = NULL;
 
-    set_by_string(&jobj, NULL, "{ \"test\":\"success\", \"nested\": { \"api\":\"test2\"} }", json_type_object);
+    set_by_string(&jobj, NULL,
+        "{\
+            \"test\":\"success\",\
+            \"nested\": { \
+                \"api\":\"test2\"\
+            },\
+            \"integer\": 1,\
+            \"array\": [[1, 2, 3],[4, 5, 6]],\
+            \"complex_nested\": {\
+                \"dummy\": {},\
+                \"array\": [\
+                        {\
+                                \"test\":\"1\",\
+                                \"test2\":2\
+                         },\
+                        {\
+                                \"test\": \"3\",\
+                                \"test2\": 4\
+                        }\
+                ]\
+            }\
+        }", json_type_object);
 
     printf("file_obj=%s\n", json_object_get_string(file));
     printf("modify_obj=%s\n", json_object_get_string(jobj));
@@ -237,11 +280,11 @@ static void test_json_add_array_int(void **state)
     struct json_object *obj = json_object_new_object();
 
     json_object_array_add(arr, json_object_new_int(1));
-    json_object_object_add(file_obj, "array", arr);
+    json_object_object_add(file_obj, "array1", arr);
     json_object_object_add(obj, "array", json_object_get(arr));
     json_object_object_add(file_obj, "inner", obj);
 
-    set_by_string(&modify_obj, "array[-1]", "1", json_type_int);
+    set_by_string(&modify_obj, "array1[-1]", "1", json_type_int);
     set_by_string(&modify_obj, "inner.array[-1]", "1", json_type_int);
 
     printf("file_obj=%s\n", json_object_get_string(file_obj));
@@ -263,11 +306,11 @@ static void test_json_add_array_object(void **state)
 
     json_object_array_add(arr, obj);
     json_object_array_add(arr, json_object_get(obj)); // array will now hold two references to the same object (will double free otherwise)
-    json_object_object_add(file_obj, "array", arr);
+    json_object_object_add(file_obj, "array123", arr);
 
-    set_by_string(&modify_obj, "array[-1].integer", "1", json_type_int);
-    set_by_string(&modify_obj, "array[0].string", "test", json_type_string);
-    set_by_string(&modify_obj, "array[-1]", "{\"integer\": 1, \"string\":\"test\"}", json_type_object);
+    set_by_string(&modify_obj, "array123[-1].integer", "1", json_type_int);
+    set_by_string(&modify_obj, "array123[0].string", "test", json_type_string);
+    set_by_string(&modify_obj, "array123[-1]", "{\"integer\": 1, \"string\":\"test\"}", json_type_object);
 
     printf("file_obj=%s\n", json_object_get_string(file_obj));
     printf("modify_obj=%s\n", json_object_get_string(modify_obj));
@@ -292,13 +335,13 @@ static void test_json_add_array_nested_object(void **state)
     json_object_array_add(arr, json_object_get(obj));
     json_object_array_add(arr, json_object_get(obj));
     json_object_array_add(arr, json_object_get(obj));
-    json_object_object_add(file_obj, "array", arr);
+    json_object_object_add(file_obj, "array123", arr);
 
-    set_by_string(&modify_obj, NULL, "{\"array\": [{\"nested\": {\"nested1\": {\"integer\": 1}}}]}", json_type_object);
-    set_by_string(&modify_obj, "array[5].nested.nested1.integer", "1", json_type_int);
-    set_by_string(&modify_obj, "array[-1].nested.nested1", "{\"integer\": 1}", json_type_object);
-    set_by_string(&modify_obj, "array[-1].nested", "{\"nested1\": {\"integer\": 1}}", json_type_object);
-    set_by_string(&modify_obj, "array[-1]", "{\"nested\": {\"nested1\": {\"integer\": 1}}}", json_type_object);
+    set_by_string(&modify_obj, NULL, "{\"array123\": [{\"nested\": {\"nested1\": {\"integer\": 1}}}]}", json_type_object);
+    set_by_string(&modify_obj, "array123[5].nested.nested1.integer", "1", json_type_int);
+    set_by_string(&modify_obj, "array123[-1].nested.nested1", "{\"integer\": 1}", json_type_object);
+    set_by_string(&modify_obj, "array123[-1].nested", "{\"nested1\": {\"integer\": 1}}", json_type_object);
+    set_by_string(&modify_obj, "array123[-1]", "{\"nested\": {\"nested1\": {\"integer\": 1}}}", json_type_object);
 
     printf("file_obj=%s\n", json_object_get_string(file_obj));
     printf("modify_obj=%s\n", json_object_get_string(modify_obj));
@@ -324,16 +367,16 @@ static void test_json_add_array_nested_array(void **state)
     json_object_array_add(arr, nested_arr);
     json_object_array_add(arr, nested1_arr);
 
-    json_object_object_add(file_obj, "array", arr);
+    json_object_object_add(file_obj, "array123", arr);
 
     //set_by_string("array[0][0].test", &modify_obj, "1", json_type_int);
-    set_by_string(&modify_obj, "array[0][0]", "1", json_type_int);
-    set_by_string(&modify_obj, "array[0][1]", "2", json_type_int);
-    set_by_string(&modify_obj, "array[0][2]", "3", json_type_int);
+    set_by_string(&modify_obj, "array123[0][0]", "1", json_type_int);
+    set_by_string(&modify_obj, "array123[0][1]", "2", json_type_int);
+    set_by_string(&modify_obj, "array123[0][2]", "3", json_type_int);
 
-    set_by_string(&modify_obj, "array[1][-1]", "5", json_type_int);
-    set_by_string(&modify_obj, "array[1][-1]", "6", json_type_int);
-    set_by_string(&modify_obj, "array[1][-1]", "7", json_type_int);
+    set_by_string(&modify_obj, "array123[1][-1]", "5", json_type_int);
+    set_by_string(&modify_obj, "array123[1][-1]", "6", json_type_int);
+    set_by_string(&modify_obj, "array123[1][-1]", "7", json_type_int);
 
     printf("file_obj=%s\n", json_object_get_string(file_obj));
     printf("modify_obj=%s\n", json_object_get_string(modify_obj));
@@ -344,8 +387,8 @@ static void test_json_add_array_nested_array(void **state)
 }
 
 static int setup (void** state) {
-    file_obj = path_to_obj("/home/jakob/git/json-editor-api/test.json");
-    modify_obj = path_to_obj("/home/jakob/git/json-editor-api/test.json");
+    file_obj = path_to_obj("test.json");
+    modify_obj = path_to_obj("test.json");
 	return 0;
 }
 
-- 
GitLab