]> git.evergreen-ils.org Git - OpenSRF.git/blob - tests/check_osrf_message.c
LP#1268619: websocket : add JS lib to makefile
[OpenSRF.git] / tests / check_osrf_message.c
1 #include <check.h>
2 #include "opensrf/osrf_json.h"
3 #include "opensrf/osrf_message.h"
4
5 osrfMessage *o;
6
7 //Set up the test fixture
8 void setup(void) {
9   o = osrf_message_init(CONNECT, 1, 1);
10 }
11
12 //Clean up the test fixture
13 void teardown(void) {
14   osrfMessageFree(o);
15 }
16
17 //Tests
18
19 START_TEST(test_osrf_message_init)
20   fail_if (o == NULL, "osrfMessage not created");
21 END_TEST
22
23 START_TEST(test_osrf_message_get_last_locale)
24     fail_unless(osrf_message_get_last_locale() == NULL,
25         "osrf_message_get_last_locale should return the value of current_locale");
26 END_TEST
27
28 START_TEST(test_osrf_message_set_locale)
29   const char* new_locale = "en-CA";
30   fail_unless(osrf_message_set_locale(o, NULL) == NULL,
31       "osrf_message_set_locale should return NULL if locale is NULL");
32   fail_unless(osrf_message_set_locale(NULL, new_locale) == NULL,
33       "osrf_message_set_locale should return NULL if msg is NULL");
34   const char* l = osrf_message_set_locale(o, new_locale);
35   fail_unless(strcmp(l, "en-CA") == 0,
36       "osrf_message_set_locale should return the new locale");
37   fail_unless(strcmp(o->sender_locale, "en-CA") == 0,
38       "osrf_message_set_locale should set osrfMessage->sender_locale to the new locale");
39 END_TEST
40
41 START_TEST(test_osrf_message_set_default_locale)
42   fail_unless(osrf_message_set_default_locale(NULL) == NULL,
43       "osrf_message_set_default_locale should return NULL if given a NULL arg");
44   fail_unless(osrf_message_set_default_locale("This string is \
45         longer than 16 characters for sure") == NULL,
46       "osrf_message_set_default_locale should return NULL if locale arg is longer than 16 chars");
47   fail_unless(strcmp(osrf_message_set_default_locale("fr-CA"),
48       "fr-CA") == 0,
49       "osrf_message_set_default_locale should return the new default locale if successful");
50 END_TEST
51
52 START_TEST(test_osrf_message_set_method)
53   osrf_message_set_method(o, NULL);
54   fail_unless(o->method_name == NULL,
55       "osrf_message_set_method should return NULL if given a NULL method_name arg");
56   osrf_message_set_method(o, "add");
57   fail_unless(strcmp(o->method_name, "add") == 0,
58       "osrf_message_set_method should set osrfMessage->method_name if successful");
59 END_TEST
60
61 START_TEST(test_osrf_message_set_params)
62   osrf_message_set_params(o, NULL);
63   fail_unless(o->_params == NULL,
64       "osrf_message_set_params should set msg->_params to NULL when passed a NULL o arg");
65   jsonObject *testJSONObject;
66   testJSONObject = jsonNewObject("test");
67   osrf_message_set_params(o, testJSONObject);
68   fail_unless(strcmp(jsonObjectGetIndex(o->_params, 0)->value.s, "test") == 0,
69       "osrf_message_set_params should set msg->_params to an array containing the\
70       jsonObject passed");
71   jsonObjectFree(testJSONObject);
72 END_TEST
73
74 //END Tests
75
76 Suite *osrf_message_suite(void) {
77   //Create test suite, test case, initialize fixture
78   Suite *s = suite_create("osrf_message");
79   TCase *tc_core = tcase_create("Core");
80   tcase_add_checked_fixture (tc_core, setup, teardown);
81
82   //Add tests to test case
83   tcase_add_test(tc_core, test_osrf_message_init);
84   tcase_add_test(tc_core, test_osrf_message_get_last_locale);
85   tcase_add_test(tc_core, test_osrf_message_set_locale);
86   tcase_add_test(tc_core, test_osrf_message_set_default_locale);
87   tcase_add_test(tc_core, test_osrf_message_set_method);
88   tcase_add_test(tc_core, test_osrf_message_set_params);
89
90   //Add test case to test suite
91   suite_add_tcase(s, tc_core);
92
93   return s;
94 }
95
96 void run_tests(SRunner *sr) {
97   srunner_add_suite(sr, osrf_message_suite());
98 }