From 3429622d754a87ecca98b3fbde994f24e40d34b4 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 20 Apr 2020 20:10:25 +0300 Subject: Rewrite configuration tests in catch2 - Drop s_conf check in operator<< as s_conf cannot be nullptr there - Add arithmetic type cast to string values --- lib/configuration/test/main.cpp | 174 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 lib/configuration/test/main.cpp (limited to 'lib/configuration/test/main.cpp') diff --git a/lib/configuration/test/main.cpp b/lib/configuration/test/main.cpp new file mode 100644 index 0000000..21a02cf --- /dev/null +++ b/lib/configuration/test/main.cpp @@ -0,0 +1,174 @@ +#define CATCH_CONFIG_RUNNER + +#include "configuration.h" +#include +#include + +SCENARIO("Configuration") +{ + GIVEN("a Configuration object with some initial values") + { + Configuration conf{ + { "name", std::string() }, + { "over", std::string() }, + // this entry is not in the conf file + { "other", std::string("not in cfg") }, + // commented out entry in the conf file + { "comment", std::string("123.456") }, + { "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) }, + }; + + WHEN("reading default values") + { + REQUIRE(conf.value("name")); + REQUIRE(conf.value("name").value() == std::string()); + REQUIRE(conf.value("number")); + REQUIRE(conf.value("number").value() == 0); + REQUIRE(conf.value("toggle")); + REQUIRE(conf.value("toggle").value() == false); + + REQUIRE(!conf.value("nullopt")); + REQUIRE(!conf.value("nullopt")); + REQUIRE(!conf.value("nullopt")); + + REQUIRE(conf.value("main/name")); + REQUIRE(conf.value("main/name").value() == std::string()); + REQUIRE(conf.value("main/number")); + REQUIRE(conf.value("main/number").value() == 0); + REQUIRE(conf.value("main/toggle")); + REQUIRE(conf.value("main/toggle").value() == true); + + REQUIRE(!conf.value("main/nullopt")); + REQUIRE(!conf.value("main/nullopt")); + REQUIRE(!conf.value("main/nullopt")); + + THEN("value casting") + { + REQUIRE(conf.value("number").value() == false); + REQUIRE(conf.value("number").value() == "0"); + + REQUIRE(conf.value("toggle").value() == "false"); + REQUIRE(conf.value("main/toggle").value() == "true"); + + REQUIRE(conf.value("comment").value() == 123); + REQUIRE(std::abs(conf.value("comment").value() - 123.456) < 0.001); + REQUIRE(!conf.value("name")); + } + } + + WHEN("reading configuration file") + { + conf.read_file(std::getenv("CONFIGFILE")); + + THEN("reading no section") + { + REQUIRE(conf.value("name").value() == "Top level"); + REQUIRE(conf.value("other").value() == "not in cfg"); + REQUIRE(conf.value("comment").value() == "123.456"); + REQUIRE(conf.value("number").value() == 12); + REQUIRE(conf.value("toggle").value() == true); + } + + THEN("reading main section") + { + REQUIRE(conf.value("main/name").value() == "Section Testing"); + REQUIRE(conf.value("main/number").value() == 10); + REQUIRE(conf.value("main/toggle").value() == false); + } + + THEN("reading included section") + { + REQUIRE(conf.value("over").value() == "extra"); + REQUIRE(conf.value("extra/name").value() == "extra section"); + REQUIRE(conf.value("extra/number").value() == 12); + REQUIRE(conf.value("extra/toggle").value() == true); + } + + THEN("value casting") + { + REQUIRE(conf.value("number").value() == "12"); + REQUIRE(conf.value("toggle").value() == "true"); + REQUIRE(conf.value("main/toggle").value() == "false"); + } + + THEN("Qt cast specialization") + { + REQUIRE(conf.value("name").value() == "Top level"); + REQUIRE(conf.value("number").value() == "12"); + REQUIRE(conf.value("toggle").value() == "true"); + REQUIRE(conf.value("main/toggle").value() == "false"); + REQUIRE(!conf.value("nullopt")); + + REQUIRE(conf.value("list").value() == QStringList({ "one", "two", "three", "for four" })); + REQUIRE(!conf.value("nullopt")); + } + + THEN("Qt shortcut") + { + REQUIRE(conf.value("qt/shortcut") == "Ctrl+Q"); + QAction action; + REQUIRE(conf.shortcut(action, "qt/shortcut").shortcut().toString() == "Ctrl+Q"); + REQUIRE(conf.shortcut(action, "qt/nil").shortcut().toString() == "Ctrl+Q"); + + QKeySequence sequence; + REQUIRE(conf.shortcut(sequence, "qt/shortcut").toString() == "Ctrl+Q"); + REQUIRE(conf.shortcut(sequence, "qt/nil").toString() == "Ctrl+Q"); + } + } + } + + GIVEN("global configuration") + { + std::unique_ptr global_conf = std::make_unique>>({ + { "name", std::string("global") }, + { "number", int(123) }, + { "toggle", bool(true) }, + + }); + + WHEN("no configuration is set") + { + REQUIRE_THROWS(Configuration()); + + std::stringstream output; + REQUIRE_THROWS(output << Configuration()); + } + WHEN("global instance is set") + { + std::stringstream output; + + output << *global_conf; + REQUIRE(output.str() == "name=global\nnumber=123\ntoggle=true\n"); + + Configuration::move_global(std::move(global_conf)); + Configuration g; + REQUIRE(g.value("name")); + REQUIRE(g.value("name").value() == "global"); + REQUIRE(g.value("number")); + REQUIRE(g.value("number").value() == 123); + REQUIRE(g.value("toggle")); + REQUIRE(g.value("toggle").value() == true); + REQUIRE(!g.value("nullopt")); + + output.str(std::string()); + output << g; + REQUIRE(output.str() == "name=global\nnumber=123\ntoggle=true\n"); + } + } +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + int result = Catch::Session().run(argc, argv); + return result; +} -- cgit v1.2.1