2007-04-25 Murray Cumming <murrayc@murrayc.com>
[modest] / tests / check_modest-conf.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <check.h>
31 #include <modest-conf.h>
32 #include <string.h>
33
34 START_TEST (test_modest_conf_new)
35 {
36         ModestConf *conf = modest_conf_new ();
37         fail_unless (MODEST_IS_CONF(conf),
38                      "modest_conf_new should return a valid"
39                      " ModestConf instance");
40         g_object_unref (conf);
41 }
42 END_TEST
43
44
45 START_TEST (test_modest_conf_store_retrieve_string)
46 {
47         ModestConf *conf  = modest_conf_new ();
48         const gchar *key  =  MODEST_CONF_NAMESPACE "/teststring";
49         const gchar *key2 =  MODEST_CONF_NAMESPACE "/teststring2";
50
51         const gchar *data = "hello in Korean:  안녕하세요";
52         gchar *data2;
53         
54         fail_unless (MODEST_IS_CONF(conf),
55                      "modest_conf_new should return a valid"
56                      " ModestConf instance");
57
58         fail_unless (modest_conf_set_string (
59                              conf, key, data, NULL),
60                      "modest_conf_set_string should return TRUE");
61
62         fail_unless (modest_conf_key_exists(conf, key, NULL),
63                      "modest_conf_key_exists should return TRUE for <key>");
64         fail_unless (!modest_conf_key_exists(conf, key2, NULL),
65                      "modest_conf_key_exists should return FALSE for <key2>");
66         
67         data2 = modest_conf_get_string (conf, key, NULL);
68         fail_unless (data2 && strcmp (data2, data) == 0,
69                      "modest_conf_get_string should return what we put there");                                 
70         g_free (data2);
71         
72         fail_unless (modest_conf_remove_key (conf, key, NULL),
73                      "modest_conf_remove_key should return TRUE");
74         
75         fail_unless (!modest_conf_key_exists(conf, key, NULL),
76                      "modest_conf_key should return FALSE after we"
77                      "removed the key");
78         
79         g_object_unref (conf);
80 }
81 END_TEST
82
83
84
85 START_TEST (test_modest_conf_store_retrieve_bool)
86 {
87         ModestConf *conf  = modest_conf_new ();
88         const gchar *key  =  MODEST_CONF_NAMESPACE "/teststring";
89         const gchar *key2 =  MODEST_CONF_NAMESPACE "/teststring2";
90
91         gboolean data = TRUE, data2;
92         
93         fail_unless (MODEST_IS_CONF(conf),
94                      "modest_conf_new should return a valid"
95                      " ModestConf instance");
96
97         fail_unless (modest_conf_set_bool (conf, key, data, NULL),
98                      "modest_conf_set_bool should return TRUE");
99         
100         fail_unless (modest_conf_key_exists(conf, key, NULL),
101                      "modest_conf_key_exists should return TRUE for <key>");
102         fail_unless (!modest_conf_key_exists(conf, key2, NULL),
103                      "modest_conf_key_exists should return FALSE for <key2>");
104         
105         data2 = modest_conf_get_bool (conf, key, NULL);
106         fail_unless (data2 == data,
107                      "modest_conf_get_bool should return what we put there");                                   
108         fail_unless (modest_conf_remove_key (conf, key, NULL),
109                      "modest_conf_remove_key should return TRUE");
110         
111         fail_unless (!modest_conf_key_exists(conf, key, NULL),
112                      "modest_conf_key should return FALSE after we"
113                      "removed the key");
114
115         g_object_unref (conf);
116 }
117 END_TEST
118
119
120 START_TEST (test_modest_conf_store_retrieve_int)
121 {
122         ModestConf *conf  = modest_conf_new ();
123         const gchar *key  =  MODEST_CONF_NAMESPACE "/teststring";
124         const gchar *key2 =  MODEST_CONF_NAMESPACE "/teststring2";
125
126         gint data = 99, data2;
127         
128         fail_unless (MODEST_IS_CONF(conf),
129                      "modest_conf_new should return a valid"
130                      " ModestConf instance");
131
132         fail_unless (modest_conf_set_int (conf, key, data, NULL),
133                      "modest_conf_set_int should return TRUE");
134         
135         fail_unless (modest_conf_key_exists(conf, key, NULL),
136                      "modest_conf_key_exists should return TRUE for <key>");
137         fail_unless (!modest_conf_key_exists(conf, key2, NULL),
138                      "modest_conf_key_exists should return FALSE for <key2>");
139         
140         data2 = modest_conf_get_int (conf, key, NULL);
141         fail_unless (data2 == data,
142                      "modest_conf_get_int should return what we put there");                                    
143         fail_unless (modest_conf_remove_key (conf, key, NULL),
144                      "modest_conf_remove_key should return TRUE");
145         
146         fail_unless (!modest_conf_key_exists(conf, key, NULL),
147                      "modest_conf_key should return FALSE after we"
148                      "removed the key");
149
150         g_object_unref (conf);
151 }
152 END_TEST
153
154
155
156 static Suite*
157 modest_conf_suite (void)
158 {
159         Suite *suite = suite_create ("ModestConf");
160
161         TCase *tc_core = tcase_create ("core");
162         tcase_add_test (tc_core, test_modest_conf_new);
163         tcase_add_test (tc_core, test_modest_conf_store_retrieve_string);
164         tcase_add_test (tc_core, test_modest_conf_store_retrieve_bool);
165         tcase_add_test (tc_core, test_modest_conf_store_retrieve_int);
166
167         suite_add_tcase (suite, tc_core);
168
169         return suite;
170 }
171
172
173 int
174 main ()
175 {
176         SRunner *srunner;
177         Suite   *suite;
178         int     failures;
179         
180         g_type_init();
181
182         suite   = modest_conf_suite ();
183         srunner = srunner_create (suite);
184
185         srunner_run_all (srunner, CK_NORMAL);
186         failures = srunner_ntests_failed (srunner);
187         srunner_free (srunner);
188         
189         return failures;
190 }