GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/configuration.cc Lines: 194 194 100.0 %
Date: 2018-12-09 Branches: 1046 2566 40.8 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2018  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "unittests/unittests.h"
22
23
#include "configuration.h"
24
#include "configmanager.h"
25
#include "dirs.h"
26
27
#include "listeners/configlistener.h"
28
29
#include "debug.h"
30
31
namespace
32
{
33
    int mCalled = 0;
34
35
3
    class TestConfigListener : public ConfigListener
36
    {
37
        public:
38
2
            void optionChanged(const std::string &name) override final
39
            {
40
2
                if (name == "testkey123")
41
2
                    mCalled ++;
42
2
            }
43
1
    } testListener;
44
}  // namespace
45
46
19
TEST_CASE("configuration tests", "configuration")
47
{
48
17
    Dirs::initRootDir();
49
17
    Dirs::initHomeDir();
50
51
17
    ConfigManager::initConfiguration();
52
53



119
    SECTION("configuration undefined")
54
    {
55
1
        const char *const key = "nonsetvalue";
56





11
        REQUIRE(config.getValue(key, "not set") == "not set");
57




7
        REQUIRE(config.getValue(key, 12345) == 12345);
58




7
        REQUIRE(config.getValue(key, 12345U) == 12345U);
59




7
        REQUIRE(config.getValueInt(key, 12345) == 12345);
60




7
        REQUIRE(config.getValueBool(key, false) == false);
61




7
        REQUIRE(config.getValueBool(key, true) == true);
62




7
        REQUIRE(config.getValue(key, -12345) == -12345);
63




7
        REQUIRE(config.getValue(key, 12.345) > 12.3);
64
    }
65
66



119
    SECTION("configuration getint default")
67
    {
68
1
        const char *const key = "sfxVolume";
69




7
        REQUIRE(config.getIntValue(key) == 100);
70




7
        REQUIRE(config.getFloatValue(key) >= 100.0F);
71




8
        REQUIRE(config.getStringValue(key) == "100");
72




7
        REQUIRE(config.getBoolValue(key) == true);
73
    }
74
75



119
    SECTION("configuration getfloat default")
76
    {
77
1
        const char *const key = "guialpha";
78




7
        REQUIRE(config.getIntValue(key) == 0);
79




7
        REQUIRE(config.getFloatValue(key) >= 0.8F);
80





9
        REQUIRE(config.getStringValue(key).substr(0, 3) == "0.8");
81




7
        REQUIRE(config.getBoolValue(key) == false);
82
    }
83
84



119
    SECTION("configuration getstring default")
85
    {
86
1
        const char *const key = "soundwhisper";
87




7
        REQUIRE(config.getIntValue(key) == 0);
88




7
        REQUIRE(config.getFloatValue(key) >= 0.0F);
89




8
        REQUIRE(config.getStringValue(key) == "newmessage");
90




7
        REQUIRE(config.getBoolValue(key) == true);
91
    }
92
93



119
    SECTION("configuration getbool default1")
94
    {
95
1
        const char *const key = "showgender";
96




7
        REQUIRE(config.getIntValue(key) == 1);
97




7
        REQUIRE(config.getFloatValue(key) >= 1.0F);
98




8
        REQUIRE(config.getStringValue(key) == "1");
99




7
        REQUIRE(config.getBoolValue(key) == true);
100
    }
101
102



119
    SECTION("configuration getbool default2")
103
    {
104
1
        const char *const key = "showlevel";
105




7
        REQUIRE(config.getIntValue(key) == 0);
106




7
        REQUIRE(config.getFloatValue(key) >= 0.0F);
107




8
        REQUIRE(config.getStringValue(key) == "0");
108




7
        REQUIRE(config.getBoolValue(key) == false);
109
    }
110
111



119
    SECTION("configuration getint set")
112
    {
113
1
        const char *const key = "sfxVolume";
114

4
        config.setValue(key, 50);
115




7
        REQUIRE(config.getIntValue(key) == 50);
116




7
        REQUIRE(config.getFloatValue(key) >= 50.0F);
117




8
        REQUIRE(config.getStringValue(key) == "50");
118




7
        REQUIRE(config.getBoolValue(key) == true);
119
120





11
        REQUIRE(config.getValue(key, "not set") == "50");
121




7
        REQUIRE(config.getValue(key, 12345) == 50);
122




7
        REQUIRE(config.getValue(key, 12345U) == 50U);
123




7
        REQUIRE(config.getValueInt(key, 12345) == 50);
124




7
        REQUIRE(config.getValueBool(key, false) == true);
125




7
        REQUIRE(config.getValueBool(key, true) == true);
126




7
        REQUIRE(config.getValue(key, -12345) == 50);
127




7
        REQUIRE(config.getValue(key, 12.345) >= 50.0);
128
    }
129
130



119
    SECTION("configuration getfloat set")
131
    {
132
1
        const char *const key = "guialpha";
133

4
        config.setValue(key, 50.5);
134




7
        REQUIRE(config.getIntValue(key) == 50);
135




7
        REQUIRE(config.getFloatValue(key) >= 50.4F);
136





9
        REQUIRE(config.getStringValue(key).substr(0, 2) == "50");
137




7
        REQUIRE(config.getBoolValue(key) == true);
138
139




10
        REQUIRE(config.getValue(key, "not set").substr(0, 2) == "50");
140




7
        REQUIRE(config.getValue(key, 12345) == 50);
141




7
        REQUIRE(config.getValue(key, 12345U) == 50U);
142




7
        REQUIRE(config.getValueInt(key, 12345) == 50);
143




7
        REQUIRE(config.getValueBool(key, false) == true);
144




7
        REQUIRE(config.getValueBool(key, true) == true);
145




7
        REQUIRE(config.getValue(key, -12345) == 50);
146




7
        REQUIRE(config.getValue(key, 12.345) >= 50.4);
147
    }
148
149



119
    SECTION("configuration getstring set")
150
    {
151
1
        const char *const key = "soundwhisper";
152

4
        config.setValue(key, "test line");
153




7
        REQUIRE(config.getIntValue(key) == 0);
154




7
        REQUIRE(config.getFloatValue(key) >= 0.0F);
155




8
        REQUIRE(config.getStringValue(key) == "test line");
156




7
        REQUIRE(config.getBoolValue(key) == false);
157
158





11
        REQUIRE(config.getValue(key, "not set") == "test line");
159




7
        REQUIRE(config.getValue(key, 12345) == 0);
160




7
        REQUIRE(config.getValue(key, 12345U) == 0U);
161




7
        REQUIRE(config.getValueInt(key, 12345) == 0);
162




7
        REQUIRE(config.getValueBool(key, false) == false);
163




7
        REQUIRE(config.getValueBool(key, true) == false);
164




7
        REQUIRE(config.getValue(key, -12345) == 0);
165




7
        REQUIRE(config.getValue(key, 12.345) >= 0.0);
166
    }
167
168



119
    SECTION("configuration getbool set1")
169
    {
170
1
        const char *const key = "showgender";
171
5
        config.setValue(key, true);
172




7
        REQUIRE(config.getIntValue(key) == 1);
173




7
        REQUIRE(config.getFloatValue(key) >= 1.0F);
174




8
        REQUIRE(config.getStringValue(key) == "1");
175




7
        REQUIRE(config.getBoolValue(key) == true);
176
177





11
        REQUIRE(config.getValue(key, "not set") == "1");
178




7
        REQUIRE(config.getValue(key, 12345) == 1);
179




7
        REQUIRE(config.getValue(key, 12345U) == 1U);
180




7
        REQUIRE(config.getValueInt(key, 12345) == 1);
181




7
        REQUIRE(config.getValueBool(key, false) == true);
182




7
        REQUIRE(config.getValueBool(key, true) == true);
183




7
        REQUIRE(config.getValue(key, -12345) == 1);
184




7
        REQUIRE(config.getValue(key, 12.345) >= 1.0);
185
    }
186
187



119
    SECTION("configuration getbool set2")
188
    {
189
1
        const char *const key = "showgender";
190
5
        config.setValue(key, false);
191




7
        REQUIRE(config.getIntValue(key) == 0);
192




7
        REQUIRE(config.getFloatValue(key) >= 0.0F);
193




8
        REQUIRE(config.getStringValue(key) == "0");
194




7
        REQUIRE(config.getBoolValue(key) == false);
195
196





11
        REQUIRE(config.getValue(key, "not set") == "0");
197




7
        REQUIRE(config.getValue(key, 12345) == 0);
198




7
        REQUIRE(config.getValue(key, 12345U) == 0U);
199




7
        REQUIRE(config.getValueInt(key, 12345) == 0);
200




7
        REQUIRE(config.getValueBool(key, false) == false);
201




7
        REQUIRE(config.getValueBool(key, true) == false);
202




7
        REQUIRE(config.getValue(key, -12345) == 0);
203




7
        REQUIRE(config.getValue(key, 12.345) >= 0.0);
204
    }
205
206



119
    SECTION("configuration deletekey")
207
    {
208
1
        const char *const key = "testkey123";
209

4
        config.setValue(key, 123);
210




7
        REQUIRE(config.getValueInt(key, 12345) == 123);
211

4
        config.deleteKey(key);
212




7
        REQUIRE(config.getValueInt(key, 12345) == 12345);
213
    }
214
215



119
    SECTION("configuration addlistener")
216
    {
217
1
        const char *const key = "testkey123";
218



4
        REQUIRE(mCalled == 0);
219

4
        config.addListener(key, &testListener);
220



4
        REQUIRE(mCalled == 0);
221

4
        config.setValue(key, 123);
222



4
        REQUIRE(mCalled == 1);
223




7
        REQUIRE(config.getValueInt(key, 12345) == 123);
224



4
        REQUIRE(mCalled == 1);
225

4
        config.setValue(key, 123);
226



4
        REQUIRE(mCalled == 2);
227
5
        config.setSilent(key, true);
228



4
        REQUIRE(mCalled == 2);
229




7
        REQUIRE(config.getBoolValue(key) == true);
230



4
        REQUIRE(mCalled == 2);
231
5
        config.setSilent(key, false);
232



4
        REQUIRE(mCalled == 2);
233




7
        REQUIRE(config.getBoolValue(key) == false);
234



4
        REQUIRE(mCalled == 2);
235

4
        config.removeListener(key, &testListener);
236
    }
237
238



119
    SECTION("configuration incvalue")
239
    {
240
1
        const char *const key = "testkey123";
241

4
        config.setValue(key, 10);
242




7
        REQUIRE(config.getValueInt(key, 12345) == 10);
243

4
        config.incValue(key);
244




7
        REQUIRE(config.getValueInt(key, 12345) == 11);
245
    }
246
247



119
    SECTION("configuration resetintvalue")
248
    {
249
1
        const char *const key = "sfxVolume";
250

4
        config.setValue(key, 20);
251




7
        REQUIRE(config.getIntValue(key) == 20);
252




7
        REQUIRE(config.getFloatValue(key) >= 20.0F);
253




8
        REQUIRE(config.getStringValue(key) == "20");
254




7
        REQUIRE(config.getBoolValue(key) == true);
255

4
        config.resetIntValue(key);
256




7
        REQUIRE(config.getIntValue(key) == 100);
257




7
        REQUIRE(config.getFloatValue(key) >= 100.0F);
258




8
        REQUIRE(config.getStringValue(key) == "100");
259




7
        REQUIRE(config.getBoolValue(key) == true);
260
    }
261
262



119
    SECTION("configuration resetboolvalue1")
263
    {
264
1
        const char *const key = "showgender";
265
5
        config.setValue(key, false);
266




7
        REQUIRE(config.getIntValue(key) == 0);
267




7
        REQUIRE(config.getFloatValue(key) >= 0.0F);
268




8
        REQUIRE(config.getStringValue(key) == "0");
269




7
        REQUIRE(config.getBoolValue(key) == false);
270
271

4
        config.resetBoolValue(key);
272




7
        REQUIRE(config.getIntValue(key) == 1);
273




7
        REQUIRE(config.getFloatValue(key) >= 1.0F);
274




8
        REQUIRE(config.getStringValue(key) == "1");
275




7
        REQUIRE(config.getBoolValue(key) == true);
276
    }
277
278



119
    SECTION("configuration resetboolvalue2")
279
    {
280
1
        const char *const key = "showlevel";
281
5
        config.setValue(key, true);
282




7
        REQUIRE(config.getIntValue(key) == 1);
283




7
        REQUIRE(config.getFloatValue(key) >= 1.0F);
284




8
        REQUIRE(config.getStringValue(key) == "1");
285




7
        REQUIRE(config.getBoolValue(key) == true);
286
287

4
        config.resetBoolValue(key);
288




7
        REQUIRE(config.getIntValue(key) == 0);
289




7
        REQUIRE(config.getFloatValue(key) >= 0.0F);
290




8
        REQUIRE(config.getStringValue(key) == "0");
291




7
        REQUIRE(config.getBoolValue(key) == false);
292
    }
293

20
}