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.
32 #include <modest-text-utils.h>
35 const gchar *original;
36 const gchar *expected;
39 /* ----------------- display address tests -------------- */
42 * Test regular usage of modest_text_utils_display_address
43 * - Test 1: Check "<...>" deletion
44 * - Test 2: Check "(...)" deletion
45 * - Test 3: Check address left and right trim, also non ASCII chars
46 * - Test 4: Check issues in tests 1, 2 and 3 together
47 * - Test 5: Check with an empty address
49 START_TEST (test_display_address_regular)
52 const StringPair tests[] = {
53 { "John Doe <foo@bar>", "John Doe" },
54 { "Rupert Griffin (test)", "Rupert Griffin"},
55 { " Hyvää päivää ", "Hyvää päivää"},
56 { " John Doe <foo@bar> (test) ", "John Doe" },
60 /* Tests 1, 2, 3, 4 */
61 for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) {
62 gchar *str = g_strdup (tests[i].original);
63 str = modest_text_utils_get_display_address (str);
64 fail_unless (str && strcmp(str, tests[i].expected) == 0,
65 "modest_text_utils_display_address failed for '%s': "
66 "expected '%s' but got '%s'",
67 tests[i].original, tests[i].expected, str);
75 * Test invalid usage of modest_text_utils_display_address
76 * - Test 1: Check with NULL address (should return NULL)
78 START_TEST (test_display_address_invalid)
81 fail_unless (modest_text_utils_get_display_address (NULL) == NULL,
82 "modest_text_utils_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\n> lines\n> of text\n> \n" },
159 { "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš",
160 "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\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> \n");
218 quoted_text = modest_text_utils_quote (text, NULL, "foo@bar", 0, 15);
219 fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text),
220 "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \
221 "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"",
222 text, expected_quoted_text, quoted_text);
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 && !strcmp (expected_quoted_text, quoted_text),
232 "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \
233 "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"",
234 text, expected_quoted_text, quoted_text);
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 && !strcmp (expected_inline, inlined_text),
408 "modest_text_utils_inline failed:\nOriginal text:\nNULL\n" \
409 "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"",
410 expected_inline, inlined_text);
411 g_free (inlined_text);
414 text = g_strdup ("Some text");
415 expected_inline = g_strdup("-----Forwarded Message-----\n" \
417 "Sent: Thu Jan 1 01:00:00 1970\n" \
419 "Subject: Any subject\n\n" \
421 inlined_text = modest_text_utils_inline (text,
427 fail_unless (inlined_text && !strcmp (expected_inline, inlined_text),
428 "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \
429 "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"",
430 text, expected_inline, inlined_text);
431 g_free (inlined_text);
434 text = g_strdup ("Some text");
435 expected_inline = g_strdup("-----Forwarded Message-----\n" \
437 "Sent: Thu Jan 1 01:00:00 1970\n" \
439 "Subject: Any subject\n\n" \
441 inlined_text = modest_text_utils_inline (text,
447 fail_unless (inlined_text && !strcmp (expected_inline, inlined_text),
448 "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \
449 "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"",
450 text, expected_inline, inlined_text);
451 g_free (inlined_text);
454 text = g_strdup ("Some text");
455 expected_inline = g_strdup("-----Forwarded Message-----\n" \
457 "Sent: Thu Jan 1 01:00:00 1970\n" \
459 "Subject: Any subject\n\n" \
461 inlined_text = modest_text_utils_inline (text,
467 fail_unless (inlined_text && !strcmp (expected_inline, inlined_text),
468 "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \
469 "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"",
470 text, expected_inline, inlined_text);
471 g_free (inlined_text);
474 text = g_strdup ("Some text");
475 expected_inline = g_strdup("-----Forwarded Message-----\n" \
477 "Sent: Thu Jan 1 01:00:00 1970\n" \
479 "Subject: (null)\n\n" \
481 inlined_text = modest_text_utils_inline (text,
487 fail_unless (inlined_text && !strcmp (expected_inline, inlined_text),
488 "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \
489 "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"",
490 text, expected_inline, inlined_text);
491 g_free (inlined_text);
495 /* ---------------- remove address tests ---------------- */
498 * Test regular usage of modest_text_utils_remove_address
499 * - Test 1: Remove a single address in the middle of a list
500 * - Test 2: Remove an extended address in the middle of a list
501 * - Test 3: Remove an address that appears more than once in the list
502 * - Test 4: Remove address from an empty list
503 * - Test 5: Check with non ASCII characters
505 START_TEST (test_remove_address_regular)
508 gchar *new_list = NULL;
509 gchar *address = NULL;
512 list = g_strdup ("foo@bar <foo>, myname@mycompany.com (myname), " \
513 "xxx@yyy.zzz (xxx yyy zzz), bar@foo");
514 address = g_strdup ("xxx@yyy.zzz");
515 new_list = modest_text_utils_remove_address (list, address);
517 fail_unless (new_list && strstr (new_list, "foo@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, "myname@mycompany.com (myname)") != 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, "bar@foo") != NULL,
526 "modest_text_utils_remove_address failed: " \
527 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
528 address, list, new_list);
529 fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz") == NULL,
530 "modest_text_utils_remove_address failed: " \
531 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
532 address, list, new_list);
533 fail_unless (new_list && strstr (new_list, "(xxx yyy zzz)") == NULL,
534 "modest_text_utils_remove_address failed: " \
535 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
536 address, list, new_list);
543 list = g_strdup ("foo@bar <foo>, xxx@yyy.zzz (xxx yyy zzz), bar@foo");
544 address = g_strdup ("xxx@yyy.zzz (xxx yyy zzz)");
545 new_list = modest_text_utils_remove_address (list, address);
547 fail_unless (new_list && strstr (new_list, "foo@bar <foo>") != 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, "bar@foo") != NULL,
552 "modest_text_utils_remove_address failed: " \
553 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
554 address, list, new_list);
555 fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz") == NULL,
556 "modest_text_utils_remove_address failed: " \
557 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
558 address, list, new_list);
559 fail_unless (new_list && strstr (new_list, "(xxx yyy zzz)") == NULL,
560 "modest_text_utils_remove_address failed: " \
561 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
562 address, list, new_list);
569 list = g_strdup ("foo@bar <foo>, bar@foo (BAR), xxx@yyy.zzz (xxx yyy zzz), bar@foo");
570 address = g_strdup ("bar@foo");
571 new_list = modest_text_utils_remove_address (list, address);
573 fail_unless (new_list && strstr (new_list, "foo@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, "xxx@yyy.zzz (xxx yyy zzz)") != NULL,
578 "modest_text_utils_remove_address failed: " \
579 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
580 address, list, new_list);
581 fail_unless (new_list && strstr (new_list, "bar@foo") == NULL,
582 "modest_text_utils_remove_address failed: " \
583 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
584 address, list, new_list);
585 fail_unless (new_list && strstr (new_list, "(BAR)") == NULL,
586 "modest_text_utils_remove_address failed: " \
587 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
588 address, list, new_list);
595 list = g_strdup ("");
596 address = g_strdup ("bar@foo");
597 new_list = modest_text_utils_remove_address (list, address);
599 fail_unless (new_list && !strcmp (new_list, list),
600 "modest_text_utils_remove_address failed: " \
601 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
602 address, list, new_list);
609 list = g_strdup ("foo@bar, áñï@Èž.com (Àç ÑŸž), bar@foo");
610 address = g_strdup ("áñï@Èž.com");
611 new_list = modest_text_utils_remove_address (list, address);
613 fail_unless (new_list && strstr (new_list, "foo@bar") != 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, "bar@foo") != NULL,
618 "modest_text_utils_remove_address failed: " \
619 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
620 address, list, new_list);
621 fail_unless (new_list && strstr (new_list, "áñï@Èž.com") == NULL,
622 "modest_text_utils_remove_address failed: " \
623 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
624 address, list, new_list);
625 fail_unless (new_list && strstr (new_list, "(Àç ÑŸž)") == NULL,
626 "modest_text_utils_remove_address failed: " \
627 "Removing \"%s\" from address list \"%s\" returns \"%s\"",
628 address, list, new_list);
637 * Test limits usage of modest_text_utils_remove_address
638 * - Test 1: Remove address at the beginning of the list
639 * - Test 2: Remove address at the end of the list
640 * - Test 3: Remove the address of a one-element-size list
642 START_TEST (test_remove_address_limits)
645 gchar *new_list = NULL;
646 gchar *expected_list = NULL;
647 gchar *address = NULL;
650 const StringPair tests[] = {
651 { "foo@bar <FOO BAR>, bar@foo (BAR FOO)", "bar@foo (BAR FOO)" },
652 { "foo@bar <FOO BAR>, bar@foo (BAR FOO)", "foo@bar <FOO BAR>" },
653 { "foo@bar <FOO BAR>", "" },
655 const gchar *remove[] = { "foo@bar", "bar@foo", "foo@bar" };
658 for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) {
659 list = g_strdup (tests[i].original);
660 expected_list = g_strdup (tests[i].expected);
661 new_list = modest_text_utils_remove_address (list, remove[i]);
662 fail_unless (new_list && !strcmp (expected_list, new_list),
663 "modest_text_utils_remove_address failed: " \
664 "Removing \"%s\" from address list \"%s\" returns \"%s\" instead of \"%s\"",
665 remove[i], list, new_list, expected_list);
667 g_free (expected_list);
674 * Test invalid usage of modest_text_utils_remove_address
675 * - Test 1: Remove a NULL address (should return the same list)
676 * - Test 2: Remove an address form a NULL list (should return NULL)
678 START_TEST (test_remove_address_invalid)
681 gchar *new_list = NULL;
684 list = g_strdup ("foo@bar, bar@foo");
685 new_list = modest_text_utils_remove_address (list, NULL);
686 fail_unless (new_list && !strcmp (list, new_list),
687 "modest_text_utils_remove_address failed: " \
688 "Removing a NULL address from \"%s\" returns \"%s\"",
694 new_list = modest_text_utils_remove_address (NULL, "foo@bar");
695 fail_unless (new_list == NULL,
696 "modest_text_utils_remove_address failed: " \
697 "Removing an address from a NULL list does not return NULL");
702 /* --------------- convert to html tests ---------------- */
705 * Test regular usage of modest_text_utils_convert_to_html
706 * - Test 1: Check conversion of a regular text
707 * - Test 2: Check conversion of a regular text with links
708 * - Test 3: Check conversion of a regular text with special html chars
710 START_TEST (test_convert_to_html_regular)
712 gchar text_in_html[256];
713 gchar *html_start = NULL;
714 gchar *html_end = NULL;
717 const StringPair tests[] = {
718 { "This is some text.",
719 "<tt>This is some text.</tt>" },
720 { "http://xxx.yyy.zzz/myfile",
721 "<tt><a href=\"http://xxx.yyy.zzz/myfile\">http://xxx.yyy.zzz/myfile</a></tt>" },
723 "<tt><a " b><br>\n x</tt>" },
727 for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) {
728 html = modest_text_utils_convert_to_html (tests[i].original);
729 fail_unless (html != NULL,
730 "modest_text_utils_convert_to_html failed:" \
731 "Original text:\n\"%s\"\nExpected html:\n\"%s\"\nObtained html:\n\NULL",
732 tests[i].original, tests[i].expected);
733 html_start = strstr (html, "<tt>");
734 html_end = strstr (html, "</body>");
735 bytes = html_end - html_start;
736 memset (text_in_html, 0, 256);
737 memcpy (text_in_html, html_start, bytes);
738 fail_unless (strstr (html, tests[i].expected) != NULL,
739 "modest_text_utils_convert_to_html failed:" \
740 "Original text:\n\"%s\"\nExpected html:\n\"%s\"\nObtained html:\n\"%s\"",
741 tests[i].original, tests[i].expected, text_in_html);
750 * Test invalid usage of modest_text_utils_convert_to_html
751 * - Test 1: Check with NULL data
753 START_TEST (test_convert_to_html_invalid)
758 html = modest_text_utils_convert_to_html (NULL);
759 fail_unless (html == NULL,
760 "modest_text_utils_convert_to_html failed:" \
761 "Does not return NULL when passed NULL data");
766 /* ------------------- Suite creation ------------------- */
769 text_utils_suite (void)
771 Suite *suite = suite_create ("ModestTextUtils");
774 /* Tests case for "display adress" */
775 tc = tcase_create ("display_adress");
776 tcase_add_test (tc, test_display_address_regular);
777 tcase_add_test (tc, test_display_address_invalid);
778 suite_add_tcase (suite, tc);
780 /* Test case for "derived subject" */
781 tc = tcase_create ("derived_subject");
782 tcase_add_test (tc, test_derived_subject_regular);
783 tcase_add_test (tc, test_derived_subject_invalid);
784 suite_add_tcase (suite, tc);
786 /* Test case for "quote" */
787 tc = tcase_create ("quote");
788 tcase_add_test (tc, test_quote_regular);
789 tcase_add_test (tc, test_quote_invalid);
790 suite_add_tcase (suite, tc);
792 /* Test case for "cite" */
793 tc = tcase_create ("cite");
794 tcase_add_test (tc, test_cite_regular);
795 tcase_add_test (tc, test_cite_invalid);
796 suite_add_tcase (suite, tc);
798 /* Test case for "inline" */
799 tc = tcase_create ("inline");
800 tcase_add_test (tc, test_inline_regular);
801 tcase_add_test (tc, test_inline_invalid);
802 suite_add_tcase (suite, tc);
804 /* Test case for "remove address" */
805 tc = tcase_create ("remove_address");
806 tcase_add_test (tc, test_remove_address_regular);
807 tcase_add_test (tc, test_remove_address_limits);
808 tcase_add_test (tc, test_remove_address_invalid);
809 suite_add_tcase (suite, tc);
811 /* Test case for "convert to html" */
812 tc = tcase_create ("convert_to_html");
813 tcase_add_test (tc, test_convert_to_html_regular);
814 tcase_add_test (tc, test_convert_to_html_invalid);
815 suite_add_tcase (suite, tc);
820 /* --------------------- Main program ------------------- */
829 suite = text_utils_suite ();
830 srunner = srunner_create (suite);
832 srunner_run_all (srunner, CK_ENV);
833 failures = srunner_ntests_failed (srunner);
834 srunner_free (srunner);