Forráskód Böngészése

Improve EventHandler class error handling

- cover more error cases in EventHandler class
- increase test coverage for EventHandler class
Patrick-Christopher Mattulat 3 éve
szülő
commit
b602f640a0

+ 4 - 2
include/ls_std/event/EventHandler.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-27
- * Changed:         2021-04-23
+ * Changed:         2021-05-27
  *
  * */
 
@@ -23,7 +23,7 @@ namespace ls_std
   {
     public:
 
-      explicit EventHandler(ls_std::event_id _id);
+      explicit EventHandler(const ls_std::event_id& _id);
       ~EventHandler() override = default;
 
       ls_std::event_id getId();
@@ -31,6 +31,8 @@ namespace ls_std
     private:
 
       ls_std::event_id id{};
+
+      void _assignId(const ls_std::event_id& _id);
   };
 }
 

+ 16 - 5
source/ls_std/event/EventHandler.cpp

@@ -3,18 +3,29 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-27
- * Changed:         2021-04-23
+ * Changed:         2021-05-27
  *
  * */
 
 #include <ls_std/event/EventHandler.hpp>
+#include <ls_std/exception/IllegalArgumentException.hpp>
 
-ls_std::EventHandler::EventHandler(ls_std::event_id _id)
-    : ls_std::Narrator(),
-      id(std::move(_id))
-{}
+ls_std::EventHandler::EventHandler(const ls_std::event_id& _id) : ls_std::Narrator()
+{
+  this->_assignId(_id);
+}
 
 ls_std::event_id ls_std::EventHandler::getId()
 {
   return this->id;
 }
+
+void ls_std::EventHandler::_assignId(const ls_std::event_id &_id)
+{
+  if (_id.empty())
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->id = _id;
+}

+ 15 - 1
test/cases/event/EventHandlerTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-27
- * Changed:         2021-04-23
+ * Changed:         2021-05-27
  *
  * */
 
@@ -27,6 +27,20 @@ namespace
       {}
   };
 
+  TEST_F(EventHandlerTest, constructor_empty_parameter)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::EventHandler eventHandler{""};
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(EventHandlerTest, getId)
   {
     ls_std::EventHandler eventHandler{"EventId"};