]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | /*============================================================================= |
2 | Copyright (c) 2015 Daniel James | |
3 | ||
4 | Use, modification and distribution is subject to the Boost Software | |
5 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
6 | http://www.boost.org/LICENSE_1_0.txt) | |
7 | =============================================================================*/ | |
8 | ||
b32b8144 FG |
9 | #include <boost/detail/lightweight_test.hpp> |
10 | #include <boost/filesystem/operations.hpp> | |
11 | #include <boost/predef.h> | |
11fdf7f2 | 12 | #include "path.hpp" |
b32b8144 | 13 | |
11fdf7f2 TL |
14 | void file_path_to_url_tests() |
15 | { | |
b32b8144 FG |
16 | using boost::filesystem::path; |
17 | using quickbook::file_path_to_url; | |
18 | ||
19 | BOOST_TEST_EQ(std::string(), file_path_to_url(path())); | |
20 | BOOST_TEST_EQ(std::string("."), file_path_to_url(path("."))); | |
21 | BOOST_TEST_EQ(std::string("./"), file_path_to_url(path("./"))); | |
22 | BOOST_TEST_EQ(std::string("a/b"), file_path_to_url(path("a/b"))); | |
23 | BOOST_TEST_EQ(std::string("a/b/"), file_path_to_url(path("a/b/"))); | |
24 | BOOST_TEST_EQ(std::string("./a/b"), file_path_to_url(path("./a/./././b"))); | |
25 | BOOST_TEST_EQ(std::string("../a/b"), file_path_to_url(path("../a/b"))); | |
11fdf7f2 TL |
26 | BOOST_TEST_EQ( |
27 | std::string("A%20B%2bC%2520"), file_path_to_url(path("A B+C%20"))); | |
b32b8144 FG |
28 | BOOST_TEST_EQ(std::string("file:///"), file_path_to_url(path("/"))); |
29 | BOOST_TEST_EQ(std::string("file:///a/b"), file_path_to_url(path("/a/b"))); | |
30 | BOOST_TEST_EQ(std::string("file:///a/b/"), file_path_to_url(path("/a/b/"))); | |
11fdf7f2 TL |
31 | BOOST_TEST_EQ( |
32 | std::string("file://hello/a/b"), file_path_to_url(path("//hello/a/b"))); | |
b32b8144 FG |
33 | |
34 | #if BOOST_OS_WINDOWS || BOOST_OS_CYGWIN | |
35 | // Should this be file:///c:/x ? | |
11fdf7f2 TL |
36 | BOOST_TEST_EQ( |
37 | std::string("file://?/a:/x"), file_path_to_url(path("\\\\?\\a:\\x"))); | |
b32b8144 FG |
38 | BOOST_TEST_EQ(std::string("file:///a"), file_path_to_url(path("\\a"))); |
39 | BOOST_TEST_EQ(std::string("file:///c:/"), file_path_to_url(path("c:\\"))); | |
11fdf7f2 TL |
40 | BOOST_TEST_EQ( |
41 | std::string("file:///c:/foo/bar"), | |
42 | file_path_to_url(path("c:\\foo\\bar"))); | |
43 | BOOST_TEST_EQ( | |
44 | std::string("file://localhost/c:/foo/bar"), | |
45 | file_path_to_url(path("\\\\localhost\\c:\\foo\\bar"))); | |
b32b8144 FG |
46 | |
47 | // Really not sure what to do with these examples. | |
48 | // Maybe an error? | |
49 | BOOST_TEST_EQ(std::string("file:///c:"), file_path_to_url(path("c:"))); | |
11fdf7f2 TL |
50 | BOOST_TEST_EQ( |
51 | std::string("file:///c:foo/bar"), file_path_to_url(path("c:foo\\bar"))); | |
b32b8144 FG |
52 | #endif |
53 | } | |
54 | ||
11fdf7f2 TL |
55 | void dir_path_to_url_tests() |
56 | { | |
b32b8144 FG |
57 | using boost::filesystem::path; |
58 | using quickbook::dir_path_to_url; | |
59 | ||
60 | BOOST_TEST_EQ(std::string("./"), dir_path_to_url(path())); | |
61 | BOOST_TEST_EQ(std::string("./"), dir_path_to_url(path("."))); | |
62 | BOOST_TEST_EQ(std::string("./"), dir_path_to_url(path("./"))); | |
63 | BOOST_TEST_EQ(std::string("a/b/"), dir_path_to_url(path("a/b"))); | |
64 | BOOST_TEST_EQ(std::string("a/b/"), dir_path_to_url(path("a/b/"))); | |
65 | BOOST_TEST_EQ(std::string("./a/b/"), dir_path_to_url(path("./a/./././b"))); | |
66 | BOOST_TEST_EQ(std::string("../a/b/"), dir_path_to_url(path("../a/b"))); | |
11fdf7f2 TL |
67 | BOOST_TEST_EQ( |
68 | std::string("A%20B%2bC%2520/"), dir_path_to_url(path("A B+C%20"))); | |
b32b8144 FG |
69 | BOOST_TEST_EQ(std::string("file:///"), dir_path_to_url(path("/"))); |
70 | BOOST_TEST_EQ(std::string("file:///a/b/"), dir_path_to_url(path("/a/b"))); | |
71 | BOOST_TEST_EQ(std::string("file:///a/b/"), dir_path_to_url(path("/a/b/"))); | |
11fdf7f2 TL |
72 | BOOST_TEST_EQ( |
73 | std::string("file://hello/a/b/"), dir_path_to_url(path("//hello/a/b"))); | |
b32b8144 FG |
74 | |
75 | #if BOOST_OS_WINDOWS || BOOST_OS_CYGWIN | |
76 | // Should this be file:///c:/x/ ? | |
11fdf7f2 TL |
77 | BOOST_TEST_EQ( |
78 | std::string("file://?/a:/x/"), dir_path_to_url(path("\\\\?\\a:\\x"))); | |
b32b8144 FG |
79 | BOOST_TEST_EQ(std::string("file:///a/"), dir_path_to_url(path("\\a"))); |
80 | BOOST_TEST_EQ(std::string("file:///c:/"), dir_path_to_url(path("c:\\"))); | |
11fdf7f2 TL |
81 | BOOST_TEST_EQ( |
82 | std::string("file:///c:/foo/bar/"), | |
83 | dir_path_to_url(path("c:\\foo\\bar"))); | |
84 | BOOST_TEST_EQ( | |
85 | std::string("file://localhost/c:/foo/bar/"), | |
86 | dir_path_to_url(path("\\\\localhost\\c:\\foo\\bar"))); | |
b32b8144 FG |
87 | |
88 | // Really not sure what to do with these examples. | |
89 | // Maybe an error? | |
90 | BOOST_TEST_EQ(std::string("file:///c:"), dir_path_to_url(path("c:"))); | |
11fdf7f2 TL |
91 | BOOST_TEST_EQ( |
92 | std::string("file:///c:foo/bar/"), dir_path_to_url(path("c:foo\\bar"))); | |
b32b8144 | 93 | #endif |
b32b8144 FG |
94 | } |
95 | ||
11fdf7f2 TL |
96 | void path_difference_tests() |
97 | { | |
b32b8144 FG |
98 | using boost::filesystem::current_path; |
99 | using boost::filesystem::path; | |
100 | using quickbook::path_difference; | |
101 | ||
102 | BOOST_TEST(path(".") == path_difference(path(""), path(""))); | |
103 | BOOST_TEST(path(".") == path_difference(path("a"), path("a"))); | |
104 | BOOST_TEST(path(".") == path_difference(path("a/../b"), path("b"))); | |
105 | BOOST_TEST(path(".") == path_difference(current_path(), current_path())); | |
106 | BOOST_TEST(path("..") == path_difference(path("a"), path(""))); | |
11fdf7f2 TL |
107 | BOOST_TEST( |
108 | path("..") == path_difference(current_path() / "a", current_path())); | |
b32b8144 | 109 | BOOST_TEST(path("a") == path_difference(path(""), path("a"))); |
11fdf7f2 TL |
110 | BOOST_TEST( |
111 | path("a") == path_difference(current_path(), current_path() / "a")); | |
b32b8144 | 112 | BOOST_TEST(path("b") == path_difference(path("a"), path("a/b"))); |
11fdf7f2 TL |
113 | BOOST_TEST( |
114 | path("b") == | |
115 | path_difference(current_path() / "a", current_path() / "a" / "b")); | |
b32b8144 | 116 | BOOST_TEST(path("../a/b") == path_difference(path("c"), path("a/b"))); |
11fdf7f2 TL |
117 | BOOST_TEST( |
118 | path("../a/b") == | |
119 | path_difference(current_path() / "c", current_path() / "a" / "b")); | |
b32b8144 | 120 | BOOST_TEST(path("..") == path_difference(path(""), path(".."))); |
11fdf7f2 TL |
121 | BOOST_TEST( |
122 | path("..") == | |
123 | path_difference(current_path(), current_path().parent_path())); | |
b32b8144 FG |
124 | BOOST_TEST(path("b") == path_difference(path("a/c/.."), path("a/b"))); |
125 | BOOST_TEST(path("b") == path_difference(path("b/c/../../a"), path("a/b"))); | |
11fdf7f2 TL |
126 | BOOST_TEST( |
127 | path("b") == | |
128 | path_difference(path("b/c/../../a"), path("d/f/../../a/b"))); | |
129 | BOOST_TEST( | |
130 | path("../../x/a/b") == | |
131 | path_difference(path("b/c/../../a"), path("d/f/../../../x/a/b"))); | |
b32b8144 FG |
132 | |
133 | // path_difference to a file, try to include the filename in the result, | |
134 | // although not always possible. Maybe nonsense calls should be an error? | |
135 | // | |
136 | // Commented out cases are wrong because path_difference resolves the paths | |
137 | // to the current working directory. In use this doesn't matter as it's | |
138 | // always called with the full path, but it'd be nice to get this right. | |
139 | // Or maybe just add the pre-condition to path_difference? | |
140 | std::cout << path_difference(path(""), path(""), true) << std::endl; | |
11fdf7f2 | 141 | // BOOST_TEST(path(".") == path_difference(path(""), path(""), true)); |
b32b8144 | 142 | BOOST_TEST(path("../a") == path_difference(path("a"), path("a"), true)); |
11fdf7f2 TL |
143 | BOOST_TEST( |
144 | path("../../a") == path_difference(path("a/b"), path("a"), true)); | |
145 | BOOST_TEST( | |
146 | path("../b") == path_difference(path("a/../b"), path("b"), true)); | |
147 | BOOST_TEST( | |
148 | ".." / current_path().filename() == | |
149 | path_difference(current_path(), current_path(), true)); | |
150 | // BOOST_TEST(path("..") == path_difference(path("a"), path(""), true)); | |
151 | BOOST_TEST( | |
152 | "../.." / current_path().filename() == | |
153 | path_difference(current_path() / "a", current_path(), true)); | |
b32b8144 | 154 | BOOST_TEST(path("a") == path_difference(path(""), path("a"), true)); |
11fdf7f2 TL |
155 | BOOST_TEST( |
156 | path("a") == | |
157 | path_difference(current_path(), current_path() / "a", true)); | |
b32b8144 | 158 | BOOST_TEST(path("b") == path_difference(path("a"), path("a/b"), true)); |
11fdf7f2 TL |
159 | BOOST_TEST( |
160 | path("b") == | |
161 | path_difference( | |
162 | current_path() / "a", current_path() / "a" / "b", true)); | |
b32b8144 | 163 | BOOST_TEST(path("../a/b") == path_difference(path("c"), path("a/b"), true)); |
11fdf7f2 TL |
164 | BOOST_TEST( |
165 | path("../a/b") == | |
166 | path_difference( | |
167 | current_path() / "c", current_path() / "a" / "b", true)); | |
168 | // BOOST_TEST(path("..") == path_difference(path(""), path(".."), true)); | |
169 | BOOST_TEST( | |
170 | "../.." / current_path().parent_path().filename() == | |
b32b8144 FG |
171 | path_difference(current_path(), current_path().parent_path(), true)); |
172 | BOOST_TEST(path("b") == path_difference(path("a/c/.."), path("a/b"), true)); | |
11fdf7f2 TL |
173 | BOOST_TEST( |
174 | path("b") == path_difference(path("b/c/../../a"), path("a/b"), true)); | |
175 | BOOST_TEST( | |
176 | path("b") == | |
177 | path_difference(path("b/c/../../a"), path("d/f/../../a/b"), true)); | |
178 | BOOST_TEST( | |
179 | path("../../x/a/b") == | |
180 | path_difference(path("b/c/../../a"), path("d/f/../../../x/a/b"), true)); | |
b32b8144 FG |
181 | } |
182 | ||
11fdf7f2 TL |
183 | int main() |
184 | { | |
b32b8144 FG |
185 | file_path_to_url_tests(); |
186 | dir_path_to_url_tests(); | |
187 | path_difference_tests(); | |
188 | return boost::report_errors(); | |
189 | } |