1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
33 #include <modest-text-utils.h>
36 const gchar *original;
37 const gchar *expected;
40 /* ----------------- display address tests -------------- */
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
50 START_TEST (test_display_address_regular)
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" },
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);
76 * Test invalid usage of modest_text_utils_get_display_address
77 * - Test 1: Check with NULL address (should return NULL)
79 START_TEST (test_display_address_invalid)
82 fail_unless (modest_text_utils_get_display_address (NULL) == NULL,
83 "modest_text_utils_get_display_address(NULL) should be NULL");
87 /* ----------------- derived address tests -------------- */
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
96 START_TEST (test_derived_subject_regular)
99 const gchar *prefix="Re:";
100 const StringPair tests[] = {
101 { "subject", "Re: subject" },
103 { "Hyvää päivää", "Re: Hyvää päivää"},
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);
122 * Test invalid usage of modest_text_utils_derived_subject
123 * - Test 1: Check with NULL prefix (should return NULL)
125 START_TEST (test_derived_subject_invalid)
128 fail_unless (modest_text_utils_derived_subject (NULL, NULL) == NULL,
129 "modest_text_utils_derived_subject (*,NULL) should be NULL");
133 /* --------------------- quote tests -------------------- */
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
144 START_TEST (test_quote_regular)
148 gchar *expected_quoted_text = NULL;
149 gchar *quoted_text = NULL;
152 const StringPair tests[] = {
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" },
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);
172 g_free (expected_quoted_text);
173 g_free (quoted_text);
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);
186 g_free (expected_quoted_text);
187 g_free (quoted_text);
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
199 START_TEST (test_quote_invalid)
202 gchar *expected_quoted_text = NULL;
203 gchar *quoted_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\"",
212 g_free (quoted_text);
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\"",
224 g_free (expected_quoted_text);
225 g_free (quoted_text);
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\"",
236 g_free (expected_quoted_text);
237 g_free (quoted_text);
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);
248 g_free (expected_quoted_text);
249 g_free (quoted_text);
253 /* ---------------------- cite tests -------------------- */
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
262 START_TEST (test_cite_regular)
265 gchar *cited_text = NULL;
266 const StringPair tests[] = {
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" },
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);
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
295 START_TEST (test_cite_invalid)
298 gchar *cited_text = NULL;
299 gchar *expected_cite = 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);
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);
321 g_free (expected_cite);
326 /* -------------------- inline tests -------------------- */
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
335 START_TEST (test_inline_regular)
338 gchar *inlined_text = NULL;
339 const StringPair tests[] = {
341 "-----Forwarded Message-----\n" \
343 "Sent: Thu Jan 1 01:00:00 1970\n" \
345 "Subject: Any subject\n\n" },
346 { "Some text\nto inline",
347 "-----Forwarded Message-----\n" \
349 "Sent: Thu Jan 1 01:00:00 1970\n" \
351 "Subject: Any subject\n\n" \
352 "Some text\nto inline" },
353 { "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš",
354 "-----Forwarded Message-----\n" \
356 "Sent: Thu Jan 1 01:00:00 1970\n" \
358 "Subject: Any subject\n\n" \
359 "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš" },
363 for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) {
364 inlined_text = modest_text_utils_inline (tests[i].original,
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);
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
388 START_TEST (test_inline_invalid)
391 gchar *expected_inline = NULL;
392 gchar *inlined_text = NULL;
395 expected_inline = g_strdup("-----Forwarded Message-----\n" \
397 "Sent: Thu Jan 1 01:00:00 1970\n" \
399 "Subject: Any subject\n\n" \
401 inlined_text = modest_text_utils_inline (NULL,
407 fail_unless (inlined_text == NULL,
408 "modest_text_utils_inline failed: it should return NULL");
409 g_free (inlined_text);
412 text = g_strdup ("Some text");
413 expected_inline = g_strdup("-----Forwarded Message-----\n" \
415 "Sent: Thu Jan 1 01:00:00 1970\n" \
417 "Subject: Any subject\n\n" \
419 inlined_text = modest_text_utils_inline (text,
425 fail_unless (inlined_text == NULL,
426 "modest_text_utils_inline failed: it should return NULL");
427 g_free (inlined_text);
430 text = g_strdup ("Some text");
431 expected_inline = g_strdup("-----Forwarded Message-----\n" \
433 "Sent: Thu Jan 1 01:00:00 1970\n" \
435 "Subject: Any subject\n\n" \
437 inlined_text = modest_text_utils_inline (text,
443 fail_unless (inlined_text == NULL,
444 "modest_text_utils_inline failed: it should return NULL");
446 g_free (inlined_text);
449 text = g_strdup ("Some text");
450 expected_inline = g_strdup("-----Forwarded Message-----\n" \
452 "Sent: Thu Jan 1 01:00:00 1970\n" \
454 "Subject: Any subject\n\n" \
456 inlined_text = modest_text_utils_inline (text,
462 fail_unless (inlined_text == NULL,
463 "modest_text_utils_inline failed: it should return NULL");
464 g_free (inlined_text);
467 text = g_strdup ("Some text");
468 expected_inline = g_strdup("-----Forwarded Message-----\n" \
470 "Sent: Thu Jan 1 01:00:00 1970\n" \
472 "Subject: (null)\n\n" \
474 inlined_text = modest_text_utils_inline (text,
480 fail_unless (inlined_text == NULL,
481 "modest_text_utils_inline failed: it should return NULL");
483 g_free (inlined_text);
487 /* ---------------- remove address tests ---------------- */
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
497 START_TEST (test_remove_address_regular)
500 gchar *new_list = NULL;
501 gchar *address = NULL;
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);
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);
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);
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);
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);
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);
587 list = g_strdup ("");
588 address = g_strdup ("bar@foo");
589 new_list = modest_text_utils_remove_address (list, address);
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);
601 list = g_strdup ("foo@bar, áñï@Èž.com (Àç ÑŸž), bar@foo");
602 address = g_strdup ("áñï@Èž.com");
603 new_list = modest_text_utils_remove_address (list, address);
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);
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
634 START_TEST (test_remove_address_limits)
637 gchar *new_list = NULL;
638 gchar *expected_list = NULL;
639 gchar *address = NULL;
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>", "" },
647 const gchar *remove[] = { "foo@bar", "bar@foo", "foo@bar" };
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);
659 g_free (expected_list);
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)
670 START_TEST (test_remove_address_invalid)
673 gchar *new_list = NULL;
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\"",
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");
694 /* --------------- convert to html tests ---------------- */
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
702 START_TEST (test_convert_to_html_regular)
705 gchar *html_start = NULL;
706 gchar *html_end = NULL;
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>" },
715 "<tt><a " b><br>\n x</tt>" },
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);
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);
742 * Test invalid usage of modest_text_utils_convert_to_html
743 * - Test 1: Check with NULL data
745 START_TEST (test_convert_to_html_invalid)
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");
758 /* ------------------- Suite creation ------------------- */
761 text_utils_suite (void)
763 Suite *suite = suite_create ("ModestTextUtils");
766 /* Tests case for "display adress" */
767 tc = tcase_create ("display_adress");
768 tcase_add_test (tc, test_display_address_regular);
769 tcase_add_test (tc, test_display_address_invalid);
770 suite_add_tcase (suite, tc);
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);
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);
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);
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);
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);
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);
812 /* --------------------- Main program ------------------- */
821 setenv ("TZ", "Europe/Paris", 1);
823 suite = text_utils_suite ();
824 srunner = srunner_create (suite);
826 srunner_run_all (srunner, CK_ENV);
827 failures = srunner_ntests_failed (srunner);
828 srunner_free (srunner);