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

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2017  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
6
    class TestConfigListener : public ConfigListener
36
    {
37
        public:
38
4
            void optionChanged(const std::string &name) override final
39
            {
40
4
                if (name == "testkey123")
41
4
                    mCalled ++;
42
4
            }
43
2
    } testListener;
44
}  // namespace
45
46
38
TEST_CASE("configuration tests", "configuration")
47
{
48
34
    Dirs::initRootDir();
49
34
    Dirs::initHomeDir();
50
51
34
    ConfigManager::initConfiguration();
52
53



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





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




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




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




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




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




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




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




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



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




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




14
        REQUIRE(config.getFloatValue(key) >= 100.0f);
71




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




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



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




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




14
        REQUIRE(config.getFloatValue(key) >= 0.8f);
80





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




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



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




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




14
        REQUIRE(config.getFloatValue(key) >= 0.0f);
89




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




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



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




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




14
        REQUIRE(config.getFloatValue(key) >= 1.0f);
98




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




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



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




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




14
        REQUIRE(config.getFloatValue(key) >= 0.0f);
107




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




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



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

8
        config.setValue(key, 50);
115




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




14
        REQUIRE(config.getFloatValue(key) >= 50.0f);
117




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




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





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




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




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




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




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




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




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




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



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

8
        config.setValue(key, 50.5);
134




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




14
        REQUIRE(config.getFloatValue(key) >= 50.4f);
136





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




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




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




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




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




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




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




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




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




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



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

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




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




14
        REQUIRE(config.getFloatValue(key) >= 0.0f);
155




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




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





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




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




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




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




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




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




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




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



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




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




14
        REQUIRE(config.getFloatValue(key) >= 1.0f);
174




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




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





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




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




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




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




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




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




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




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



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




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




14
        REQUIRE(config.getFloatValue(key) >= 0.0f);
193




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




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





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




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




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




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




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




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




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




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



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

8
        config.setValue(key, 123);
210




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

8
        config.deleteKey(key);
212




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



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



8
        REQUIRE(mCalled == 0);
219

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



8
        REQUIRE(mCalled == 0);
221

8
        config.setValue(key, 123);
222



8
        REQUIRE(mCalled == 1);
223




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



8
        REQUIRE(mCalled == 1);
225

8
        config.setValue(key, 123);
226



8
        REQUIRE(mCalled == 2);
227
10
        config.setSilent(key, true);
228



8
        REQUIRE(mCalled == 2);
229




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



8
        REQUIRE(mCalled == 2);
231
10
        config.setSilent(key, false);
232



8
        REQUIRE(mCalled == 2);
233




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



8
        REQUIRE(mCalled == 2);
235

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



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

8
        config.setValue(key, 10);
242




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

8
        config.incValue(key);
244




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



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

8
        config.setValue(key, 20);
251




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




14
        REQUIRE(config.getFloatValue(key) >= 20.0f);
253




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




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

8
        config.resetIntValue(key);
256




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




14
        REQUIRE(config.getFloatValue(key) >= 100.0f);
258




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




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



238
    SECTION("configuration resetboolvalue1")
263
    {
264
2
        const char *const key = "showgender";
265
10
        config.setValue(key, false);
266




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




14
        REQUIRE(config.getFloatValue(key) >= 0.0f);
268




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




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

8
        config.resetBoolValue(key);
272




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




14
        REQUIRE(config.getFloatValue(key) >= 1.0f);
274




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




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



238
    SECTION("configuration resetboolvalue2")
279
    {
280
2
        const char *const key = "showlevel";
281
10
        config.setValue(key, true);
282




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




14
        REQUIRE(config.getFloatValue(key) >= 1.0f);
284




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




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

8
        config.resetBoolValue(key);
288




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




14
        REQUIRE(config.getFloatValue(key) >= 0.0f);
290




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




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

40
}