• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/filesystem/operations.hpp>
11 #include <boost/predef.h>
12 #include "path.hpp"
13 
file_path_to_url_tests()14 void file_path_to_url_tests()
15 {
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")));
26     BOOST_TEST_EQ(
27         std::string("A%20B%2bC%2520"), file_path_to_url(path("A B+C%20")));
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/")));
31     BOOST_TEST_EQ(
32         std::string("file://hello/a/b"), file_path_to_url(path("//hello/a/b")));
33 
34 #if BOOST_OS_WINDOWS || BOOST_OS_CYGWIN
35     // Should this be file:///c:/x ?
36     BOOST_TEST_EQ(
37         std::string("file://?/a:/x"), file_path_to_url(path("\\\\?\\a:\\x")));
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:\\")));
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")));
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:")));
50     BOOST_TEST_EQ(
51         std::string("file:///c:foo/bar"), file_path_to_url(path("c:foo\\bar")));
52 #endif
53 }
54 
dir_path_to_url_tests()55 void dir_path_to_url_tests()
56 {
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")));
67     BOOST_TEST_EQ(
68         std::string("A%20B%2bC%2520/"), dir_path_to_url(path("A B+C%20")));
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/")));
72     BOOST_TEST_EQ(
73         std::string("file://hello/a/b/"), dir_path_to_url(path("//hello/a/b")));
74 
75 #if BOOST_OS_WINDOWS || BOOST_OS_CYGWIN
76     // Should this be file:///c:/x/ ?
77     BOOST_TEST_EQ(
78         std::string("file://?/a:/x/"), dir_path_to_url(path("\\\\?\\a:\\x")));
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:\\")));
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")));
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:")));
91     BOOST_TEST_EQ(
92         std::string("file:///c:foo/bar/"), dir_path_to_url(path("c:foo\\bar")));
93 #endif
94 }
95 
path_difference_tests()96 void path_difference_tests()
97 {
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("")));
107     BOOST_TEST(
108         path("..") == path_difference(current_path() / "a", current_path()));
109     BOOST_TEST(path("a") == path_difference(path(""), path("a")));
110     BOOST_TEST(
111         path("a") == path_difference(current_path(), current_path() / "a"));
112     BOOST_TEST(path("b") == path_difference(path("a"), path("a/b")));
113     BOOST_TEST(
114         path("b") ==
115         path_difference(current_path() / "a", current_path() / "a" / "b"));
116     BOOST_TEST(path("../a/b") == path_difference(path("c"), path("a/b")));
117     BOOST_TEST(
118         path("../a/b") ==
119         path_difference(current_path() / "c", current_path() / "a" / "b"));
120     BOOST_TEST(path("..") == path_difference(path(""), path("..")));
121     BOOST_TEST(
122         path("..") ==
123         path_difference(current_path(), current_path().parent_path()));
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")));
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")));
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;
141     // BOOST_TEST(path(".") == path_difference(path(""), path(""), true));
142     BOOST_TEST(path("../a") == path_difference(path("a"), path("a"), true));
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));
154     BOOST_TEST(path("a") == path_difference(path(""), path("a"), true));
155     BOOST_TEST(
156         path("a") ==
157         path_difference(current_path(), current_path() / "a", true));
158     BOOST_TEST(path("b") == path_difference(path("a"), path("a/b"), true));
159     BOOST_TEST(
160         path("b") ==
161         path_difference(
162             current_path() / "a", current_path() / "a" / "b", true));
163     BOOST_TEST(path("../a/b") == path_difference(path("c"), path("a/b"), true));
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() ==
171         path_difference(current_path(), current_path().parent_path(), true));
172     BOOST_TEST(path("b") == path_difference(path("a/c/.."), path("a/b"), true));
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));
181 }
182 
main()183 int main()
184 {
185     file_path_to_url_tests();
186     dir_path_to_url_tests();
187     path_difference_tests();
188     return boost::report_errors();
189 }
190