-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_path.cpp
More file actions
123 lines (97 loc) · 3.86 KB
/
Copy pathtest_path.cpp
File metadata and controls
123 lines (97 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// SPDX-License-Identifier: MIT
#include <stdcorelib/path.h>
#include <stdcorelib/system.h>
#include <boost/test/unit_test.hpp>
namespace fs = std::filesystem;
using namespace stdc;
BOOST_AUTO_TEST_SUITE(test_path)
namespace {
// clean_path() builds the result with native separators. Compare in generic form so the
// expectations read the same on every platform.
std::string cleaned(const std::string &s) {
return path::clean_path(fs::path(s)).generic_string();
}
}
BOOST_AUTO_TEST_CASE(test_clean_path) {
// "." is dropped
BOOST_CHECK_EQUAL(cleaned("a/./b"), "a/b");
BOOST_CHECK_EQUAL(cleaned("./a"), "a");
BOOST_CHECK_EQUAL(cleaned("a/."), "a");
BOOST_CHECK_EQUAL(cleaned("./././a"), "a");
// ".." cancels the preceding component
BOOST_CHECK_EQUAL(cleaned("a/b/.."), "a");
BOOST_CHECK_EQUAL(cleaned("a/b/../c"), "a/c");
BOOST_CHECK_EQUAL(cleaned("a/b/c/../.."), "a");
BOOST_CHECK_EQUAL(cleaned("a/./b/../c"), "a/c");
// a leading ".." has nothing to cancel and is kept
BOOST_CHECK_EQUAL(cleaned("../a"), "../a");
BOOST_CHECK_EQUAL(cleaned("../../a"), "../../a");
BOOST_CHECK_EQUAL(cleaned("a/../.."), "..");
// nothing to do
BOOST_CHECK_EQUAL(cleaned("a/b/c"), "a/b/c");
BOOST_CHECK_EQUAL(cleaned(""), "");
// absolute paths keep their root
BOOST_CHECK_EQUAL(cleaned("/a/b/../c"), "/a/c");
#ifdef _WIN32
BOOST_CHECK_EQUAL(cleaned("C:/a/../b"), "C:/b");
#endif
}
BOOST_AUTO_TEST_CASE(test_normalize_separators) {
// backslashes always fold to '/' when native is not requested
BOOST_CHECK_EQUAL(path::normalize_separators(fs::path("a\\b\\c")), "a/b/c");
BOOST_CHECK_EQUAL(path::normalize_separators(fs::path("a/b/c")), "a/b/c");
BOOST_CHECK_EQUAL(path::normalize_separators(fs::path("a\\b/c")), "a/b/c");
#ifdef _WIN32
// native uses the platform separator
BOOST_CHECK_EQUAL(path::normalize_separators(fs::path("a/b/c"), true), "a\\b\\c");
BOOST_CHECK_EQUAL(path::normalize_separators(fs::path("a\\b\\c"), true), "a\\b\\c");
#else
// on POSIX there is only one separator, so `native` changes nothing
BOOST_CHECK_EQUAL(path::normalize_separators(fs::path("a/b/c"), true), "a/b/c");
#endif
BOOST_CHECK_EQUAL(path::normalize_separators(fs::path("")), "");
// the free function in namespace stdc is the same one
BOOST_CHECK_EQUAL(normalize_separators(fs::path("a\\b")), "a/b");
}
BOOST_AUTO_TEST_CASE(test_utf8_conversion) {
// ASCII round trip
{
auto p = path::from_utf8("a/b/c.txt");
BOOST_CHECK_EQUAL(path::to_utf8(p), "a/b/c.txt");
}
// non-ASCII round trip ("测试/文件.txt", spelled out in bytes so the source encoding
// cannot affect the test)
{
const std::string original = "\xE6\xB5\x8B\xE8\xAF\x95/\xE6\x96\x87\xE4\xBB\xB6.txt";
auto p = path::from_utf8(original);
BOOST_CHECK_EQUAL(path::to_utf8(p), original);
}
// the string_type overload agrees with the path one
{
auto p = path::from_utf8("hello.txt");
BOOST_CHECK_EQUAL(path::to_utf8(p.native()), path::to_utf8(p));
}
BOOST_CHECK_EQUAL(path::to_utf8(fs::path()), "");
}
BOOST_AUTO_TEST_CASE(test_canonical) {
// a path that exists resolves to an absolute, existing path
{
auto dir = system::application_directory();
auto c = path::canonical(dir);
BOOST_CHECK(!c.empty());
BOOST_CHECK(c.is_absolute());
BOOST_CHECK(fs::exists(c));
}
// "." resolves relative to the current directory
{
auto c = path::canonical(fs::path("."));
BOOST_CHECK(!c.empty());
BOOST_CHECK(fs::exists(c));
}
// a missing path reports failure by returning empty instead of throwing
{
auto c = path::canonical(fs::path("no_such_dir_9f3a/no_such_file_9f3a.txt"));
BOOST_CHECK(c.empty());
}
}
BOOST_AUTO_TEST_SUITE_END()