aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/test/parser.cpp
blob: 3ad64a59c5e6666e6b402965025e52086e791eee (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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();
}