GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/itemxmlutils.cc Lines: 182 210 86.7 %
Date: 2018-06-18 21:15:20 Branches: 448 1292 34.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-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 "client.h"
24
#include "configmanager.h"
25
#include "configuration.h"
26
#include "dirs.h"
27
#include "graphicsmanager.h"
28
29
#include "being/actorsprite.h"
30
31
#include "fs/virtfs/fs.h"
32
33
#include "gui/userpalette.h"
34
#include "gui/theme.h"
35
36
#include "utils/delete2.h"
37
#include "utils/env.h"
38
#include "utils/foreach.h"
39
#include "utils/xml.h"
40
#include "utils/itemxmlutils.h"
41
42
#include "utils/translation/translationmanager.h"
43
44
#include "render/sdlgraphics.h"
45
46
#include "resources/item/itemfieldtype.h"
47
48
#include "resources/resourcemanager/resourcemanager.h"
49
50
#include "resources/sdlimagehelper.h"
51
52
#include "debug.h"
53
54
24
TEST_CASE("itemxmlutils readItemStatsString 1", "")
55
{
56
20
    setEnv("SDL_VIDEODRIVER", "dummy");
57
58
20
    client = new Client;
59
20
    XML::initXML();
60

80
    VirtFs::mountDirSilent("data", Append_false);
61

80
    VirtFs::mountDirSilent("../data", Append_false);
62

80
    VirtFs::mountDirSilent("data/test", Append_false);
63

80
    VirtFs::mountDirSilent("../data/test", Append_false);
64
65
20
    mainGraphics = new SDLGraphics;
66
40
    imageHelper = new SDLImageHelper();
67
68
20
    Dirs::initHomeDir();
69
70
20
    setBrandingDefaults(branding);
71
20
    ConfigManager::initConfiguration();
72
73
#ifdef USE_SDL2
74
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
75
        graphicsManager.createWindow(640, 480, 0,
76
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
77
#else  // USE_SDL2
78
79
20
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
80
#endif  // USE_SDL2
81
82
20
    userPalette = new UserPalette;
83
84
20
    Dirs::initRootDir();
85
20
    theme = new Theme;
86
20
    Theme::selectSkin();
87
88
20
    ActorSprite::load();
89
90
20
    TranslationManager::init();
91
92
40
    std::string effect;
93
40
    ItemFieldInfos fields;
94


220
    fields["hp"] = new ItemFieldType(
95
        "hp",
96
        "HP %s",
97

20
        true);
98


220
    fields["mp"] = new ItemFieldType(
99
        "mp",
100
        "MP %s",
101

20
        true);
102


220
    fields["str"] = new ItemFieldType(
103
        "str",
104
        "Str %s",
105

20
        true);
106


220
    fields["weight"] = new ItemFieldType(
107
        "weight",
108
        "Weight %s",
109

20
        false);
110

100
    XML::Document doc("itemxmlutils_test.xml", UseVirtFs_true, SkipError_false);
111
20
    XmlNodeConstPtrConst root = doc.rootNode();
112
113



140
    SECTION("empty1")
114
    {
115
2
        XmlNodePtr node = XML::findFirstChildByName(root, "empty1");
116
2
        readItemStatsString(effect, node, fields);
117



8
        REQUIRE(effect.empty());
118
    }
119
120



140
    SECTION("empty2")
121
    {
122
2
        XmlNodePtr node = XML::findFirstChildByName(root, "empty2");
123
2
        readItemStatsString(effect, node, fields);
124



8
        REQUIRE(effect.empty());
125
    }
126
127



140
    SECTION("test1")
128
    {
129
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test1");
130
2
        readItemStatsString(effect, node, fields);
131



8
        REQUIRE(effect == "HP +20");
132
    }
133
134



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



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



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



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



140
    SECTION("test4")
176
    {
177
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test4");
178
2
        readItemStatsString(effect, node, fields);
179



8
        REQUIRE(effect == "HP +10 - +15");
180
    }
181
182



140
    SECTION("test5")
183
    {
184
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test5");
185
2
        readItemStatsString(effect, node, fields);
186



8
        REQUIRE(effect == "Weight 2");
187
    }
188
189



140
    SECTION("test6")
190
    {
191
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test6");
192
2
        readItemStatsString(effect, node, fields);
193



8
        REQUIRE(effect == "Weight 2 - 3");
194
    }
195
196



140
    SECTION("test7")
197
    {
198
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test7");
199
2
        readItemStatsString(effect, node, fields);
200



8
        REQUIRE(effect == "Weight 2-");
201
    }
202
203



140
    SECTION("test8")
204
    {
205
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test8");
206
2
        readItemStatsString(effect, node, fields);
207



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

80
    VirtFs::unmountDirSilent("data/test");
219

80
    VirtFs::unmountDirSilent("../data/test");
220

80
    VirtFs::unmountDirSilent("data");
221

80
    VirtFs::unmountDirSilent("../data");
222
20
}
223
224
24
TEST_CASE("itemxmlutils readItemStatsVector 1", "")
225
{
226
20
    setEnv("SDL_VIDEODRIVER", "dummy");
227
228
20
    client = new Client;
229
20
    XML::initXML();
230

80
    VirtFs::mountDirSilent("data", Append_false);
231

80
    VirtFs::mountDirSilent("../data", Append_false);
232

80
    VirtFs::mountDirSilent("data/test", Append_false);
233

80
    VirtFs::mountDirSilent("../data/test", Append_false);
234
235
20
    mainGraphics = new SDLGraphics;
236
40
    imageHelper = new SDLImageHelper();
237
238
20
    Dirs::initHomeDir();
239
240
20
    setBrandingDefaults(branding);
241
20
    ConfigManager::initConfiguration();
242
243
#ifdef USE_SDL2
244
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
245
        graphicsManager.createWindow(640, 480, 0,
246
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
247
#else  // USE_SDL2
248
249
20
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
250
#endif  // USE_SDL2
251
252
20
    userPalette = new UserPalette;
253
254
20
    Dirs::initRootDir();
255
20
    theme = new Theme;
256
20
    Theme::selectSkin();
257
258
20
    ActorSprite::load();
259
260
20
    TranslationManager::init();
261
262
40
    STD_VECTOR<std::string> effect;
263
40
    ItemFieldInfos fields;
264


220
    fields["hp"] = new ItemFieldType(
265
        "hp",
266
        "HP %s",
267

20
        true);
268


220
    fields["mp"] = new ItemFieldType(
269
        "mp",
270
        "MP %s",
271

20
        true);
272


220
    fields["str"] = new ItemFieldType(
273
        "str",
274
        "Str %s",
275

20
        true);
276


220
    fields["weight"] = new ItemFieldType(
277
        "weight",
278
        "Weight %s",
279

20
        false);
280

100
    XML::Document doc("itemxmlutils_test.xml", UseVirtFs_true, SkipError_false);
281
20
    XmlNodeConstPtrConst root = doc.rootNode();
282
283



140
    SECTION("empty1")
284
    {
285
2
        XmlNodePtr node = XML::findFirstChildByName(root, "empty1");
286
2
        readItemStatsVector(effect, node, fields);
287



8
        REQUIRE(effect.empty());
288
    }
289
290



140
    SECTION("empty2")
291
    {
292
2
        XmlNodePtr node = XML::findFirstChildByName(root, "empty2");
293
2
        readItemStatsVector(effect, node, fields);
294



10
        REQUIRE(effect.empty() == true);
295
    }
296
297



140
    SECTION("test1")
298
    {
299
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test1");
300
2
        readItemStatsVector(effect, node, fields);
301



10
        REQUIRE(effect.size() == 1);
302



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



140
    SECTION("test2")
306
    {
307
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test2");
308
2
        readItemStatsVector(effect, node, fields);
309



10
        REQUIRE(effect.size() == 2);
310
4
        if (effect[0][0] == 'H')
311
        {
312



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



140
    SECTION("test3")
321
    {
322
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test3");
323
2
        readItemStatsVector(effect, node, fields);
324



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



8
                REQUIRE(effect[0] == "HP +10");
330



10
                REQUIRE(effect[1] == "Str +1");
331



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



140
    SECTION("test4")
373
    {
374
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test4");
375
2
        readItemStatsVector(effect, node, fields);
376



10
        REQUIRE(effect.size() == 1);
377



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



140
    SECTION("test5")
381
    {
382
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test5");
383
2
        readItemStatsVector(effect, node, fields);
384



8
        REQUIRE(effect.size() == 1);
385



8
        REQUIRE(effect[0] == "Weight 2");
386
    }
387
388



140
    SECTION("test6")
389
    {
390
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test6");
391
2
        readItemStatsVector(effect, node, fields);
392



10
        REQUIRE(effect.size() == 1);
393



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



140
    SECTION("test7")
397
    {
398
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test7");
399
2
        readItemStatsVector(effect, node, fields);
400



10
        REQUIRE(effect.size() == 1);
401



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



140
    SECTION("test8")
405
    {
406
2
        XmlNodePtr node = XML::findFirstChildByName(root, "test8");
407
2
        readItemStatsVector(effect, node, fields);
408



10
        REQUIRE(effect.size() == 1);
409



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

80
    VirtFs::unmountDirSilent("data/test");
421

80
    VirtFs::unmountDirSilent("../data/test");
422

80
    VirtFs::unmountDirSilent("data");
423

80
    VirtFs::unmountDirSilent("../data");
424

26
}