commit 7f693fe7ce0a4dfc2a6a2e20066ff9826026e1cb
parent b0e648341a6344828b41f1f98a5487988ccf78d2
Author: cfillion <cfillion@users.noreply.github.com>
Date: Wed, 25 Nov 2015 22:24:00 -0500
move most of the database validation code to the generic implementation
Diffstat:
10 files changed, 80 insertions(+), 45 deletions(-)
diff --git a/src/database.cpp b/src/database.cpp
@@ -39,6 +39,8 @@ void Database::addCategory(CategoryPtr cat)
Category::Category(const std::string &name)
: m_name(name)
{
+ if(m_name.empty())
+ throw database_error("empty category name");
}
void Category::addPackage(PackagePtr pack)
@@ -49,16 +51,23 @@ void Category::addPackage(PackagePtr pack)
Package::Type Package::convertType(const char *type)
{
- if(!type)
- return UnknownType;
-
if(!strcmp(type, "script"))
return ScriptType;
-
- return UnknownType;
+ else
+ return UnknownType;
}
-Package::Package()
- : m_category(0)
+Package::Package(const Type type,
+ const std::string &name, const std::string &author)
+ : m_category(0), m_type(type), m_name(name), m_author(author)
{
+ if(m_type == Package::UnknownType)
+ throw database_error("unsupported package type");
+
+ if(m_name.empty())
+ throw database_error("empty package name");
+
+ if(m_author.empty())
+ throw database_error("empty package author");
+
}
diff --git a/src/database.hpp b/src/database.hpp
@@ -59,7 +59,7 @@ public:
static Type convertType(const char *);
- Package();
+ Package(const Type, const std::string &name, const std::string &author);
void setCategory(Category *cat) { m_category = cat; }
Category *category() const { return m_category; }
@@ -69,6 +69,9 @@ private:
static PackagePtr loadV1(TiXmlElement *);
Category *m_category;
+ Type m_type;
+ std::string m_name;
+ std::string m_author;
};
#endif
diff --git a/src/database_v1.cpp b/src/database_v1.cpp
@@ -28,9 +28,7 @@ CategoryPtr Category::loadV1(TiXmlElement *catNode)
throw database_error("not a category");
const char *name = catNode->Attribute("name");
-
- if(!name || !strlen(name))
- throw database_error("missing category name");
+ if(!name) name = "";
CategoryPtr cat = make_shared<Category>(name);
@@ -51,20 +49,13 @@ PackagePtr Package::loadV1(TiXmlElement *packNode)
throw database_error("not a package");
const char *name = packNode->Attribute("name");
+ if(!name) name = "";
- if(!name || !strlen(name))
- throw database_error("missing package name");
-
- const char *typeStr = packNode->Attribute("type");
- const Package::Type type = Package::convertType(typeStr);
-
- if(type == Package::UnknownType)
- throw database_error("unsupported package type");
+ const char *type = packNode->Attribute("type");
+ if(!type) type = "";
const char *author = packNode->Attribute("author");
+ if(!author) author = "";
- if(!author || !strlen(author))
- throw database_error("package is anonymous");
-
- return 0;
+ return make_shared<Package>(Package::convertType(type), name, author);
}
diff --git a/test/database.cpp b/test/database.cpp
@@ -71,7 +71,7 @@ TEST_CASE("add category to database", M) {
}
TEST_CASE("add package to category", M) {
- PackagePtr pack = make_shared<Package>();
+ PackagePtr pack = make_shared<Package>(Package::ScriptType, "a", "b");
Category cat("a");
CHECK(cat.packages().size() == 0);
@@ -84,10 +84,6 @@ TEST_CASE("add package to category", M) {
}
TEST_CASE("package type from string", M) {
- SECTION("null") {
- REQUIRE(Package::convertType(0) == Package::UnknownType);
- }
-
SECTION("unknown") {
REQUIRE(Package::convertType("yoyo") == Package::UnknownType);
}
@@ -96,3 +92,43 @@ TEST_CASE("package type from string", M) {
REQUIRE(Package::convertType("script") == Package::ScriptType);
}
}
+
+TEST_CASE("empty category name", M) {
+ try {
+ Category cat{string()};
+ FAIL();
+ }
+ catch(const database_error &e) {
+ REQUIRE(string(e.what()) == "empty category name");
+ }
+}
+
+TEST_CASE("unknown package type", M) {
+ try {
+ Package pack(Package::UnknownType, "a", "b");
+ FAIL();
+ }
+ catch(const database_error &e) {
+ REQUIRE(string(e.what()) == "unsupported package type");
+ }
+}
+
+TEST_CASE("empty package name", M) {
+ try {
+ Package pack(Package::ScriptType, string(), "a");
+ FAIL();
+ }
+ catch(const database_error &e) {
+ REQUIRE(string(e.what()) == "empty package name");
+ }
+}
+
+TEST_CASE("empty package author", M) {
+ try {
+ Package pack(Package::ScriptType, "a", string());
+ FAIL();
+ }
+ catch(const database_error &e) {
+ REQUIRE(string(e.what()) == "empty package author");
+ }
+}
diff --git a/test/database_v1.cpp b/test/database_v1.cpp
@@ -17,7 +17,7 @@ TEST_CASE("unnamed category", M) {
FAIL();
}
catch(const database_error &e) {
- REQUIRE(string(e.what()) == "missing category name");
+ REQUIRE(string(e.what()) == "empty category name");
}
}
@@ -39,19 +39,19 @@ TEST_CASE("invalid category tag", M) {
}
}
-TEST_CASE("unnamed package", M) {
+TEST_CASE("null package name", M) {
try {
Database::load(DBPATH "unnamed_package.xml");
FAIL();
}
catch(const database_error &e) {
- REQUIRE(string(e.what()) == "missing package name");
+ REQUIRE(string(e.what()) == "empty package name");
}
}
-TEST_CASE("invalid package type", M) {
+TEST_CASE("null package type", M) {
try {
- Database::load(DBPATH "invalid_type.xml");
+ Database::load(DBPATH "missing_type.xml");
FAIL();
}
catch(const database_error &e) {
@@ -59,12 +59,12 @@ TEST_CASE("invalid package type", M) {
}
}
-TEST_CASE("anonymous package", M) {
+TEST_CASE("null author", M) {
try {
Database::load(DBPATH "anonymous_package.xml");
FAIL();
}
catch(const database_error &e) {
- REQUIRE(string(e.what()) == "package is anonymous");
+ REQUIRE(string(e.what()) == "empty package author");
}
}
diff --git a/test/db/v1/anonymous_package.xml b/test/db/v1/anonymous_package.xml
@@ -1,6 +1,5 @@
<index version="1">
<category name="test">
<reapack name="abc" type="script" />
- <reapack name="abc" type="script" author="" />
</category>
</index>
diff --git a/test/db/v1/invalid_type.xml b/test/db/v1/invalid_type.xml
@@ -1,6 +0,0 @@
-<index version="1">
- <category name="test">
- <reapack name="a" />
- <reapack name="c" type="yoyo" />
- </category>
-</index>
diff --git a/test/db/v1/missing_type.xml b/test/db/v1/missing_type.xml
@@ -0,0 +1,5 @@
+<index version="1">
+ <category name="test">
+ <reapack name="a" />
+ </category>
+</index>
diff --git a/test/db/v1/unnamed_category.xml b/test/db/v1/unnamed_category.xml
@@ -1,4 +1,3 @@
<index version="1">
<category />
- <category name="" />
</index>
diff --git a/test/db/v1/unnamed_package.xml b/test/db/v1/unnamed_package.xml
@@ -1,6 +1,5 @@
<index version="1">
<category name="test">
- <reapack />
- <reapack name="" />
+ <reapack type="script" />
</category>
</index>