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: 2023-02-23
  7. *
  8. * */
  9. #include <algorithm>
  10. #include <classes/TestHelper.hpp>
  11. #include <climits>
  12. #include <fstream>
  13. #include <ls-std/core/type/Types.hpp>
  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. #include <windows.h>
  22. #endif
  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. char buffer[PATH_MAX];
  79. if (getcwd(buffer, sizeof(buffer)) == nullptr)
  80. {
  81. throw runtime_error{"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 runtime_error{"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 const char unixSeparator = '/';
  138. static const 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. }