TestHelper.cpp 4.0 KB

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