Преглед изворни кода

Extended Long class

- implemented ISerializable interface
- extended tests of Long class
pcmattulat пре 4 година
родитељ
комит
fc21d875f5
3 измењених фајлова са 61 додато и 4 уклоњено
  1. 25 1
      source/boxing/Long.cpp
  2. 8 2
      source/boxing/Long.hpp
  3. 28 1
      test/cases/boxing/LongTest.cpp

+ 25 - 1
source/boxing/Long.cpp

@@ -3,11 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-26
+ * Changed:         2020-08-27
  *
  * */
 
 #include "Long.hpp"
+
+#include <utility>
 #include "../exception/IllegalArithmeticOperationException.hpp"
 
 ls_std::Long::Long(ls_std::long_type _value) : Class("Long"),
@@ -247,6 +249,17 @@ void ls_std::Long::operator--()
   this->value -= 1;
 }
 
+ls_std::byte_field ls_std::Long::marshal()
+{
+  ls_std::byte_field data {};
+
+  if(this->serializable != nullptr) {
+    data = this->serializable->marshal();
+  }
+
+  return data;
+}
+
 void ls_std::Long::parse(std::string _parseText)
 {
   this->value = std::stoi(_parseText);
@@ -257,6 +270,17 @@ std::string ls_std::Long::toString()
   return std::to_string(this->value);
 }
 
+void ls_std::Long::unmarshal(const ls_std::byte_field& _data)
+{
+  if(this->serializable != nullptr) {
+    this->serializable->unmarshal(_data);
+  }
+}
+
 ls_std::long_type ls_std::Long::getValue() const {
   return this->value;
 }
+
+void ls_std::Long::setSerializable(std::shared_ptr<ISerializable> _serializable) {
+  this->serializable = std::move(_serializable);
+}

+ 8 - 2
source/boxing/Long.hpp

@@ -3,19 +3,21 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-26
+ * Changed:         2020-08-27
  *
  * */
 
 #ifndef LONG_HPP
 #define LONG_HPP
 
+#include <memory>
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
 #include "../base/Types.hpp"
+#include "../serialization/ISerializable.hpp"
 
 namespace ls_std {
-  class Long : public Class, IBoxing {
+  class Long : public Class, public IBoxing, public ISerializable {
     public:
 
       explicit Long(ls_std::long_type _value);
@@ -90,15 +92,19 @@ namespace ls_std {
 
       // implementation
 
+      ls_std::byte_field marshal() override;
       void parse(std::string _parseText) override;
       std::string toString() override;
+      void unmarshal(const ls_std::byte_field& _data) override;
 
       // additional functionality
 
       ls_std::long_type getValue() const;
+      void setSerializable(std::shared_ptr<ISerializable> _serializable);
 
     private:
 
+      std::shared_ptr<ISerializable> serializable {};
       ls_std::long_type value {};
   };
 }

+ 28 - 1
test/cases/boxing/LongTest.cpp

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-26
+ * Changed:         2020-08-27
  *
  * */
 
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Long.hpp"
+#include "../../../source/serialization/boxing/SerializableJSONLong.hpp"
 
 namespace {
   class LongTest : public ::testing::Test {
@@ -337,6 +338,19 @@ namespace {
 
   // implementation
 
+  TEST_F(LongTest, marshal)
+  {
+    std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>(3);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONLong>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    ASSERT_STREQ(R"({"class":"Long","value":3})", x->marshal().c_str());
+
+    *x = 17;
+    ASSERT_STREQ(R"({"class":"Long","value":17})", x->marshal().c_str());
+  }
+
   TEST_F(LongTest, parse)
   {
     ls_std::Long x {};
@@ -354,6 +368,19 @@ namespace {
     ASSERT_STREQ("112", x.toString().c_str());
   }
 
+  TEST_F(LongTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>(13);
+    ASSERT_EQ(13, *x);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONLong>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    x->unmarshal(R"({"class":"Long","value":1989})");
+
+    ASSERT_EQ(1989, *x);
+  }
+
   // additional functionality
 
   TEST_F(LongTest, getValue)