TestHelper.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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-05
  7. *
  8. * */
  9. #include <algorithm>
  10. #include <classes/TestHelper.hpp>
  11. #include <climits>
  12. #include <fstream>
  13. #include <ls-std/core/types/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. ls::std::test::TestHelper::TestHelper() = default;
  24. ls::std::test::TestHelper::~TestHelper() = default;
  25. ::std::string ls::std::test::TestHelper::getResourcesFolderLocation()
  26. {
  27. return TestHelper::getTestFolderLocation() + "resources" + ls::std::test::TestHelper::_getFilePathSeparator();
  28. }
  29. ::std::string ls::std::test::TestHelper::getTestFolderLocation()
  30. {
  31. ::std::string buildDirectory = ls::std::test::TestHelper::_getWorkingDirectory();
  32. buildDirectory = ls::std::test::TestHelper::_normalizePath(buildDirectory);
  33. return ls::std::test::TestHelper::_getParent(buildDirectory) + "test" + ls::std::test::TestHelper::_getFilePathSeparator();
  34. }
  35. ::std::string ls::std::test::TestHelper::normalize(const ::std::string &_path)
  36. {
  37. return ls::std::test::TestHelper::_normalizePath(_path);
  38. }
  39. char ls::std::test::TestHelper::_getFilePathSeparator()
  40. {
  41. char separator;
  42. #ifdef _WIN32
  43. separator = '\\';
  44. #endif
  45. #if defined(unix) || defined(__APPLE__)
  46. separator = '/';
  47. #endif
  48. return separator;
  49. }
  50. ::std::string ls::std::test::TestHelper::_getParent(const ::std::string &_path)
  51. {
  52. ::std::string parent{};
  53. ::std::vector<::std::string> subDirectoryNames = ls::std::test::TestHelper::_splitIntoSubDirectoryNames(_path);
  54. const char separator = ls::std::test::TestHelper::_getFilePathSeparator();
  55. subDirectoryNames.pop_back();
  56. for (auto const &subDirectoryName : subDirectoryNames)
  57. {
  58. parent += subDirectoryName + separator;
  59. }
  60. return parent;
  61. }
  62. ::std::string ls::std::test::TestHelper::_getWorkingDirectory()
  63. {
  64. ::std::string workingDirectory{};
  65. #if defined(unix) || defined(__APPLE__)
  66. workingDirectory = ls::std::test::TestHelper::_getWorkingDirectoryUnix();
  67. #endif
  68. #ifdef _WIN32
  69. workingDirectory = ls::std::test::TestHelper::_getWorkingDirectoryWindows();
  70. #endif
  71. return workingDirectory;
  72. }
  73. #if defined(unix) || defined(__APPLE__)
  74. ::std::string ls::std::test::TestHelper::_getWorkingDirectoryUnix()
  75. {
  76. ::std::string workingDirectory{};
  77. char buffer[PATH_MAX];
  78. if (getcwd(buffer, sizeof(buffer)) == nullptr)
  79. {
  80. throw ::std::runtime_error{"invalid file operation!"};
  81. }
  82. else
  83. {
  84. workingDirectory = ::std::string(buffer);
  85. }
  86. return workingDirectory;
  87. }
  88. #endif
  89. #ifdef _WIN32
  90. ::std::string ls::std::test::TestHelper::_getWorkingDirectoryWindows()
  91. {
  92. ::std::string workingDirectory{};
  93. TCHAR buffer[MAX_PATH];
  94. if (!GetCurrentDirectory(MAX_PATH, buffer))
  95. {
  96. throw ::std::runtime_error{"invalid file operation!"};
  97. }
  98. else
  99. {
  100. workingDirectory = ::std::string(buffer);
  101. }
  102. return workingDirectory;
  103. }
  104. #endif
  105. ::std::string ls::std::test::TestHelper::_normalizePath(::std::string _path)
  106. {
  107. _path = ls::std::test::TestHelper::_replaceWrongSeparator(_path);
  108. _path = ls::std::test::TestHelper::_reduceSeparators(_path);
  109. return _path;
  110. }
  111. ::std::string ls::std::test::TestHelper::_reduceSeparators(const ::std::string &_path)
  112. {
  113. static const char separator = {ls::std::test::TestHelper::_getFilePathSeparator()};
  114. ::std::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. ::std::string ls::std::test::TestHelper::_replaceWrongSeparator(::std::string _path)
  135. {
  136. static const char unixSeparator = '/';
  137. static const char windowsSeparator = '\\';
  138. #if defined(unix) || defined(__APPLE__)
  139. ::std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  140. #endif
  141. #ifdef _WIN32
  142. ::std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  143. #endif
  144. return _path;
  145. }
  146. ::std::vector<::std::string> ls::std::test::TestHelper::_splitIntoSubDirectoryNames(const ::std::string &_path)
  147. {
  148. ::std::vector<::std::string> subDirectoryNames{};
  149. ::std::stringstream _stream{_path};
  150. ::std::string subDirectoryName{};
  151. const char separator = ls::std::test::TestHelper::_getFilePathSeparator();
  152. while (::std::getline(_stream, subDirectoryName, separator))
  153. {
  154. subDirectoryNames.push_back(subDirectoryName);
  155. }
  156. return subDirectoryNames;
  157. }