TestHelper.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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-04
  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 <sstream>
  16. #include <stdexcept>
  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 // TODO: create source file & move to "classes" location
  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. {
  68. };
  69. return (stat(_path.c_str(), &_stat) == 0);
  70. }
  71. static char _getFilePathSeparator()
  72. {
  73. char separator;
  74. #ifdef _WIN32
  75. separator = '\\';
  76. #endif
  77. #if defined(unix) || defined(__APPLE__)
  78. separator = '/';
  79. #endif
  80. return separator;
  81. }
  82. static long _getFileSize(const ::std::string &_absoluteFilePath)
  83. {
  84. ::std::streampos fileSize{};
  85. if (ls::std::test::TestHelper::_fileExists(_absoluteFilePath))
  86. {
  87. ::std::ifstream fileHandler{_absoluteFilePath, ::std::ios::in};
  88. fileSize = fileHandler.tellg();
  89. fileHandler.seekg(0, ::std::ios::end);
  90. fileSize = fileHandler.tellg() - fileSize;
  91. fileHandler.close();
  92. }
  93. return (long) fileSize;
  94. }
  95. static ::std::string _getParent(const ::std::string &_path)
  96. {
  97. ::std::string parent{};
  98. ::std::vector<::std::string> subDirectoryNames = ls::std::test::TestHelper::_splitIntoSubDirectoryNames(_path);
  99. const char separator = ls::std::test::TestHelper::_getFilePathSeparator();
  100. subDirectoryNames.pop_back();
  101. for (auto const &subDirectoryName : subDirectoryNames)
  102. {
  103. parent += subDirectoryName + separator;
  104. }
  105. return parent;
  106. }
  107. static ::std::string _getWorkingDirectory()
  108. {
  109. ::std::string workingDirectory{};
  110. #if defined(unix) || defined(__APPLE__)
  111. workingDirectory = ls::std::test::TestHelper::_getWorkingDirectoryUnix();
  112. #endif
  113. #ifdef _WIN32
  114. workingDirectory = ls::std::test::TestHelper::_getWorkingDirectoryWindows();
  115. #endif
  116. return workingDirectory;
  117. }
  118. #if defined(unix) || defined(__APPLE__)
  119. static ::std::string _getWorkingDirectoryUnix()
  120. {
  121. ::std::string workingDirectory{};
  122. char buffer[PATH_MAX];
  123. if (getcwd(buffer, sizeof(buffer)) == nullptr)
  124. {
  125. throw ::std::runtime_error{"invalid file operation!"};
  126. }
  127. else
  128. {
  129. workingDirectory = ::std::string(buffer);
  130. }
  131. return workingDirectory;
  132. }
  133. #endif
  134. #ifdef _WIN32
  135. static ::std::string _getWorkingDirectoryWindows()
  136. {
  137. ::std::string workingDirectory{};
  138. TCHAR buffer[MAX_PATH];
  139. if (!GetCurrentDirectory(MAX_PATH, buffer))
  140. {
  141. throw ::std::runtime_error{"invalid file operation!"};
  142. }
  143. else
  144. {
  145. workingDirectory = ::std::string(buffer);
  146. }
  147. return workingDirectory;
  148. }
  149. #endif
  150. static ::std::string _normalizePath(::std::string _path)
  151. {
  152. _path = ls::std::test::TestHelper::_replaceWrongSeparator(_path);
  153. _path = ls::std::test::TestHelper::_reduceSeparators(_path);
  154. return _path;
  155. }
  156. static ::std::string _reduceSeparators(const ::std::string &_path)
  157. {
  158. static const char separator = {ls::std::test::TestHelper::_getFilePathSeparator()};
  159. ::std::string normalizedPath{};
  160. int index{};
  161. while (index < _path.size())
  162. {
  163. if (_path[index] == separator)
  164. {
  165. normalizedPath += _path[index];
  166. do
  167. {
  168. index++;
  169. } while (_path[index] == separator);
  170. }
  171. else
  172. {
  173. normalizedPath += _path[index];
  174. index++;
  175. }
  176. }
  177. return normalizedPath;
  178. }
  179. static ::std::string _replaceWrongSeparator(::std::string _path)
  180. {
  181. static const char unixSeparator = '/';
  182. static const char windowsSeparator = '\\';
  183. #if defined(unix) || defined(__APPLE__)
  184. ::std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  185. #endif
  186. #ifdef _WIN32
  187. ::std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  188. #endif
  189. return _path;
  190. }
  191. static ::std::vector<::std::string> _splitIntoSubDirectoryNames(const ::std::string &_path)
  192. {
  193. ::std::vector<::std::string> subDirectoryNames{};
  194. ::std::stringstream _stream{_path};
  195. ::std::string subDirectoryName{};
  196. const char separator = ls::std::test::TestHelper::_getFilePathSeparator();
  197. while (::std::getline(_stream, subDirectoryName, separator))
  198. {
  199. subDirectoryNames.push_back(subDirectoryName);
  200. }
  201. return subDirectoryNames;
  202. }
  203. };
  204. }
  205. #endif