TestHelper.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-05
  6. * Changed: 2025-12-23
  7. *
  8. * */
  9. #include "TestException.hpp"
  10. #include <algorithm>
  11. #include <classes/TestHelper.hpp>
  12. #include <climits>
  13. #include <sstream>
  14. #include <stdexcept>
  15. #include <string>
  16. #if defined(unix) || defined(__APPLE__)
  17. #include <unistd.h>
  18. #endif
  19. #ifdef _WIN32
  20. #define NOMINMAX
  21. #include <windows.h>
  22. #endif
  23. using ls::std::test::TestException;
  24. using ls::std::test::TestHelper;
  25. using std::getline;
  26. using std::replace;
  27. using std::runtime_error;
  28. using std::string;
  29. using std::stringstream;
  30. using std::vector;
  31. TestHelper::TestHelper() = default;
  32. TestHelper::~TestHelper() = default;
  33. string TestHelper::getResourcesFolderLocation()
  34. {
  35. return TestHelper::getTestFolderLocation() + "resources" + TestHelper::_getFilePathSeparator();
  36. }
  37. string TestHelper::getTestFolderLocation()
  38. {
  39. string buildDirectory = TestHelper::_getWorkingDirectory();
  40. buildDirectory = TestHelper::_normalizePath(buildDirectory);
  41. return TestHelper::_getParent(buildDirectory) + "test" + TestHelper::_getFilePathSeparator();
  42. }
  43. char TestHelper::_getFilePathSeparator()
  44. {
  45. #ifdef _WIN32
  46. return '\\';
  47. #endif
  48. #if defined(unix) || defined(__APPLE__)
  49. return '/';
  50. #endif
  51. }
  52. string TestHelper::_getParent(const string &_path)
  53. {
  54. string parent{};
  55. vector<string> subDirectoryNames = TestHelper::_splitIntoSubDirectoryNames(_path);
  56. const char separator = TestHelper::_getFilePathSeparator();
  57. subDirectoryNames.pop_back();
  58. for (auto const &subDirectoryName : subDirectoryNames)
  59. {
  60. parent += subDirectoryName + separator;
  61. }
  62. return parent;
  63. }
  64. string TestHelper::_getWorkingDirectory()
  65. {
  66. string workingDirectory{};
  67. #if defined(unix) || defined(__APPLE__)
  68. workingDirectory = TestHelper::_getWorkingDirectoryUnix();
  69. #endif
  70. #ifdef _WIN32
  71. workingDirectory = TestHelper::_getWorkingDirectoryWindows();
  72. #endif
  73. return workingDirectory;
  74. }
  75. #if defined(unix) || defined(__APPLE__)
  76. string TestHelper::_getWorkingDirectoryUnix()
  77. {
  78. string workingDirectory{};
  79. if (string buffer(PATH_MAX, 'x'); getcwd(buffer.data(), buffer.size()) == nullptr)
  80. {
  81. throw TestException{"invalid file operation!"};
  82. }
  83. else
  84. {
  85. workingDirectory = string(buffer);
  86. }
  87. return workingDirectory;
  88. }
  89. #endif
  90. #ifdef _WIN32
  91. string TestHelper::_getWorkingDirectoryWindows()
  92. {
  93. string workingDirectory{};
  94. TCHAR buffer[MAX_PATH];
  95. if (!GetCurrentDirectory(MAX_PATH, buffer))
  96. {
  97. throw TestException{"invalid file operation!"};
  98. }
  99. else
  100. {
  101. workingDirectory = string(buffer);
  102. }
  103. return workingDirectory;
  104. }
  105. #endif
  106. string TestHelper::_normalizePath(string _path)
  107. {
  108. _path = TestHelper::_replaceWrongSeparator(_path);
  109. _path = TestHelper::_reduceSeparators(_path);
  110. return _path;
  111. }
  112. string TestHelper::_reduceSeparators(const string &_path)
  113. {
  114. static const char separator = {TestHelper::_getFilePathSeparator()};
  115. string normalizedPath{};
  116. int index{};
  117. while (index < _path.size())
  118. {
  119. if (_path[index] == separator)
  120. {
  121. normalizedPath += _path[index];
  122. do
  123. {
  124. index++;
  125. } while (_path[index] == separator);
  126. }
  127. else
  128. {
  129. normalizedPath += _path[index];
  130. index++;
  131. }
  132. }
  133. return normalizedPath;
  134. }
  135. string TestHelper::_replaceWrongSeparator(string _path)
  136. {
  137. static constexpr char unixSeparator = '/';
  138. static constexpr char windowsSeparator = '\\';
  139. #if defined(unix) || defined(__APPLE__)
  140. replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  141. #endif
  142. #ifdef _WIN32
  143. replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  144. #endif
  145. return _path;
  146. }
  147. vector<string> TestHelper::_splitIntoSubDirectoryNames(const string &_path)
  148. {
  149. vector<string> subDirectoryNames{};
  150. stringstream _stream{_path};
  151. string subDirectoryName{};
  152. const char separator = TestHelper::_getFilePathSeparator();
  153. while (getline(_stream, subDirectoryName, separator))
  154. {
  155. subDirectoryNames.push_back(subDirectoryName);
  156. }
  157. return subDirectoryNames;
  158. }