filepath_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/filepath.h>
  7. struct FilePathTest
  8. {
  9. const char* filePath;
  10. const char* expected;
  11. };
  12. FilePathTest s_filePathTest[] =
  13. {
  14. // Already clean
  15. {"", "."},
  16. {"abc", "abc"},
  17. {"abc/def", "abc/def"},
  18. {"a/b/c", "a/b/c"},
  19. {".", "."},
  20. {"..", ".."},
  21. {"../..", "../.."},
  22. {"../../abc", "../../abc"},
  23. {"/abc", "/abc"},
  24. {"/", "/"},
  25. // Remove trailing slash
  26. {"abc/", "abc"},
  27. {"abc/def/", "abc/def"},
  28. {"a/b/c/", "a/b/c"},
  29. {"./", "."},
  30. {"../", ".."},
  31. {"../../", "../.."},
  32. {"/abc/", "/abc"},
  33. // Remove doubled slash
  34. {"abc//def//ghi", "abc/def/ghi"},
  35. {"//abc", "/abc"},
  36. {"///abc", "/abc"},
  37. {"//abc//", "/abc"},
  38. {"abc//", "abc"},
  39. // Remove . elements
  40. {"abc/./def", "abc/def"},
  41. {"/./abc/def", "/abc/def"},
  42. {"abc/.", "abc"},
  43. // Remove .. elements
  44. {"abc/def/ghi/../jkl", "abc/def/jkl"},
  45. {"abc/def/../ghi/../jkl", "abc/jkl"},
  46. {"abc/def/..", "abc"},
  47. {"abc/def/../..", "."},
  48. {"/abc/def/../..", "/"},
  49. {"abc/def/../../..", ".."},
  50. {"/abc/def/../../..", "/"},
  51. {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
  52. // Combinations
  53. {"abc/./../def", "def"},
  54. {"abc//./../def", "def"},
  55. {"abc/../../././../def", "../../def"},
  56. {"abc\\/../..\\/././../def", "../../def"},
  57. {"\\abc/def\\../..\\..", "/"},
  58. };
  59. struct FilePathSplit
  60. {
  61. const char* filePath;
  62. bool absolute;
  63. const char* path;
  64. const char* fileName;
  65. const char* baseName;
  66. const char* extension;
  67. };
  68. static const FilePathSplit s_filePathSplit[] =
  69. {
  70. { "\\abc/def\\../..\\../test.txt", true, "/", "test.txt", "test", ".txt" },
  71. { "/abv/gd/555/333/pod.mac", true, "/abv/gd/555/333/", "pod.mac", "pod", ".mac" },
  72. { "archive.tar.gz", false, "", "archive.tar.gz", "archive", ".tar.gz" },
  73. { "tmp/archive.tar.gz", false, "tmp/", "archive.tar.gz", "archive", ".tar.gz" },
  74. { "/tmp/archive.tar.gz", true, "/tmp/", "archive.tar.gz", "archive", ".tar.gz" },
  75. { "d:/tmp/archive.tar.gz", true, "D:/tmp/", "archive.tar.gz", "archive", ".tar.gz" },
  76. };
  77. TEST_CASE("FilePath", "")
  78. {
  79. bx::FilePath fp;
  80. for (uint32_t ii = 0; ii < BX_COUNTOF(s_filePathTest); ++ii)
  81. {
  82. const FilePathTest& test = s_filePathTest[ii];
  83. fp.set(test.filePath);
  84. const bx::StringView result = fp.get();
  85. REQUIRE(0 == bx::strCmp(test.expected, result) );
  86. }
  87. for (uint32_t ii = 0; ii < BX_COUNTOF(s_filePathSplit); ++ii)
  88. {
  89. const FilePathSplit& test = s_filePathSplit[ii];
  90. fp.set(test.filePath);
  91. const bx::StringView path = fp.getPath();
  92. const bx::StringView fileName = fp.getFileName();
  93. const bx::StringView baseName = fp.getBaseName();
  94. const bx::StringView ext = fp.getExt();
  95. REQUIRE(0 == bx::strCmp(test.path, path) );
  96. REQUIRE(0 == bx::strCmp(test.fileName, fileName) );
  97. REQUIRE(0 == bx::strCmp(test.baseName, baseName) );
  98. REQUIRE(0 == bx::strCmp(test.extension, ext) );
  99. REQUIRE(test.absolute == fp.isAbsolute() );
  100. };
  101. }
  102. TEST_CASE("FilePath temp", "")
  103. {
  104. bx::FilePath tmp(bx::TempDir::Tag);
  105. REQUIRE(0 != bx::strCmp(".", tmp.getPath().getPtr() ) );
  106. }