GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/configuration.cc Lines: 194 194 100.0 %
Date: 2021-03-17 Branches: 1046 2570 40.7 %

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



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





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




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




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




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




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




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




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




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



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




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




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




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




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



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




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




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





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




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



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




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




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




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




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



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




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




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




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




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



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




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




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




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




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



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

4
        config.setValue(key, 50);
116




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




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




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




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





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




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




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




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




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




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




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




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



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

4
        config.setValue(key, 50.5);
135




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




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





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




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





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




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




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




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




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




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




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




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



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

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




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




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




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




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





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




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




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




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




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




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




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




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



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




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




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




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




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





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




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




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




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




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




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




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




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



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




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




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




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




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





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




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




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




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




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




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




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




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



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

4
        config.setValue(key, 123);
211




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

4
        config.deleteKey(key);
213




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



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



4
        REQUIRE(mCalled == 0);
220

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



4
        REQUIRE(mCalled == 0);
222

4
        config.setValue(key, 123);
223



4
        REQUIRE(mCalled == 1);
224




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



4
        REQUIRE(mCalled == 1);
226

4
        config.setValue(key, 123);
227



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



4
        REQUIRE(mCalled == 2);
230




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



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



4
        REQUIRE(mCalled == 2);
234




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



4
        REQUIRE(mCalled == 2);
236

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



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

4
        config.setValue(key, 10);
243




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

4
        config.incValue(key);
245




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



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

4
        config.setValue(key, 20);
252




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




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




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




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

4
        config.resetIntValue(key);
257




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




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




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




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



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




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




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




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




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

4
        config.resetBoolValue(key);
273




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




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




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




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



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




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




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




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




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

4
        config.resetBoolValue(key);
289




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




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




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




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

20
}