summaryrefslogtreecommitdiff
path: root/src/utils/xml_unittest.cc
blob: fc40f44398af1e6a095d5eec8182099b3072be31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 *  The ManaPlus Client
 *  Copyright (C) 2014-2017  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "catch.hpp"
#include "client.h"
#include "graphicsmanager.h"

#include "being/actorsprite.h"

#include "fs/virtfs.h"

#include "gui/gui.h"
#include "gui/theme.h"

#include "utils/delete2.h"
#include "utils/env.h"
#ifdef ENABLE_PUGIXML
#include "utils/xmlwriter.h"
#endif  // ENABLE_PUGIXML

#include "render/sdlgraphics.h"

#include "resources/sdlimagehelper.h"

#include "resources/resourcemanager/resourcemanager.h"

#include "debug.h"

TEST_CASE("xml doc")
{
    setEnv("SDL_VIDEODRIVER", "dummy");

    client = new Client;
    dirSeparator = "/";
    XML::initXML();
    logger = new Logger();
    ResourceManager::init();
    VirtFs::addDirToSearchPath("data", Append_false);
    VirtFs::addDirToSearchPath("../data", Append_false);
    mainGraphics = new SDLGraphics;
    imageHelper = new SDLImageHelper();
#ifdef USE_SDL2
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
        graphicsManager.createWindow(640, 480, 0,
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
#else  // USE_SDL2

    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
#endif  // USE_SDL2

    theme = new Theme;
    Theme::selectSkin();
    const char *const tempXmlName = "tempxml.xml";
    ActorSprite::load();
    gui = new Gui();
    gui->postInit(mainGraphics);

    SECTION("load1")
    {
        XML::Document doc("graphics/gui/browserbox.xml",
            UseVirtFs_true,
            SkipError_false);
        REQUIRE(doc.isLoaded() == true);
        REQUIRE(doc.isValid() == true);
        REQUIRE(doc.rootNode() != nullptr);
        REQUIRE(xmlNameEqual(doc.rootNode(), "skinset") == true);
        REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false);
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
//        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
    }

    SECTION("load2")
    {
        const char *const xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            "<root><data option1=\"false\" option2=\"true\"/>"
            "<cont>this is test</cont></root>";
        XML::Document doc(xml, strlen(xml));
        REQUIRE(doc.isLoaded() == true);
        REQUIRE(doc.isValid() == true);
        REQUIRE(doc.rootNode() != nullptr);
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
        REQUIRE(xmlNameEqual(doc.rootNode(), "root123") == false);
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
        REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false);
        REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false);
        REQUIRE(XmlHaveChildContent(doc.rootNode()) == false);
    }

    SECTION("load3")
    {
        const std::string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            "<!-- comment here\nand here -->"
            "<root><data option1=\"false\" option2=\"true\"/></root>";
        XML::Document doc(xml.c_str(), xml.size());
        REQUIRE(doc.isLoaded() == true);
        REQUIRE(doc.isValid() == true);
        REQUIRE(doc.rootNode() != nullptr);
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
        REQUIRE(xmlNameEqual(doc.rootNode(), "root123") == false);
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
        REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false);
        REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false);
        REQUIRE(XmlHaveChildContent(doc.rootNode()) == false);
    }

    SECTION("load4")
    {
        const char *const xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            "<root>this is test</root>";
        XML::Document doc(xml, strlen(xml));
        REQUIRE(doc.isLoaded() == true);
        REQUIRE(doc.isValid() == true);
        REQUIRE(doc.rootNode() != nullptr);
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
        REQUIRE(xmlNameEqual(doc.rootNode(), "root123") == false);
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
        REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false);
        REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false);
        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
        REQUIRE(!strcmp(XmlChildContent(doc.rootNode()), "this is test"));
    }

    SECTION("properties")
    {
        XML::Document doc("graphics/gui/browserbox.xml",
            UseVirtFs_true,
            SkipError_false);

        const XmlNodePtr rootNode = doc.rootNode();
        REQUIRE(XML::getProperty(rootNode, "image", "") == "window.png");
    }

    SECTION("for each")
    {
        XML::Document doc("graphics/gui/browserbox.xml",
            UseVirtFs_true,
            SkipError_false);

        const XmlNodePtr rootNode = doc.rootNode();
//        REQUIRE(XmlHaveChildContent(rootNode) == true);
        XmlNodePtr node = XmlNodeDefault;
        for_each_xml_child_node(widgetNode, rootNode)
        {
            node = widgetNode;
            if (xmlNameEqual(node, "widget"))
                break;
        }
        REQUIRE(node != nullptr);
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
        REQUIRE(xmlNameEqual(node, "widget") == true);
//        REQUIRE(XmlHaveChildContent(node) == true);
        for_each_xml_child_node(optionNode, node)
        {
            node = optionNode;
            if (xmlNameEqual(node, "option"))
                break;
        }
        REQUIRE(node != nullptr);
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
        REQUIRE(xmlNameEqual(node, "option") == true);
        REQUIRE(XmlHaveChildContent(node) == false);
        REQUIRE(XmlHasProp(node, "name") == true);
        REQUIRE(XmlHasProp(node, "value") == true);
        REQUIRE(XmlHasProp(node, "option123") == false);
        REQUIRE(XML::getProperty(node, "name", "") == "padding");
        REQUIRE(XML::langProperty(node, "name", "") == "padding");
        REQUIRE(XML::getProperty(node, "value", 0) == 1);
        REQUIRE(XML::getBoolProperty(node, "value", true) == true);
        REQUIRE(XML::getBoolProperty(node, "value", false) == false);
        REQUIRE(XML::getIntProperty(node, "value", -1, -10, 100) == 1);
    }

    SECTION("child1")
    {
        XML::Document doc("graphics/gui/browserbox.xml",
            UseVirtFs_true,
            SkipError_false);

        const XmlNodePtr rootNode = doc.rootNode();
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "widget");
        REQUIRE(node != nullptr);
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
        REQUIRE(xmlNameEqual(node, "widget") == true);
