GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/itemxmlutils.cc Lines: 182 210 86.7 %
Date: 2018-07-14 Branches: 440 1272 34.6 %

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
12
TEST_CASE("itemxmlutils readItemStatsString 1", "")
55
{
56
10
    setEnv("SDL_VIDEODRIVER", "dummy");
57
58
10
    client = new Client;
59
10
    XML::initXML();
60
40
    VirtFs::mountDirSilent("data", Append_false);
61
40
    VirtFs::mountDirSilent("../data", Append_false);
62
40
    VirtFs::mountDirSilent("data/test", Append_false);
63
40
    VirtFs::mountDirSilent("../data/test", Append_false);
64
65
10
    mainGraphics = new SDLGraphics;
66
20
    imageHelper = new SDLImageHelper();
67
68
10
    Dirs::initHomeDir();
69
70
10
    setBrandingDefaults(branding);
71
10
    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
10
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
80
#endif  // USE_SDL2
81
82
10
    userPalette = new UserPalette;
83
84
10
    Dirs::initRootDir();
85
10
    theme = new Theme;
86
10
    Theme::selectSkin();
87
88
10
    ActorSprite::load();
89
90
10
    TranslationManager::init();
91
92
20
    std::string effect;
93
20
    ItemFieldInfos fields;
94

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


70
        true);
98

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


70
        true);
102

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


70
        true);
106

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


70
        false);
110

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



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



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



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



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



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



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



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



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



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



4
                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



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



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



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



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



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



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



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



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



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



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

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

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

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

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

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


70
        true);
268

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


70
        true);
272

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


70
        true);
276

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


70
        false);
280

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



5
                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



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



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



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



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


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



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



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



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



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



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



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



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



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



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



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

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

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

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

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

13
}