summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2018-03-02 03:48:14 +0300
committerAndrei Karas <akaras@inbox.ru>2018-03-02 03:48:14 +0300
commitd7f491da52e985ea50464804d342d9d45ae407f7 (patch)
treeab3c2fe76047e0fc966f24bf0de8ac159cb64b62 /src
parent359c97f1bdb7ce773c3a7cf9671ece7113685c76 (diff)
downloadplus-d7f491da52e985ea50464804d342d9d45ae407f7.tar.gz
plus-d7f491da52e985ea50464804d342d9d45ae407f7.tar.bz2
plus-d7f491da52e985ea50464804d342d9d45ae407f7.tar.xz
plus-d7f491da52e985ea50464804d342d9d45ae407f7.zip
Add unit tests for itemxmlutils.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am1
-rw-r--r--src/unittests/fs/virtfs/virtfs.cc12
-rw-r--r--src/unittests/fs/virtfs/virtfs1_enumerate.cc4
-rw-r--r--src/unittests/fs/virtfs/virtfs2.cc4
-rw-r--r--src/unittests/utils/itemxmlutils.cc340
-rw-r--r--src/unittests/utils/stringutils.cc3
-rw-r--r--src/utils/itemxmlutils.cpp7
7 files changed, 361 insertions, 10 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 6d36f0c08..8e44e386c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2176,6 +2176,7 @@ manaplustests_SOURCES += \
unittests/resources/map/maplayer/updateconditiontiles.cc \
unittests/resources/resourcemanager/resourcemanager.cc \
unittests/resources/sdlimagehelper.cc \
+ unittests/utils/itemxmlutils.cc \
unittests/gui/windowmanager.cc
manaplustests_SOURCES += ${SRC}
diff --git a/src/unittests/fs/virtfs/virtfs.cc b/src/unittests/fs/virtfs/virtfs.cc
index d316b4ac5..cb318963b 100644
--- a/src/unittests/fs/virtfs/virtfs.cc
+++ b/src/unittests/fs/virtfs/virtfs.cc
@@ -256,8 +256,8 @@ TEST_CASE("VirtFs enumerateFiles1", "")
VirtFs::List *list = nullptr;
- const int cnt1 = VirtFs::exists("test/test2.txt") ? 29 : 28;
- const int cnt2 = 29;
+ const int cnt1 = VirtFs::exists("test/test2.txt") ? 30 : 29;
+ const int cnt2 = 30;
VirtFs::permitLinks(false);
list = VirtFs::enumerateFiles("test");
@@ -744,8 +744,8 @@ TEST_CASE("VirtFs permitLinks1", "")
VirtFs::mountDirSilent("data", Append_false);
VirtFs::mountDirSilent("../data", Append_false);
- const int cnt1 = VirtFs::exists("test/test2.txt") ? 26 : 25;
- const int cnt2 = 26;
+ const int cnt1 = VirtFs::exists("test/test2.txt") ? 27 : 26;
+ const int cnt2 = 27;
StringVect list;
VirtFs::permitLinks(false);
@@ -779,8 +779,8 @@ TEST_CASE("VirtFs permitLinks2", "")
"test",
Append_false);
- const int cnt1 = VirtFs::exists("test2.txt") ? 26 : 25;
- const int cnt2 = 26;
+ const int cnt1 = VirtFs::exists("test2.txt") ? 27 : 26;
+ const int cnt2 = 27;
StringVect list;
VirtFs::permitLinks(false);
diff --git a/src/unittests/fs/virtfs/virtfs1_enumerate.cc b/src/unittests/fs/virtfs/virtfs1_enumerate.cc
index 25f523adf..f77d465ea 100644
--- a/src/unittests/fs/virtfs/virtfs1_enumerate.cc
+++ b/src/unittests/fs/virtfs/virtfs1_enumerate.cc
@@ -82,8 +82,8 @@ TEST_CASE("VirtFs1 enumerateFiles1", "")
VirtFs::List *list = nullptr;
- const int cnt1 = VirtFs::exists("test/test2.txt") ? 29 : 28;
- const int cnt2 = 29;
+ const int cnt1 = VirtFs::exists("test/test2.txt") ? 30 : 29;
+ const int cnt2 = 30;
VirtFs::permitLinks(false);
list = VirtFs::enumerateFiles("test");
diff --git a/src/unittests/fs/virtfs/virtfs2.cc b/src/unittests/fs/virtfs/virtfs2.cc
index cbff4826d..111e3ba44 100644
--- a/src/unittests/fs/virtfs/virtfs2.cc
+++ b/src/unittests/fs/virtfs/virtfs2.cc
@@ -605,8 +605,8 @@ TEST_CASE("VirtFs2 permitLinks", "")
VirtFs::mountDir(prefix + "data",
Append_false);
- const int cnt1 = VirtFs::exists("test/test2.txt") ? 26 : 25;
- const int cnt2 = 26;
+ const int cnt1 = VirtFs::exists("test/test2.txt") ? 27 : 26;
+ const int cnt2 = 27;
StringVect list;
VirtFs::permitLinks(false);
diff --git a/src/unittests/utils/itemxmlutils.cc b/src/unittests/utils/itemxmlutils.cc
new file mode 100644
index 000000000..44eba8e61
--- /dev/null
+++ b/src/unittests/utils/itemxmlutils.cc
@@ -0,0 +1,340 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014-2018 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 "unittests/unittests.h"
+
+#include "client.h"
+#include "configmanager.h"
+#include "configuration.h"
+#include "dirs.h"
+#include "graphicsmanager.h"
+
+#include "being/actorsprite.h"
+
+#include "fs/virtfs/fs.h"
+
+#include "gui/userpalette.h"
+#include "gui/theme.h"
+
+#include "utils/delete2.h"
+#include "utils/env.h"
+#include "utils/xml.h"
+#include "utils/itemxmlutils.h"
+
+#include "utils/translation/translationmanager.h"
+
+#include "render/sdlgraphics.h"
+
+#include "resources/item/itemfieldtype.h"
+
+#include "resources/resourcemanager/resourcemanager.h"
+
+#include "resources/sdlimagehelper.h"
+
+#include "debug.h"
+
+TEST_CASE("itemxmlutils readItemStatsString 1", "")
+{
+ setEnv("SDL_VIDEODRIVER", "dummy");
+
+ client = new Client;
+ XML::initXML();
+ VirtFs::mountDirSilent("data", Append_false);
+ VirtFs::mountDirSilent("../data", Append_false);
+ VirtFs::mountDirSilent("data/test", Append_false);
+ VirtFs::mountDirSilent("../data/test", Append_false);
+
+ mainGraphics = new SDLGraphics;
+ imageHelper = new SDLImageHelper();
+
+ Dirs::initHomeDir();
+
+ setBrandingDefaults(branding);
+ ConfigManager::initConfiguration();
+
+#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
+
+ userPalette = new UserPalette;
+
+ Dirs::initRootDir();
+ theme = new Theme;
+ Theme::selectSkin();
+
+ ActorSprite::load();
+
+ TranslationManager::init();
+
+ std::string effect;
+ ItemFieldInfos fields;
+ fields["hp"] = new ItemFieldType(
+ "hp",
+ "HP %s",
+ true);
+ fields["mp"] = new ItemFieldType(
+ "mp",
+ "MP %s",
+ true);
+ fields["str"] = new ItemFieldType(
+ "str",
+ "Str %s",
+ true);
+ fields["weight"] = new ItemFieldType(
+ "weight",
+ "Weight %s",
+ false);
+ XML::Document doc("itemxmlutils_test.xml", UseVirtFs_true, SkipError_false);
+ XmlNodeConstPtrConst root = doc.rootNode();
+
+ SECTION("empty1")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "empty1");
+ readItemStatsString(effect, node, fields);
+ REQUIRE(effect == "");
+ }
+
+ SECTION("empty2")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "empty2");
+ readItemStatsString(effect, node, fields);
+ REQUIRE(effect == "");
+ }
+
+ SECTION("test1")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test1");
+ readItemStatsString(effect, node, fields);
+ REQUIRE(effect == "HP +20");
+ }
+
+ SECTION("test2")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test2");
+ readItemStatsString(effect, node, fields);
+ if (effect[0] == 'H')
+ {
+ REQUIRE(effect == "HP +10 / Weight 2");
+ }
+ else
+ {
+ REQUIRE(effect == "Weight 2 / HP +10");
+ }
+ }
+
+ SECTION("test3")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test3");
+ readItemStatsString(effect, node, fields);
+ if (effect[0] == 'H')
+ {
+ if (effect[9] == 'S')
+ REQUIRE(effect == "HP +10 / Str +1 / Weight 2");
+ else
+ REQUIRE(effect == "HP +10 / Weight 2 / Str +1");
+ }
+ else if (effect[0] == 'W')
+ {
+ if (effect[11] == 'H')
+ REQUIRE(effect == "Weight 2 / HP +10 / Str +1");
+ else
+ REQUIRE(effect == "Weight 2 / Str +1 / HP +10");
+ }
+ else
+ {
+ if (effect[9] == 'W')
+ REQUIRE(effect == "Str +1 / Weight 2 / HP +10");
+ else
+ REQUIRE(effect == "Str +1 / HP +10 / Weight 2");
+ }
+ }
+
+ delete2(userPalette);
+ delete2(theme);
+ delete2(client);
+ ResourceManager::deleteInstance();
+ VirtFs::unmountDirSilent("data/test");
+ VirtFs::unmountDirSilent("../data/test");
+ VirtFs::unmountDirSilent("data");
+ VirtFs::unmountDirSilent("../data");
+}
+
+TEST_CASE("itemxmlutils readItemStatsVector 1", "")
+{
+ setEnv("SDL_VIDEODRIVER", "dummy");
+
+ client = new Client;
+ XML::initXML();
+ VirtFs::mountDirSilent("data", Append_false);
+ VirtFs::mountDirSilent("../data", Append_false);
+ VirtFs::mountDirSilent("data/test", Append_false);
+ VirtFs::mountDirSilent("../data/test", Append_false);
+
+ mainGraphics = new SDLGraphics;
+ imageHelper = new SDLImageHelper();
+
+ Dirs::initHomeDir();
+
+ setBrandingDefaults(branding);
+ ConfigManager::initConfiguration();
+
+#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
+
+ userPalette = new UserPalette;
+
+ Dirs::initRootDir();
+ theme = new Theme;
+ Theme::selectSkin();
+
+ ActorSprite::load();
+
+ TranslationManager::init();
+
+ STD_VECTOR<std::string> effect;
+ ItemFieldInfos fields;
+ fields["hp"] = new ItemFieldType(
+ "hp",
+ "HP %s",
+ true);
+ fields["mp"] = new ItemFieldType(
+ "mp",
+ "MP %s",
+ true);
+ fields["str"] = new ItemFieldType(
+ "str",
+ "Str %s",
+ true);
+ fields["weight"] = new ItemFieldType(
+ "weight",
+ "Weight %s",
+ false);
+ XML::Document doc("itemxmlutils_test.xml", UseVirtFs_true, SkipError_false);
+ XmlNodeConstPtrConst root = doc.rootNode();
+
+ SECTION("empty1")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "empty1");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.empty());
+ }
+
+ SECTION("empty2")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "empty2");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.empty() == true);
+ }
+
+ SECTION("test1")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test1");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.size() == 1);
+ REQUIRE(effect[0] == "HP +20");
+ }
+
+ SECTION("test2")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test2");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.size() == 2);
+ if (effect[0][0] == 'H')
+ {
+ REQUIRE(effect[0] == "HP +10");
+ }
+ else
+ {
+ REQUIRE(effect[1] == "Weight 2");
+ }
+ }
+
+ SECTION("test3")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test3");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.size() == 3);
+ if (effect[0][0] == 'H')
+ {
+ if (effect[1][0] == 'S')
+ {
+ REQUIRE(effect[0] == "HP +10");
+ REQUIRE(effect[1] == "Str +1");
+ REQUIRE(effect[2] == "Weight 2");
+ }
+ else
+ {
+ REQUIRE(effect[0] == "HP +10");
+ REQUIRE(effect[1] == "Weight 2");
+ REQUIRE(effect[2] == "Str +1");
+ }
+ }
+ else if (effect[0][0] == 'W')
+ {
+ if (effect[1][0] == 'H')
+ {
+ REQUIRE(effect[0] == "Weight 2");
+ REQUIRE(effect[1] == "HP +10");
+ REQUIRE(effect[2] == "Str +1");
+ }
+ else
+ {
+ REQUIRE(effect[0] == "Weight 2");
+ REQUIRE(effect[1] == "Str +1");
+ REQUIRE(effect[2] == "HP +10");
+ }
+ }
+ else
+ {
+ if (effect[1][0] == 'W')
+ {
+ REQUIRE(effect[0] == "Str +1");
+ REQUIRE(effect[1] == "Weight 2");
+ REQUIRE(effect[2] == "HP +10");
+ }
+ else
+ {
+ REQUIRE(effect[0] == "Str +1");
+ REQUIRE(effect[1] == "HP +10");
+ REQUIRE(effect[2] == "Weight 2");
+ }
+ }
+ }
+
+ delete2(userPalette);
+ delete2(theme);
+ delete2(client);
+ ResourceManager::deleteInstance();
+ VirtFs::unmountDirSilent("data/test");
+ VirtFs::unmountDirSilent("../data/test");
+ VirtFs::unmountDirSilent("data");
+ VirtFs::unmountDirSilent("../data");
+}
diff --git a/src/unittests/utils/stringutils.cc b/src/unittests/utils/stringutils.cc
index ddcdcbf3f..ddb94ddd9 100644
--- a/src/unittests/utils/stringutils.cc
+++ b/src/unittests/utils/stringutils.cc
@@ -28,6 +28,8 @@
#include "utils/dtor.h"
+#include "utils/translation/translationmanager.h"
+
#include "resources/iteminfo.h"
#include "resources/db/itemdb.h"
@@ -1489,6 +1491,7 @@ TEST_CASE("stringuntils replaceItemLinks", "")
VirtFs::mountDirSilent("data/test", Append_false);
VirtFs::mountDirSilent("../data/test", Append_false);
+ TranslationManager::init();
ItemDB::NamedItemInfos &namedInfos = ItemDB::getNamedItemInfosTest();
ItemDB::ItemInfos &infos = ItemDB::getItemInfosTest();
setPathsDefaults(paths);
diff --git a/src/utils/itemxmlutils.cpp b/src/utils/itemxmlutils.cpp
index 2f849b8ce..5dc68b249 100644
--- a/src/utils/itemxmlutils.cpp
+++ b/src/utils/itemxmlutils.cpp
@@ -20,6 +20,7 @@
#include "utils/itemxmlutils.h"
+#include "utils/checkutils.h"
#include "utils/foreach.h"
#include "utils/stringutils.h"
#include "utils/translation/podict.h"
@@ -33,7 +34,10 @@ void readItemStatsString(std::string &effect,
const ItemFieldInfos &fields)
{
if (translator == nullptr)
+ {
+ reportAlways("called readItemStatsString without translator");
return;
+ }
FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
{
@@ -60,7 +64,10 @@ void readItemStatsVector(STD_VECTOR<std::string> &effect,
const ItemFieldInfos &fields)
{
if (translator == nullptr)
+ {
+ reportAlways("called readItemStatsVector without translator");
return;
+ }
FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
{