summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data/test/itemxmlutils_test.xml18
-rw-r--r--src/unittests/utils/itemxmlutils.cc75
-rw-r--r--src/utils/itemxmlutils.cpp76
3 files changed, 145 insertions, 24 deletions
diff --git a/data/test/itemxmlutils_test.xml b/data/test/itemxmlutils_test.xml
index f0c3c5c96..dcd829c13 100644
--- a/data/test/itemxmlutils_test.xml
+++ b/data/test/itemxmlutils_test.xml
@@ -24,4 +24,22 @@ Copyright (C) 2018 ManaPlus developers -->
other="3"
str="1"
/>
+ <test4
+ hp="10-15"
+ other="3"
+ />
+ <test5
+ hp1="10"
+ weight="2"
+ other="3"
+ />
+ <test6
+ weight="2-3"
+ />
+ <test7
+ weight="2-"
+ />
+ <test8
+ weight="-2"
+ />
</root>
diff --git a/src/unittests/utils/itemxmlutils.cc b/src/unittests/utils/itemxmlutils.cc
index 44eba8e61..825bc2f97 100644
--- a/src/unittests/utils/itemxmlutils.cc
+++ b/src/unittests/utils/itemxmlutils.cc
@@ -171,6 +171,41 @@ TEST_CASE("itemxmlutils readItemStatsString 1", "")
}
}
+ SECTION("test4")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test4");
+ readItemStatsString(effect, node, fields);
+ REQUIRE(effect == "HP +10 - +15");
+ }
+
+ SECTION("test5")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test5");
+ readItemStatsString(effect, node, fields);
+ REQUIRE(effect == "Weight 2");
+ }
+
+ SECTION("test6")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test6");
+ readItemStatsString(effect, node, fields);
+ REQUIRE(effect == "Weight 2 - 3");
+ }
+
+ SECTION("test7")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test7");
+ readItemStatsString(effect, node, fields);
+ REQUIRE(effect == "Weight 2-");
+ }
+
+ SECTION("test8")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test8");
+ readItemStatsString(effect, node, fields);
+ REQUIRE(effect == "Weight -2");
+ }
+
delete2(userPalette);
delete2(theme);
delete2(client);
@@ -329,6 +364,46 @@ TEST_CASE("itemxmlutils readItemStatsVector 1", "")
}
}
+ SECTION("test4")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test4");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.size() == 1);
+ REQUIRE(effect[0] == "HP +10 - +15");
+ }
+
+ SECTION("test5")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test5");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.size() == 1);
+ REQUIRE(effect[0] == "Weight 2");
+ }
+
+ SECTION("test6")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test6");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.size() == 1);
+ REQUIRE(effect[0] == "Weight 2 - 3");
+ }
+
+ SECTION("test7")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test7");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.size() == 1);
+ REQUIRE(effect[0] == "Weight 2-");
+ }
+
+ SECTION("test8")
+ {
+ XmlNodePtr node = XML::findFirstChildByName(root, "test8");
+ readItemStatsVector(effect, node, fields);
+ REQUIRE(effect.size() == 1);
+ REQUIRE(effect[0] == "Weight -2");
+ }
+
delete2(userPalette);
delete2(theme);
delete2(client);
diff --git a/src/utils/itemxmlutils.cpp b/src/utils/itemxmlutils.cpp
index 5dc68b249..8ecbeeda6 100644
--- a/src/utils/itemxmlutils.cpp
+++ b/src/utils/itemxmlutils.cpp
@@ -29,6 +29,52 @@
#include "debug.h"
+static bool readField(ItemFieldInfos::const_iterator it,
+ XmlNodeConstPtr node,
+ std::string &combined)
+{
+ const std::string fieldName = (*it).first;
+ const ItemFieldType *const field = (*it).second;
+
+ std::string value = XML::getProperty(node,
+ fieldName.c_str(),
+ "");
+ if (value.empty())
+ return false;
+
+ StringVect tokens;
+ splitToStringVector(tokens,
+ value,
+ '-');
+ if (tokens.size() > 1)
+ {
+ std::string value1;
+ std::string value2;
+ if (field->sign)
+ {
+ value1 = std::string("+").append(tokens[0]);
+ value2 = std::string("+").append(tokens[1]);
+ }
+ else
+ {
+ value1 = tokens[0];
+ value2 = tokens[1];
+ }
+ value = strprintf("%s - %s",
+ value1.c_str(),
+ value2.c_str());
+ }
+ else
+ {
+ if (field->sign)
+ value = std::string("+").append(value);
+ }
+ const std::string format = translator->getStr(field->description);
+ combined = strprintf(format.c_str(),
+ value.c_str());
+ return true;
+}
+
void readItemStatsString(std::string &effect,
XmlNodeConstPtr node,
const ItemFieldInfos &fields)
@@ -41,21 +87,12 @@ void readItemStatsString(std::string &effect,
FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
{
- const std::string fieldName = (*it).first;
- const ItemFieldType *const field = (*it).second;
-
- std::string value = XML::getProperty(node,
- fieldName.c_str(),
- "");
- if (value.empty())
+ std::string field;
+ if (!readField(it, node, field))
continue;
if (!effect.empty())
effect.append(" / ");
- if (field->sign && isDigit(value))
- value = std::string("+").append(value);
- const std::string format = translator->getStr(field->description);
- effect.append(strprintf(format.c_str(),
- value.c_str()));
+ effect.append(field);
}
}
@@ -71,18 +108,9 @@ void readItemStatsVector(STD_VECTOR<std::string> &effect,
FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
{
- const std::string fieldName = (*it).first;
- const ItemFieldType *const field = (*it).second;
-
- std::string value = XML::getProperty(node,
- fieldName.c_str(),
- "");
- if (value.empty())
+ std::string field;
+ if (!readField(it, node, field))
continue;
- if (field->sign && isDigit(value))
- value = std::string("+").append(value);
- const std::string format = translator->getStr(field->description);
- effect.push_back(strprintf(format.c_str(),
- value.c_str()));
+ effect.push_back(field);
}
}