Kaynağa Gözat

Completed Version class implementation

- added missing class functionality
- added tests for Version class
Patrick-Laptop 4 yıl önce
ebeveyn
işleme
703b3ad461

+ 2 - 1
CMakeLists.txt

@@ -162,7 +162,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/xml/logic/SerializableXMLStateConnectionTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/xml/logic/SerializableXMLStateTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLAttributeTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLNodeTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLNodeTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/base/VersionTest.cpp)
 
 ##########################################################
 # Build

+ 48 - 0
source/base/Version.cpp

@@ -7,6 +7,7 @@
  *
  * */
 
+#include <regex>
 #include "Version.hpp"
 
 ls_std::Version::Version(version_type _major, version_type _minor, version_type _patch) :
@@ -28,7 +29,23 @@ ls_std::byte_field ls_std::Version::marshal()
 
 void ls_std::Version::unmarshal(const ls_std::byte_field &_data)
 {
+  std::string field = _data;
+  std::string subSequence {};
+  size_t position;
 
+  if(ls_std::Version::_isValid(_data)) {
+    position = field.find('.');
+    subSequence = field.substr(0, position);
+    this->major = std::stoi(subSequence);
+    field.erase(0, position + 1);
+
+    position = field.find('.');
+    subSequence = field.substr(0, position);
+    this->minor = std::stoi(subSequence);
+    field.erase(0, position + 1);
+
+    this->patch = std::stoi(field);
+  }
 }
 
 ls_std::version_type ls_std::Version::getMajor() const
@@ -40,3 +57,34 @@ ls_std::version_type ls_std::Version::getMinor() const
 {
   return this->minor;
 }
+
+ls_std::version_type ls_std::Version::getPatch() const
+{
+  return this->patch;
+}
+
+bool ls_std::Version::isValid(const std::string &_versionString)
+{
+  return ls_std::Version::_isValid(_versionString);
+}
+
+void ls_std::Version::setMajor(version_type _major)
+{
+  this->major = _major;
+}
+
+void ls_std::Version::setMinor(version_type _minor)
+{
+  this->minor = _minor;
+}
+
+void ls_std::Version::setPatch(version_type _patch)
+{
+  this->patch = _patch;
+}
+
+bool ls_std::Version::_isValid(const std::string &_versionString)
+{
+  static std::regex versionRegex {R"(\d+[.]\d+[.]\d+)"};
+  return std::regex_match(_versionString.begin(), _versionString.end(), versionRegex);
+}

+ 9 - 2
source/base/Version.hpp

@@ -16,15 +16,22 @@
 
 namespace ls_std {
   class Version : public ISerializable {
+    public:
+
       explicit Version(version_type _major, version_type _minor, version_type _patch);
       ~Version() = default;
 
+      // implementation
+
       ls_std::byte_field marshal() override;
       void unmarshal(const ls_std::byte_field& _data) override;
 
+      // other functionality
+
       version_type getMajor() const;
       version_type getMinor() const;
-      version_type getPatch();
+      version_type getPatch() const;
+      static bool isValid(const std::string& _versionString);
       void setMajor(version_type _major);
       void setMinor(version_type _minor);
       void setPatch(version_type _patch);
@@ -35,7 +42,7 @@ namespace ls_std {
       version_type minor {};
       version_type patch {};
 
-      bool _isValidVersionString(const std::string& _versionString);
+      static bool _isValid(const std::string& _versionString);
   };
 }
 

+ 105 - 0
test/cases/base/VersionTest.cpp

@@ -0,0 +1,105 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-28
+ * Changed:         2020-09-28
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../source/base/Version.hpp"
+
+namespace {
+  class VersionTest : public ::testing::Test {
+    protected:
+
+      VersionTest() = default;
+      ~VersionTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  // implementation
+
+  TEST_F(VersionTest, marshal)
+  {
+    ls_std::Version version {2020, 2, 0};
+    ASSERT_STREQ("2020.2.0", version.marshal().c_str());
+  }
+
+  TEST_F(VersionTest, unmarshal)
+  {
+    ls_std::Version version {0, 0, 0};
+    version.unmarshal("2020.2.13");
+
+    ASSERT_EQ(2020, version.getMajor());
+    ASSERT_EQ(2, version.getMinor());
+    ASSERT_EQ(13, version.getPatch());
+  }
+
+  // other functionality
+
+  TEST_F(VersionTest, getMajor)
+  {
+    ls_std::Version version {13, 2, 4};
+    ASSERT_EQ(13, version.getMajor());
+  }
+
+  TEST_F(VersionTest, getMinor)
+  {
+    ls_std::Version version {13, 2, 4};
+    ASSERT_EQ(2, version.getMinor());
+  }
+
+  TEST_F(VersionTest, getPatch)
+  {
+    ls_std::Version version {13, 2, 4};
+    ASSERT_EQ(4, version.getPatch());
+  }
+
+  TEST_F(VersionTest, isValid)
+  {
+    ASSERT_TRUE(ls_std::Version::isValid("2020.1.2"));
+    ASSERT_TRUE(ls_std::Version::isValid("2.5.1"));
+  }
+
+  TEST_F(VersionTest, isValidNegative)
+  {
+    ASSERT_FALSE(ls_std::Version::isValid("v2020.1.2"));
+    ASSERT_FALSE(ls_std::Version::isValid("2.5"));
+    ASSERT_FALSE(ls_std::Version::isValid("2020"));
+    ASSERT_FALSE(ls_std::Version::isValid("blaaaa"));
+  }
+
+  TEST_F(VersionTest, setMajor)
+  {
+    ls_std::Version version {13, 2, 4};
+    ASSERT_EQ(13, version.getMajor());
+
+    version.setMajor(14);
+    ASSERT_EQ(14, version.getMajor());
+  }
+
+  TEST_F(VersionTest, setMinor)
+  {
+    ls_std::Version version {13, 2, 4};
+    ASSERT_EQ(2, version.getMinor());
+
+    version.setMinor(3);
+    ASSERT_EQ(3, version.getMinor());
+  }
+
+  TEST_F(VersionTest, setPatch)
+  {
+    ls_std::Version version {13, 2, 4};
+    ASSERT_EQ(4, version.getPatch());
+
+    version.setPatch(5);
+    ASSERT_EQ(5, version.getPatch());
+  }
+}