[geary/wip/714104-refine-account-dialog: 27/69] Allow checking for and removing ConfigFile keys and groups. Add tests.



commit d24720db87d123abaf033f4dd3e2540792d1615c
Author: Michael James Gratton <mike vee net>
Date:   Sun Jun 17 13:51:05 2018 +1000

    Allow checking for and removing ConfigFile keys and groups. Add tests.

 src/engine/util/util-config-file.vala  | 39 ++++++++++++++++++++++++++++---
 test/engine/util-config-file-test.vala | 42 ++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 3 deletions(-)
---
diff --git a/src/engine/util/util-config-file.vala b/src/engine/util/util-config-file.vala
index 2f72e0ec..f6c57c20 100644
--- a/src/engine/util/util-config-file.vala
+++ b/src/engine/util/util-config-file.vala
@@ -7,11 +7,16 @@
 
 /**
  * A simple ini-file-like configuration file.
+ *
+ * This class provides a convenient, high-level API for the {@link
+ * GLib.KeyFile} class.
  */
 public class Geary.ConfigFile {
 
 
-    /** A set of configuration items under a "[Name]" heading. */
+    /**
+     * A set of configuration keys grouped under a "[Name]" heading.
+     */
     public class Group {
 
 
@@ -32,6 +37,11 @@ public class Geary.ConfigFile {
         /** The name of this group, as specified by a [Name] heading. */
         public string name { get; private set; }
 
+        /** Determines if this group already exists in the config or not. */
+        public bool exists {
+            get { return this.backing.has_group(this.name); }
+        }
+
         private GLib.KeyFile backing;
         private GroupLookup[] lookups;
 
@@ -58,6 +68,14 @@ public class Geary.ConfigFile {
             this.lookups = { this.lookups[0], GroupLookup(group, prefix) };
         }
 
+        /** Determines if this group as a specific config key set. */
+        public bool has_key(string name) {
+            try {
+                return this.backing.has_key(this.name, name);
+            } catch (GLib.Error err) {
+                return false;
+            }
+        }
 
         public string get_string(string key, string def = "") {
             string ret = def;
@@ -158,6 +176,16 @@ public class Geary.ConfigFile {
             this.backing.set_integer(this.name, key, (int) value);
         }
 
+        /** Removes a key from this group. */
+        public void remove_key(string name) throws GLib.Error {
+            this.backing.remove_key(this.name, name);
+        }
+
+        /** Removes this group from the config file. */
+        public void remove() throws GLib.Error {
+            this.backing.remove_group(this.name);
+        }
+
     }
 
 
@@ -173,9 +201,14 @@ public class Geary.ConfigFile {
     }
 
     /**
-     * Returns the config group under the given named heading.
+     * Returns the config key group under the given heading name.
+     *
+     * If the group does not already exist, it will be created when a
+     * key is first set, but an error will be thrown if a value is
+     * accessed from it before doing so. Use {@link Group.exists} to
+     * determine if the group has previously been created.
      */
-    public Group get_group(string name) {
+    public Group get_group(string name) throws GLib.Error {
         return new Group(this, name, this.backing);
     }
 
diff --git a/test/engine/util-config-file-test.vala b/test/engine/util-config-file-test.vala
index 15c600fe..ed79841f 100644
--- a/test/engine/util-config-file-test.vala
+++ b/test/engine/util-config-file-test.vala
@@ -25,6 +25,10 @@ class Geary.ConfigFileTest : TestCase {
         add_test("test_bool", test_bool);
         add_test("test_int", test_int);
         add_test("test_uint16", test_uint16);
+        add_test("test_has_key", test_has_key);
+        add_test("test_key_remove", test_key_remove);
+        add_test("test_group_exists", test_group_exists);
+        add_test("test_group_remove", test_group_remove);
     }
 
     public override void set_up() throws GLib.Error {
@@ -90,5 +94,43 @@ class Geary.ConfigFileTest : TestCase {
         assert_int(42, this.test_group.get_uint16(TEST_KEY_MISSING, 42));
     }
 
+    public void test_has_key() throws Error {
+        assert_false(
+            this.test_group.has_key(TEST_KEY),
+            "Should not already exist"
+        );
+        this.test_group.set_string(TEST_KEY, "a string");
+        assert_true(
+            this.test_group.has_key(TEST_KEY), "Should now exist"
+        );
+    }
+
+    public void test_key_remove() throws Error {
+        // create the key
+        this.test_group.set_string(TEST_KEY, "a string");
+        assert_true(
+            this.test_group.has_key(TEST_KEY), "Should exist"
+        );
+
+        this.test_group.remove_key(TEST_KEY);
+        assert_false(
+            this.test_group.has_key(TEST_KEY), "Should no longer exist"
+        );
+    }
+
+    public void test_group_exists() throws Error {
+        assert_false(this.test_group.exists, "Should not already exist");
+        this.test_group.set_string(TEST_KEY, "a string");
+        assert_true(this.test_group.exists, "Should now exist");
+    }
+
+    public void test_group_remove() throws Error {
+        // create the group
+        this.test_group.set_string(TEST_KEY, "a string");
+        assert_true(this.test_group.exists, "Should exist");
+
+        this.test_group.remove();
+        assert_false(this.test_group.exists, "Should no longer exist");
+    }
 
 }


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