Ver código fonte

Extended State class

- added overloaded "addStateConnection" method
- extended tests for State class
Patrick-Laptop 3 anos atrás
pai
commit
2b98041d3c
3 arquivos alterados com 27 adições e 0 exclusões
  1. 12 0
      source/logic/State.cpp
  2. 1 0
      source/logic/State.hpp
  3. 14 0
      test/cases/logic/StateTest.cpp

+ 12 - 0
source/logic/State.cpp

@@ -27,6 +27,18 @@ bool ls_std::State::addStateConnection(const StateConnectionId& _connectionId, c
   return added;
 }
 
+bool ls_std::State::addStateConnection(const std::shared_ptr<StateConnection>& _connection)
+{
+  bool added {};
+
+  if(_connection != nullptr) {
+    this->connectedStates.insert({_connection->getConnectionId(), _connection});
+    added = this->_hasConnection(_connection->getConnectionId());
+  }
+
+  return added;
+}
+
 std::unordered_map<ls_std::StateConnectionId, std::shared_ptr<ls_std::StateConnection>> ls_std::State::getConnectedStates()
 {
   return this->connectedStates;

+ 1 - 0
source/logic/State.hpp

@@ -24,6 +24,7 @@ namespace ls_std {
       ~State() = default;
 
       bool addStateConnection(const StateConnectionId& _connectionId, const std::shared_ptr<State>& _connectedState);
+      bool addStateConnection(const std::shared_ptr<StateConnection>& _connection);
       std::unordered_map<StateConnectionId, std::shared_ptr<StateConnection>> getConnectedStates();
       StateId getId();
       bool hasConnection(const StateConnectionId& _connectionId);

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

@@ -40,6 +40,20 @@ namespace {
     ASSERT_FALSE(stateA.addStateConnection("XX", nullptr));
   }
 
+  TEST_F(StateTest, addStateConnectionV2)
+  {
+    ls_std::State stateA {"A"};
+    ls_std::State stateB {"B"};
+
+    ASSERT_TRUE(stateA.addStateConnection(std::make_shared<ls_std::StateConnection>("AB", stateB.getId())));
+  }
+
+  TEST_F(StateTest, addStateConnectionV2Negative)
+  {
+    ls_std::State stateA {"A"};
+    ASSERT_FALSE(stateA.addStateConnection(nullptr));
+  }
+
   TEST_F(StateTest, getConnectedStates)
   {
     ls_std::State stateA {"A"};