Selaa lähdekoodia

Improve StateConnection class

- add additional error checks to StateConnection class
- increase test coverage for StateConnection class
Patrick-Christopher Mattulat 3 vuotta sitten
vanhempi
commit
e6ed24728d

+ 7 - 4
include/ls_std/logic/StateConnection.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-10
- * Changed:         2021-05-01
+ * Changed:         2021-05-27
  *
  * */
 
@@ -20,14 +20,14 @@ namespace ls_std
   {
     public:
 
-      explicit StateConnection(ls_std::StateConnectionId _connectionId, ls_std::StateId _stateId);
+      explicit StateConnection(const ls_std::StateConnectionId& _connectionId, const ls_std::StateId& _stateId);
       ~StateConnection() override = default;
 
       StateConnectionId getConnectionId();
       ls_std::StateId getStateId();
       bool isPassable() const;
-      void setConnectionId(ls_std::StateConnectionId _connectionId);
-      void setStateId(ls_std::StateId _stateId);
+      void setConnectionId(const ls_std::StateConnectionId& _connectionId);
+      void setStateId(const ls_std::StateId& _stateId);
       void updatePassCondition(bool _condition);
 
     private:
@@ -35,6 +35,9 @@ namespace ls_std
       bool condition{};
       ls_std::StateConnectionId connectionId{};
       ls_std::StateId stateId{};
+
+      void _assignConnectionId(const ls_std::StateConnectionId& _connectionId);
+      void _assignStateId(const ls_std::StateId& _stateId);
   };
 }
 

+ 31 - 10
source/ls_std/logic/StateConnection.cpp

@@ -3,17 +3,18 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-10
- * Changed:         2021-04-23
+ * Changed:         2021-05-27
  *
  * */
 
 #include <ls_std/logic/StateConnection.hpp>
+#include <ls_std/exception/IllegalArgumentException.hpp>
 
-ls_std::StateConnection::StateConnection(ls_std::StateConnectionId _connectionId, ls_std::StateId _stateId)
-    : ls_std::Class("StateConnection"),
-      connectionId(std::move(_connectionId)),
-      stateId(std::move(_stateId))
-{}
+ls_std::StateConnection::StateConnection(const ls_std::StateConnectionId& _connectionId, const ls_std::StateId& _stateId) : ls_std::Class("StateConnection")
+{
+  this->_assignConnectionId(_connectionId);
+  this->_assignStateId(_stateId);
+}
 
 ls_std::StateConnectionId ls_std::StateConnection::getConnectionId()
 {
@@ -30,17 +31,37 @@ bool ls_std::StateConnection::isPassable() const
   return this->condition;
 }
 
-void ls_std::StateConnection::setConnectionId(ls_std::StateConnectionId _connectionId)
+void ls_std::StateConnection::setConnectionId(const ls_std::StateConnectionId& _connectionId)
 {
-  this->connectionId = std::move(_connectionId);
+  this->_assignConnectionId(_connectionId);
 }
 
-void ls_std::StateConnection::setStateId(ls_std::StateId _stateId)
+void ls_std::StateConnection::setStateId(const ls_std::StateId& _stateId)
 {
-  this->stateId = std::move(_stateId);
+  this->_assignStateId(_stateId);
 }
 
 void ls_std::StateConnection::updatePassCondition(bool _condition)
 {
   this->condition = _condition;
 }
+
+void ls_std::StateConnection::_assignConnectionId(const ls_std::StateConnectionId &_connectionId)
+{
+  if (_connectionId.empty())
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->connectionId = _connectionId;
+}
+
+void ls_std::StateConnection::_assignStateId(const ls_std::StateId &_stateId)
+{
+  if (_stateId.empty())
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->stateId = _stateId;
+}

+ 66 - 2
test/cases/logic/StateConnectionTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-10
- * Changed:         2021-04-23
+ * Changed:         2021-05-27
  *
  * */
 
@@ -26,13 +26,47 @@ namespace
       {}
   };
 
+  TEST_F(StateConnectionTest, getClassName)
+  {
+    ls_std::StateConnection connection{"AB", "B"};
+    ASSERT_STREQ("StateConnection", connection.getClassName().c_str());
+  }
+
+  TEST_F(StateConnectionTest, constructor_empty_connection_id)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::StateConnection connection = ls_std::StateConnection("", "B");
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
+  TEST_F(StateConnectionTest, constructor_empty_state_id)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::StateConnection connection = ls_std::StateConnection("AB", "");
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(StateConnectionTest, getConnectionId)
   {
     ls_std::StateConnection connection{"AB", "B"};
     ASSERT_STREQ("AB", connection.getConnectionId().c_str());
   }
 
-  TEST_F(StateConnectionTest, getState)
+  TEST_F(StateConnectionTest, getStateId)
   {
     ls_std::StateConnection connection{"AB", "B"};
     ASSERT_STREQ("B", connection.getStateId().c_str());
@@ -53,6 +87,21 @@ namespace
     ASSERT_STREQ("BC", connection.getConnectionId().c_str());
   }
 
+  TEST_F(StateConnectionTest, setConnectionId_empty_connection_id)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::StateConnection connection = ls_std::StateConnection("AB", "B");
+                     connection.setConnectionId("");
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(StateConnectionTest, setStateId)
   {
     ls_std::StateConnection connection{"AB", "B"};
@@ -62,6 +111,21 @@ namespace
     ASSERT_STREQ("C", connection.getStateId().c_str());
   }
 
+  TEST_F(StateConnectionTest, setStateId_empty_state_id)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::StateConnection connection = ls_std::StateConnection("AB", "B");
+                     connection.setStateId("");
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(StateConnectionTest, updatePassCondition)
   {
     ls_std::StateConnection connection{"AB", "B"};