geary/test/engine/util-config-file-test.vala
Michael James Gratton 4e0950f9bc Introduce a ConfigFile class to improve config management.
ConfigFile is a GLib.KeyFile-like class (and is backed by a KeyFile) that
nonetheless provides convenient a high-level API for getting and setting
grouped config values and asynchronous loading and saving.

* src/engine/util/util-config-file.vala: New ConfigFile class.

* src/engine/api/geary-service-information.vala (ServiceInformation):
  Require ConfigFile groups rather than KeyFile instances for loading and
  saving. Update subclasses and unit tests.

* src/client/accounts/account-manager.vala (AccountManager): Move generic
  account key consts here from Config. Instead of using KeyFile directly,
  use ConfigFile groups for loading and saving settings. Rename load and
  save methods to be a bit more consistent with the class's role, and
  make save_account() throw its errors so they can be reported to the
  user. Update call sites.

* src/client/accounts/local-service-information.vala
  (LocalServiceInformation): Move service-specific config key consts
  here, use new ConfigFile groups for loading and saving.

* src/engine/api/geary-config.vala: Removed, all config consts have been
  moved to the classes using them and KeyFile convenience methods
  subsumed by ConfigFile.
2018-05-25 15:42:56 +10:00

94 lines
3.4 KiB
Vala

/*
* Copyright 2018 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.ConfigFileTest : TestCase {
private const string TEST_KEY = "test-key";
private const string TEST_KEY_MISSING = "test-key-missing";
private ConfigFile? test_config = null;
private ConfigFile.Group? test_group = null;
public ConfigFileTest() {
base("Geary.ConfigFileTest");
add_test("test_string", test_string);
add_test("test_string_fallback", test_string_fallback);
add_test("test_escaped_string", test_escaped_string);
add_test("test_string_list", test_string_list);
add_test("test_string_list", test_string_list);
add_test("test_bool", test_bool);
add_test("test_int", test_int);
add_test("test_uint16", test_uint16);
}
public override void set_up() throws GLib.Error {
this.test_config = new ConfigFile(GLib.File.new_for_path("/tmp/config.ini"));
this.test_group = this.test_config.get_group("Test");
}
public override void tear_down() throws GLib.Error {
this.test_group = null;
this.test_config = null;
}
public void test_string() throws Error {
this.test_group.set_string(TEST_KEY, "a string");
assert_string("a string", this.test_group.get_string(TEST_KEY));
assert_string("default", this.test_group.get_string(TEST_KEY_MISSING, "default"));
}
public void test_string_fallback() throws Error {
ConfigFile.Group fallback = this.test_config.get_group("fallback");
fallback.set_string("fallback-test-key", "a string");
this.test_group.set_fallback("fallback", "fallback-");
assert_string("a string", this.test_group.get_string(TEST_KEY));
}
public void test_escaped_string() throws Error {
this.test_group.set_escaped_string(TEST_KEY, "a = string");
assert_string("a = string", this.test_group.get_escaped_string(TEST_KEY));
assert_string("=default", this.test_group.get_escaped_string(TEST_KEY_MISSING, "=default"));
}
public void test_string_list() throws Error {
this.test_group.set_string_list(
TEST_KEY, new Gee.ArrayList<string>.wrap({ "a", "b"})
);
Gee.List<string> saved = this.test_group.get_string_list(TEST_KEY);
assert_int(2, saved.size, "Saved string list");
assert_string("a", saved[0]);
assert_string("b", saved[1]);
Gee.List<string> def = this.test_group.get_string_list(TEST_KEY_MISSING);
assert_int(0, def.size, "Default string list");
}
public void test_bool() throws Error {
this.test_group.set_bool(TEST_KEY, true);
assert_true(this.test_group.get_bool(TEST_KEY));
assert_true(this.test_group.get_bool(TEST_KEY_MISSING, true));
assert_false(this.test_group.get_bool(TEST_KEY_MISSING, false));
}
public void test_int() throws Error {
this.test_group.set_int(TEST_KEY, 42);
assert_int(42, this.test_group.get_int(TEST_KEY));
assert_int(42, this.test_group.get_int(TEST_KEY_MISSING, 42));
}
public void test_uint16() throws Error {
this.test_group.set_uint16(TEST_KEY, 42);
assert_int(42, this.test_group.get_uint16(TEST_KEY));
assert_int(42, this.test_group.get_uint16(TEST_KEY_MISSING, 42));
}
}