From 6952bab705e46e978013b8a336bc767ea23579cd Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Mon, 27 Mar 2023 15:37:37 +0300 Subject: [PATCH] v0.3.0-dev.2 --- source/readconf.d | 11 +++++++++++ tests/database.conf | 5 +++++ tests/test.d | 41 +++++++++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 tests/database.conf diff --git a/source/readconf.d b/source/readconf.d index ecb71ee..21efba8 100644 --- a/source/readconf.d +++ b/source/readconf.d @@ -169,6 +169,11 @@ struct ConfigFile private string name = "[]"; private ConfigSection[string] sections; + @property bool exist() + { + return this.sections.length > 0; + } + /** * Get the section * Params: @@ -176,6 +181,10 @@ struct ConfigFile */ @property ConfigSection sectionName(string section = "[]") { + if (!this.exist) + throw new Exception("The configuration file does not exist"); + if (sections.length == 1) + return sections[sections.byKey.front]; return section in sections ? sections[section] : ConfigSection(); } @@ -219,6 +228,8 @@ struct ConfigSection */ ConfigParameter key(string key) { + if (this.empty) + throw new Exception("The selected section has no parameters or does not exist"); return key in this.parameters ? this.parameters[key] : ConfigParameter(); } diff --git a/tests/database.conf b/tests/database.conf new file mode 100644 index 0000000..a20cdf8 --- /dev/null +++ b/tests/database.conf @@ -0,0 +1,5 @@ +[postgres] +host => "//myhost" +port => 5432 +name => mydatabase +password => "/&#BD&@MXLE" diff --git a/tests/test.d b/tests/test.d index ff4bb36..b5390d8 100644 --- a/tests/test.d +++ b/tests/test.d @@ -1,23 +1,36 @@ import readconf; +import std.conv; unittest { - rc.read("./tests/settings.conf"); - rc.read("./tests/settings.conf", "new"); + rc.read("./tests/settings.conf", "old"); + rc.read("./tests/database.conf", "pgconf"); - assert(rc.cf("settings.conf").sn.key("value1") == "text without quotes"); - assert(rc.cf("settings.conf").sn.key("value2") == "Yes!"); - assert(rc.cf("settings.conf").sn.key("value3") == "value in apostrophes"); - assert(rc.cf("settings.conf").sn.key("value4") == "1000"); - assert(rc.cf("settings.conf").sn.key("value5") == "0.000"); - assert(rc.cf("settings.conf").sn.key("value7") == "//path"); - assert(rc.cf("settings.conf").sn.key("value8") == "\"Hey!\""); - assert(rc.cf("settings.conf").sn("part2").key("value1") == "this value will be in the new section"); - assert(rc.cf("settings.conf").sn("part2").key("value3") == "good value!"); - assert(rc.cf("settings.conf").sn("part3").key("value1") == "-2"); - assert(rc.cf("settings.conf").sn("part3").key("value3") == "100"); + assert(rc.cf("old").sn.key("value1") == "text without quotes"); + assert(rc.cf("old").sn.key("value2") == "Yes!"); + assert(rc.cf("old").sn.key("value3") == "value in apostrophes"); + assert(rc.cf("old").sn.key("value4") == "1000"); + assert(rc.cf("old").sn.key("value5") == "0.000"); + assert(rc.cf("old").sn.key("value7") == "//path"); + assert(rc.cf("old").sn.key("value8") == "\"Hey!\""); + assert(rc.cf("old").sn("part2").key("value1") == "this value will be in the new section"); + assert(rc.cf("old").sn("part2").key("value3") == "good value!"); + assert(rc.cf("old").sn("part3").key("value1") == "-2"); + assert(rc.cf("old").sn("part3").key("value3") == "100"); - assert(rc.cf("new").sn("part3").key("value3") == "100"); + auto pgconf = rc.cf("pgconf").sn("postgres"); + + assert(pgconf.key("host") == "//myhost"); + assert(pgconf.key("port").to!int == 5432); + assert(pgconf.key("name") == "mydatabase"); + assert(pgconf.key("password") == "/&#BD&@MXLE"); + + auto pgconf2 = rc.cf("pgconf"); + + assert(pgconf2.sn.key("host") == "//myhost"); + assert(pgconf2.sn.key("port").to!int == 5432); + assert(pgconf2.sn.key("name") == "mydatabase"); + assert(pgconf2.sn.key("password") == "/&#BD&@MXLE"); } // void main()