//        REQUIRE(XmlHaveChildContent(node) == true);
        node = XML::findFirstChildByName(node, "option");
        REQUIRE(node != nullptr);
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
        REQUIRE(xmlNameEqual(node, "option") == true);
        REQUIRE(XmlHaveChildContent(node) == false);
        REQUIRE(XmlHasProp(node, "name") == true);
        REQUIRE(XmlHasProp(node, "value") == true);
        REQUIRE(XmlHasProp(node, "option123") == false);
        REQUIRE(XML::getProperty(node, "name", "") == "padding");
        REQUIRE(XML::langProperty(node, "name", "") == "padding");
        REQUIRE(XML::langProperty(node, "name123", "") == "");
        REQUIRE(XML::getProperty(node, "value", 0) == 1);
        REQUIRE(XML::getProperty(node, "value123", -1) == -1);
        REQUIRE(XML::getBoolProperty(node, "value", true) == true);
        REQUIRE(XML::getBoolProperty(node, "value", false) == false);
        REQUIRE(XML::getBoolProperty(node, "value123", true) == true);
        REQUIRE(XML::getIntProperty(node, "value", -1, -10, 100) == 1);
        REQUIRE(XML::getIntProperty(node, "value123", -1, -10, 100) == -1);
    }

    SECTION("child2")
    {
        const char *const xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            "<root><data option1=\"false\" option2=\"true\" "
            "option3=\"10.5\"/></root>";
        XML::Document doc(xml, strlen(xml));
        const XmlNodePtr rootNode = doc.rootNode();
        REQUIRE(XmlHaveChildContent(rootNode) == false);
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "data");
        REQUIRE(node != nullptr);
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
        REQUIRE(xmlNameEqual(node, "data") == true);
        REQUIRE(XmlHaveChildContent(node) == false);
        REQUIRE(XmlHasProp(node, "option1") == true);
        REQUIRE(XmlHasProp(node, "option123") == false);
        REQUIRE(XML::getBoolProperty(node, "option1", true) == false);
        REQUIRE(XML::getBoolProperty(node, "option2", false) == true);
        const float opt3 = XML::getFloatProperty(node, "option3", 0.0);
        REQUIRE(opt3 > 10);
        REQUIRE(opt3 < 11);
    }

    SECTION("child3")
    {
        const std::string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            "<!-- comment --><root><!-- comment -->"
            "<data option1=\"false\" option2=\"true\" "
            "option3=\"10.5\"/><!-- comment --></root>";
        XML::Document doc(xml.c_str(), xml.size());
        const XmlNodePtr rootNode = doc.rootNode();
//        REQUIRE(XmlHaveChildContent(rootNode) == true);
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "data");
        REQUIRE(node != nullptr);
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
        REQUIRE(xmlNameEqual(node, "data") == true);
        REQUIRE(XmlHaveChildContent(node) == false);
        REQUIRE(XmlHasProp(node, "option1") == true);
        REQUIRE(XmlHasProp(node, "option123") == false);
        REQUIRE(XML::getBoolProperty(node, "option1", true) == false);
        REQUIRE(XML::getBoolProperty(node, "option2", false) == true);
        const float opt3 = XML::getFloatProperty(node, "option3", 0.0);
        REQUIRE(opt3 > 10);
        REQUIRE(opt3 < 11);
    }

    SECTION("validate")
    {
//        REQUIRE(XML::Document::validateXml(
//            "graphics/gui/browserbox.xml") == true);
        REQUIRE(XML::Document::validateXml(
            "graphics/gui/bubble.png") == false);
        REQUIRE(XML::Document::validateXml(
            "graphics/gui/testfile123.xml") == false);
    }

    SECTION("save1")
    {
        // clean
        ::remove(tempXmlName);

        // save
        FILE *const testFile = fopen(tempXmlName, "w");
        REQUIRE(testFile);
        fclose(testFile);
        const XmlTextWriterPtr writer = XmlNewTextWriterFilename(
            tempXmlName,
            0);
        XmlTextWriterSetIndent(writer, 1);
        XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
        XmlTextWriterStartElement(writer, "root");
        XmlTextWriterEndDocument(writer);
        XmlFreeTextWriter(writer);

        // load
        XML::Document doc(tempXmlName,
            UseVirtFs_false,
            SkipError_false);
        REQUIRE(doc.isLoaded() == true);
        REQUIRE(doc.isValid() == true);
        REQUIRE(doc.rootNode() != nullptr);
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
        REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false);
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
//        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);

        // clean again
        ::remove(tempXmlName);
    }

    SECTION("save2")
    {
        // clean
        ::remove(tempXmlName);

        // save
        FILE *const testFile = fopen(tempXmlName, "w");
        REQUIRE(testFile);
        fclose(testFile);
        const XmlTextWriterPtr writer = XmlNewTextWriterFilename(
            tempXmlName,
            0);
        XmlTextWriterSetIndent(writer, 1);
        XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
        XmlTextWriterStartElement(writer, "root");

        XmlTextWriterStartElement(writer, "option");
        XmlTextWriterWriteAttribute(writer, "name", "the name");
        XmlTextWriterWriteAttribute(writer, "value", "the value");
        XmlTextWriterEndElement(writer);

        XmlTextWriterEndDocument(writer);
        XmlFreeTextWriter(writer);

        // load
        XML::Document doc(tempXmlName,
            UseVirtFs_false,
            SkipError_false);
        REQUIRE(doc.isLoaded() == true);
        REQUIRE(doc.isValid() == true);
        REQUIRE(doc.rootNode() != nullptr);
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
        REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false);
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
//        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
        XmlNodePtr node = XML::findFirstChildByName(doc.rootNode(), "option");
        REQUIRE(node != nullptr);
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
        REQUIRE(xmlNameEqual(node, "option") == true);
        REQUIRE(XmlHaveChildContent(node) == false);
        REQUIRE(XmlHasProp(node, "name") == true);
        REQUIRE(XmlHasProp(node, "value") == true);
        REQUIRE(XmlHasProp(node, "option123") == false);
        REQUIRE(XML::getProperty(node, "name", "") == "the name");
        REQUIRE(XML::getProperty(node, "value", "") == "the value");

        // clean again
        ::remove(tempXmlName);
    }
    delete2(theme);
    delete2(client);
    delete2(logger);
//    VirtFs::deinit();
}