Browse Source

Improved SerializableJSONStateConnection class

- added "_clear" method
- added "_clear" method call to "setValue" method
- extended tests for
SerializableJSONStateConnection class
Patrick 4 years ago
parent
commit
1901cb1dbb

+ 7 - 1
source/serialization/json/logic/SerializableJSONStateConnection.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-14
- * Changed:         2020-09-15
+ * Changed:         2020-09-19
  *
  * */
 
@@ -35,6 +35,12 @@ void ls_std::SerializableJSONStateConnection::unmarshal(const ls_std::byte_field
 void ls_std::SerializableJSONStateConnection::setValue(std::shared_ptr<ls_std::StateConnection> _value)
 {
   this->value = std::move(_value);
+  this->_clear();
+}
+
+void ls_std::SerializableJSONStateConnection::_clear()
+{
+  this->jsonObject.clear();
 }
 
 void ls_std::SerializableJSONStateConnection::_update()

+ 2 - 1
source/serialization/json/logic/SerializableJSONStateConnection.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-14
- * Changed:         2020-09-15
+ * Changed:         2020-09-19
  *
  * */
 
@@ -37,6 +37,7 @@ namespace ls_std {
       nlohmann::json jsonObject {};
       std::shared_ptr<ls_std::StateConnection> value {};
 
+      void _clear();
       void _update();
   };
 }

+ 18 - 1
test/cases/serialization/json/logic/SerializableJSONStateConnectionTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-14
- * Changed:         2020-09-15
+ * Changed:         2020-09-19
  *
  * */
 
@@ -48,4 +48,21 @@ namespace {
     ASSERT_STREQ("C", x->getStateId().c_str());
     ASSERT_TRUE(x->isPassable());
   }
+
+  TEST_F(SerializableJSONStateConnectionTest, setValue)
+  {
+    ls_std::StateConnection x {"AB", "B"};
+    ls_std::SerializableJSONStateConnection serializable {std::make_shared<ls_std::StateConnection>(x)};
+    ls_std::String jsonString {serializable.marshal()};
+
+    ASSERT_STREQ(R"({"condition":false,"connectionId":"AB","stateId":"B"})", jsonString.toString().c_str());
+
+    // set value should now reset json
+
+    ls_std::StateConnection y {"BC", "C"};
+    serializable.setValue(std::make_shared<ls_std::StateConnection>(y));
+    jsonString = serializable.marshal();
+
+    ASSERT_STREQ(R"({"condition":false,"connectionId":"BC","stateId":"C"})", jsonString.toString().c_str());
+  }
 }