Преглед на файлове

Improved SerializableJSONState class

- added "_clear" method
- added "_clear" method call to "setValue" method
- extended tests for SerializableJSONState class
Patrick преди 4 години
родител
ревизия
e63b3d10d7

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

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-15
- * Changed:         2020-09-17
+ * Changed:         2020-09-19
  *
  * */
 
@@ -32,6 +32,12 @@ void ls_std::SerializableJSONState::unmarshal(const ls_std::byte_field &_data)
 void ls_std::SerializableJSONState::setValue(std::shared_ptr<State> _value)
 {
   this->value = std::move(_value);
+  this->_clear();
+}
+
+void ls_std::SerializableJSONState::_clear()
+{
+  this->jsonObject.clear();
 }
 
 void ls_std::SerializableJSONState::_unmarshalExistingStateConnection(nlohmann::json _jsonObject)

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

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

+ 22 - 1
test/cases/serialization/json/logic/SerializableJSONStateTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-15
- * Changed:         2020-09-15
+ * Changed:         2020-09-19
  *
  * */
 
@@ -58,4 +58,25 @@ namespace {
     ASSERT_FALSE(x->getConnectedStates().at("AB")->isPassable());
     ASSERT_STREQ("B", x->getConnectedStates().at("AB")->getStateId().c_str());
   }
+
+  TEST_F(SerializableJSONStateTest, setValue)
+  {
+    std::shared_ptr<ls_std::State> x = std::make_shared<ls_std::State>("A");
+    x->addStateConnection(std::make_shared<ls_std::StateConnection>("AB", "B"));
+    x->addStateConnection(std::make_shared<ls_std::StateConnection>("AC", "C"));
+
+    ls_std::SerializableJSONState serializable {x};
+    ls_std::byte_field jsonString = serializable.marshal();
+
+    std::string expectedJSONString = R"({"connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"},"AC":{"condition":false,"connectionId":"AC","stateId":"C"}},"id":"A"})";
+    ASSERT_STREQ(expectedJSONString.c_str(), jsonString.c_str());
+
+    // setValue should now clear json
+
+    std::shared_ptr<ls_std::State> y = std::make_shared<ls_std::State>("B");
+    serializable.setValue(y);
+    jsonString = serializable.marshal();
+
+    ASSERT_STREQ(R"({"id":"B"})", jsonString.c_str());
+  }
 }