#define CATCH_CONFIG_RUNNER // clazy:excludeall=non-pod-global-static #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", 0 }, { "toggle", false }, { "main/name", std::string() }, { "main/number", 0 }, { "main/toggle", true }, { "extra/name", std::string() }, { "extra/number", 0 }, { "extra/toggle", 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", 123 }, { "toggle", 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"); global_conf->make_global(); 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; }