diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-04-20 20:10:25 +0300 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-04-20 22:08:20 +0300 |
commit | 3429622d754a87ecca98b3fbde994f24e40d34b4 (patch) | |
tree | 1e69a898fb2a364f4bb77afd3a9e1997d11cbd98 /lib/configuration/test | |
parent | move lib/about to src/about (diff) | |
download | smolbote-3429622d754a87ecca98b3fbde994f24e40d34b4.tar.xz |
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
Diffstat (limited to 'lib/configuration/test')
-rw-r--r-- | lib/configuration/test/main.cpp | 174 | ||||
-rw-r--r-- | lib/configuration/test/parser.cpp | 126 |
2 files changed, 174 insertions, 126 deletions
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 <QApplication> +#include <catch2/catch.hpp> + +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<std::string>("name")); + REQUIRE(conf.value<std::string>("name").value() == std::string()); + REQUIRE(conf.value<int>("number")); + REQUIRE(conf.value<int>("number").value() == 0); + REQUIRE(conf.value<bool>("toggle")); + REQUIRE(conf.value<bool>("toggle").value() == false); + + REQUIRE(!conf.value<int>("nullopt")); + REQUIRE(!conf.value<bool>("nullopt")); + REQUIRE(!conf.value<std::string>("nullopt")); + + REQUIRE(conf.value<std::string>("main/name")); + REQUIRE(conf.value<std::string>("main/name").value() == std::string()); + REQUIRE(conf.value<int>("main/number")); + REQUIRE(conf.value<int>("main/number").value() == 0); + REQUIRE(conf.value<bool>("main/toggle")); + REQUIRE(conf.value<bool>("main/toggle").value() == true); + + REQUIRE(!conf.value<int>("main/nullopt")); + REQUIRE(!conf.value<bool>("main/nullopt")); + REQUIRE(!conf.value<std::string>("main/nullopt")); + + THEN("value casting") + { + REQUIRE(conf.value<bool>("number").value() == false); + REQUIRE(conf.value<std::string>("number").value() == "0"); + + REQUIRE(conf.value<std::string>("toggle").value() == "false"); + REQUIRE(conf.value<std::string>("main/toggle").value() == "true"); + + REQUIRE(conf.value<int>("comment").value() == 123); + REQUIRE(std::abs(conf.value<double>("comment").value() - 123.456) < 0.001); + REQUIRE(!conf.value<bool>("name")); + } + } + + WHEN("reading configuration file") + { + conf.read_file(std::getenv("CONFIGFILE")); + + THEN("reading no section") + { + REQUIRE(conf.value<std::string>("name").value() == "Top level"); + REQUIRE(conf.value<std::string>("other").value() == "not in cfg"); + REQUIRE(conf.value<std::string>("comment").value() == "123.456"); + REQUIRE(conf.value<int>("number").value() == 12); + REQUIRE(conf.value<bool>("toggle").value() == true); + } + + THEN("reading main section") + { + REQUIRE(conf.value<std::string>("main/name").value() == "Section Testing"); + REQUIRE(conf.value<int>("main/number").value() == 10); + REQUIRE(conf.value<bool>("main/toggle").value() == false); + } + + THEN("reading included section") + { + REQUIRE(conf.value<std::string>("over").value() == "extra"); + REQUIRE(conf.value<std::string>("extra/name").value() == "extra section"); + REQUIRE(conf.value<int>("extra/number").value() == 12); + REQUIRE(conf.value<bool>("extra/toggle").value() == true); + } + + THEN("value casting") + { + REQUIRE(conf.value<std::string>("number").value() == "12"); + REQUIRE(conf.value<std::string>("toggle").value() == "true"); + REQUIRE(conf.value<std::string>("main/toggle").value() == "false"); + } + + THEN("Qt cast specialization") + { + REQUIRE(conf.value<QString>("name").value() == "Top level"); + REQUIRE(conf.value<QString>("number").value() == "12"); + REQUIRE(conf.value<QString>("toggle").value() == "true"); + REQUIRE(conf.value<QString>("main/toggle").value() == "false"); + REQUIRE(!conf.value<QString>("nullopt")); + + REQUIRE(conf.value<QStringList>("list").value() == QStringList({ "one", "two", "three", "for four" })); + REQUIRE(!conf.value<QStringList>("nullopt")); + } + + THEN("Qt shortcut") + { + REQUIRE(conf.value<std::string>("qt/shortcut") == "Ctrl+Q"); + QAction action; + REQUIRE(conf.shortcut<QAction>(action, "qt/shortcut").shortcut().toString() == "Ctrl+Q"); + REQUIRE(conf.shortcut<QAction>(action, "qt/nil").shortcut().toString() == "Ctrl+Q"); + + QKeySequence sequence; + REQUIRE(conf.shortcut<QKeySequence>(sequence, "qt/shortcut").toString() == "Ctrl+Q"); + REQUIRE(conf.shortcut<QKeySequence>(sequence, "qt/nil").toString() == "Ctrl+Q"); + } + } + } + + GIVEN("global configuration") + { + 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) }, + + }); + + 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<std::string>("name")); + REQUIRE(g.value<std::string>("name").value() == "global"); + REQUIRE(g.value<int>("number")); + REQUIRE(g.value<int>("number").value() == 123); + REQUIRE(g.value<bool>("toggle")); + REQUIRE(g.value<bool>("toggle").value() == true); + REQUIRE(!g.value<std::string>("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; +} diff --git a/lib/configuration/test/parser.cpp b/lib/configuration/test/parser.cpp deleted file mode 100644 index 3ad64a5..0000000 --- a/lib/configuration/test/parser.cpp +++ /dev/null @@ -1,126 +0,0 @@ -#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(); -} |