1 /*
2  * This program is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
15 
16 #include <locale.h>
17 #include <string.h>
18 
19 #include <libebook/libebook.h>
20 
21 #define QUERY_STRING1
22 #define QUERY_STRING2
23 
24 /* Pick a locale category to set and test. */
25 #ifdef LC_ADDRESS
26 /* LC_ADDRESS is a GNU extension. */
27 #define CATEGORY LC_ADDRESS
28 #else
29 /* Mimic the fallback branch in EBookQuery. */
30 #ifdef G_OS_WIN32
31 #ifndef LC_MESSAGES
32 #define LC_MESSAGES LC_CTYPE
33 #endif
34 #endif
35 #define CATEGORY LC_MESSAGES
36 #endif /* LC_ADDRESS */
37 
38 typedef struct {
39 	EBookQuery *query;
40 	gchar *locale;
41 	gchar *sexp;
42 } TestData;
43 
44 static void
normalize_space(gchar * str)45 normalize_space (gchar *str)
46 {
47 	while (*str) {
48 		gchar *tail;
49 
50 		if (*str++ != ' ')
51 			continue;
52 
53 		tail = str;
54 
55 		while (*tail == ' ')
56 			++tail;
57 
58 		memmove (str, tail, strlen (tail) + 1);
59 	}
60 }
61 
62 static void
test_query(gconstpointer data)63 test_query (gconstpointer data)
64 {
65 	const TestData *const test = data;
66 	EBookQuery *query;
67 	gchar *sexp;
68 
69 	g_assert (setlocale (CATEGORY, test->locale) != NULL);
70 	sexp = e_book_query_to_string (test->query);
71 	normalize_space (sexp);
72 
73 	g_assert_cmpstr (test->sexp, ==, sexp);
74 	g_free (sexp);
75 
76 	query = e_book_query_from_string (test->sexp);
77 	g_assert (query != NULL);
78 
79 	sexp = e_book_query_to_string (query);
80 	e_book_query_unref (query);
81 	normalize_space (sexp);
82 
83 	g_assert_cmpstr (test->sexp, ==, sexp);
84 	g_free (sexp);
85 }
86 
87 static void
test_data_free(gpointer data)88 test_data_free (gpointer data)
89 {
90 	TestData *const test = data;
91 
92 	e_book_query_unref (test->query);
93 	g_free (test->sexp);
94 	g_free (test->locale);
95 
96 	g_slice_free (TestData, test);
97 }
98 
99 static void
add_query_test(const gchar * path,EBookQuery * query,const gchar * sexp)100 add_query_test (const gchar *path,
101                 EBookQuery *query,
102                 const gchar *sexp)
103 {
104 	TestData *data = g_slice_new (TestData);
105 
106 	data->locale = g_strdup (setlocale (CATEGORY, NULL));
107 	data->sexp = g_strdup (sexp);
108 	data->query = query;
109 
110 	g_test_add_data_func_full (path, data, test_query, test_data_free);
111 }
112 
113 gint
main(gint argc,gchar ** argv)114 main (gint argc,
115       gchar **argv)
116 {
117 	g_test_init (&argc, &argv, NULL);
118 	g_test_bug_base ("http://bugzilla.gnome.org/");
119 
120 	add_query_test (
121 		"/libebook/test-query/sexp/all",
122 		e_book_query_any_field_contains (NULL),
123 		"(contains \"x-evolution-any-field\" \"\")");
124 
125 	add_query_test (
126 		"/libebook/test-query/sexp/any",
127 		e_book_query_any_field_contains ("liberty"),
128 		"(contains \"x-evolution-any-field\" \"liberty\")");
129 
130 	add_query_test (
131 		"/libebook/test-query/sexp/not",
132 		e_book_query_not (
133 			e_book_query_any_field_contains ("liberty"), TRUE),
134 		"(not (contains \"x-evolution-any-field\" \"liberty\"))");
135 
136 	add_query_test (
137 		"/libebook/test-query/sexp/and",
138 		e_book_query_andv (
139 			e_book_query_any_field_contains ("liberty"),
140 			e_book_query_any_field_contains ("friendship"),
141 			NULL),
142 		"(and (contains \"x-evolution-any-field\" \"liberty\")"
143 		" (contains \"x-evolution-any-field\" \"friendship\")"
144 		" )");
145 
146 	add_query_test (
147 		"/libebook/test-query/sexp/or",
148 		e_book_query_orv (
149 			e_book_query_any_field_contains ("liberty"),
150 			e_book_query_any_field_contains ("friendship"),
151 			NULL),
152 		"(or (contains \"x-evolution-any-field\" \"liberty\")"
153 		" (contains \"x-evolution-any-field\" \"friendship\")"
154 		" )");
155 
156 	add_query_test (
157 		"/libebook/test-query/sexp/exists",
158 		e_book_query_field_exists (
159 			E_CONTACT_FULL_NAME),
160 			"(exists \"full_name\")");
161 
162 	add_query_test (
163 		"/libebook/test-query/sexp/contains",
164 		e_book_query_field_test (
165 			E_CONTACT_FULL_NAME,
166 			E_BOOK_QUERY_CONTAINS,
167 			"Miguel"),
168 		"(contains \"full_name\" \"Miguel\")");
169 
170 	add_query_test (
171 		"/libebook/test-query/sexp/is",
172 		e_book_query_field_test (
173 			E_CONTACT_GIVEN_NAME,
174 			E_BOOK_QUERY_IS,
175 			"Miguel"),
176 		"(is \"given_name\" \"Miguel\")");
177 
178 	add_query_test (
179 		"/libebook/test-query/sexp/beginswith",
180 		e_book_query_field_test (
181 			E_CONTACT_FULL_NAME,
182 			E_BOOK_QUERY_BEGINS_WITH,
183 			"Mig"),
184 		"(beginswith \"full_name\" \"Mig\")");
185 
186 	add_query_test (
187 		"/libebook/test-query/sexp/endswith",
188 		e_book_query_field_test (
189 			E_CONTACT_TEL,
190 			E_BOOK_QUERY_ENDS_WITH,
191 			"5423789"),
192 		"(endswith \"phone\" \"5423789\")");
193 
194 	if (setlocale (CATEGORY, "en_US.UTF-8") != NULL) {
195 		add_query_test (
196 			"/libebook/test-query/sexp/eqphone/us",
197 			e_book_query_orv (
198 				e_book_query_field_test (
199 					E_CONTACT_TEL,
200 					E_BOOK_QUERY_EQUALS_PHONE_NUMBER,
201 					"+1-2215423789"),
202 				e_book_query_field_test (
203 					E_CONTACT_TEL,
204 					E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER,
205 					"2215423789"),
206 				e_book_query_field_test (
207 					E_CONTACT_TEL,
208 					E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER,
209 					"5423789"),
210 					NULL),
211 			"(or (eqphone \"phone\" \"+1-2215423789\" \"en_US.UTF-8\")"
212 			" (eqphone_national \"phone\" \"2215423789\" \"en_US.UTF-8\")"
213 			" (eqphone_short \"phone\" \"5423789\" \"en_US.UTF-8\")"
214 			" )");
215 	} else {
216 		g_message ("Failed to set locale to en_US.UTF-8");
217 		g_message ("Skipping /libebook/test-query/sexp/eqphone/us");
218 	}
219 
220 	if (setlocale (CATEGORY, "en_GB.UTF-8") != NULL) {
221 		add_query_test (
222 			"/libebook/test-query/sexp/eqphone/gb",
223 			e_book_query_orv (
224 				e_book_query_field_test (
225 					E_CONTACT_TEL,
226 					E_BOOK_QUERY_EQUALS_PHONE_NUMBER,
227 					"+1-2215423789"),
228 				e_book_query_field_test (
229 					E_CONTACT_TEL,
230 					E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER,
231 					"2215423789"),
232 				e_book_query_field_test (
233 					E_CONTACT_TEL,
234 					E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER,
235 					"5423789"),
236 				NULL),
237 			"(or (eqphone \"phone\" \"+1-2215423789\" \"en_GB.UTF-8\")"
238 			" (eqphone_national \"phone\" \"2215423789\" \"en_GB.UTF-8\")"
239 			" (eqphone_short \"phone\" \"5423789\" \"en_GB.UTF-8\")"
240 			" )");
241 	} else {
242 		g_message ("Failed to set locale to en_GB.UTF-8");
243 		g_message ("Skipping /libebook/test-query/sexp/eqphone/gb");
244 	}
245 
246 	return g_test_run ();
247 }
248