GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/itemxmlutils.cc Lines: 182 210 86.7 %
Date: 2021-03-17 Branches: 440 1276 34.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-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 "client.h"
25
#include "configmanager.h"
26
#include "configuration.h"
27
#include "dirs.h"
28
#include "graphicsmanager.h"
29
30
#include "being/actorsprite.h"
31
32
#include "fs/virtfs/fs.h"
33
34
#include "gui/userpalette.h"
35
#include "gui/theme.h"
36
37
#include "utils/delete2.h"
38
#include "utils/env.h"
39
#include "utils/foreach.h"
40
#include "utils/xml.h"
41
#include "utils/itemxmlutils.h"
42
43
#include "utils/translation/translationmanager.h"
44
45
#include "render/sdlgraphics.h"
46
47
#include "resources/item/itemfieldtype.h"
48
49
#include "resources/resourcemanager/resourcemanager.h"
50
51
#include "resources/sdlimagehelper.h"
52
53
#include "debug.h"
54
55
12
TEST_CASE("itemxmlutils readItemStatsString 1", "")
56
{
57
10
    setEnv("SDL_VIDEODRIVER", "dummy");
58
59
10
    client = new Client;
60
10
    XML::initXML();
61
40
    VirtFs::mountDirSilent("data", Append_false);
62
40
    VirtFs::mountDirSilent("../data", Append_false);
63
40
    VirtFs::mountDirSilent("data/test", Append_false);
64
40
    VirtFs::mountDirSilent("../data/test", Append_false);
65
66
10
    mainGraphics = new SDLGraphics;
67
20
    imageHelper = new SDLImageHelper();
68
69
10
    Dirs::initHomeDir();
70
71
10
    setBrandingDefaults(branding);
72
10
    ConfigManager::initConfiguration();
73
74
#ifdef USE_SDL2
75
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
76
        GraphicsManager::createWindow(640, 480, 0,
77
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
78
#else  // USE_SDL2
79
80
10
    GraphicsManager::createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
81
#endif  // USE_SDL2
82
83
10
    userPalette = new UserPalette;
84
85
10
    Dirs::initRootDir();
86
10
    theme = new Theme;
87
10
    Theme::selectSkin();
88
89
10
    ActorSprite::load();
90
91
10
    TranslationManager::init();
92
93
20
    std::string effect;
94
20
    ItemFieldInfos fields;
95

40
    fields["hp"] = new ItemFieldType(
96
        "hp",
97
        "HP %s",
98


70
        true);
99

40
    fields["mp"] = new ItemFieldType(
100
        "mp",
101
        "MP %s",
102


70
        true);
103

40
    fields["str"] = new ItemFieldType(
104
        "str",
105
        "Str %s",
106


70
        true);
107

40
    fields["weight"] = new ItemFieldType(
108
        "weight",
109
        "Weight %s",
110


70
        false);
111

50
    XML::Document doc("itemxmlutils_test.xml", UseVirtFs_true, SkipError_false);
112
10
    XmlNodeConstPtrConst root = doc.rootNode();
113
114



70
    SECTION("empty1")
115
    {
116
1
        XmlNodePtr node = XML::findFirstChildByName(root, "empty1");
117
1
        readItemStatsString(effect, node, fields);
118



4
        REQUIRE(effect.empty());
119
    }
120
121



70
    SECTION("empty2")
122
    {
123
1
        XmlNodePtr node = XML::findFirstChildByName(root, "empty2");
124
1
        readItemStatsString(effect, node, fields);
125



4
        REQUIRE(effect.empty());
126
    }
127
128



70
    SECTION("test1")
129
    {
130
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test1");
131
1
        readItemStatsString(effect, node, fields);
132



4
        REQUIRE(effect == "HP +20");
133
    }
134
135



70
    SECTION("test2")
136
    {
137
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test2");
138
1
        readItemStatsString(effect, node, fields);
139
1
        if (effect[0] == 'H')
140
        {
141



4
            REQUIRE(effect == "HP +10 / Weight 2");
142
        }
143
        else
144
        {
145
            REQUIRE(effect == "Weight 2 / HP +10");
146
        }
147
    }
148
149



70
    SECTION("test3")
150
    {
151
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test3");
152
1
        readItemStatsString(effect, node, fields);
153
1
        if (effect[0] == 'H')
154
        {
155
1
            if (effect[9] == 'S')
156



4
                REQUIRE(effect == "HP +10 / Str +1 / Weight 2");
157
            else
158
                REQUIRE(effect == "HP +10 / Weight 2 / Str +1");
159
        }
160
        else if (effect[0] == 'W')
161
        {
162
            if (effect[11] == 'H')
163
                REQUIRE(effect == "Weight 2 / HP +10 / Str +1");
164
            else
165
                REQUIRE(effect == "Weight 2 / Str +1 / HP +10");
166
        }
167
        else
168
        {
169
            if (effect[9] == 'W')
170
                REQUIRE(effect == "Str +1 / Weight 2 / HP +10");
171
            else
172
                REQUIRE(effect == "Str +1 / HP +10 / Weight 2");
173
        }
174
    }
175
176



70
    SECTION("test4")
177
    {
178
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test4");
179
1
        readItemStatsString(effect, node, fields);
180



4
        REQUIRE(effect == "HP +10 - +15");
181
    }
182
183



70
    SECTION("test5")
184
    {
185
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test5");
186
1
        readItemStatsString(effect, node, fields);
187



4
        REQUIRE(effect == "Weight 2");
188
    }
189
190



70
    SECTION("test6")
191
    {
192
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test6");
193
1
        readItemStatsString(effect, node, fields);
194



4
        REQUIRE(effect == "Weight 2 - 3");
195
    }
196
197



70
    SECTION("test7")
198
    {
199
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test7");
200
1
        readItemStatsString(effect, node, fields);
201



4
        REQUIRE(effect == "Weight 2-");
202
    }
203
204



70
    SECTION("test8")
205
    {
206
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test8");
207
1
        readItemStatsString(effect, node, fields);
208



4
        REQUIRE(effect == "Weight -2");
209
    }
210
211
20
    FOR_EACH(ItemFieldInfos::iterator, it, fields)
212
    {
213
40
        delete (*it).second;
214
    }
215
10
    delete2(userPalette)
216
10
    delete2(theme)
217
10
    delete2(client)
218
10
    ResourceManager::deleteInstance();
219

40
    VirtFs::unmountDirSilent("data/test");
220

40
    VirtFs::unmountDirSilent("../data/test");
221

40
    VirtFs::unmountDirSilent("data");
222

40
    VirtFs::unmountDirSilent("../data");
223
10
}
224
225
12
TEST_CASE("itemxmlutils readItemStatsVector 1", "")
226
{
227
10
    setEnv("SDL_VIDEODRIVER", "dummy");
228
229
10
    client = new Client;
230
10
    XML::initXML();
231
40
    VirtFs::mountDirSilent("data", Append_false);
232
40
    VirtFs::mountDirSilent("../data", Append_false);
233
40
    VirtFs::mountDirSilent("data/test", Append_false);
234
40
    VirtFs::mountDirSilent("../data/test", Append_false);
235
236
10
    mainGraphics = new SDLGraphics;
237
20
    imageHelper = new SDLImageHelper();
238
239
10
    Dirs::initHomeDir();
240
241
10
    setBrandingDefaults(branding);
242
10
    ConfigManager::initConfiguration();
243
244
#ifdef USE_SDL2
245
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
246
        GraphicsManager::createWindow(640, 480, 0,
247
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
248
#else  // USE_SDL2
249
250
10
    GraphicsManager::createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
251
#endif  // USE_SDL2
252
253
10
    userPalette = new UserPalette;
254
255
10
    Dirs::initRootDir();
256
10
    theme = new Theme;
257
10
    Theme::selectSkin();
258
259
10
    ActorSprite::load();
260
261
10
    TranslationManager::init();
262
263
20
    STD_VECTOR<std::string> effect;
264
20
    ItemFieldInfos fields;
265

40
    fields["hp"] = new ItemFieldType(
266
        "hp",
267
        "HP %s",
268


70
        true);
269

40
    fields["mp"] = new ItemFieldType(
270
        "mp",
271
        "MP %s",
272


70
        true);
273

40
    fields["str"] = new ItemFieldType(
274
        "str",
275
        "Str %s",
276


70
        true);
277

40
    fields["weight"] = new ItemFieldType(
278
        "weight",
279
        "Weight %s",
280


70
        false);
281

50
    XML::Document doc("itemxmlutils_test.xml", UseVirtFs_true, SkipError_false);
282
10
    XmlNodeConstPtrConst root = doc.rootNode();
283
284



70
    SECTION("empty1")
285
    {
286
1
        XmlNodePtr node = XML::findFirstChildByName(root, "empty1");
287
1
        readItemStatsVector(effect, node, fields);
288



4
        REQUIRE(effect.empty());
289
    }
290
291



70
    SECTION("empty2")
292
    {
293
1
        XmlNodePtr node = XML::findFirstChildByName(root, "empty2");
294
1
        readItemStatsVector(effect, node, fields);
295



5
        REQUIRE(effect.empty() == true);
296
    }
297
298



70
    SECTION("test1")
299
    {
300
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test1");
301
1
        readItemStatsVector(effect, node, fields);
302



5
        REQUIRE(effect.size() == 1);
303



4
        REQUIRE(effect[0] == "HP +20");
304
    }
305
306



70
    SECTION("test2")
307
    {
308
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test2");
309
1
        readItemStatsVector(effect, node, fields);
310



5
        REQUIRE(effect.size() == 2);
311
1
        if (effect[0][0] == 'H')
312
        {
313



4
            REQUIRE(effect[0] == "HP +10");
314
        }
315
        else
316
        {
317
            REQUIRE(effect[1] == "Weight 2");
318
        }
319
    }
320
321



70
    SECTION("test3")
322
    {
323
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test3");
324
1
        readItemStatsVector(effect, node, fields);
325



5
        REQUIRE(effect.size() == 3);
326
1
        if (effect[0][0] == 'H')
327
        {
328
1
            if (effect[1][0] == 'S')
329
            {
330



4
                REQUIRE(effect[0] == "HP +10");
331



5
                REQUIRE(effect[1] == "Str +1");
332



5
                REQUIRE(effect[2] == "Weight 2");
333
            }
334
            else
335
            {
336
                REQUIRE(effect[0] == "HP +10");
337
                REQUIRE(effect[1] == "Weight 2");
338
                REQUIRE(effect[2] == "Str +1");
339
            }
340
        }
341
        else if (effect[0][0] == 'W')
342
        {
343
            if (effect[1][0] == 'H')
344
            {
345
                REQUIRE(effect[0] == "Weight 2");
346
                REQUIRE(effect[1] == "HP +10");
347
                REQUIRE(effect[2] == "Str +1");
348
            }
349
            else
350
            {
351
                REQUIRE(effect[0] == "Weight 2");
352
                REQUIRE(effect[1] == "Str +1");
353
                REQUIRE(effect[2] == "HP +10");
354
            }
355
        }
356
        else
357
        {
358
            if (effect[1][0] == 'W')
359
            {
360
                REQUIRE(effect[0] == "Str +1");
361
                REQUIRE(effect[1] == "Weight 2");
362
                REQUIRE(effect[2] == "HP +10");
363
            }
364
            else
365
            {
366
                REQUIRE(effect[0] == "Str +1");
367
                REQUIRE(effect[1] == "HP +10");
368
                REQUIRE(effect[2] == "Weight 2");
369
            }
370
        }
371
    }
372
373



70
    SECTION("test4")
374
    {
375
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test4");
376
1
        readItemStatsVector(effect, node, fields);
377



5
        REQUIRE(effect.size() == 1);
378



4
        REQUIRE(effect[0] == "HP +10 - +15");
379
    }
380
381



70
    SECTION("test5")
382
    {
383
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test5");
384
1
        readItemStatsVector(effect, node, fields);
385



5
        REQUIRE(effect.size() == 1);
386



4
        REQUIRE(effect[0] == "Weight 2");
387
    }
388
389



70
    SECTION("test6")
390
    {
391
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test6");
392
1
        readItemStatsVector(effect, node, fields);
393



5
        REQUIRE(effect.size() == 1);
394



4
        REQUIRE(effect[0] == "Weight 2 - 3");
395
    }
396
397



70
    SECTION("test7")
398
    {
399
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test7");
400
1
        readItemStatsVector(effect, node, fields);
401



5
        REQUIRE(effect.size() == 1);
402



4
        REQUIRE(effect[0] == "Weight 2-");
403
    }
404
405



70
    SECTION("test8")
406
    {
407
1
        XmlNodePtr node = XML::findFirstChildByName(root, "test8");
408
1
        readItemStatsVector(effect, node, fields);
409



5
        REQUIRE(effect.size() == 1);
410



4
        REQUIRE(effect[0] == "Weight -2");
411
    }
412
413
20
    FOR_EACH(ItemFieldInfos::iterator, it, fields)
414
    {
415
40
        delete (*it).second;
416
    }
417
10
    delete2(userPalette)
418
10
    delete2(theme)
419
10
    delete2(client)
420
10
    ResourceManager::deleteInstance();
421

40
    VirtFs::unmountDirSilent("data/test");
422

40
    VirtFs::unmountDirSilent("../data/test");
423

40
    VirtFs::unmountDirSilent("data");
424

40
    VirtFs::unmountDirSilent("../data");
425

13
}