+ /* Test 3 */
+ text = g_strdup ("Some text");
+ expected_inline = g_strdup("-----Forwarded Message-----\n" \
+ "From: (null)\n" \
+ "Sent: Thu Jan 1 01:00:00 1970\n" \
+ "To: bar@foo\n" \
+ "Subject: Any subject\n\n" \
+ "Some text");
+ inlined_text = modest_text_utils_inline (text,
+ "text/plain",
+ NULL,
+ 0,
+ "bar@foo",
+ "Any subject");
+ fail_unless (inlined_text == NULL,
+ "modest_text_utils_inline failed: it should return NULL");
+
+ g_free (inlined_text);
+
+ /* Test 4 */
+ text = g_strdup ("Some text");
+ expected_inline = g_strdup("-----Forwarded Message-----\n" \
+ "From: foo@bar\n" \
+ "Sent: Thu Jan 1 01:00:00 1970\n" \
+ "To: (null)\n" \
+ "Subject: Any subject\n\n" \
+ "Some text");
+ inlined_text = modest_text_utils_inline (text,
+ "text/plain",
+ "foo@bar",
+ 0,
+ NULL,
+ "Any subject");
+ fail_unless (inlined_text == NULL,
+ "modest_text_utils_inline failed: it should return NULL");
+ g_free (inlined_text);
+
+ /* Test 5 */
+ text = g_strdup ("Some text");
+ expected_inline = g_strdup("-----Forwarded Message-----\n" \
+ "From: foo@bar\n" \
+ "Sent: Thu Jan 1 01:00:00 1970\n" \
+ "To: bar@foo\n" \
+ "Subject: (null)\n\n" \
+ "Some text");
+ inlined_text = modest_text_utils_inline (text,
+ "text/plain",
+ "foo@bar",
+ 0,
+ "bar@foo",
+ NULL);
+ fail_unless (inlined_text == NULL,
+ "modest_text_utils_inline failed: it should return NULL");
+
+ g_free (inlined_text);
+}
+END_TEST
+
+/* ---------------- remove address tests ---------------- */
+
+/**
+ * Test regular usage of modest_text_utils_remove_address
+ * - Test 1: Remove a single address in the middle of a list
+ * - Test 2: Remove an extended address in the middle of a list
+ * - Test 3: Remove an address that appears more than once in the list
+ * - Test 4: Remove address from an empty list
+ * - Test 5: Check with non ASCII characters
+ */
+START_TEST (test_remove_address_regular)
+{
+ gchar *list = NULL;
+ gchar *new_list = NULL;
+ gchar *address = NULL;
+
+ /* Test 1 */
+ list = g_strdup ("foo@bar <foo>, myname@mycompany.com (myname), " \
+ "xxx@yyy.zzz (xxx yyy zzz), bar@foo");
+ address = g_strdup ("xxx@yyy.zzz");
+ new_list = modest_text_utils_remove_address (list, address);
+
+ fail_unless (new_list && strstr (new_list, "foo@bar <foo>") != NULL,
+ "modest_text_utils_remove_address failed: "\
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "myname@mycompany.com (myname)") != NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "bar@foo") != NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz") == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "(xxx yyy zzz)") == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+
+ g_free (list);
+ g_free (new_list);
+ g_free (address);
+
+ /* Test 2 */
+ list = g_strdup ("foo@bar <foo>, xxx@yyy.zzz (xxx yyy zzz), bar@foo");
+ address = g_strdup ("xxx@yyy.zzz (xxx yyy zzz)");
+ new_list = modest_text_utils_remove_address (list, address);
+
+ fail_unless (new_list && strstr (new_list, "foo@bar <foo>") != NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "bar@foo") != NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz") == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "(xxx yyy zzz)") == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+
+ g_free (list);
+ g_free (new_list);
+ g_free (address);
+
+ /* Test 3 */
+ list = g_strdup ("foo@bar <foo>, bar@foo (BAR), xxx@yyy.zzz (xxx yyy zzz), bar@foo");
+ address = g_strdup ("bar@foo");
+ new_list = modest_text_utils_remove_address (list, address);
+
+ fail_unless (new_list && strstr (new_list, "foo@bar <foo>") != NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz (xxx yyy zzz)") != NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "bar@foo") == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "(BAR)") == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+
+ g_free (list);
+ g_free (new_list);
+ g_free (address);
+
+ /* Test 4 */
+ list = g_strdup ("");
+ address = g_strdup ("bar@foo");
+ new_list = modest_text_utils_remove_address (list, address);
+
+ fail_unless (new_list && !strcmp (new_list, list),
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+
+ g_free (list);
+ g_free (new_list);
+ g_free (address);
+
+ /* Test 5 */
+ list = g_strdup ("foo@bar, áñï@Èž.com (Àç ÑŸž), bar@foo");
+ address = g_strdup ("áñï@Èž.com");
+ new_list = modest_text_utils_remove_address (list, address);
+
+ fail_unless (new_list && strstr (new_list, "foo@bar") != NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "bar@foo") != NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "áñï@Èž.com") == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+ fail_unless (new_list && strstr (new_list, "(Àç ÑŸž)") == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\"",
+ address, list, new_list);
+
+ g_free (list);
+ g_free (new_list);
+ g_free (address);
+}
+END_TEST
+
+/**
+ * Test limits usage of modest_text_utils_remove_address
+ * - Test 1: Remove address at the beginning of the list
+ * - Test 2: Remove address at the end of the list
+ * - Test 3: Remove the address of a one-element-size list
+ */
+START_TEST (test_remove_address_limits)
+{
+ gchar *list = NULL;
+ gchar *new_list = NULL;
+ gchar *expected_list = NULL;
+ gchar *address = NULL;
+ gint i;
+
+ const StringPair tests[] = {
+ { "foo@bar <FOO BAR>, bar@foo (BAR FOO)", "bar@foo (BAR FOO)" },
+ { "foo@bar <FOO BAR>, bar@foo (BAR FOO)", "foo@bar <FOO BAR>" },
+ { "foo@bar <FOO BAR>", "" },
+ };
+ const gchar *remove[] = { "foo@bar", "bar@foo", "foo@bar" };
+
+ /* Tests 1, 2, 3 */
+ for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) {
+ list = g_strdup (tests[i].original);
+ expected_list = g_strdup (tests[i].expected);
+ new_list = modest_text_utils_remove_address (list, remove[i]);
+ fail_unless (new_list && !strcmp (expected_list, new_list),
+ "modest_text_utils_remove_address failed: " \
+ "Removing \"%s\" from address list \"%s\" returns \"%s\" instead of \"%s\"",
+ remove[i], list, new_list, expected_list);
+ g_free (list);
+ g_free (expected_list);
+ g_free (new_list);
+ }
+}
+END_TEST
+
+/**
+ * Test invalid usage of modest_text_utils_remove_address
+ * - Test 1: Remove a NULL address (should return the same list)
+ * - Test 2: Remove an address form a NULL list (should return NULL)
+ */
+START_TEST (test_remove_address_invalid)
+{
+ gchar *list = NULL;
+ gchar *new_list = NULL;
+
+ /* Test 1 (Fault) */
+ list = g_strdup ("foo@bar, bar@foo");
+ new_list = modest_text_utils_remove_address (list, NULL);
+ fail_unless (new_list && !strcmp (list, new_list),
+ "modest_text_utils_remove_address failed: " \
+ "Removing a NULL address from \"%s\" returns \"%s\"",
+ list, new_list);
+ g_free (list);
+ g_free (new_list);
+
+ /* Test 2 */
+ new_list = modest_text_utils_remove_address (NULL, "foo@bar");
+ fail_unless (new_list == NULL,
+ "modest_text_utils_remove_address failed: " \
+ "Removing an address from a NULL list does not return NULL");
+ g_free (new_list);
+}
+END_TEST
+
+/* --------------- convert to html tests ---------------- */
+
+/**
+ * Test regular usage of modest_text_utils_convert_to_html
+ * - Test 1: Check conversion of a regular text
+ * - Test 2: Check conversion of a regular text with links
+ * - Test 3: Check conversion of a regular text with special html chars
+ */
+START_TEST (test_convert_to_html_regular)
+{
+ gchar *text_in_html;
+ gchar *html_start = NULL;
+ gchar *html_end = NULL;
+ gchar *html = NULL;
+ gint bytes;
+ const StringPair tests[] = {
+ { "This is some text.",
+ "<tt>This is some text.</tt>" },
+ { "http://xxx.yyy.zzz/myfile",
+ "<tt><a href=\"http://xxx.yyy.zzz/myfile\">http://xxx.yyy.zzz/myfile</a></tt>" },
+ { "<a & b>\n\tx",
+ "<tt><a " b><br>\n x</tt>" },
+ };
+
+ /* Tests 1, 2, 3 */
+ for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) {
+ html = modest_text_utils_convert_to_html (tests[i].original);
+ fail_unless (html != NULL,
+ "modest_text_utils_convert_to_html failed:" \
+ "Original text:\n\"%s\"\nExpected html:\n\"%s\"\nObtained html:\nNULL",
+ tests[i].original, tests[i].expected);
+ html_start = strstr (html, "<tt>");
+ html_end = strstr (html, "</body>");
+ bytes = html_end - html_start;
+ text_in_html = g_strndup (html_start, bytes);
+
+ fail_unless (strstr (html, tests[i].expected) != NULL,
+ "modest_text_utils_convert_to_html failed:" \
+ "Original text:\n\"%s\"\nExpected html:\n\"%s\"\nObtained html:\n\"%s\"",
+ tests[i].original, tests[i].expected, text_in_html);
+// g_free (html_start);
+// g_free (text_in_html);
+// g_free (html);
+ }
+}
+END_TEST
+
+/**
+ * Test invalid usage of modest_text_utils_convert_to_html
+ * - Test 1: Check with NULL data
+ */
+START_TEST (test_convert_to_html_invalid)
+{
+ gchar *html = NULL;
+
+ /* Test 1 */
+ html = modest_text_utils_convert_to_html (NULL);
+ fail_unless (html == NULL,
+ "modest_text_utils_convert_to_html failed:" \
+ "Does not return NULL when passed NULL data");
+}
+END_TEST
+
+
+/* ------------------- Suite creation ------------------- */