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

Extended Boolean class

- implemented ISerializable interface
- added tests for Boolean class
pcmattulat пре 4 година
родитељ
комит
1a26a4a8c8
3 измењених фајлова са 56 додато и 3 уклоњено
  1. 21 1
      source/boxing/Boolean.cpp
  2. 7 1
      source/boxing/Boolean.hpp
  3. 28 1
      test/cases/boxing/BooleanTest.cpp

+ 21 - 1
source/boxing/Boolean.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2020-08-16
+ * Changed:         2020-09-04
  *
  * */
 
@@ -65,6 +65,16 @@ bool ls_std::Boolean::operator||(int _value) const
   return this->value || _value;
 }
 
+ls_std::byte_field ls_std::Boolean::marshal() {
+  ls_std::byte_field data {};
+
+  if(this->serializable != nullptr) {
+    data = this->serializable->marshal();
+  }
+
+  return data;
+}
+
 void ls_std::Boolean::parse(std::string _parseText)
 {
   std::transform(_parseText.begin(), _parseText.end(), _parseText.begin(), ::tolower);
@@ -87,10 +97,20 @@ std::string ls_std::Boolean::toString()
   return this->_toString();
 }
 
+void ls_std::Boolean::unmarshal(const ls_std::byte_field &_data) {
+  if(this->serializable != nullptr) {
+    this->serializable->unmarshal(_data);
+  }
+}
+
 bool ls_std::Boolean::getValue() {
   return this->value;
 }
 
+void ls_std::Boolean::setSerializable(std::shared_ptr<ISerializable> _serializable) {
+  this->serializable = std::move(_serializable);
+}
+
 bool ls_std::Boolean::XOR(const Boolean &_leftExpression, const Boolean &_rightExpression) {
   return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
 }

+ 7 - 1
source/boxing/Boolean.hpp

@@ -10,11 +10,13 @@
 #ifndef LS_STD_BOOLEAN_HPP
 #define LS_STD_BOOLEAN_HPP
 
+#include <memory>
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
+#include "../serialization/ISerializable.hpp"
 
 namespace ls_std {
-  class Boolean : public Class, IBoxing {
+  class Boolean : public Class, public IBoxing, public ISerializable {
     public:
 
       explicit Boolean(bool _value);
@@ -53,12 +55,15 @@ 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
 
       bool getValue();
+      void setSerializable(std::shared_ptr<ISerializable> _serializable);
       static bool XOR(const Boolean& _leftExpression, const Boolean& _rightExpression);
       static bool XOR(const Boolean& _leftExpression, bool _rightExpression);
       static bool XOR(bool _leftExpression, const Boolean& _rightExpression);
@@ -66,6 +71,7 @@ namespace ls_std {
 
     private:
 
+      std::shared_ptr<ISerializable> serializable {};
       bool value {};
       const std::string TRUE_STRING = "true";
       const std::string FALSE_STRING = "false";

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

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2020-08-14
+ * Changed:         2020-09-04
  *
  * */
 
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Boolean.hpp"
+#include "../../../source/serialization/boxing/SerializableJSONBoolean.hpp"
 
 namespace {
   class BooleanTest : public ::testing::Test {
@@ -106,6 +107,19 @@ namespace {
 
   // implementation
 
+  TEST_F(BooleanTest, marshal)
+  {
+    std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(true);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    ASSERT_STREQ(R"({"value":true})", x->marshal().c_str());
+
+    *x = false;
+    ASSERT_STREQ(R"({"value":false})", x->marshal().c_str());
+  }
+
   TEST_F(BooleanTest, parse)
   {
     ls_std::Boolean expression {};
@@ -143,6 +157,19 @@ namespace {
     ASSERT_STREQ("false", expression.toString().c_str());
   }
 
+  TEST_F(BooleanTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(false);
+
+    ASSERT_FALSE(*x);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+    x->unmarshal(R"({"value":true})");
+
+    ASSERT_TRUE(*x);
+  }
+
   // additional functionality
 
   TEST_F(BooleanTest, getValue)