[geary/wip/768422-namespace-support] Add unit tests for NAMESPACE parsing, CREATE serialisation, fix an error.



commit 21421f8d4830b535a06c69eaa8264c90edb97aea
Author: Michael James Gratton <mike vee net>
Date:   Fri Nov 3 16:52:21 2017 +1100

    Add unit tests for NAMESPACE parsing, CREATE serialisation, fix an error.

 .../imap/response/imap-namespace-response.vala     |   11 +-
 test/CMakeLists.txt                                |    2 +
 .../imap/command/imap-create-command-test.vala     |   29 ++++
 .../response/imap-namespace-response-test.vala     |  138 ++++++++++++++++++++
 test/main.vala                                     |    2 +
 5 files changed, 178 insertions(+), 4 deletions(-)
---
diff --git a/src/engine/imap/response/imap-namespace-response.vala 
b/src/engine/imap/response/imap-namespace-response.vala
index c6abb9d..2520b82 100644
--- a/src/engine/imap/response/imap-namespace-response.vala
+++ b/src/engine/imap/response/imap-namespace-response.vala
@@ -34,7 +34,7 @@ public class Geary.Imap.NamespaceResponse : BaseObject {
 
         if (server_data.size <= 2) {
             throw new ImapError.PARSE_ERROR(
-                "No NAMESPACEs privided: %s", server_data.to_string()
+                "No NAMESPACEs provided: %s", server_data.to_string()
             );
         }
 
@@ -56,9 +56,12 @@ public class Geary.Imap.NamespaceResponse : BaseObject {
     }
 
     private static Namespace[]? parse_namespaces(ListParameter? list) throws ImapError {
-        Namespace[] nss = new Namespace[list.size];
-        for (int i = 0; i < list.size; i++) {
-            nss[i] = parse_namespace(list.get_as_list(i));
+        Namespace[]? nss = null;
+        if (list != null) {
+            nss = new Namespace[list.size];
+            for (int i = 0; i < list.size; i++) {
+                nss[i] = parse_namespace(list.get_as_list(i));
+            }
         }
         return nss;
     }
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 065435e..0cb9e7e 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -8,6 +8,8 @@ set(TEST_SRC
 
   engine/api/geary-attachment-test.vala
   engine/api/geary-engine-test.vala
+  engine/imap/command/imap-create-command-test.vala
+  engine/imap/response/imap-namespace-response-test.vala
   engine/mime-content-type-test.vala
   engine/rfc822-mailbox-address-test.vala
   engine/rfc822-message-test.vala
diff --git a/test/engine/imap/command/imap-create-command-test.vala 
b/test/engine/imap/command/imap-create-command-test.vala
new file mode 100644
index 0000000..bcb5af9
--- /dev/null
+++ b/test/engine/imap/command/imap-create-command-test.vala
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2017 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+class Geary.Imap.CreateCommandTest : Gee.TestCase {
+
+
+    public CreateCommandTest() {
+        base("Geary.Imap.CreateCommandTest");
+        add_test("test_basic_create", test_basic_create);
+        add_test("test_special_use", test_special_use);
+    }
+
+    public void test_basic_create() {
+        assert(new CreateCommand(new MailboxSpecifier("owatagusiam/")).to_string() ==
+               "---- create owatagusiam/");
+    }
+
+    public void test_special_use() {
+        assert(new CreateCommand.special_use(
+                   new MailboxSpecifier("Everything"),
+                   SpecialFolderType.ALL_MAIL
+                   ).to_string() == "---- create Everything (use (\\All))");
+    }
+
+}
diff --git a/test/engine/imap/response/imap-namespace-response-test.vala 
b/test/engine/imap/response/imap-namespace-response-test.vala
new file mode 100644
index 0000000..d897e11
--- /dev/null
+++ b/test/engine/imap/response/imap-namespace-response-test.vala
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2017 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+class Geary.Imap.NamespaceResponseTest : Gee.TestCase {
+
+
+    public NamespaceResponseTest() {
+        base("Geary.Imap.NamespaceResponseTest");
+        add_test("test_minimal", test_minimal);
+        add_test("test_complete", test_complete);
+        add_test("test_cyrus", test_cyrus);
+        add_test("test_anonymous", test_anonymous);
+    }
+
+    public void test_minimal() {
+        // * NAMESPACE NIL NIL NIL
+        try {
+            ServerData data = newNamespaceServerData(null, null, null);
+
+            NamespaceResponse response = NamespaceResponse.decode(data);
+            assert(response.personal == null);
+            assert(response.user == null);
+            assert(response.shared == null);
+        } catch (Error err) {
+            assert_not_reached();
+        }
+    }
+
+    public void test_complete() {
+        // * NAMESPACE (("" "/")) (("~" "/")) (("#shared/" "/")
+        ListParameter personal = new ListParameter();
+        personal.add(newNamespace("", "/"));
+        ListParameter user = new ListParameter();
+        user.add(newNamespace("~", "/"));
+        ListParameter shared = new ListParameter();
+        shared.add(newNamespace("#shared/", "/"));
+        try {
+            ServerData data = newNamespaceServerData(personal, user, shared);
+
+            NamespaceResponse response = NamespaceResponse.decode(data);
+            assert(response.personal != null);
+            assert(response.personal.length == 1);
+            assert(response.personal[0].prefix == "");
+            assert(response.personal[0].delim == "/");
+
+            assert(response.user != null);
+            assert(response.user.length == 1);
+            assert(response.user[0].prefix == "~");
+            assert(response.user[0].delim == "/");
+
+            assert(response.shared != null);
+            assert(response.shared.length == 1);
+            assert(response.shared[0].prefix == "#shared/");
+            assert(response.shared[0].delim == "/");
+        } catch (Error err) {
+            assert_not_reached();
+        }
+    }
+
+    public void test_cyrus() {
+        // * NAMESPACE (("INBOX." ".")) NIL (("" "."))
+        ListParameter personal = new ListParameter();
+        personal.add(newNamespace("INBOX.", "."));
+        ListParameter shared = new ListParameter();
+        shared.add(newNamespace("", "."));
+        try {
+            ServerData data = newNamespaceServerData(personal, null, shared);
+
+            NamespaceResponse response = NamespaceResponse.decode(data);
+            assert(response.personal != null);
+            assert(response.personal[0].prefix == "INBOX.");
+            assert(response.personal[0].delim == ".");
+            assert(response.user == null);
+            assert(response.shared != null);
+            assert(response.shared.length == 1);
+            assert(response.shared[0].prefix == "");
+            assert(response.shared[0].delim == ".");
+        } catch (Error err) {
+            assert_not_reached();
+        }
+    }
+
+    public void test_anonymous() {
+        // * NAMESPACE NIL NIL (("" "."))
+        ListParameter shared = new ListParameter();
+        shared.add(newNamespace("", ","));
+        try {
+            ServerData data = newNamespaceServerData(null, null, shared);
+
+            NamespaceResponse response = NamespaceResponse.decode(data);
+            assert(response.personal == null);
+            assert(response.user == null);
+            assert(response.shared != null);
+            assert(response.shared.length == 1);
+            assert(response.shared[0].prefix == "");
+            assert(response.shared[0].delim == ",");
+        } catch (Error err) {
+            assert_not_reached();
+        }
+    }
+
+    private ServerData newNamespaceServerData(ListParameter? personal,
+                                              ListParameter? users,
+                                              ListParameter? shared)
+    throws Error {
+        RootParameters root = new RootParameters();
+        root.add(new UnquotedStringParameter("*"));
+        root.add(new AtomParameter("namespace"));
+        // Vala's ternary op support is all like :'(
+        if (personal == null)
+            root.add(NilParameter.instance);
+        else
+            root.add(personal);
+
+        if (users == null)
+            root.add(NilParameter.instance);
+        else
+            root.add(users);
+
+        if (shared == null)
+            root.add(NilParameter.instance);
+        else
+            root.add(shared);
+
+        return new ServerData.migrate(root);
+    }
+
+    private ListParameter newNamespace(string prefix, string? delim) {
+        ListParameter ns = new ListParameter();
+        ns.add(new QuotedStringParameter(prefix));
+        ns.add(delim == null ? (Parameter) NilParameter.instance : new QuotedStringParameter(delim));
+        return ns;
+    }
+}
diff --git a/test/main.vala b/test/main.vala
index b959602..2c31f32 100644
--- a/test/main.vala
+++ b/test/main.vala
@@ -41,6 +41,8 @@ int main(string[] args) {
     engine.add_suite(new Geary.EngineTest().get_suite());
     engine.add_suite(new Geary.HTML.UtilTest().get_suite());
     engine.add_suite(new Geary.IdleManagerTest().get_suite());
+    engine.add_suite(new Geary.Imap.CreateCommandTest().get_suite());
+    engine.add_suite(new Geary.Imap.NamespaceResponseTest().get_suite());
     engine.add_suite(new Geary.Inet.Test().get_suite());
     engine.add_suite(new Geary.JS.Test().get_suite());
     engine.add_suite(new Geary.Mime.ContentTypeTest().get_suite());


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