commit 5a90504a1bccf7d4fbd0215b353f7c2783c18c3b
parent 20eddbe2d8091e27c384d3e190f5877e58d4ec1f
Author: cfillion <cfillion@users.noreply.github.com>
Date: Fri, 8 Apr 2016 00:16:43 -0400
refactoring over previous commit 20eddbe2d8091e27c384d3e190f5877e58d4ec1f
Diffstat:
4 files changed, 73 insertions(+), 22 deletions(-)
diff --git a/src/registry.cpp b/src/registry.cpp
@@ -207,10 +207,7 @@ auto Registry::getEntry(const Package *pkg) const -> Entry
entry.category = m_findEntry->stringColumn(col++);
entry.package = m_findEntry->stringColumn(col++);
entry.type = static_cast<Package::Type>(m_findEntry->intColumn(col++));
-
- try { entry.version = Version(m_findEntry->stringColumn(col++)); }
- catch(const reapack_error &) {}
-
+ entry.version.tryParse(m_findEntry->stringColumn(col++));
entry.version.setAuthor(m_findEntry->stringColumn(col++));
return false;
@@ -233,10 +230,7 @@ auto Registry::getEntries(const string &remoteName) const -> vector<Entry>
entry.category = m_allEntries->stringColumn(col++);
entry.package = m_allEntries->stringColumn(col++);
entry.type = static_cast<Package::Type>(m_allEntries->intColumn(col++));
-
- try { entry.version = Version(m_allEntries->stringColumn(col++)); }
- catch(const reapack_error &) {}
-
+ entry.version.tryParse(m_allEntries->stringColumn(col++));
entry.version.setAuthor(m_allEntries->stringColumn(col++));
list.push_back(entry);
diff --git a/src/version.cpp b/src/version.cpp
@@ -31,8 +31,26 @@ Version::Version()
{
}
-Version::Version(const std::string &str, const Package *pkg)
- : m_name(str), m_code(0), m_time(), m_package(pkg), m_mainSource(nullptr)
+Version::Version(const string &str, const Package *pkg)
+ : m_code(0), m_time(), m_package(pkg), m_mainSource(nullptr)
+{
+ parse(str);
+}
+
+Version::Version(const Version &o, const Package *pkg)
+ : m_name(o.name()), m_code(o.code()),
+ m_author(o.author()), m_changelog(o.changelog()), m_time(o.time()),
+ m_package(pkg), m_mainSource(nullptr)
+{
+}
+
+Version::~Version()
+{
+ for(const auto &pair : m_sources)
+ delete pair.second;
+}
+
+void Version::parse(const string &str)
{
static const regex pattern("(\\d+)");
@@ -56,19 +74,19 @@ Version::Version(const std::string &str, const Package *pkg)
m_code += stoi(match) * (Code)pow(10000, size - index - 1);
}
-}
-Version::Version(const Version &o, const Package *pkg)
- : m_name(o.name()), m_code(o.code()),
- m_author(o.author()), m_changelog(o.changelog()), m_time(o.time()),
- m_package(pkg), m_mainSource(nullptr)
-{
+ m_name = str;
}
-Version::~Version()
+bool Version::tryParse(const string &str)
{
- for(const auto &pair : m_sources)
- delete pair.second;
+ try {
+ parse(str);
+ return true;
+ }
+ catch(const reapack_error &) {
+ return false;
+ }
}
string Version::fullName() const
@@ -125,7 +143,7 @@ void Version::addSource(Source *source)
m_mainSource = source;
}
-void Version::setChangelog(const std::string &changelog)
+void Version::setChangelog(const string &changelog)
{
m_changelog = changelog;
}
diff --git a/src/version.hpp b/src/version.hpp
@@ -36,6 +36,9 @@ public:
Version(const Version &, const Package * = nullptr);
~Version();
+ void parse(const std::string &);
+ bool tryParse(const std::string &);
+
const std::string &name() const { return m_name; }
std::string fullName() const;
uint64_t code() const { return m_code; }
@@ -60,12 +63,12 @@ public:
const std::set<Path> &files() const { return m_files; }
- bool operator==(const Version &) const;
- bool operator!=(const Version &) const;
bool operator<(const Version &) const;
bool operator<=(const Version &) const;
bool operator>(const Version &) const;
bool operator>=(const Version &) const;
+ bool operator==(const Version &) const;
+ bool operator!=(const Version &) const;
private:
std::string m_name;
diff --git a/test/version.cpp b/test/version.cpp
@@ -236,6 +236,42 @@ TEST_CASE("construct null version", M) {
REQUIRE(ver.mainSource() == nullptr);
}
+TEST_CASE("parse version", M) {
+ Version ver;
+
+ SECTION("valid") {
+ ver.parse("1.0");
+ REQUIRE(ver.name() == "1.0");
+ REQUIRE(ver.code() == UINT64_C(1000000000000));
+ }
+
+ SECTION("invalid") {
+ try { ver.parse("hello"); FAIL(); }
+ catch(const reapack_error &) {}
+
+ REQUIRE(ver.name().empty());
+ REQUIRE(ver.code() == 0);
+ }
+}
+
+TEST_CASE("parse version failsafe", M) {
+ Version ver;
+
+ SECTION("valid") {
+ REQUIRE(ver.tryParse("1.0"));
+
+ REQUIRE(ver.name() == "1.0");
+ REQUIRE(ver.code() == UINT64_C(1000000000000));
+ }
+
+ SECTION("invalid") {
+ REQUIRE_FALSE(ver.tryParse("hello"));
+
+ REQUIRE(ver.name().empty());
+ REQUIRE(ver.code() == 0);
+ }
+}
+
TEST_CASE("copy version constructor", M) {
const Package pkg(Package::UnknownType, "Hello");