Browse Source

Address SonarLint findings in TestHelper class

Patrick-Christopher Mattulat 1 year ago
parent
commit
dc14c501a7

+ 6 - 0
CMakeLists.txt

@@ -475,6 +475,7 @@ endif ()
 if (${LS_STD_BUILD_WITH_TESTS})
     message("${MODULE_NAME_IO}: Building unit tests...")
     add_executable(${MODULE_NAME_IO}-unit-test
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestException.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestHelper.cpp
             ${UNIT_TEST_FILES_IO})
 endif ()
@@ -496,6 +497,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
     if (${LS_STD_BUILD_WITH_JNI})
         message("${PROJECT_NAME}: Building unit tests with JNI support...")
         add_executable(${PROJECT_NAME}-unit-test
+                ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestException.cpp
                 ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestHelper.cpp
                 ${UNIT_TEST_FILES_BOXING}
                 ${UNIT_TEST_FILES_CORE}
@@ -508,6 +510,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
     else ()
         message("${PROJECT_NAME}: Building unit tests...")
         add_executable(${PROJECT_NAME}-unit-test
+                ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestException.cpp
                 ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestHelper.cpp
                 ${UNIT_TEST_FILES_BOXING}
                 ${UNIT_TEST_FILES_CORE}
@@ -547,6 +550,7 @@ endif ()
 if (${LS_STD_BUILD_WITH_TESTS})
     message("${MODULE_NAME_IO}: Building integration tests...")
     add_executable(${MODULE_NAME_IO}-integration-test
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestException.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestHelper.cpp
             ${INTEGRATION_TEST_FILES_IO})
 endif ()
@@ -563,6 +567,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
     if (${LS_STD_BUILD_WITH_JNI})
         message("${PROJECT_NAME}: Building all tests...")
         add_executable(${PROJECT_NAME}-test
+                ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestException.cpp
                 ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestHelper.cpp
                 ${UNIT_TEST_FILES_BOXING}
                 ${UNIT_TEST_FILES_CORE}
@@ -576,6 +581,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
     else ()
         message("${PROJECT_NAME}: Building all tests with JNI support...")
         add_executable(${PROJECT_NAME}-test
+                ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestException.cpp
                 ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/TestHelper.cpp
                 ${UNIT_TEST_FILES_BOXING}
                 ${UNIT_TEST_FILES_CORE}

+ 25 - 0
test/classes/TestException.cpp

@@ -0,0 +1,25 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-05-18
+* Changed:         2023-05-18
+*
+* */
+
+#include "TestException.hpp"
+#include <string>
+
+using ls::std::test::TestException;
+using std::string;
+using std::string_view;
+
+TestException::TestException(string_view _message) : message(_message)
+{}
+
+TestException::~TestException() noexcept = default;
+
+const char *TestException::what() const noexcept
+{
+  return this->message.data();
+}

+ 33 - 0
test/classes/TestException.hpp

@@ -0,0 +1,33 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-05-18
+* Changed:         2023-05-18
+*
+* */
+
+#ifndef LS_STD_TEST_EXCEPTION_HPP
+#define LS_STD_TEST_EXCEPTION_HPP
+
+#include <exception>
+#include <string_view>
+
+namespace ls::std::test
+{
+  class TestException : public ::std::exception
+  {
+    public:
+
+      explicit TestException(::std::string_view _message);
+      ~TestException() noexcept override;
+
+      [[nodiscard]] const char *what() const noexcept override;
+
+    private:
+
+      ::std::string_view message{};
+  };
+}
+
+#endif

+ 5 - 6
test/classes/TestHelper.cpp

@@ -3,15 +3,14 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-05
-* Changed:         2023-02-23
+* Changed:         2023-05-18
 *
 * */
 
+#include "TestException.hpp"
 #include <algorithm>
 #include <classes/TestHelper.hpp>
 #include <climits>
-#include <fstream>
-#include <ls-std/core/type/Types.hpp>
 #include <sstream>
 #include <stdexcept>
 #include <string>
@@ -22,6 +21,7 @@
   #include <windows.h>
 #endif
 
+using ls::std::test::TestException;
 using ls::std::test::TestHelper;
 using std::getline;
 using std::replace;
@@ -90,11 +90,10 @@ string TestHelper::_getWorkingDirectory()
 string TestHelper::_getWorkingDirectoryUnix()
 {
   string workingDirectory{};
-  char buffer[PATH_MAX];
 
-  if (getcwd(buffer, sizeof(buffer)) == nullptr)
+  if (string buffer(PATH_MAX, 'x'); getcwd(buffer.data(), buffer.size()) == nullptr)
   {
-    throw runtime_error{"invalid file operation!"};
+    throw TestException{"invalid file operation!"};
   }
   else
   {

+ 3 - 3
test/classes/TestHelper.hpp

@@ -3,12 +3,12 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2023-03-27
+ * Changed:         2023-05-18
  *
  * */
 
-#ifndef LS_STD_IO_TEST_HELPER_HPP
-#define LS_STD_IO_TEST_HELPER_HPP
+#ifndef LS_STD_TEST_HELPER_HPP
+#define LS_STD_TEST_HELPER_HPP
 
 #include <string>
 #include <vector>