TestHelper.hpp 6.7 KB

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