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