From 6d5f63692b0192b595d7c0068bda331c65fb786b Mon Sep 17 00:00:00 2001 From: Michael Gratton Date: Sat, 23 Nov 2019 10:38:40 +1100 Subject: [PATCH] Geary.Mime.ContentType: Rename ::deserialise to ::parse Add unit tests. --- src/engine/imap-db/imap-db-attachment.vala | 2 +- src/engine/mime/mime-content-type.vala | 12 +++++++---- test/engine/api/geary-attachment-test.vala | 2 +- .../{ => mime}/mime-content-type-test.vala | 21 +++++++++++++++++++ test/meson.build | 2 +- 5 files changed, 32 insertions(+), 7 deletions(-) rename test/engine/{ => mime}/mime-content-type-test.vala (78%) diff --git a/src/engine/imap-db/imap-db-attachment.vala b/src/engine/imap-db/imap-db-attachment.vala index 4db81717..d8e8f9db 100644 --- a/src/engine/imap-db/imap-db-attachment.vala +++ b/src/engine/imap-db/imap-db-attachment.vala @@ -71,7 +71,7 @@ private class Geary.ImapDB.Attachment : Geary.Attachment { this( result.rowid_for("message_id"), - Mime.ContentType.deserialize(result.nonnull_string_for("mime_type")), + Mime.ContentType.parse(result.nonnull_string_for("mime_type")), result.string_for("content_id"), result.string_for("description"), disposition, diff --git a/src/engine/mime/mime-content-type.vala b/src/engine/mime/mime-content-type.vala index 9fe333d8..41bd4b2b 100644 --- a/src/engine/mime/mime-content-type.vala +++ b/src/engine/mime/mime-content-type.vala @@ -65,9 +65,9 @@ public class Geary.Mime.ContentType : Geary.BaseObject { TYPES_TO_EXTENSIONS["image/x-bmp"] = ".bmp"; } - public static ContentType deserialize(string str) throws MimeError { - // perform a little sanity checking here, as it doesn't appear the GMime constructor has - // any error-reporting at all + public static ContentType parse(string str) throws MimeError { + // perform a little sanity checking here, as it doesn't appear + // the GMime constructor has any error-reporting at all if (String.is_empty(str)) throw new MimeError.PARSE("Empty MIME Content-Type"); @@ -113,7 +113,11 @@ public class Geary.Mime.ContentType : Geary.BaseObject { mime_type = GLib.ContentType.get_mime_type(glib_type); } - return !Geary.String.is_empty(mime_type) ? deserialize(mime_type) : null; + return ( + !Geary.String.is_empty_or_whitespace(mime_type) + ? ContentType.parse(mime_type) + : null + ); } diff --git a/test/engine/api/geary-attachment-test.vala b/test/engine/api/geary-attachment-test.vala index 6db441cf..f101c683 100644 --- a/test/engine/api/geary-attachment-test.vala +++ b/test/engine/api/geary-attachment-test.vala @@ -61,7 +61,7 @@ class Geary.AttachmentTest : TestCase { public override void set_up() { try { - this.content_type = Mime.ContentType.deserialize(CONTENT_TYPE); + this.content_type = Mime.ContentType.parse(CONTENT_TYPE); this.default_type = Mime.ContentType.ATTACHMENT_DEFAULT; this.content_disposition = new Mime.ContentDisposition("attachment", null); diff --git a/test/engine/mime-content-type-test.vala b/test/engine/mime/mime-content-type-test.vala similarity index 78% rename from test/engine/mime-content-type-test.vala rename to test/engine/mime/mime-content-type-test.vala index 4b766000..5fc1160e 100644 --- a/test/engine/mime-content-type-test.vala +++ b/test/engine/mime/mime-content-type-test.vala @@ -10,6 +10,7 @@ class Geary.Mime.ContentTypeTest : TestCase { public ContentTypeTest() { base("Geary.Mime.ContentTypeTest"); add_test("static_defaults", static_defaults); + add_test("parse", parse); add_test("get_file_name_extension", get_file_name_extension); add_test("guess_type_from_name", guess_type_from_name); add_test("guess_type_from_buf", guess_type_from_buf); @@ -26,6 +27,26 @@ class Geary.Mime.ContentTypeTest : TestCase { ); } + public void parse() throws GLib.Error { + var test_article = ContentType.parse("text/plain"); + assert_string("text", test_article.media_type); + assert_string("plain", test_article.media_subtype); + + try { + ContentType.parse(""); + assert_not_reached(); + } catch (MimeError.PARSE error) { + // All good + } + + try { + ContentType.parse("textplain"); + assert_not_reached(); + } catch (MimeError.PARSE error) { + // All good + } + } + public void get_file_name_extension() throws Error { assert(new ContentType("image", "jpeg", null).get_file_name_extension() == ".jpeg"); assert(new ContentType("test", "unknown", null).get_file_name_extension() == null); diff --git a/test/meson.build b/test/meson.build index 9cd4717f..7de5e857 100644 --- a/test/meson.build +++ b/test/meson.build @@ -52,7 +52,7 @@ geary_test_engine_sources = [ 'engine/imap-db/imap-db-folder-test.vala', 'engine/imap-engine/account-processor-test.vala', 'engine/imap-engine/imap-engine-generic-account-test.vala', - 'engine/mime-content-type-test.vala', + 'engine/mime/mime-content-type-test.vala', 'engine/outbox/outbox-email-identifier-test.vala', 'engine/rfc822-mailbox-address-test.vala', 'engine/rfc822-mailbox-addresses-test.vala',