Geary.Mime.ContentType: Rename ::deserialise to ::parse

Add unit tests.
This commit is contained in:
Michael Gratton 2019-11-23 10:38:40 +11:00 committed by Michael James Gratton
parent f77bb50b5c
commit 6d5f63692b
5 changed files with 32 additions and 7 deletions

View file

@ -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,

View file

@ -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
);
}

View file

@ -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);

View file

@ -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);

View file

@ -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',