]> git.evergreen-ils.org Git - OpenSRF.git/blob - tests/check_osrf_json_object.c
LP#1285915: document that perl2JSON doesn't order hash keys
[OpenSRF.git] / tests / check_osrf_json_object.c
1 #include <check.h>
2 #include "opensrf/osrf_json.h"
3
4 jsonObject *jsonObj;
5 jsonObject *jsonHash;
6 jsonObject *jsonNumber;
7 jsonObject *jsonBool;
8 jsonObject *jsonArray;
9
10 //Set up the test fixture
11 void setup (void) {
12   jsonObj = jsonNewObject("test");
13   jsonHash = jsonNewObject(NULL);
14   jsonNumber = jsonNewNumberObject(123.456);
15   jsonBool = jsonNewBoolObject(0);
16   jsonArray = jsonNewObjectType(JSON_ARRAY);
17 }
18
19 //Clean up the test fixture
20 void teardown (void) {
21   jsonObjectFree(jsonObj);
22   jsonObjectFree(jsonHash);
23   jsonObjectFree(jsonNumber);
24   jsonObjectFree(jsonBool);
25   jsonObjectFree(jsonArray);
26 }
27
28 //Tests
29
30 START_TEST(test_osrf_json_object_jsonNewObject)
31   fail_if(jsonObj == NULL, "jsonObject not created");
32 END_TEST
33
34 START_TEST(test_osrf_json_object_jsonNewObjectFmt)
35   jsonObject *fmtObj;
36   jsonObject *nullObj;
37   fmtObj = jsonNewObjectFmt("string %d %d", 10, 20);
38   nullObj = jsonNewObjectFmt(NULL);
39
40   fail_if(fmtObj == NULL, "jsonObject not created");
41   fail_unless(strcmp(fmtObj->value.s, "string 10 20") == 0,
42       "jsonObject->value.s should contain the formatted string passed to jsonNewObjectFmt()");
43   fail_unless(fmtObj->type == JSON_STRING,
44       "jsonNewObjectFmt should set the jsonObject->type to JSON_STRING");
45   fail_unless(nullObj->value.s == NULL,
46       "jsonNewObjectFmt should set jsonObject->value.s to NULL if passed a NULL arg");
47   fail_unless(nullObj->type == JSON_NULL,
48       "jsonNewObjectFmt should set jsonObject->type to JSON_NULL if passed a NULL arg");
49 END_TEST
50
51 START_TEST(test_osrf_json_object_jsonNewNumberObject)
52   jsonObject *numObj;
53   numObj = jsonNewNumberObject(123);
54
55   fail_if(numObj == NULL, "jsonObject not created");
56   fail_unless(strcmp(numObj->value.s, "123") == 0,
57       "jsonNewNumberObject should set jsonObject->value.s to the string value of the num arg");
58   fail_unless(numObj->type == JSON_NUMBER,
59       "jsonNewNumberObject should set jsonObject->type to JSON_NUMBER");
60 END_TEST
61
62 START_TEST(test_osrf_json_object_jsonNewNumberStringObject)
63   jsonObject *nullobj = jsonNewNumberStringObject(NULL);
64   fail_unless(strcmp(nullobj->value.s, "0") == 0,
65       "jsonNewNumberStringObject should return a jsonObject with a value of 0 if passed a NULL numstr arg");
66   fail_unless(nullobj->type == JSON_NUMBER,
67       "jsonNewNumberStringObject should return a jsonObject with type JSON_NUMBER");
68   jsonObject *notnumobj = jsonNewNumberStringObject("not a number");
69   fail_unless(notnumobj == NULL,
70       "jsonNewNumberStringObject should return NULL if passed an arg that is not a number string");
71   jsonObject *numstrobj = jsonNewNumberStringObject("123");
72   fail_unless(strcmp(numstrobj->value.s, "123") == 0,
73       "jsonNewNumberStringObject should return a jsonObject with value.s = the value of the numstr arg");
74   fail_unless(numstrobj->type == JSON_NUMBER,
75       "jsonNewNumberStringObject should return a jsonObject of type JSON_NUMBER");
76 END_TEST
77
78 START_TEST(test_osrf_json_object_jsonNewBoolObject)
79   fail_unless(jsonBool->type == JSON_BOOL,
80       "jsonNewBoolObject should return a jsonObject of type JSON_BOOL");
81   fail_unless(jsonBool->value.b == 0,
82       "jsonNewBoolObject should return an object with a value of the val arg");
83 END_TEST
84
85 START_TEST(test_osrf_json_object_jsonSetBool)
86   jsonSetBool(jsonBool, -1);
87   fail_unless(jsonBool->value.b == -1,
88       "jsonSetBool should set jsonObject->value.b to the value of the val arg");
89 END_TEST
90
91 START_TEST(test_osrf_json_object_jsonObjectSetKey)
92   fail_unless(jsonObjectSetKey(NULL, "key1", NULL) == -1);
93   fail_unless(jsonObjectSetKey(jsonHash, "key1", NULL) == 1);
94   fail_unless(jsonObjectSetKey(jsonHash, "key2", jsonNewObject("test2")) == 2);
95   fail_unless(jsonObjectGetKey(jsonHash, "key1")->value.s == NULL);
96   fail_unless(strcmp(jsonObjectGetKey(jsonHash, "key2")->value.s, "test2") == 0);
97 END_TEST
98
99 START_TEST(test_osrf_json_object_jsonObjectRemoveKey)
100   jsonObjectSetKey(jsonHash, "key1", jsonNewObject("value"));
101   fail_unless(jsonObjectRemoveKey(jsonHash, NULL) == -1);
102   fail_unless(jsonObjectRemoveKey(jsonHash, "key1") == 1);
103 END_TEST
104
105 START_TEST(test_osrf_json_object_jsonObjectGetKey)
106   jsonObjectSetKey(jsonHash, "key1", jsonNewObject("value"));
107   fail_unless(strcmp(jsonObjectGetKey(jsonHash, "key1")->value.s, "value") == 0);
108 END_TEST
109
110 START_TEST(test_osrf_json_object_jsonObjectSetClass)
111   jsonObjectSetClass(jsonObj, NULL);
112   fail_unless(jsonObj->classname == NULL);
113   jsonObjectSetClass(jsonObj, "aClass");
114   fail_unless(strcmp(jsonObj->classname, "aClass") == 0);
115 END_TEST
116
117 START_TEST(test_osrf_json_object_jsonObjectGetClass)
118   fail_unless(jsonObjectGetClass(NULL) == NULL);
119   jsonObjectSetClass(jsonObj, "aClass");
120   fail_unless(strcmp(jsonObjectGetClass(jsonObj), "aClass") == 0);
121 END_TEST
122
123 START_TEST(test_osrf_json_object_jsonObjectSetIndex)
124   jsonObject *jsonArrayValue = jsonNewObject("value");
125   fail_unless(jsonObjectSetIndex(NULL, 0, jsonArrayValue) == -1,
126       "jsonObjectSetIndex should return -1 if dest arg is NULL");
127   fail_unless(jsonObjectSetIndex(jsonArray, 0, NULL) == 1,
128       "jsonObjectSetIndex should return the size of the json array after setting the new index");
129   fail_unless(jsonObjectSetIndex(jsonArray, 1, jsonArrayValue) == 2,
130       "jsonObjectSetIndex should return the size of the json array after setting the new index");
131   jsonObject *jsonArrayResult = jsonObjectGetIndex(jsonArray, 1);
132   fail_unless(strcmp(jsonArrayResult->value.s, "value") == 0,
133       "the value inserted into the jsonArray should be the value of the newObj arg");
134   fail_unless(jsonArrayResult->parent == jsonArray,
135       "the parent of the element inserted should be equal to the newObj arg");
136 END_TEST
137
138 START_TEST(test_osrf_json_object_jsonObjectGetIndex)
139   jsonObject *jsonArrayValue = jsonNewObject("value");
140   jsonObjectSetIndex(jsonArray, 0, jsonArrayValue);
141   fail_unless(jsonObjectGetIndex(NULL, 0) == NULL,
142       "if no obj arg is passed to jsonObjectGetIndex, it should return NULL");
143   fail_unless(jsonObjectGetIndex(jsonArray, 2) == NULL,
144       "if the index in the jsonArray is NULL, jsonObjectGetIndex should return NULL");
145   fail_unless(jsonObjectGetIndex(jsonNumber, 0) == NULL,
146       "if the obj arg isn't of type JSON_ARRAY, return NULL");
147   jsonObject *getIndexValue = jsonObjectGetIndex(jsonArray, 0);
148   fail_unless(strcmp(getIndexValue->value.s, "value") == 0,
149       "jsonObjectGetIndex should return the jsonObject at the index given");
150 END_TEST
151
152 START_TEST(test_osrf_json_object_jsonObjectToJSONRaw)
153   fail_unless(jsonObjectToJSONRaw(NULL) == NULL,
154       "when passed NULL, jsonObjectToJSONRaw should return NULL");
155
156   jsonObject *val1 = jsonNewObject("value1");
157   jsonObject *val2 = jsonNewObject("value2");
158   jsonObjectSetClass(val1, "class1");
159   jsonObjectSetClass(val2, "class2");
160   jsonObjectSetKey(jsonHash, "key1", val1);
161   jsonObjectSetKey(jsonHash, "key2", val2);
162
163   fail_unless(strcmp(jsonObjectToJSONRaw(jsonHash),
164       "{\"key1\":\"value1\",\"key2\":\"value2\"}") == 0,
165       "jsonObjectToJSONRaw should return a string of raw JSON, without expanding\
166       class names, built from the obj arg");
167 END_TEST
168
169 START_TEST(test_osrf_json_object_jsonObjectToJSON)
170   fail_unless(jsonObjectToJSON(NULL) == NULL,
171       "jsonObjectToJSON should return NULL if passed a NULL obj arg");
172   jsonObject *val1 = jsonNewObject("value1");
173   jsonObject *val2 = jsonNewObject("value2");
174   jsonObjectSetClass(val1, "class1");
175   jsonObjectSetClass(val2, "class2");
176
177   jsonObjectSetKey(jsonHash, "key1", val1);
178   jsonObjectSetKey(jsonHash, "key2", val2);
179   fail_unless(strcmp(jsonObjectToJSON(jsonHash),
180       "{\"key1\":{\"__c\":\"class1\",\"__p\":\"value1\"},\"key2\":{\"__c\":\"class2\",\"__p\":\"value2\"}}") == 0,
181       "jsonObjectToJSON should return the obj arg as raw json, expanding class names");
182 END_TEST
183
184 START_TEST(test_osrf_json_object_doubleToString)
185   fail_unless(strcmp(doubleToString(123.456),
186       "123.456000000000003069544618484") == 0,
187       "doubleToString should return a string version of the given double, with a precision of 30 digits");
188 END_TEST
189
190 START_TEST(test_osrf_json_object_jsonObjectGetString)
191   fail_unless(strcmp(jsonObjectGetString(jsonObj), "test") == 0,
192       "jsonObjectGetString should return the value of the given object, if it is of type JSON_STRING");
193   fail_unless(strcmp(jsonObjectGetString(jsonNumber),
194       "123.456000000000003069544618484") == 0,
195       "jsonObjectGetString should return the value of the given JSON_NUMBER object if it is not NULL");
196   jsonObject *jsonNullNumber = jsonNewNumberObject(0);
197   jsonObjectSetNumberString(jsonNullNumber, "NaN"); //set jsonNullNumber->value to NULL
198   fail_unless(strcmp(jsonObjectGetString(jsonNullNumber), "0") == 0,
199       "jsonObjectGetString should return 0 if value of the given JSON_NUMBER object is NULL");
200   fail_unless(jsonObjectGetString(jsonHash) == NULL,
201       "jsonObjectGetString should return NULL if the given arg is not of type JSON_NUMBER or JSON_STRING");
202   fail_unless(jsonObjectGetString(NULL) == NULL,
203       "jsonObjectGetString should return NULL if the given arg is NULL");
204 END_TEST
205
206 START_TEST(test_osrf_json_object_jsonObjectGetNumber)
207   fail_unless(jsonObjectGetNumber(NULL) == 0,
208       "jsonObjectGetNumber should return 0 if given arg is NULL");
209   fail_unless(jsonObjectGetNumber(jsonHash) == 0,
210       "jsonObjectGetNumber should return 0 if given arg is not of type JSON_NUMBER");
211   jsonObject *jsonNullNumber = jsonNewNumberObject(0);
212   jsonObjectSetNumberString(jsonNullNumber, "NaN");
213   fail_unless(jsonObjectGetNumber(jsonNullNumber) == 0,
214       "jsonObjectGetNumber should return 0 if given args value is NULL");
215   fail_unless(jsonObjectGetNumber(jsonNumber) == 123.456000000000003069544618484,
216       "jsonObjectGetNumber should return the value of the given obj in double form");
217 END_TEST
218
219 START_TEST(test_osrf_json_object_jsonObjectSetString)
220   jsonObjectSetString(jsonObj, NULL);
221   fail_unless(strcmp(jsonObj->value.s, "test") == 0,
222       "jsonObjectSetString should not change the value of the dest arg if passed a NULL string arg");
223   jsonObjectSetString(jsonObj, "changed");
224   fail_unless(strcmp(jsonObj->value.s, "changed") == 0,
225       "jsonObjectSetString should change the value of the dest arg to the value of the string arg");
226 END_TEST
227
228 START_TEST(test_osrf_json_object_jsonObjectSetNumberString)
229   fail_unless(jsonObjectSetNumberString(NULL, "asdf") == -1,
230       "jsonObjectSetNumberString should return -1 when dest arg is NULL");
231   fail_unless(jsonObjectSetNumberString(jsonNumber, NULL) == -1,
232       "jsonObjectSetNumberString should return -1 when string arg is NULL");
233   fail_unless(jsonObjectSetNumberString(jsonNumber, "111.111") == 0,
234       "jsonObjectSetNumberString should return 0 upon success");
235   fail_unless(strcmp(jsonNumber->value.s, "111.111") == 0,
236       "jsonObjectSetNumberString should set the value of the dest arg to the value of the string arg");
237   fail_unless(jsonObjectSetNumberString(jsonNumber, "not a number") == -1,
238       "jsonObjectSetNumber should return -1 if the string arg is not numeric");
239   fail_unless(jsonNumber->value.s == NULL,
240       "when the string arg is not numeric, dest->value.s should be set to NULL");
241 END_TEST
242
243 START_TEST(test_osrf_json_object_jsonObjectSetNumber)
244   jsonObjectSetNumber(jsonNumber, 999.999);
245   fail_unless(strcmp(jsonNumber->value.s, "999.999000000000023646862246096") == 0,
246       "jsonObjectSetNumber should set dest->value.s to the stringified version of the num arg");
247 END_TEST
248
249 START_TEST(test_osrf_json_object_jsonObjectClone)
250   jsonObject *nullClone = jsonObjectClone(NULL);
251   fail_unless(nullClone->type == JSON_NULL && nullClone->value.s == NULL,
252       "when passed a NULL arg, jsonObjectClone should return a jsonObject of type JSON_NULL with a value of NULL ");
253
254   jsonObject *anotherNullClone = jsonObjectClone(nullClone);
255   fail_unless(anotherNullClone->type == JSON_NULL && anotherNullClone->value.s == NULL,
256       "jsonObjectClone should return a clone of an object with type JSON_NULL");
257
258   jsonObject *stringClone = jsonObjectClone(jsonObj);
259   fail_unless(stringClone->type == JSON_STRING && strcmp(stringClone->value.s, "test") == 0,
260       "jsonObjectClone should return a clone of an object with type JSON_STRING");
261
262   jsonObject *numberClone = jsonObjectClone(jsonNumber);
263   fail_unless(numberClone->type == JSON_NUMBER,
264       "jsonObjectClone should return a clone of a JSON_NUMBER object");
265   fail_unless(strcmp(numberClone->value.s, "123.456000000000003069544618484") == 0,
266       "jsonObjectClone should return a clone of a JSON_NUMBER object");
267
268   jsonObject *boolClone = jsonObjectClone(jsonBool);
269   fail_unless(boolClone->type == JSON_BOOL && boolClone->value.b == 0,
270       "jsonObjectClone should return a clone of a JSON_BOOL object");
271
272   //Array
273   jsonObject *arrayVal1 = jsonNewObject("arrayval1");
274   jsonObject *arrayVal2 = jsonNewObject("arrayval2");
275   jsonObjectSetIndex(jsonArray, 0, arrayVal1);
276   jsonObjectSetIndex(jsonArray, 0, arrayVal2);
277   jsonObject *arrayClone = jsonObjectClone(jsonArray);
278   fail_unless(strcmp(jsonObjectToJSON(arrayClone), jsonObjectToJSON(jsonArray)) == 0,
279       "jsonObjectClone should return a clone of a JSON_ARRAY object");
280
281   //Hash
282   jsonObject *val1 = jsonNewObject("value1");
283   jsonObject *val2 = jsonNewObject("value2");
284   jsonObjectSetClass(val1, "class1");
285   jsonObjectSetClass(val2, "class2");
286   jsonObjectSetKey(jsonHash, "key1", val1);
287   jsonObjectSetKey(jsonHash, "key2", val2);
288   jsonObject *hashClone = jsonObjectClone(jsonHash);
289   fail_unless(strcmp(jsonObjectToJSON(hashClone), jsonObjectToJSON(jsonHash)) == 0,
290       "jsonObjectClone should return a clone of a JSON_HASH object");
291 END_TEST
292
293 START_TEST(test_osrf_json_object_jsonBoolIsTrue)
294   fail_unless(jsonBoolIsTrue(NULL) == 0,
295       "jsonBoolIsTrue should return 0 if a NULL arg is passed");
296   fail_unless(jsonBoolIsTrue(jsonObj) == 0,
297       "jsonBoolIsTrue should return 0 if a non JSON_BOOL arg is passed");
298   fail_unless(jsonBoolIsTrue(jsonBool) == 0,
299       "jsonBoolIsTrue should return 0 if the value of boolObj is 0");
300   jsonObject *newBool = jsonNewBoolObject(123);
301   fail_unless(jsonBoolIsTrue(newBool) == 1,
302       "jsonBoolIsTrue should return 1 if the value of boolObj is not 0");
303 END_TEST
304
305 //END Tests
306
307
308 Suite *osrf_json_object_suite (void) {
309   //Create test suite, test case, initialize fixture
310   Suite *s = suite_create("osrf_json_object");
311   TCase *tc_core = tcase_create("Core");
312   tcase_add_checked_fixture(tc_core, setup, teardown);
313
314   //Add tests to test case
315   tcase_add_test(tc_core, test_osrf_json_object_jsonNewObject);
316   tcase_add_test(tc_core, test_osrf_json_object_jsonNewObjectFmt);
317   tcase_add_test(tc_core, test_osrf_json_object_jsonNewBoolObject);
318   tcase_add_test(tc_core, test_osrf_json_object_jsonSetBool);
319   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectToJSONRaw);
320   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectToJSON);
321   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectSetKey);
322   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectGetKey);
323   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectSetClass);
324   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectGetClass);
325   tcase_add_test(tc_core, test_osrf_json_object_jsonNewNumberObject);
326   tcase_add_test(tc_core, test_osrf_json_object_jsonNewNumberStringObject);
327   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectRemoveKey);
328   tcase_add_test(tc_core, test_osrf_json_object_doubleToString);
329   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectGetString);
330   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectGetNumber);
331   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectSetString);
332   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectSetNumberString);
333   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectSetNumber);
334   tcase_add_test(tc_core, test_osrf_json_object_jsonBoolIsTrue);
335   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectSetIndex);
336   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectGetIndex);
337   tcase_add_test(tc_core, test_osrf_json_object_jsonObjectClone);
338
339   //Add test case to test suite
340   suite_add_tcase(s, tc_core);
341
342   return s;
343 }
344
345 void run_tests (SRunner *sr) {
346   srunner_add_suite (sr, osrf_json_object_suite());
347 }