TestHelper.cpp 4.1 KB

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