Add more UTF-8 path edge cases.

- Add macro for automatically testing both / and \ of all path
  input/outputs.
- Add more UTF-8 path edge cases.
This commit is contained in:
gibbed 2021-05-02 08:13:02 -05:00 committed by Rick Gibbed
parent b7674f0bd8
commit 9185c36b41
1 changed files with 136 additions and 67 deletions

View File

@ -315,99 +315,168 @@ TEST_CASE("utf8::equal_case_z", "UTF-8 Equal Case Z") {
// TODO(gibbed): ends_with_case // TODO(gibbed): ends_with_case
// TODO(gibbed): split_path // TODO(gibbed): split_path
#define TEST_PATH(func, input, output) \
do { \
std::string input_value = input; \
std::string output_value = output; \
REQUIRE(func(input_value, '/') == output_value); \
std::replace(input_value.begin(), input_value.end(), '/', '\\'); \
std::replace(output_value.begin(), output_value.end(), '/', '\\'); \
REQUIRE(func(input_value, '\\') == output_value); \
} while (0)
#define TEST_PATH_RAW(func, input, output) \
do { \
std::string output_value = output; \
REQUIRE(func(input, '/') == output_value); \
std::replace(output_value.begin(), output_value.end(), '/', '\\'); \
REQUIRE(func(input, '\\') == output_value); \
} while (0)
#define TEST_PATHS(func, output, ...) \
do { \
std::vector<std::string> input_values = {__VA_ARGS__}; \
std::string output_value = output; \
REQUIRE(func(input_values, '/') == output_value); \
for (auto it = input_values.begin(); it != input_values.end(); ++it) { \
std::replace((*it).begin(), (*it).end(), '/', '\\'); \
} \
std::replace(output_value.begin(), output_value.end(), '/', '\\'); \
REQUIRE(func(input_values, '\\') == output_value); \
} while (0)
TEST_CASE("utf8::join_paths", "UTF-8 Join Paths") { TEST_CASE("utf8::join_paths", "UTF-8 Join Paths") {
REQUIRE(utf8::join_paths({u8"X:", u8"foo", u8"bar", u8"baz", u8"qux"}, TEST_PATHS(utf8::join_paths, u8"");
'\\') == "X:\\foo\\bar\\baz\\qux"); TEST_PATHS(utf8::join_paths, u8"foo", u8"foo");
REQUIRE(utf8::join_paths({u8"X:", u8"foo", u8"bar", u8"baz", u8"qux"}, '/') == TEST_PATHS(utf8::join_paths, u8"foo/bar", u8"foo", u8"bar");
"X:/foo/bar/baz/qux"); TEST_PATHS(utf8::join_paths, "X:/foo/bar/baz/qux", u8"X:", u8"foo", u8"bar",
u8"baz", u8"qux");
} }
// TODO(gibbed): join_guest_paths // TODO(gibbed): join_guest_paths
TEST_CASE("utf8::fix_path_separators", "UTF-8 Fix Path Separators") { TEST_CASE("utf8::fix_path_separators", "UTF-8 Fix Path Separators") {
REQUIRE(utf8::fix_path_separators("X:\\foo/bar\\baz/qux", '\\') == TEST_PATH_RAW(utf8::fix_path_separators, "", "");
"X:\\foo\\bar\\baz\\qux"); TEST_PATH_RAW(utf8::fix_path_separators, "\\", "/");
REQUIRE(utf8::fix_path_separators("X:\\foo/bar\\baz/qux", '/') == TEST_PATH_RAW(utf8::fix_path_separators, "/", "/");
"X:/foo/bar/baz/qux"); TEST_PATH_RAW(utf8::fix_path_separators, "\\foo", "/foo");
TEST_PATH_RAW(utf8::fix_path_separators, "\\foo/", "/foo/");
TEST_PATH_RAW(utf8::fix_path_separators, "/foo", "/foo");
TEST_PATH_RAW(utf8::fix_path_separators, "\\foo/bar\\baz/qux",
"/foo/bar/baz/qux");
TEST_PATH_RAW(utf8::fix_path_separators, "\\\\foo//bar\\\\baz//qux",
"/foo/bar/baz/qux");
TEST_PATH_RAW(utf8::fix_path_separators, "foo", "foo");
TEST_PATH_RAW(utf8::fix_path_separators, "foo/", "foo/");
TEST_PATH_RAW(utf8::fix_path_separators, "foo/bar\\baz/qux",
"foo/bar/baz/qux");
TEST_PATH_RAW(utf8::fix_path_separators, "foo//bar\\\\baz//qux",
"foo/bar/baz/qux");
TEST_PATH_RAW(utf8::fix_path_separators, "X:", "X:");
TEST_PATH_RAW(utf8::fix_path_separators, "X:\\", "X:/");
TEST_PATH_RAW(utf8::fix_path_separators, "X:/", "X:/");
TEST_PATH_RAW(utf8::fix_path_separators, "X:\\foo", "X:/foo");
TEST_PATH_RAW(utf8::fix_path_separators, "X:\\foo/", "X:/foo/");
TEST_PATH_RAW(utf8::fix_path_separators, "X:/foo", "X:/foo");
TEST_PATH_RAW(utf8::fix_path_separators, "X:\\foo/bar\\baz/qux",
"X:/foo/bar/baz/qux");
TEST_PATH_RAW(utf8::fix_path_separators, "X:\\\\foo//bar\\\\baz//qux",
"X:/foo/bar/baz/qux");
} }
// TODO(gibbed): fix_guest_path_separators // TODO(gibbed): fix_guest_path_separators
TEST_CASE("utf8::find_name_from_path", "UTF-8 Find Name From Path") { TEST_CASE("utf8::find_name_from_path", "UTF-8 Find Name From Path") {
REQUIRE(utf8::find_name_from_path("X:\\foo\\bar\\baz\\qux.txt", '\\') == TEST_PATH(utf8::find_name_from_path, "/", "");
"qux.txt"); TEST_PATH(utf8::find_name_from_path, "foo/bar/baz/qux.txt", "qux.txt");
REQUIRE(utf8::find_name_from_path( TEST_PATH(utf8::find_name_from_path, "ほげ/ぴよ/ふが/ほげら/ほげほげ.txt",
"X:\\ほげ\\ぴよ\\ふが\\ほげら\\ほげほげ.txt", '\\') == "ほげほげ.txt");
"ほげほげ.txt"); TEST_PATH(utf8::find_name_from_path, "/foo/bar/baz/qux.txt", "qux.txt");
REQUIRE(utf8::find_name_from_path("X:\\ほげ\\ぴよ\\ふが\\ほげら.ほげほげ", TEST_PATH(utf8::find_name_from_path, "/ほげ/ぴよ/ふが/ほげら/ほげほげ.txt",
'\\') == "ほげら.ほげほげ"); "ほげほげ.txt");
REQUIRE(utf8::find_name_from_path("X:/foo/bar/baz/qux.txt", '/') == TEST_PATH(utf8::find_name_from_path, "X:/foo/bar/baz/qux.txt", "qux.txt");
"qux.txt"); TEST_PATH(utf8::find_name_from_path, "X:/ほげ/ぴよ/ふが/ほげら/ほげほげ.txt",
REQUIRE(utf8::find_name_from_path("X:/ほげ/ぴよ/ふが/ほげら/ほげほげ.txt", "ほげほげ.txt");
'/') == "ほげほげ.txt"); TEST_PATH(utf8::find_name_from_path, "X:/ほげ/ぴよ/ふが/ほげら.ほげほげ",
REQUIRE(utf8::find_name_from_path("X:/ほげ/ぴよ/ふが/ほげら.ほげほげ", '/') == "ほげら.ほげほげ");
"ほげら.ほげほげ");
} }
// TODO(gibbed): find_name_from_guest_path // TODO(gibbed): find_name_from_guest_path
TEST_CASE("utf8::find_base_name_from_path", "UTF-8 Find Base Name From Path") { TEST_CASE("utf8::find_base_name_from_path", "UTF-8 Find Base Name From Path") {
REQUIRE(utf8::find_base_name_from_path("X:\\foo\\bar\\baz\\qux.txt", '\\') == TEST_PATH(utf8::find_base_name_from_path, "foo/bar/baz/qux.txt", "qux");
"qux"); TEST_PATH(utf8::find_base_name_from_path,
REQUIRE(utf8::find_base_name_from_path( "ほげ/ぴよ/ふが/ほげら/ほげほげ.txt", "ほげほげ");
"X:\\ほげ\\ぴよ\\ふが\\ほげら\\ほげほげ.txt", '\\') == TEST_PATH(utf8::find_base_name_from_path, "ほげ/ぴよ/ふが/ほげら.ほげほげ",
"ほげほげ"); "ほげら");
REQUIRE(utf8::find_base_name_from_path( TEST_PATH(utf8::find_base_name_from_path, "/foo/bar/baz/qux.txt", "qux");
"X:\\ほげ\\ぴよ\\ふが\\ほげら.ほげほげ", '\\') == "ほげら"); TEST_PATH(utf8::find_base_name_from_path,
REQUIRE(utf8::find_base_name_from_path("X:/foo/bar/baz/qux.txt", '/') == "/ほげ/ぴよ/ふが/ほげら/ほげほげ.txt", "ほげほげ");
"qux"); TEST_PATH(utf8::find_base_name_from_path, "/ほげ/ぴよ/ふが/ほげら.ほげほげ",
REQUIRE(utf8::find_base_name_from_path( "ほげら");
"X:/ほげ/ぴよ/ふが/ほげら/ほげほげ.txt", '/') == "ほげほげ"); TEST_PATH(utf8::find_base_name_from_path, "X:/foo/bar/baz/qux.txt", "qux");
REQUIRE(utf8::find_base_name_from_path("X:/ほげ/ぴよ/ふが/ほげら.ほげほげ", TEST_PATH(utf8::find_base_name_from_path,
'/') == "ほげら"); "X:/ほげ/ぴよ/ふが/ほげら/ほげほげ.txt", "ほげほげ");
TEST_PATH(utf8::find_base_name_from_path, "X:/ほげ/ぴよ/ふが/ほげら.ほげほげ",
"ほげら");
} }
// TODO(gibbed): find_base_name_from_guest_path // TODO(gibbed): find_base_name_from_guest_path
TEST_CASE("utf8::find_base_path", "UTF-8 Find Base Path") { TEST_CASE("utf8::find_base_path", "UTF-8 Find Base Path") {
REQUIRE(utf8::find_base_path("X:\\foo\\bar\\baz\\qux", '\\') == TEST_PATH(utf8::find_base_path, "", "");
"X:\\foo\\bar\\baz"); TEST_PATH(utf8::find_base_path, "/", "");
REQUIRE(utf8::find_base_path("X:/foo/bar/baz/qux", '/') == "X:/foo/bar/baz"); TEST_PATH(utf8::find_base_path, "//", "");
REQUIRE(utf8::find_base_path("X:/ほげ/ぴよ/ふが/ほげら/ほげほげ", '/') == TEST_PATH(utf8::find_base_path, "/foo", "");
"X:/ほげ/ぴよ/ふが/ほげら"); TEST_PATH(utf8::find_base_path, "/foo/bar", "/foo");
TEST_PATH(utf8::find_base_path, "/foo/bar/baz/qux", "/foo/bar/baz");
TEST_PATH(utf8::find_base_path, "/ほげ/ぴよ/ふが/ほげら/ほげほげ",
"/ほげ/ぴよ/ふが/ほげら");
TEST_PATH(utf8::find_base_path, "foo", "");
TEST_PATH(utf8::find_base_path, "foo/bar", "foo");
TEST_PATH(utf8::find_base_path, "foo/bar/baz/qux", "foo/bar/baz");
TEST_PATH(utf8::find_base_path, "ほげ/ぴよ/ふが/ほげら/ほげほげ",
"ほげ/ぴよ/ふが/ほげら");
TEST_PATH(utf8::find_base_path, "X:", "");
TEST_PATH(utf8::find_base_path, "X:/", "");
TEST_PATH(utf8::find_base_path, "X:/foo", "X:");
TEST_PATH(utf8::find_base_path, "X:/foo/bar", "X:/foo");
TEST_PATH(utf8::find_base_path, "X:/foo/bar/baz/qux", "X:/foo/bar/baz");
TEST_PATH(utf8::find_base_path, "X:/ほげ/ぴよ/ふが/ほげら/ほげほげ",
"X:/ほげ/ぴよ/ふが/ほげら");
} }
// TODO(gibbed): find_base_guest_path // TODO(gibbed): find_base_guest_path
TEST_CASE("utf8::canonicalize_path", "UTF-8 Canonicalize Path") { TEST_CASE("utf8::canonicalize_path", "UTF-8 Canonicalize Path") {
REQUIRE(utf8::canonicalize_path("X:\\foo\\bar\\baz\\qux", '\\') == TEST_PATH(utf8::canonicalize_path, "foo/bar/baz/qux", "foo/bar/baz/qux");
"X:\\foo\\bar\\baz\\qux"); TEST_PATH(utf8::canonicalize_path, "foo/./baz/qux", "foo/baz/qux");
REQUIRE(utf8::canonicalize_path("X:\\foo\\.\\baz\\qux", '\\') == TEST_PATH(utf8::canonicalize_path, "foo/../baz/qux", "baz/qux");
"X:\\foo\\baz\\qux"); TEST_PATH(utf8::canonicalize_path, "foo/./baz/../qux", "foo/qux");
REQUIRE(utf8::canonicalize_path("X:\\foo\\..\\baz\\qux", '\\') == TEST_PATH(utf8::canonicalize_path, "foo/./../baz/qux", "baz/qux");
"X:\\baz\\qux"); TEST_PATH(utf8::canonicalize_path, "./bar/baz/qux", "bar/baz/qux");
REQUIRE(utf8::canonicalize_path("X:\\foo\\.\\baz\\..\\qux", '\\') == TEST_PATH(utf8::canonicalize_path, "../bar/baz/qux", "bar/baz/qux");
"X:\\foo\\qux"); TEST_PATH(utf8::canonicalize_path, "ほげ/ぴよ/./ふが/../ほげら/ほげほげ",
REQUIRE(utf8::canonicalize_path("X:\\foo\\.\\..\\baz\\qux", '\\') == "ほげ/ぴよ/ほげら/ほげほげ");
"X:\\baz\\qux"); TEST_PATH(utf8::canonicalize_path, "/foo/bar/baz/qux", "/foo/bar/baz/qux");
REQUIRE(utf8::canonicalize_path("X:\\.\\bar\\baz\\qux", '\\') == TEST_PATH(utf8::canonicalize_path, "/foo/./baz/qux", "/foo/baz/qux");
"X:\\bar\\baz\\qux"); TEST_PATH(utf8::canonicalize_path, "/foo/../baz/qux", "/baz/qux");
REQUIRE(utf8::canonicalize_path("X:\\..\\bar\\baz\\qux", '\\') == TEST_PATH(utf8::canonicalize_path, "/foo/./baz/../qux", "/foo/qux");
"X:\\bar\\baz\\qux"); TEST_PATH(utf8::canonicalize_path, "/foo/./../baz/qux", "/baz/qux");
REQUIRE(utf8::canonicalize_path("X:/foo/bar/baz/qux", '/') == TEST_PATH(utf8::canonicalize_path, "/./bar/baz/qux", "/bar/baz/qux");
"X:/foo/bar/baz/qux"); TEST_PATH(utf8::canonicalize_path, "/../bar/baz/qux", "/bar/baz/qux");
REQUIRE(utf8::canonicalize_path( TEST_PATH(utf8::canonicalize_path, "/ほげ/ぴよ/./ふが/../ほげら/ほげほげ",
"X:\\ほげ\\ぴよ\\.\\ふが\\..\\ほげら\\ほげほげ", '\\') == "/ほげ/ぴよ/ほげら/ほげほげ");
"X:\\ほげ\\ぴよ\\ほげら\\ほげほげ"); TEST_PATH(utf8::canonicalize_path, "X:/foo/bar/baz/qux",
REQUIRE(utf8::canonicalize_path("X:/foo/./baz/qux", '/') == "X:/foo/baz/qux"); "X:/foo/bar/baz/qux");
REQUIRE(utf8::canonicalize_path("X:/foo/../baz/qux", '/') == "X:/baz/qux"); TEST_PATH(utf8::canonicalize_path, "X:/foo/./baz/qux", "X:/foo/baz/qux");
REQUIRE(utf8::canonicalize_path("X:/foo/./baz/../qux", '/') == "X:/foo/qux"); TEST_PATH(utf8::canonicalize_path, "X:/foo/../baz/qux", "X:/baz/qux");
REQUIRE(utf8::canonicalize_path("X:/foo/./../baz/qux", '/') == "X:/baz/qux"); TEST_PATH(utf8::canonicalize_path, "X:/foo/./baz/../qux", "X:/foo/qux");
REQUIRE(utf8::canonicalize_path("X:/./bar/baz/qux", '/') == "X:/bar/baz/qux"); TEST_PATH(utf8::canonicalize_path, "X:/foo/./../baz/qux", "X:/baz/qux");
REQUIRE(utf8::canonicalize_path("X:/../bar/baz/qux", '/') == TEST_PATH(utf8::canonicalize_path, "X:/./bar/baz/qux", "X:/bar/baz/qux");
"X:/bar/baz/qux"); TEST_PATH(utf8::canonicalize_path, "X:/../bar/baz/qux", "X:/bar/baz/qux");
REQUIRE(utf8::canonicalize_path("X:/ほげ/ぴよ/./ふが/../ほげら/ほげほげ", TEST_PATH(utf8::canonicalize_path, "X:/ほげ/ぴよ/./ふが/../ほげら/ほげほげ",
'/') == "X:/ほげ/ぴよ/ほげら/ほげほげ"); "X:/ほげ/ぴよ/ほげら/ほげほげ");
} }
// TODO(gibbed): canonicalize_guest_path // TODO(gibbed): canonicalize_guest_path