Ver Fonte

Implemented State class

- Implemented State class, which will later be used by
StateMachine class
- added tests for State class
Patrick-Laptop há 4 anos atrás
pai
commit
526d34929c
4 ficheiros alterados com 174 adições e 5 exclusões
  1. 2 1
      CMakeLists.txt
  2. 63 1
      source/logic/State.cpp
  3. 14 3
      source/logic/State.hpp
  4. 95 0
      test/cases/logic/StateTest.cpp

+ 2 - 1
CMakeLists.txt

@@ -121,7 +121,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONStringTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONFloatTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONDoubleTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONBooleanTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONBooleanTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateTest.cpp)
 
 ##########################################################
 # Build

+ 63 - 1
source/logic/State.cpp

@@ -9,5 +9,67 @@
 
 #include "State.hpp"
 
-ls_std::State::State() : Class("State")
+ls_std::State::State(std::string  _id) : Class("State"),
+id(std::move(_id))
 {}
+
+bool ls_std::State::addStateConnection(const std::shared_ptr<State>& _connectedState)
+{
+  bool added {};
+
+  if(_connectedState != nullptr && !this->_stateIsConnected(_connectedState->getId())) {
+    this->connectedStates.emplace_back(_connectedState);
+    added = true;
+  }
+
+  return added;
+}
+
+std::list<std::shared_ptr<ls_std::State>> ls_std::State::getConnectedStates()
+{
+  return this->connectedStates;
+}
+
+std::string ls_std::State::getId()
+{
+  return this->id;
+}
+
+bool ls_std::State::isAccessible() const
+{
+  return this->accessCondition;
+}
+
+void ls_std::State::removeStateConnection(const std::string &_id)
+{
+  auto iterator = this->connectedStates.begin();
+
+  while(iterator != this->connectedStates.end()) {
+    if(iterator->get()->getId() == _id) {
+      this->connectedStates.erase(iterator);
+      break;
+    }
+
+    iterator++;
+  }
+}
+
+void ls_std::State::updateAccessCondition(bool _enteredCondition)
+{
+  this->accessCondition = _enteredCondition;
+}
+
+bool ls_std::State::_stateIsConnected(const std::string &_id)
+{
+  bool connected {};
+
+  for(const std::shared_ptr<State>& state : this->connectedStates) {
+    connected = state->getId() == _id;
+
+    if(connected) {
+      break;
+    }
+  }
+
+  return connected;
+}

+ 14 - 3
source/logic/State.hpp

@@ -11,19 +11,30 @@
 #define LS_STD_STATE_HPP
 
 #include <memory>
-#include <vector>
+#include <list>
 #include "../base/Class.hpp"
 
 namespace ls_std {
   class State : public Class {
     public:
 
-      State();
+      explicit State(std::string  _id);
       ~State() = default;
 
+      bool addStateConnection(const std::shared_ptr<State>& _connectedState);
+      std::list<std::shared_ptr<State>> getConnectedStates();
+      std::string getId();
+      bool isAccessible() const;
+      void removeStateConnection(const std::string& _id);
+      void updateAccessCondition(bool _enteredCondition);
+
     private:
 
-      std::vector<std::shared_ptr<State>> connectedStates {};
+      bool accessCondition {};
+      std::list<std::shared_ptr<State>> connectedStates {};
+      std::string id {};
+
+      bool _stateIsConnected(const std::string& _id);
   };
 }
 

+ 95 - 0
test/cases/logic/StateTest.cpp

@@ -0,0 +1,95 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-05
+ * Changed:         2020-09-05
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include <memory>
+#include "../../../source/logic/State.hpp"
+
+namespace {
+  class StateTest : public ::testing::Test {
+    protected:
+
+      StateTest() = default;
+      ~StateTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(StateTest, addStateConnection)
+  {
+    ls_std::State stateA {"A"};
+    ls_std::State stateB {"B"};
+
+    ASSERT_TRUE(stateA.addStateConnection(std::make_shared<ls_std::State>(stateB)));
+  }
+
+  TEST_F(StateTest, addStateConnectionNegative)
+  {
+    ls_std::State stateA {"A"};
+    ls_std::State stateB {"B"};
+
+    ASSERT_TRUE(stateA.addStateConnection(std::make_shared<ls_std::State>(stateB)));
+    ASSERT_FALSE(stateA.addStateConnection(std::make_shared<ls_std::State>(stateB)));
+    ASSERT_FALSE(stateA.addStateConnection(nullptr));
+  }
+
+  TEST_F(StateTest, getConnectedStates)
+  {
+    ls_std::State stateA {"A"};
+    ls_std::State stateB {"B"};
+
+    ASSERT_TRUE(stateA.addStateConnection(std::make_shared<ls_std::State>(stateB)));
+    ASSERT_FALSE(stateA.getConnectedStates().empty());
+    ASSERT_EQ(1, stateA.getConnectedStates().size());
+  }
+
+  TEST_F(StateTest, getConnectedStatesNegative)
+  {
+    ls_std::State stateA {"A"};
+
+    ASSERT_TRUE(stateA.getConnectedStates().empty());
+    ASSERT_EQ(0, stateA.getConnectedStates().size());
+  }
+
+  TEST_F(StateTest, getId)
+  {
+    ls_std::State stateA {"A"};
+    ASSERT_STREQ("A", stateA.getId().c_str());
+  }
+
+  TEST_F(StateTest, isAccessible)
+  {
+    ls_std::State stateA {"A"};
+    ASSERT_FALSE(stateA.isAccessible());
+  }
+
+  TEST_F(StateTest, removeStateConnection)
+  {
+    ls_std::State stateA {"A"};
+    ls_std::State stateB {"B"};
+
+    ASSERT_TRUE(stateA.addStateConnection(std::make_shared<ls_std::State>(stateB)));
+    ASSERT_FALSE(stateA.getConnectedStates().empty());
+    ASSERT_EQ(1, stateA.getConnectedStates().size());
+
+    stateA.removeStateConnection("B");
+    ASSERT_TRUE(stateA.getConnectedStates().empty());
+    ASSERT_EQ(0, stateA.getConnectedStates().size());
+  }
+
+  TEST_F(StateTest, updateAccessCondition)
+  {
+    ls_std::State stateA {"A"};
+    ASSERT_FALSE(stateA.isAccessible());
+
+    stateA.updateAccessCondition(true);
+    ASSERT_TRUE(stateA.isAccessible());
+  }
+}