TestHelper.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2022-05-21
  7. *
  8. * */
  9. #ifndef LS_STD_IO_TEST_HELPER_HPP
  10. #define LS_STD_IO_TEST_HELPER_HPP
  11. #include <string>
  12. #include <vector>
  13. #include <stdexcept>
  14. #include <algorithm>
  15. #include <sstream>
  16. #include <climits>
  17. #if defined(unix) || defined(__APPLE__)
  18. #include <unistd.h>
  19. #endif
  20. #include <fstream>
  21. namespace ls_std_test
  22. {
  23. class TestHelper
  24. {
  25. public:
  26. TestHelper() = default;
  27. ~TestHelper() = default;
  28. static ::std::string getResourcesFolderLocation()
  29. {
  30. return TestHelper::getTestFolderLocation() + "resources" + ls_std_test::TestHelper::_getFilePathSeparator();
  31. }
  32. static ::std::string getTestFolderLocation()
  33. {
  34. ::std::string buildDirectory = ls_std_test::TestHelper::_getWorkingDirectory();
  35. buildDirectory = ls_std_test::TestHelper::_normalizePath(buildDirectory);
  36. return ls_std_test::TestHelper::_getParent(buildDirectory) + "test" + ls_std_test::TestHelper::_getFilePathSeparator();
  37. }
  38. static ::std::string normalize(const ::std::string &_path)
  39. {
  40. return ls_std_test::TestHelper::_normalizePath(_path);
  41. }
  42. static ::std::string readFile(const ::std::string &_absoluteFilePath)
  43. {
  44. char *data;
  45. ::std::ifstream inputStream{_absoluteFilePath, ::std::ifstream::binary};
  46. int length = (int) ls_std_test::TestHelper::_getFileSize(_absoluteFilePath);
  47. data = new ls::std::core::type::byte[length];
  48. inputStream.read(data, length);
  49. if (inputStream.fail())
  50. {
  51. throw ::std::runtime_error("invalid file operation!");
  52. }
  53. inputStream.close();
  54. ls::std::core::type::byte_field readData = ls::std::core::type::byte_field{data, (size_t) ls_std_test::TestHelper::_getFileSize(_absoluteFilePath)};
  55. delete[] data;
  56. return readData;
  57. }
  58. private:
  59. static bool _fileExists(const ::std::string &_path)
  60. {
  61. struct stat _stat{};
  62. return (stat(_path.c_str(), &_stat) == 0);
  63. }
  64. static char _getFilePathSeparator()
  65. {
  66. char separator;
  67. #ifdef _WIN32
  68. separator = '\\';
  69. #endif
  70. #if defined(unix) || defined(__APPLE__)
  71. separator = '/';
  72. #endif
  73. return separator;
  74. }
  75. static long _getFileSize(const ::std::string &_absoluteFilePath)
  76. {
  77. ::std::streampos fileSize{};
  78. if (ls_std_test::TestHelper::_fileExists(_absoluteFilePath))
  79. {
  80. ::std::ifstream fileHandler{_absoluteFilePath, ::std::ios::in};
  81. fileSize = fileHandler.tellg();
  82. fileHandler.seekg(0, ::std::ios::end);
  83. fileSize = fileHandler.tellg() - fileSize;
  84. fileHandler.close();
  85. }
  86. return (long) fileSize;
  87. }
  88. static std::string _getParent(const ::std::string &_path)
  89. {
  90. ::std::string parent{};
  91. ::std::vector<::std::string> subDirectoryNames = ls_std_test::TestHelper::_splitIntoSubDirectoryNames(_path);
  92. const char separator = ls_std_test::TestHelper::_getFilePathSeparator();
  93. subDirectoryNames.pop_back();
  94. for (auto const &subDirectoryName: subDirectoryNames)
  95. {
  96. parent += subDirectoryName + separator;
  97. }
  98. return parent;
  99. }
  100. static ::std::string _getWorkingDirectory()
  101. {
  102. ::std::string workingDirectory{};
  103. #if defined(unix) || defined(__APPLE__)
  104. workingDirectory = ls_std_test::TestHelper::_getWorkingDirectoryUnix();
  105. #endif
  106. #ifdef _WIN32
  107. workingDirectory = ls_std_test::TestHelper::_getWorkingDirectoryWindows();
  108. #endif
  109. return workingDirectory;
  110. }
  111. #if defined(unix) || defined(__APPLE__)
  112. static ::std::string _getWorkingDirectoryUnix()
  113. {
  114. ::std::string workingDirectory{};
  115. char buffer[PATH_MAX];
  116. if (getcwd(buffer, sizeof(buffer)) == nullptr)
  117. {
  118. throw ::std::runtime_error{"invalid file operation!"};
  119. }
  120. else
  121. {
  122. workingDirectory = ::std::string(buffer);
  123. }
  124. return workingDirectory;
  125. }
  126. #endif
  127. #ifdef _WIN32
  128. static ::std::string _getWorkingDirectoryWindows()
  129. {
  130. ::std::string workingDirectory{};
  131. TCHAR buffer[MAX_PATH];
  132. if (!GetCurrentDirectory(MAX_PATH, buffer))
  133. {
  134. throw ::std::runtime_error{"invalid file operation!"};
  135. }
  136. else
  137. {
  138. workingDirectory = ::std::string(buffer);
  139. }
  140. return workingDirectory;
  141. }
  142. #endif
  143. static ::std::string _normalizePath(::std::string _path)
  144. {
  145. _path = ls_std_test::TestHelper::_replaceWrongSeparator(_path);
  146. _path = ls_std_test::TestHelper::_reduceSeparators(_path);
  147. return _path;
  148. }
  149. static ::std::string _reduceSeparators(const ::std::string &_path)
  150. {
  151. static const char separator = {ls_std_test::TestHelper::_getFilePathSeparator()};
  152. ::std::string normalizedPath{};
  153. int index{};
  154. while (index < _path.size())
  155. {
  156. if (_path[index] == separator)
  157. {
  158. normalizedPath += _path[index];
  159. do
  160. {
  161. index++;
  162. } while (_path[index] == separator);
  163. }
  164. else
  165. {
  166. normalizedPath += _path[index];
  167. index++;
  168. }
  169. }
  170. return normalizedPath;
  171. }
  172. static ::std::string _replaceWrongSeparator(::std::string _path)
  173. {
  174. static const char unixSeparator = '/';
  175. static const char windowsSeparator = '\\';
  176. #if defined(unix) || defined(__APPLE__)
  177. ::std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  178. #endif
  179. #ifdef _WIN32
  180. ::std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  181. #endif
  182. return _path;
  183. }
  184. static ::std::vector<::std::string> _splitIntoSubDirectoryNames(const ::std::string &_path)
  185. {
  186. ::std::vector<::std::string> subDirectoryNames{};
  187. ::std::stringstream _stream{_path};
  188. ::std::string subDirectoryName{};
  189. const char separator = ls_std_test::TestHelper::_getFilePathSeparator();
  190. while (::std::getline(_stream, subDirectoryName, separator))
  191. {
  192. subDirectoryNames.push_back(subDirectoryName);
  193. }
  194. return subDirectoryNames;
  195. }
  196. };
  197. }
  198. #endif