aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/test/qt.cpp
blob: 44114df2c569c48c4671797f23b85c0025d3edf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// clazy:excludeall=non-pod-global-static
#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", 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")
        {
        }

        WHEN("reading configuration file")
        {
            conf.read_file(std::getenv("CONFIGFILE"));

            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");
            }
        }
    }
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    int result = Catch::Session().run(argc, argv);
    return result;
}