* Migration to GtkUIManager almost completed
[modest] / tests / check_text-utils.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 <stdlib.h>
32 #include <string.h>
33 #include "modest-text-utils.h"
34
35 typedef struct {
36         const gchar *original;
37         const gchar *expected;
38 } StringPair;
39
40 /* ----------------- display address tests -------------- */
41
42 /**
43  * Test regular usage of modest_text_utils_get_display_address
44  *  - Test 1: Check "<...>" deletion
45  *  - Test 2: Check "(...)" deletion
46  *  - Test 3: Check address left and right trim, also non ASCII chars
47  *  - Test 4: Check issues in tests 1, 2 and 3 together
48  *  - Test 5: Check with an empty address
49  */
50 START_TEST (test_get_display_address_regular)
51 {
52         gint i;
53         const StringPair tests[] = {
54                 { "John Doe <foo@bar>", "John Doe" },
55                 { "Rupert Griffin (test)", "Rupert Griffin"},
56                 { "    Hyvää päivää    ", "Hyvää päivää"},
57                 { "  John Doe  <foo@bar>  (test)  ", "John Doe" },
58                 { "", "" },
59         };
60
61         /* Tests 1, 2, 3, 4 */
62         for (i = 0; i !=  sizeof(tests)/sizeof(StringPair); ++i) {
63                 gchar *str = g_strdup (tests[i].original);
64                 str = modest_text_utils_get_display_address (str);
65                 fail_unless (str && strcmp(str, tests[i].expected) == 0,
66                         "modest_text_utils_get_display_address failed for '%s': "
67                              "expected '%s' but got '%s'",
68                              tests[i].original, tests[i].expected, str);
69                 g_free (str);
70         }
71 }
72 END_TEST
73
74
75 /**
76  * Test invalid usage of modest_text_utils_get_display_address
77  *  - Test 1: Check with NULL address (should return NULL)
78  */
79 START_TEST (test_get_display_address_invalid)
80 {
81         /* Test 1 */
82         fail_unless (modest_text_utils_get_display_address (NULL) == NULL,
83                      "modest_text_utils_get_display_address(NULL) should be NULL");
84 }
85 END_TEST
86
87 /* ----------------- derived address tests -------------- */
88
89 /**
90  * Test regular usage of modest_text_utils_derived_subject
91  *  - Test 1: Check with a normal subject
92  *  - Test 2: Test with NULL subject
93  *  - Test 3: Test with non ASCII chars
94  *  - Test 4: Test with an empty subject
95  */
96 START_TEST (test_derived_subject_regular)
97 {
98         gint i;
99         const gchar *prefix="Re:";
100         const StringPair tests[] = {
101                 { "subject", "Re: subject" },
102                 { NULL,      "Re:"},
103                 { "Hyvää päivää", "Re: Hyvää päivää"},
104                 { "", "Re: "},
105         };
106
107         /* Tests 1, 2, 3, 4 */
108         for (i = 0; i !=  sizeof(tests)/sizeof(StringPair); ++i) {
109                 gchar *orig = g_strdup (tests[i].original);
110                 gchar *str = modest_text_utils_derived_subject (orig, prefix);
111                 fail_unless (str && strcmp(str, tests[i].expected) == 0,
112                         "modest_text_utils_derived_subject failed for '%s': "
113                              "expected '%s' but got '%s'",
114                              tests[i].original, tests[i].expected, str);
115                 g_free (orig);
116                 g_free (str);
117         }
118 }
119 END_TEST
120
121 /**
122  * Test invalid usage of modest_text_utils_derived_subject
123  *  - Test 1: Check with NULL prefix (should return NULL)
124  */
125 START_TEST (test_derived_subject_invalid)
126 {
127         /* Test 1 */
128         fail_unless (modest_text_utils_derived_subject (NULL, NULL) == NULL,
129                      "modest_text_utils_derived_subject (*,NULL) should be NULL");
130 }
131 END_TEST
132
133 /* --------------------- quote tests -------------------- */
134
135 /**
136  * Test regular usage of modest_text_utils_quote
137  *  - Test 1: Quote empty text
138  *  - Test 2: Quote 1 line of plain text 
139  *  - Test 3: Quote several lines of plain text 
140  *  - Test 4: Quote non ASCII chars
141  *  - Test 5: Quote with a limit lower than the any word in the text
142  *  - TODO: Test HTML quotation once its implementation if finished
143  */
144 START_TEST (test_quote_regular)
145 {
146         gint i;
147         gchar *text = NULL;
148         gchar *expected_quoted_text = NULL;
149         gchar *quoted_text = NULL;
150
151         /* Tests 1, 2, 3 */
152         const StringPair tests[] = {
153                 { "", 
154                   "On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n> \n" },
155                 { "This text should be quoted", 
156                   "On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n> This text\n> should be quoted\n> \n" },
157                 { "These are several\nlines\nof plain text", 
158                   "On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n> These are\n> several lines\n> of plain text\n" },
159                 { "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš", 
160                   "On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n> áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš\n" },
161         };
162
163         for (i = 0; i !=  sizeof(tests)/sizeof(StringPair); ++i) {
164                 text = g_strdup (tests[i].original);
165                 expected_quoted_text = g_strdup (tests[i].expected);
166                 quoted_text = modest_text_utils_quote (text, "text/plain", "foo@bar",  0, 15);
167                 fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text),
168                              "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \
169                              "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"",
170                              text, expected_quoted_text, quoted_text);
171                 g_free (text);
172                 g_free (expected_quoted_text);
173                 g_free (quoted_text);
174         }
175
176         /* Test 5 */
177         text = g_strdup ("Quotation test example");
178         expected_quoted_text = 
179                 g_strdup ("On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n> Quotation\n> test\n> example\n> \n");
180         quoted_text = modest_text_utils_quote (text, "text/plain", "foo@bar",  0, 1);
181         fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text),
182                      "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \
183                      "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"",
184                      text, expected_quoted_text, quoted_text);
185         g_free (text);
186         g_free (expected_quoted_text);
187         g_free (quoted_text);
188 }
189 END_TEST
190
191 /**
192  * Test invalid usage of modest_text_utils_quote
193  *  - Test 1: Check NULL text (should return NULL)
194  *  - Test 2: Check NULL content_type (should quote as text/plain)
195  *  - Test 3: Check NULL address (should cite address as "(null)")
196  *  - Test 4: Check negative limit (should write each word in its own line)
197  *  - TODO: Repeat tests for HTML quotation when its implementation is finished
198  */
199 START_TEST (test_quote_invalid)
200 {
201         gchar *text = NULL;
202         gchar *expected_quoted_text = NULL;
203         gchar *quoted_text = NULL;
204
205         /* Test 1 (Fault) */
206         text = NULL;
207         quoted_text = modest_text_utils_quote (NULL, "text/plain", "foo@bar",  0, 15);
208         fail_unless (quoted_text == NULL,
209                      "modest_text_utils_quote failed:\nOriginal text: NULL\n" \
210                      "Expected quotation: NULL\nQuoted text: \"%s\"",
211                      quoted_text);
212         g_free (quoted_text);
213         
214         
215         /* Test 2 (Fault) */
216         text = g_strdup ("Text");
217         expected_quoted_text = g_strdup ("On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n> Text\n");
218         quoted_text = modest_text_utils_quote (text, NULL, "foo@bar",  0, 15);
219         fail_unless (quoted_text == NULL,
220                      "modest_text_utils_quote failed:\nOriginal text: NULL\n" \
221                      "Expected quotation: NULL\nQuoted text: \"%s\"",
222                      quoted_text);
223         g_free (text);
224         g_free (expected_quoted_text);
225         g_free (quoted_text);
226
227         /* Test 3 */
228         text = g_strdup ("Text");
229         expected_quoted_text = g_strdup ("On Thu Jan  1 01:00:00 1970, (null) wrote:\n> Text\n");
230         quoted_text = modest_text_utils_quote (text, "text/plain", NULL,  0, 15);
231         fail_unless (quoted_text == NULL,
232                      "modest_text_utils_quote failed:\nOriginal text: NULL\n" \
233                      "Expected quotation: NULL\nQuoted text: \"%s\"",
234                      quoted_text);
235         g_free (text);
236         g_free (expected_quoted_text);
237         g_free (quoted_text);
238
239         /* Test 4 */
240         text = g_strdup ("This is a text");
241         expected_quoted_text = g_strdup ("On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n> This\n> is\n> a\n> text\n> \n");
242         quoted_text = modest_text_utils_quote (text, "text/plain", "foo@bar",  0, 0);
243         fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text),
244                      "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \
245                      "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"",
246                      text, expected_quoted_text, quoted_text);
247         g_free (text);
248         g_free (expected_quoted_text);
249         g_free (quoted_text);
250 }
251 END_TEST
252
253 /* ---------------------- cite tests -------------------- */
254
255 /**
256  * Test regular usage of modest_text_utils_cite
257  *  - Test 1: Check cite of an empty text
258  *  - Test 2: Check cite of a line of text
259  *  - Test 3: Check cite of several lines of text
260  *  - Test 4: Check with non ASCII chars
261  */
262 START_TEST (test_cite_regular)
263 {
264         gint i;
265         gchar *cited_text = NULL;
266         const StringPair tests[] = {
267                 { "", 
268                   "On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n\n" },
269                 { "This is some text", 
270                   "On Thu Jan  1 01:00:00 1970, foo@bar wrote:\nThis is some text\n" },
271                 { "This\nis some\ntext", 
272                   "On Thu Jan  1 01:00:00 1970, foo@bar wrote:\nThis\nis some\ntext\n" },
273                 { "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš",
274                   "On Thu Jan  1 01:00:00 1970, foo@bar wrote:\náéíÍÓÚäëïÏÖÜñÑçÇŽÊîš\n" },
275         };
276
277         /* Tests 1, 2, 3, 4 */
278         for (i = 0; i !=  sizeof(tests)/sizeof(StringPair); ++i) {
279                 cited_text = modest_text_utils_cite (tests[i].original, "text/plain", "foo@bar", 0);
280                 fail_unless (cited_text && !strcmp (tests[i].expected, cited_text),
281                              "modest_text_utils_cite failed:\nOriginal text:\n\"%s\"\n" \
282                              "Expected cite:\n\"%s\"\nCite obtained:\n\"%s\"",
283                              tests[i].original, tests[i].expected, cited_text);
284                 g_free (cited_text);
285         }
286 }
287 END_TEST
288
289 /**
290  * Test invalid usage of modest_text_utils_cite
291  *  - Test 1: Check NULL text (should cite text as (null))
292  *  - Test 2: Check NULL address (should cite address as (null)
293  *  TODO: add tests for content_type once it is used in the implementation
294  */
295 START_TEST (test_cite_invalid)
296 {
297         gchar *text = NULL;
298         gchar *cited_text = NULL;
299         gchar *expected_cite = NULL;
300
301         /* Test 1 */
302         text = NULL;
303         expected_cite = g_strdup ("On Thu Jan  1 01:00:00 1970, foo@bar wrote:\n(null)\n");
304         cited_text = modest_text_utils_cite (text, "text/plain", "foo@bar", 0);
305         fail_unless (cited_text && !strcmp (expected_cite, cited_text),
306                      "modest_text_utils_cite failed:\nOriginal text:\nNULL\n" \
307                      "Expected cite:\n\"%s\"\nCite obtained:\n\"%s\"",
308                      expected_cite, cited_text);
309         g_free (expected_cite);
310         g_free (cited_text);
311
312         /* Test 2 */
313         text = g_strdup ("This is some text");
314         expected_cite = g_strdup ("On Thu Jan  1 01:00:00 1970, (null) wrote:\nThis is some text\n");
315         cited_text = modest_text_utils_cite (text, "text/plain", NULL, 0);
316         fail_unless (cited_text && !strcmp (expected_cite, cited_text),
317                      "modest_text_utils_cite failed:\nOriginal text:\n\"%s\"\n" \
318                      "Expected cite:\n\"%s\"\nCite obtained:\n\"%s\"",
319                      text, expected_cite, cited_text);
320         g_free (text);
321         g_free (expected_cite);
322         g_free (cited_text);
323 }
324 END_TEST
325
326 /* -------------------- inline tests -------------------- */
327
328 /**
329  * Test regular usage of modest_text_utils_inline
330  *  - Test 1: Check inline of an empty text
331  *  - Test 2: Check inline of a regular text
332  *  - Test 3: Check with non ASCII chars
333  *  TODO: add tests for HTML when implementation is finished
334  */
335 START_TEST (test_inline_regular)
336 {
337         gint i;
338         gchar *inlined_text = NULL;
339         const StringPair tests[] = {
340                 { "", 
341                   "-----Forwarded Message-----\n" \
342                   "From: foo@bar\n" \
343                   "Sent: Thu Jan  1 01:00:00 1970\n" \
344                   "To: bar@foo\n" \
345                   "Subject: Any subject\n\n" },
346                 { "Some text\nto inline", 
347                   "-----Forwarded Message-----\n" \
348                   "From: foo@bar\n" \
349                   "Sent: Thu Jan  1 01:00:00 1970\n" \
350                   "To: bar@foo\n" \
351                   "Subject: Any subject\n\n" \
352                   "Some text\nto inline" },
353                 { "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš", 
354                   "-----Forwarded Message-----\n" \
355                   "From: foo@bar\n" \
356                   "Sent: Thu Jan  1 01:00:00 1970\n" \
357                   "To: bar@foo\n" \
358                   "Subject: Any subject\n\n" \
359                   "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš" },
360         };
361
362         /* Tests 1, 2, 3 */
363         for (i = 0; i !=  sizeof(tests)/sizeof(StringPair); ++i) {
364                 inlined_text =  modest_text_utils_inline (tests[i].original, 
365                                                           "text/plain", 
366                                                           "foo@bar", 
367                                                           0, 
368                                                           "bar@foo", 
369                                                           "Any subject");
370                 fail_unless (inlined_text && !strcmp (tests[i].expected, inlined_text),
371                              "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \
372                              "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"",
373                              tests[i].original, tests[i].expected, inlined_text);               
374                 g_free (inlined_text);
375         }
376 }
377 END_TEST
378
379 /**
380  * Test invalid usage of modest_text_utils_inline
381  *  - Test 1: Check NULL text (should inline text as (null))
382  *  - Test 2: Check NULL content_type (should assume text/plain)
383  *  - Test 3: Check NULL from (should write "(null)")
384  *  - Test 4: Check NULL to (should write ("null"))
385  *  - Test 5: Check NULL subject (should write ("null"))
386  * TODO: repeat tests from HTML when implemented
387  */
388 START_TEST (test_inline_invalid)
389 {
390         gchar *text = NULL;
391         gchar *expected_inline = NULL;
392         gchar *inlined_text = NULL;
393
394         /* Test 1 */
395         expected_inline = g_strdup("-----Forwarded Message-----\n" \
396                                    "From: foo@bar\n" \
397                                    "Sent: Thu Jan  1 01:00:00 1970\n" \
398                                    "To: bar@foo\n" \
399                                    "Subject: Any subject\n\n" \
400                                    "(null)");
401         inlined_text =  modest_text_utils_inline (NULL, 
402                                                   "text/plain", 
403                                                   "foo@bar", 
404                                                   0, 
405                                                   "bar@foo", 
406                                                   "Any subject");
407         fail_unless (inlined_text == NULL, 
408                      "modest_text_utils_inline failed: it should return NULL");
409         g_free (inlined_text);
410
411         /* Test 2 (Fault) */
412         text = g_strdup ("Some text");
413         expected_inline = g_strdup("-----Forwarded Message-----\n" \
414                                    "From: foo@bar\n" \
415                                    "Sent: Thu Jan  1 01:00:00 1970\n" \
416                                    "To: bar@foo\n" \
417                                    "Subject: Any subject\n\n" \
418                                    "Some text");
419         inlined_text =  modest_text_utils_inline (text,
420                                                   NULL,
421                                                   "foo@bar",
422                                                   0,
423                                                   "bar@foo",
424                                                   "Any subject");
425         fail_unless (inlined_text == NULL, 
426                      "modest_text_utils_inline failed: it should return NULL");
427         g_free (inlined_text);
428
429         /* Test 3 */
430         text = g_strdup ("Some text");
431         expected_inline = g_strdup("-----Forwarded Message-----\n" \
432                                    "From: (null)\n" \
433                                    "Sent: Thu Jan  1 01:00:00 1970\n" \
434                                    "To: bar@foo\n" \
435                                    "Subject: Any subject\n\n" \
436                                    "Some text");
437         inlined_text =  modest_text_utils_inline (text, 
438                                                   "text/plain", 
439                                                   NULL, 
440                                                   0, 
441                                                   "bar@foo", 
442                                                   "Any subject");
443         fail_unless (inlined_text == NULL, 
444                      "modest_text_utils_inline failed: it should return NULL");
445
446         g_free (inlined_text);
447
448         /* Test 4 */
449         text = g_strdup ("Some text");
450         expected_inline = g_strdup("-----Forwarded Message-----\n" \
451                                    "From: foo@bar\n" \
452                                    "Sent: Thu Jan  1 01:00:00 1970\n" \
453                                    "To: (null)\n" \
454                                    "Subject: Any subject\n\n" \
455                                    "Some text");
456         inlined_text =  modest_text_utils_inline (text, 
457                                                   "text/plain", 
458                                                   "foo@bar", 
459                                                   0, 
460                                                   NULL, 
461                                                   "Any subject");
462         fail_unless (inlined_text == NULL, 
463                      "modest_text_utils_inline failed: it should return NULL");
464         g_free (inlined_text);
465         
466         /* Test 5 */
467         text = g_strdup ("Some text");
468         expected_inline = g_strdup("-----Forwarded Message-----\n" \
469                                    "From: foo@bar\n" \
470                                    "Sent: Thu Jan  1 01:00:00 1970\n" \
471                                    "To: bar@foo\n" \
472                                    "Subject: (null)\n\n" \
473                                    "Some text");
474         inlined_text =  modest_text_utils_inline (text, 
475                                                   "text/plain", 
476                                                   "foo@bar", 
477                                                   0, 
478                                                   "bar@foo", 
479                                                   NULL);
480         fail_unless (inlined_text == NULL, 
481                      "modest_text_utils_inline failed: it should return NULL");
482
483         g_free (inlined_text);
484 }
485 END_TEST
486
487 /* ---------------- remove address tests ---------------- */
488
489 /**
490  * Test regular usage of modest_text_utils_remove_address
491  *  - Test 1: Remove a single address in the middle of a list
492  *  - Test 2: Remove an extended address in the middle of a list
493  *  - Test 3: Remove an address that appears more than once in the list
494  *  - Test 4: Remove address from an empty list
495  *  - Test 5: Check with non ASCII characters
496  */
497 START_TEST (test_remove_address_regular)
498 {
499         gchar *list = NULL;
500         gchar *new_list = NULL;
501         gchar *address = NULL;
502
503         /* Test 1 */
504         list = g_strdup ("foo@bar <foo>, myname@mycompany.com (myname), " \
505                          "xxx@yyy.zzz (xxx yyy zzz), bar@foo");
506         address = g_strdup ("xxx@yyy.zzz");
507         new_list = modest_text_utils_remove_address (list, address);
508
509         fail_unless (new_list && strstr (new_list, "foo@bar <foo>") != NULL,
510                      "modest_text_utils_remove_address failed: "\
511                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
512                      address, list, new_list);
513         fail_unless (new_list && strstr (new_list, "myname@mycompany.com (myname)") != NULL,
514                      "modest_text_utils_remove_address failed: " \
515                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
516                      address, list, new_list);
517         fail_unless (new_list && strstr (new_list, "bar@foo") != NULL,
518                      "modest_text_utils_remove_address failed: " \
519                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
520                      address, list, new_list);
521         fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz") == NULL,
522                      "modest_text_utils_remove_address failed: " \
523                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
524                      address, list, new_list);
525         fail_unless (new_list && strstr (new_list, "(xxx yyy zzz)") == NULL,
526                      "modest_text_utils_remove_address failed: " \
527                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
528                      address, list, new_list);
529
530         g_free (list);
531         g_free (new_list);
532         g_free (address);
533
534         /* Test 2 */
535         list = g_strdup ("foo@bar <foo>, xxx@yyy.zzz (xxx yyy zzz), bar@foo");
536         address = g_strdup ("xxx@yyy.zzz (xxx yyy zzz)");
537         new_list = modest_text_utils_remove_address (list, address);
538
539         fail_unless (new_list && strstr (new_list, "foo@bar <foo>") != NULL,
540                      "modest_text_utils_remove_address failed: " \
541                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
542                      address, list, new_list);
543         fail_unless (new_list && strstr (new_list, "bar@foo") != NULL,
544                      "modest_text_utils_remove_address failed: " \
545                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
546                      address, list, new_list);
547         fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz") == NULL,
548                      "modest_text_utils_remove_address failed: " \
549                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
550                      address, list, new_list);
551         fail_unless (new_list && strstr (new_list, "(xxx yyy zzz)") == NULL,
552                      "modest_text_utils_remove_address failed: " \
553                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
554                      address, list, new_list);
555
556         g_free (list);
557         g_free (new_list);
558         g_free (address);
559
560         /* Test 3 */
561         list = g_strdup ("foo@bar <foo>, bar@foo (BAR), xxx@yyy.zzz (xxx yyy zzz), bar@foo");
562         address = g_strdup ("bar@foo");
563         new_list = modest_text_utils_remove_address (list, address);
564
565         fail_unless (new_list && strstr (new_list, "foo@bar <foo>") != NULL,
566                      "modest_text_utils_remove_address failed: " \
567                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
568                      address, list, new_list);
569         fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz (xxx yyy zzz)") != NULL,
570                      "modest_text_utils_remove_address failed: " \
571                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
572                      address, list, new_list);
573         fail_unless (new_list && strstr (new_list, "bar@foo") == NULL,
574                      "modest_text_utils_remove_address failed: " \
575                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
576                      address, list, new_list);
577         fail_unless (new_list && strstr (new_list, "(BAR)") == NULL,
578                      "modest_text_utils_remove_address failed: " \
579                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
580                      address, list, new_list);
581
582         g_free (list);
583         g_free (new_list);
584         g_free (address);
585
586         /* Test 4 */
587         list = g_strdup ("");
588         address = g_strdup ("bar@foo");
589         new_list = modest_text_utils_remove_address (list, address);
590
591         fail_unless (new_list && !strcmp (new_list, list),
592                      "modest_text_utils_remove_address failed: " \
593                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
594                      address, list, new_list);
595
596         g_free (list);
597         g_free (new_list);
598         g_free (address);
599
600         /* Test 5 */
601         list = g_strdup ("foo@bar, áñï@Èž.com (Àç ÑŸž), bar@foo");
602         address = g_strdup ("áñï@Èž.com");
603         new_list = modest_text_utils_remove_address (list, address);
604
605         fail_unless (new_list && strstr (new_list, "foo@bar") != NULL,
606                      "modest_text_utils_remove_address failed: " \
607                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
608                      address, list, new_list);
609         fail_unless (new_list && strstr (new_list, "bar@foo") != NULL,
610                      "modest_text_utils_remove_address failed: " \
611                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
612                      address, list, new_list);
613         fail_unless (new_list && strstr (new_list, "áñï@Èž.com") == NULL,
614                      "modest_text_utils_remove_address failed: " \
615                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
616                      address, list, new_list);
617         fail_unless (new_list && strstr (new_list, "(Àç ÑŸž)") == NULL,
618                      "modest_text_utils_remove_address failed: " \
619                      "Removing \"%s\" from address list \"%s\" returns \"%s\"",
620                      address, list, new_list);
621
622         g_free (list);
623         g_free (new_list);
624         g_free (address);
625 }
626 END_TEST
627
628 /**
629  * Test limits usage of modest_text_utils_remove_address
630  *  - Test 1: Remove address at the beginning of the list
631  *  - Test 2: Remove address at the end  of the list
632  *  - Test 3: Remove the address of a one-element-size list
633  */
634 START_TEST (test_remove_address_limits)
635 {
636         gchar *list = NULL;
637         gchar *new_list = NULL;
638         gchar *expected_list = NULL;
639         gchar *address = NULL;
640         gint i;
641
642         const StringPair tests[] = {
643                 { "foo@bar <FOO BAR>, bar@foo (BAR FOO)", "bar@foo (BAR FOO)" },
644                 { "foo@bar <FOO BAR>, bar@foo (BAR FOO)", "foo@bar <FOO BAR>" },
645                 { "foo@bar <FOO BAR>", "" },
646         };
647         const gchar *remove[] = { "foo@bar", "bar@foo", "foo@bar" };
648
649         /* Tests 1, 2, 3 */
650         for (i = 0; i !=  sizeof(tests)/sizeof(StringPair); ++i) {
651                 list = g_strdup (tests[i].original);
652                 expected_list = g_strdup (tests[i].expected);
653                 new_list = modest_text_utils_remove_address (list, remove[i]);
654                 fail_unless (new_list && !strcmp (expected_list, new_list),
655                              "modest_text_utils_remove_address failed: " \
656                              "Removing \"%s\" from address list \"%s\" returns \"%s\" instead of \"%s\"",
657                              remove[i], list, new_list, expected_list);
658                 g_free (list);
659                 g_free (expected_list);
660                 g_free (new_list);
661         }
662 }
663 END_TEST
664
665 /**
666  * Test invalid usage of modest_text_utils_remove_address
667  *  - Test 1: Remove a NULL address (should return the same list)
668  *  - Test 2: Remove an address form a NULL list (should return NULL)
669  */
670 START_TEST (test_remove_address_invalid)
671 {
672         gchar *list = NULL;
673         gchar *new_list = NULL;
674
675         /* Test 1 (Fault) */
676         list = g_strdup ("foo@bar, bar@foo");
677         new_list = modest_text_utils_remove_address (list, NULL);
678         fail_unless (new_list && !strcmp (list, new_list),
679                      "modest_text_utils_remove_address failed: " \
680                      "Removing a NULL address from \"%s\" returns \"%s\"",
681                      list, new_list);
682         g_free (list);
683         g_free (new_list);
684
685         /* Test 2 */
686         new_list = modest_text_utils_remove_address (NULL, "foo@bar");
687         fail_unless (new_list == NULL,
688                      "modest_text_utils_remove_address failed: " \
689                      "Removing an address from a NULL list does not return NULL");
690         g_free (new_list);
691 }
692 END_TEST
693
694 /* --------------- convert to html tests ---------------- */
695
696 /**
697  * Test regular usage of modest_text_utils_convert_to_html
698  *  - Test 1: Check conversion of a regular text
699  *  - Test 2: Check conversion of a regular text with links
700  *  - Test 3: Check conversion of a regular text with special html chars
701  */
702 START_TEST (test_convert_to_html_regular)
703 {
704         gchar *text_in_html;
705         gchar *html_start = NULL;
706         gchar *html_end = NULL;
707         gchar *html = NULL;
708         gint bytes;
709         const StringPair tests[] = {
710                 { "This is some text.", 
711                   "<tt>This is some text.</tt>" },
712                 { "http://xxx.yyy.zzz/myfile", 
713                   "<tt><a href=\"http://xxx.yyy.zzz/myfile\">http://xxx.yyy.zzz/myfile</a></tt>" },
714                 { "<a  &  b>\n\tx", 
715                   "<tt>&lt;a &nbsp;&quot;&nbsp;&nbsp;b&gt;<br>\n&nbsp; &nbsp;&nbsp;x</tt>" },
716         };
717
718         /* Tests 1, 2, 3 */
719         for (i = 0; i !=  sizeof(tests)/sizeof(StringPair); ++i) {
720                 html = modest_text_utils_convert_to_html (tests[i].original);
721                 fail_unless (html != NULL,
722                              "modest_text_utils_convert_to_html failed:" \
723                              "Original text:\n\"%s\"\nExpected html:\n\"%s\"\nObtained html:\nNULL",
724                              tests[i].original, tests[i].expected);
725                 html_start = strstr (html, "<tt>");
726                 html_end = strstr (html, "</body>");
727                 bytes = html_end - html_start;
728                 text_in_html = g_strndup (html_start, bytes);
729                 
730                 fail_unless (strstr (html, tests[i].expected) != NULL,
731                              "modest_text_utils_convert_to_html failed:" \
732                              "Original text:\n\"%s\"\nExpected html:\n\"%s\"\nObtained html:\n\"%s\"",
733                              tests[i].original, tests[i].expected, text_in_html);
734 //              g_free (html_start);
735 //              g_free (text_in_html);
736 //              g_free (html);
737         }
738 }
739 END_TEST
740
741 /**
742  * Test invalid usage of modest_text_utils_convert_to_html
743  *  - Test 1: Check with NULL data
744  */
745 START_TEST (test_convert_to_html_invalid)
746 {
747         gchar *html = NULL;
748
749         /* Test 1 */
750         html = modest_text_utils_convert_to_html (NULL);
751         fail_unless (html == NULL,
752                      "modest_text_utils_convert_to_html failed:" \
753                      "Does not return NULL when passed NULL data");
754 }
755 END_TEST
756
757
758 /* ------------------- Suite creation ------------------- */
759
760 static Suite*
761 text_utils_suite (void)
762 {
763         Suite *suite = suite_create ("ModestTextUtils");
764         TCase *tc = NULL;
765
766         /* Tests case for "display adress" */
767         tc = tcase_create ("display_adress");
768         tcase_add_test (tc, test_get_display_address_regular);
769         tcase_add_test (tc, test_get_display_address_invalid);
770         suite_add_tcase (suite, tc);
771
772         /* Test case for "derived subject" */
773         tc = tcase_create ("derived_subject");
774         tcase_add_test (tc, test_derived_subject_regular);
775         tcase_add_test (tc, test_derived_subject_invalid);
776         suite_add_tcase (suite, tc);
777
778         /* Test case for "quote" */
779         tc = tcase_create ("quote");
780         tcase_add_test (tc, test_quote_regular);
781         tcase_add_test (tc, test_quote_invalid);
782         suite_add_tcase (suite, tc);
783
784         /* Test case for "cite" */
785         tc = tcase_create ("cite");
786         tcase_add_test (tc, test_cite_regular);
787         tcase_add_test (tc, test_cite_invalid);
788         suite_add_tcase (suite, tc);
789
790         /* Test case for "inline" */
791         tc = tcase_create ("inline");
792         tcase_add_test (tc, test_inline_regular);
793         tcase_add_test (tc, test_inline_invalid);
794         suite_add_tcase (suite, tc);
795
796         /* Test case for "remove address" */
797         tc = tcase_create ("remove_address");
798         tcase_add_test (tc, test_remove_address_regular);
799         tcase_add_test (tc, test_remove_address_limits);
800         tcase_add_test (tc, test_remove_address_invalid);
801         suite_add_tcase (suite, tc);
802
803         /* Test case for "convert to html" */
804         tc = tcase_create ("convert_to_html");
805         tcase_add_test (tc, test_convert_to_html_regular);
806         tcase_add_test (tc, test_convert_to_html_invalid);
807         suite_add_tcase (suite, tc);
808
809         return suite;
810 }
811
812 /* --------------------- Main program ------------------- */
813
814 gint
815 main ()
816 {
817         SRunner *srunner;
818         Suite   *suite;
819         int     failures;
820
821         setenv ("TZ", "Europe/Paris", 1);
822         
823         suite   = text_utils_suite ();
824         srunner = srunner_create (suite);
825         
826         srunner_run_all (srunner, CK_ENV);
827         failures = srunner_ntests_failed (srunner);
828         srunner_free (srunner);
829         
830         return failures;
831 }