Explorar o código

Improve Class class

- add class name check
- extend Class tests
- introduce Google Test mocking framework
Patrick-Christopher Mattulat %!s(int64=3) %!d(string=hai) anos
pai
achega
384cb96d17

+ 3 - 2
CMakeLists.txt

@@ -32,6 +32,7 @@ message("${PROJECT_NAME}: Adding include directories...")
 if (${LS_STD_BUILD_WITH_TESTS})
     include_directories(${CMAKE_CURRENT_SOURCE_DIR}/test)
     include_directories(${CMAKE_CURRENT_LIST_DIR}/test/lib/googletest-1.8.1/googletest/include)
+    include_directories(${CMAKE_CURRENT_LIST_DIR}/test/lib/googletest-1.8.1/googlemock/include)
 endif ()
 
 include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
@@ -209,10 +210,10 @@ if (${LS_STD_BUILD_WITH_TESTS})
         message("${PROJECT_NAME}: library search - ${WSOCK32_LIBRARY}...")
         message("${PROJECT_NAME}: library search - ${WS2_32_LIBRARY}...")
 
-        target_link_libraries(${PROJECT_NAME}_test gtest gtest_main "${WSOCK32_LIBRARY}" "${WS2_32_LIBRARY}" "${PROJECT_NAME}_${PROJECT_VERSION}_static")
+        target_link_libraries(${PROJECT_NAME}_test gtest gmock gtest_main "${WSOCK32_LIBRARY}" "${WS2_32_LIBRARY}" "${PROJECT_NAME}_${PROJECT_VERSION}_static")
     endif ()
 
     if (UNIX)
-        target_link_libraries(${PROJECT_NAME}_test gtest gtest_main "${PROJECT_NAME}_${PROJECT_VERSION}_static")
+        target_link_libraries(${PROJECT_NAME}_test gtest gmock gtest_main "${PROJECT_NAME}_${PROJECT_VERSION}_static")
     endif ()
 endif ()

+ 4 - 3
include/ls_std/base/Class.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2021-04-23
+ * Changed:         2021-04-24
  *
  * */
 
@@ -18,8 +18,7 @@ namespace ls_std
   {
     public:
 
-      explicit Class(std::string _name);
-      Class();
+      explicit Class(const std::string &_name);
       virtual ~Class() = default;
 
       std::string getClassName();
@@ -27,6 +26,8 @@ namespace ls_std
     private:
 
       std::string name{};
+
+      void _assignClassName(const std::string &_name);
   };
 }
 

+ 15 - 6
source/ls_std/base/Class.cpp

@@ -3,20 +3,29 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2020-11-25
+ * Changed:         2021-04-24
  *
  * */
 
 #include <ls_std/base/Class.hpp>
+#include <ls_std/exception/IllegalArgumentException.hpp>
 
-ls_std::Class::Class(std::string _name):
-name(std::move(_name))
-{}
+ls_std::Class::Class(const std::string &_name)
+{
+  this->_assignClassName(_name);
+}
 
 std::string ls_std::Class::getClassName()
 {
   return this->name;
 }
 
-ls_std::Class::Class()
-{}
+void ls_std::Class::_assignClassName(const std::string &_name)
+{
+  if (_name.empty())
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->name = _name;
+}

+ 22 - 1
test/cases/base/ClassTest.cpp

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-16
- * Changed:         2021-04-23
+ * Changed:         2021-04-24
  *
  * */
 
 #include <gtest/gtest.h>
 #include <ls_std/ls_std.hpp>
+#include <ls_std_test.hpp>
 
 namespace
 {
@@ -26,9 +27,29 @@ namespace
       {}
   };
 
+  TEST_F(ClassTest, Destructor)
+  {
+    std::shared_ptr<ls_std_test::ClassMock> object = std::make_shared<ls_std_test::ClassMock>();
+    EXPECT_CALL(*object, Die());
+  }
+
   TEST_F(ClassTest, getClassName)
   {
     ls_std::Class object{"Class"};
     ASSERT_STREQ("Class", object.getClassName().c_str());
   }
+
+  TEST_F(ClassTest, getClassName_emptyName)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::Class object{""};
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
 }

+ 28 - 0
test/classes/base/ClassMock.hpp

@@ -0,0 +1,28 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2021-04-24
+ * Changed:         2021-04-24
+ *
+ * */
+
+#ifndef LS_STD_CLASS_MOCK_HPP
+#define LS_STD_CLASS_MOCK_HPP
+
+#include <ls_std/ls_std.hpp>
+#include <gmock/gmock.h>
+
+namespace ls_std_test
+{
+  class ClassMock : public ls_std::Class
+  {
+    public:
+
+      ClassMock() : ls_std::Class("ClassMock") {};
+      MOCK_METHOD0(Die, void());
+      ~ClassMock() override { Die(); }
+  };
+}
+
+#endif

+ 1 - 1
test/classes/factory/SerializableTestFactory.cpp

@@ -11,5 +11,5 @@
 
 std::shared_ptr<ls_std::Class> ls_std_test::SerializableTestFactory::build()
 {
-  return std::make_shared<ls_std::Class>();
+  return std::make_shared<ls_std::Class>("TestClass");
 }

+ 2 - 0
test/ls_std_test.hpp

@@ -10,6 +10,8 @@
 #ifndef LS_STD_LS_STD_TEST_HPP
 #define LS_STD_LS_STD_TEST_HPP
 
+#include "classes/base/ClassMock.hpp"
+
 #include "classes/event/NewsAgency.hpp"
 #include "classes/event/DailyNewsAgency.hpp"
 #include "classes/event/GossipNewsAgency.hpp"