summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-01-17 00:41:16 +0300
committerAndrei Karas <akaras@inbox.ru>2016-01-17 00:41:16 +0300
commitcaf2e3c4fb749561c37eaff324de86a9edf83808 (patch)
treec20d8b2a5863c0366f0804fe7a4361ccfc16a927
parent0ad55dae62f1c8295517bf87844368575a813d57 (diff)
downloadplus-caf2e3c4fb749561c37eaff324de86a9edf83808.tar.gz
plus-caf2e3c4fb749561c37eaff324de86a9edf83808.tar.bz2
plus-caf2e3c4fb749561c37eaff324de86a9edf83808.tar.xz
plus-caf2e3c4fb749561c37eaff324de86a9edf83808.zip
Add unit tests for links replace function.
-rw-r--r--src/resources/db/itemdb.cpp7
-rw-r--r--src/resources/db/itemdb.h4
-rw-r--r--src/utils/stringutils_unittest.cc184
3 files changed, 195 insertions, 0 deletions
diff --git a/src/resources/db/itemdb.cpp b/src/resources/db/itemdb.cpp
index b8e49a2db..b4af71c17 100644
--- a/src/resources/db/itemdb.cpp
+++ b/src/resources/db/itemdb.cpp
@@ -1055,3 +1055,10 @@ static void loadOrderSprite(ItemInfo *const itemInfo,
itemInfo->setDrawBefore(direction, sprite);
itemInfo->setDrawPriority(direction, priority);
}
+
+#ifdef UNITTESTS
+ItemDB::NamedItemInfos &ItemDB::getNamedItemInfosTest()
+{
+ return mNamedItemInfos;
+}
+#endif
diff --git a/src/resources/db/itemdb.h b/src/resources/db/itemdb.h
index 0436f604c..9231bbfc8 100644
--- a/src/resources/db/itemdb.h
+++ b/src/resources/db/itemdb.h
@@ -60,6 +60,10 @@ namespace ItemDB
const ItemDB::ItemInfos &getItemInfos();
+#ifdef UNITTESTS
+ ItemDB::NamedItemInfos &getNamedItemInfosTest();
+#endif
+
int getTagId(const std::string &tagName) A_WARN_UNUSED;
void setStatsList(const std::vector<Stat> &stats);
diff --git a/src/utils/stringutils_unittest.cc b/src/utils/stringutils_unittest.cc
index 74427f163..e2db8eed1 100644
--- a/src/utils/stringutils_unittest.cc
+++ b/src/utils/stringutils_unittest.cc
@@ -22,6 +22,10 @@
#include "utils/stringutils.h"
+#include "resources/iteminfo.h"
+
+#include "resources/db/itemdb.h"
+
#include <list>
#include <string>
#include <vector>
@@ -588,3 +592,183 @@ TEST_CASE("stringuntils isDigit")
REQUIRE_FALSE(isDigit("1.23"));
REQUIRE_FALSE(isDigit("12-34"));
}
+
+TEST_CASE("stringuntils replaceItemLinks")
+{
+ ItemDB::NamedItemInfos &infos = ItemDB::getNamedItemInfosTest();
+ ItemInfo *info = new ItemInfo;
+ info->setId(123456);
+ info->setName("test name 1");
+ infos["test name 1"] = info;
+
+ info = new ItemInfo;
+ info->setId(123);
+ info->setName("test name 2");
+ infos["test name 2"] = info;
+
+ std::string str;
+
+ SECTION("empty")
+ {
+ str = "test line";
+ replaceItemLinks(str);
+ REQUIRE(str == "test line");
+
+ str = "";
+ replaceItemLinks(str);
+ REQUIRE(str == "");
+
+ str = "[]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[]");
+
+ str = "[qqq]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[qqq]");
+ }
+
+ SECTION("simple")
+ {
+ str = "[test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[@@123456|test name 1@@]");
+
+ str = "text1 [test name 1] text2";
+ replaceItemLinks(str);
+ REQUIRE(str == "text1 [@@123456|test name 1@@] text2");
+
+ str = "[test name 1][test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[@@123456|test name 1@@][@@123456|test name 1@@]");
+
+ str = "[test name 1] [test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[@@123456|test name 1@@] [@@123456|test name 1@@]");
+
+ str = "test1 [test name 1]test2[test name 1] test3";
+ replaceItemLinks(str);
+ REQUIRE(str == "test1 [@@123456|test name 1@@]test2"
+ "[@@123456|test name 1@@] test3");
+
+ str = "[test name 1] [no link]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[@@123456|test name 1@@] [no link]");
+ }
+
+ SECTION("broken")
+ {
+ str = "[";
+ replaceItemLinks(str);
+ REQUIRE(str == "[");
+
+ str = "]";
+ replaceItemLinks(str);
+ REQUIRE(str == "]");
+
+ str = "][";
+ replaceItemLinks(str);
+ REQUIRE(str == "][");
+
+ str = "]]";
+ replaceItemLinks(str);
+ REQUIRE(str == "]]");
+
+ str = "]t";
+ replaceItemLinks(str);
+ REQUIRE(str == "]t");
+
+ str = "t[";
+ replaceItemLinks(str);
+ REQUIRE(str == "t[");
+
+ str = "t]";
+ replaceItemLinks(str);
+ REQUIRE(str == "t]");
+
+ str = "[[[";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[[");
+
+ str = "[[]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[]");
+
+ str = "[[t";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[t");
+
+ str = "[][";
+ replaceItemLinks(str);
+ REQUIRE(str == "[][");
+
+ str = "[]]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[]]");
+
+ str = "[]t";
+ replaceItemLinks(str);
+ REQUIRE(str == "[]t");
+
+ str = "[t[";
+ replaceItemLinks(str);
+ REQUIRE(str == "[t[");
+
+ str = "[t]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[t]");
+
+ str = "t[[";
+ replaceItemLinks(str);
+ REQUIRE(str == "t[[");
+
+ str = "t[]";
+ replaceItemLinks(str);
+ REQUIRE(str == "t[]");
+
+ str = "t[[";
+ replaceItemLinks(str);
+ REQUIRE(str == "t[[");
+
+ str = "]]]";
+ replaceItemLinks(str);
+ REQUIRE(str == "]]]");
+ }
+
+ SECTION("broken2")
+ {
+ str = "[][]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[][]");
+
+ str = "[[]]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[]]");
+
+ str = "][[]";
+ replaceItemLinks(str);
+ REQUIRE(str == "][[]");
+ }
+
+ SECTION("broken3")
+ {
+ str = "[[test name 1]]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[@@123456|test name 1@@]]");
+
+ str = "[[test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[@@123456|test name 1@@]");
+
+ str = "[[test] name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[test] name 1]");
+
+ str = "[[test name 1]test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[@@123456|test name 1@@]test name 1]");
+
+ str = "[[test name 1[]test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[test name 1[]test name 1]");
+ }
+}