Browse Source

Extended String class

- implemented ISerializable interfaces
- extended tests for String class
pcmattulat 4 years ago
parent
commit
a0f21dd92f
3 changed files with 57 additions and 4 deletions
  1. 21 1
      source/boxing/String.cpp
  2. 8 2
      source/boxing/String.hpp
  3. 28 1
      test/cases/boxing/StringTest.cpp

+ 21 - 1
source/boxing/String.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-21
+ * Changed:         2020-08-30
  *
  * */
 
@@ -82,6 +82,16 @@ bool ls_std::String::operator!=(const char *_value) {
   return this->value != _value;
 }
 
+ls_std::byte_field ls_std::String::marshal() {
+  ls_std::byte_field data {};
+
+  if(this->serializable != nullptr) {
+    data = this->serializable->marshal();
+  }
+
+  return data;
+}
+
 void ls_std::String::parse(std::string _parseText) {
   this->value = std::move(_parseText);
 }
@@ -90,6 +100,12 @@ std::string ls_std::String::toString() {
   return this->value;
 }
 
+void ls_std::String::unmarshal(const ls_std::byte_field &_data) {
+  if(this->serializable != nullptr) {
+    this->serializable->unmarshal(_data);
+  }
+}
+
 bool ls_std::String::contains(const std::string& _text) {
   return this->value.find(_text) != std::string::npos;
 }
@@ -121,6 +137,10 @@ std::string ls_std::String::reverse() {
   return copy;
 }
 
+void ls_std::String::setSerializable(std::shared_ptr<ISerializable> _serializable) {
+  this->serializable = std::move(_serializable);
+}
+
 bool ls_std::String::startsWith(const std::string &_text) {
   return this->value.rfind(_text, 0) == 0;
 }

+ 8 - 2
source/boxing/String.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-21
+ * Changed:         2020-08-30
  *
  * */
 
@@ -12,10 +12,12 @@
 
 #include "IBoxing.hpp"
 #include "../base/Class.hpp"
+#include "../serialization/ISerializable.hpp"
 #include <string>
+#include <memory>
 
 namespace ls_std {
-  class String : public Class, IBoxing {
+  class String : public Class, public IBoxing, public ISerializable {
     public:
 
       String();
@@ -54,8 +56,10 @@ 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
 
@@ -66,12 +70,14 @@ namespace ls_std {
       std::string padLeft(size_t _width, const char _fillCharacter);
       std::string padRight(size_t _width, const char _fillCharacter);
       std::string reverse();
+      void setSerializable(std::shared_ptr<ISerializable> _serializable);
       bool startsWith(const std::string& _text);
       std::string toLowerCase();
       std::string toUpperCase();
 
     private:
 
+      std::shared_ptr<ISerializable> serializable {};
       std::string value {};
 
       static std::string _buildCharacterChain(size_t _amount, const char _fillCharacter);

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

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-21
+ * Changed:         2020-08-30
  *
  * */
 
 #include <gtest/gtest.h>
 #include "../../../source/boxing/String.hpp"
+#include "../../../source/serialization/boxing/SerializableJSONString.hpp"
 
 namespace {
   class StringTest : public ::testing::Test {
@@ -91,6 +92,19 @@ namespace {
 
   // implementation
 
+  TEST_F(StringTest, marshal)
+  {
+    std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>("Hello!");
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONString>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    ASSERT_STREQ(R"({"class":"String","value":"Hello!"})", x->marshal().c_str());
+
+    *x = "Test!";
+    ASSERT_STREQ(R"({"class":"String","value":"Test!"})", x->marshal().c_str());
+  }
+
   TEST_F(StringTest, parse)
   {
     ls_std::String text {};
@@ -106,6 +120,19 @@ namespace {
     ASSERT_STREQ("Hello!", text.toString().c_str());
   }
 
+  TEST_F(StringTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>("Hello!");
+    ASSERT_STREQ("Hello!", *x);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONString>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    x->unmarshal(R"({"class":"String","value":"Test!"})");
+
+    ASSERT_STREQ("Test!", *x);
+  }
+
   // additional functionality
 
   TEST_F(StringTest, contains)