diff options
Diffstat (limited to 'lib/configuration/test')
-rw-r--r-- | lib/configuration/test/defaultrc.ini | 20 | ||||
-rw-r--r-- | lib/configuration/test/extrarc.ini | 8 | ||||
-rw-r--r-- | lib/configuration/test/parser.cpp | 126 |
3 files changed, 154 insertions, 0 deletions
diff --git a/lib/configuration/test/defaultrc.ini b/lib/configuration/test/defaultrc.ini new file mode 100644 index 0000000..f83bf73 --- /dev/null +++ b/lib/configuration/test/defaultrc.ini @@ -0,0 +1,20 @@ +# this is a comment and should be ignored + +name= Top level +over=default +#comment=val +number=12 +toggle=true +list=one;two;three;for four + +[main] +name=Section Testing +number=10 +toggle=false + +[qt] +shortcut=Ctrl+Q + +# include another file, absolute path or relative path to the rundir +@@include extrarc.ini + diff --git a/lib/configuration/test/extrarc.ini b/lib/configuration/test/extrarc.ini new file mode 100644 index 0000000..e22bf09 --- /dev/null +++ b/lib/configuration/test/extrarc.ini @@ -0,0 +1,8 @@ + +over=extra + +[extra] +name=extra section +number=12 +toggle=true + diff --git a/lib/configuration/test/parser.cpp b/lib/configuration/test/parser.cpp new file mode 100644 index 0000000..3ad64a5 --- /dev/null +++ b/lib/configuration/test/parser.cpp @@ -0,0 +1,126 @@ +#include "configuration.h" +#include <QApplication> +#include <cstdlib> +#include <fstream> +#include <gtest/gtest.h> +#include <sstream> + +class ConfigurationTest : public ::testing::Test +{ +protected: + void SetUp() override + { + conf.read_file(std::getenv("CONFIGFILE")); + } + + Configuration conf{ + { "name", std::string() }, + { "over", std::string() }, + { "other", std::string("not in cfg") }, // this entry is not in the conf file + { "comment", std::string() }, // commented out entry in the conf file + { "number", int(0) }, + { "toggle", bool(false) }, + + { "main/name", std::string() }, + { "main/number", int(0) }, + { "main/toggle", bool(true) }, + + { "extra/name", std::string() }, + { "extra/number", int(0) }, + { "extra/toggle", bool(false) }, + }; + + std::unique_ptr<Configuration> global_conf = std::make_unique<Configuration, std::initializer_list<std::pair<std::string, conf_value_t>>>({ + { "name", std::string("global") }, + { "number", int(123) }, + { "toggle", bool(true) }, + + }); +}; + +TEST_F(ConfigurationTest, NoSection) +{ + EXPECT_EQ(conf.value<std::string>("name").value(), "Top level"); + EXPECT_EQ(conf.value<std::string>("other").value(), "not in cfg"); + EXPECT_EQ(conf.value<std::string>("comment").value(), ""); + EXPECT_EQ(conf.value<int>("number").value(), 12); + EXPECT_TRUE(conf.value<bool>("toggle").value()); + EXPECT_FALSE(conf.value<int>("nullopt")); + EXPECT_FALSE(conf.value<bool>("nullopt")); + EXPECT_FALSE(conf.value<std::string>("nullopt")); +} + +TEST_F(ConfigurationTest, TypeCasts) +{ + EXPECT_EQ(conf.value<std::string>("number").value(), "12"); + EXPECT_EQ(conf.value<std::string>("toggle").value(), "true"); + EXPECT_EQ(conf.value<std::string>("main/toggle").value(), "false"); +} + +TEST_F(ConfigurationTest, QtSpecialization) +{ + EXPECT_EQ(conf.value<QString>("name").value(), "Top level"); + EXPECT_EQ(conf.value<QString>("number").value(), "12"); + EXPECT_EQ(conf.value<QString>("toggle").value(), "true"); + EXPECT_EQ(conf.value<QString>("main/toggle").value(), "false"); + EXPECT_FALSE(conf.value<QString>("nullopt")); + + EXPECT_EQ(conf.value<QStringList>("list").value(), QStringList({ "one", "two", "three", "for four" })); + EXPECT_FALSE(conf.value<QStringList>("nullopt")); +} + +TEST_F(ConfigurationTest, QtShortcut) +{ + QAction action; + EXPECT_EQ(conf.shortcut<QAction>(action, "qt/shortcut").shortcut().toString(), "Ctrl+Q"); + EXPECT_EQ(conf.shortcut<QAction>(action, "qt/nil").shortcut().toString(), "Ctrl+Q"); + + QKeySequence sequence; + EXPECT_EQ(conf.shortcut<QKeySequence>(sequence, "qt/shortcut").toString(), "Ctrl+Q"); + EXPECT_EQ(conf.shortcut<QKeySequence>(sequence, "qt/nil").toString(), "Ctrl+Q"); +} + +TEST_F(ConfigurationTest, MainSection) +{ + EXPECT_EQ(conf.value<std::string>("main/name").value(), "Section Testing"); + EXPECT_EQ(conf.value<int>("main/number").value(), 10); + EXPECT_FALSE(conf.value<bool>("main/toggle").value()); +} + +TEST_F(ConfigurationTest, ExtraSection) +{ + EXPECT_EQ(conf.value<std::string>("over").value(), "extra"); + EXPECT_EQ(conf.value<std::string>("extra/name").value(), "extra section"); + EXPECT_EQ(conf.value<int>("extra/number").value(), 12); + EXPECT_TRUE(conf.value<bool>("extra/toggle").value()); +} + +TEST_F(ConfigurationTest, GlobalInstance) +{ + std::stringstream output; + + output << *global_conf; + EXPECT_EQ(output.str(), "name=global\nnumber=123\ntoggle=true\n") << "operator<< on global_conf before move"; + + Configuration::move_global(std::move(global_conf)); + Configuration g; + EXPECT_TRUE(g.value<std::string>("name")); + EXPECT_EQ(g.value<std::string>("name").value(), "global"); + EXPECT_TRUE(g.value<int>("number")); + EXPECT_EQ(g.value<int>("number").value(), 123); + EXPECT_TRUE(g.value<bool>("toggle")); + EXPECT_EQ(g.value<bool>("toggle").value(), true); + EXPECT_FALSE(g.value<std::string>("nullopt")); + + output.str(std::string()); + output << g; + EXPECT_EQ(output.str(), "name=global\nnumber=123\ntoggle=true\n") << "operator<< on global_conf after move"; +} + +int main(int argc, char **argv) +{ + QApplication a(argc, argv); + + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} |