[gmime] Simplified g_mime_text_part_new() and added unit tests



commit 19a48371f500ee7214b19c7379d8e16862d26871
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Sun Dec 17 09:52:40 2017 -0500

    Simplified g_mime_text_part_new() and added unit tests

 gmime/gmime-text-part.c               |   11 +------
 tests/data/mime-part/french-fable.txt |   18 +++++++++++
 tests/test-mime-part.c                |   54 +++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 10 deletions(-)
---
diff --git a/gmime/gmime-text-part.c b/gmime/gmime-text-part.c
index ea72530..d23b0be 100644
--- a/gmime/gmime-text-part.c
+++ b/gmime/gmime-text-part.c
@@ -113,16 +113,7 @@ g_mime_text_part_finalize (GObject *object)
 GMimeTextPart *
 g_mime_text_part_new (void)
 {
-       GMimeContentType *content_type;
-       GMimeTextPart *mime_part;
-       
-       mime_part = g_object_new (GMIME_TYPE_TEXT_PART, NULL);
-       
-       content_type = g_mime_content_type_new ("text", "plain");
-       g_mime_object_set_content_type ((GMimeObject *) mime_part, content_type);
-       g_object_unref (content_type);
-       
-       return mime_part;
+       return g_mime_text_part_new_with_subtype ("plain");
 }
 
 
diff --git a/tests/data/mime-part/french-fable.txt b/tests/data/mime-part/french-fable.txt
new file mode 100644
index 0000000..571f209
--- /dev/null
+++ b/tests/data/mime-part/french-fable.txt
@@ -0,0 +1,18 @@
+Maître Corbeau, sur un arbre perché,
+Tenait en son bec un fromage.
+Maître Renard, par l'odeur alléché,
+Lui tint à peu près ce langage :
+« Hé ! bonjour, Monsieur du Corbeau.
+Que vous êtes joli ! Que vous me semblez beau !
+Sans mentir, si votre ramage
+Se rapporte à votre plumage,
+Vous êtes le Phénix des hôtes de ces bois. »
+A ces mots le Corbeau ne se sent pas de joie ;
+Et pour montrer sa belle voix,
+Il ouvre un large bec, laisse tomber sa proie.
+Le Renard s'en saisit, et dit : « Mon bon Monsieur,
+Apprenez que tout flatteur
+Vit aux dépens de celui qui l'écoute :
+Cette leçon vaut bien un fromage, sans doute. »
+Le Corbeau, honteux et confus,
+Jura, mais un peu tard, qu'on ne l'y prendrait plus.
diff --git a/tests/test-mime-part.c b/tests/test-mime-part.c
index eb5a6ab..f904306 100644
--- a/tests/test-mime-part.c
+++ b/tests/test-mime-part.c
@@ -508,6 +508,58 @@ error:
        g_object_unref (mime_part);
 }
 
+static void
+test_text_part (const char *datadir, const char *filename, const char *expected_charset)
+{
+       const char *what = "GMimeTextPart";
+       GByteArray *expected;
+       GMimeTextPart *part;
+       const char *charset;
+       char *text, *path;
+       
+       testsuite_check ("%s (%s)", what, filename);
+       
+       path = g_build_filename (datadir, filename, NULL);
+       expected = read_all_bytes (path, TRUE);
+       g_byte_array_append (expected, (guint8 *) "", 1);
+       g_free (path);
+       
+       part = g_mime_text_part_new ();
+       g_mime_text_part_set_text (part, (const char *) expected->data);
+       
+       charset = g_mime_text_part_get_charset (part);
+       if (charset == NULL) {
+               testsuite_check_failed ("%s failed: charset is NULL", what);
+               goto cleanup;
+       }
+       
+       if (strcmp (charset, expected_charset) != 0) {
+               testsuite_check_failed ("%s failed: charsets do not match: expected=%s; actual=%s",
+                                       what, expected_charset, charset);
+               goto cleanup;
+       }
+       
+       text = g_mime_text_part_get_text (part);
+       if (text == NULL) {
+               testsuite_check_failed ("%s failed: text is NULL", what);
+               goto cleanup;
+       }
+       
+       if (strcmp (text, (const char *) expected->data) != 0) {
+               testsuite_check_failed ("%s failed: text does not match: expected=%s; actual=%s",
+                                       what, (const char *) expected->data, text);
+               g_free (text);
+               goto cleanup;
+       }
+       
+       testsuite_check_passed ();
+       
+cleanup:
+       
+       g_byte_array_free (expected, TRUE);
+       g_object_unref (part);
+}
+
 int main (int argc, char **argv)
 {
        const char *datadir = "data/mime-part";
@@ -543,6 +595,8 @@ int main (int argc, char **argv)
        test_openpgp_data (datadir, "pubkey-body.txt", GMIME_OPENPGP_DATA_PUBLIC_KEY);
        test_openpgp_data (datadir, "privkey-body.txt", GMIME_OPENPGP_DATA_PRIVATE_KEY);
        
+       test_text_part (datadir, "french-fable.txt", "iso-8859-1");
+       
        testsuite_end ();
        
        g_mime_shutdown ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]