summaryrefslogtreecommitdiffstats
path: root/src/boost/tools/quickbook/test/unit/path_test.cpp
blob: 734769ef6e55cd3fc639bdf04e7efa949377bbe1 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*=============================================================================
    Copyright (c) 2015 Daniel James

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/

#include <boost/detail/lightweight_test.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/predef.h>
#include "path.hpp"

void file_path_to_url_tests()
{
    using boost::filesystem::path;
    using quickbook::file_path_to_url;

    BOOST_TEST_EQ(std::string(), file_path_to_url(path()));
    BOOST_TEST_EQ(std::string("."), file_path_to_url(path(".")));
    BOOST_TEST_EQ(std::string("./"), file_path_to_url(path("./")));
    BOOST_TEST_EQ(std::string("a/b"), file_path_to_url(path("a/b")));
    BOOST_TEST_EQ(std::string("a/b/"), file_path_to_url(path("a/b/")));
    BOOST_TEST_EQ(std::string("./a/b"), file_path_to_url(path("./a/./././b")));
    BOOST_TEST_EQ(std::string("../a/b"), file_path_to_url(path("../a/b")));
    BOOST_TEST_EQ(
        std::string("A%20B%2bC%2520"), file_path_to_url(path("A B+C%20")));
    BOOST_TEST_EQ(std::string("file:///"), file_path_to_url(path("/")));
    BOOST_TEST_EQ(std::string("file:///a/b"), file_path_to_url(path("/a/b")));
    BOOST_TEST_EQ(std::string("file:///a/b/"), file_path_to_url(path("/a/b/")));
    BOOST_TEST_EQ(
        std::string("file://hello/a/b"), file_path_to_url(path("//hello/a/b")));

#if BOOST_OS_WINDOWS || BOOST_OS_CYGWIN
    // Should this be file:///c:/x ?
    BOOST_TEST_EQ(
        std::string("file://?/a:/x"), file_path_to_url(path("\\\\?\\a:\\x")));
    BOOST_TEST_EQ(std::string("file:///a"), file_path_to_url(path("\\a")));
    BOOST_TEST_EQ(std::string("file:///c:/"), file_path_to_url(path("c:\\")));
    BOOST_TEST_EQ(
        std::string("file:///c:/foo/bar"),
        file_path_to_url(path("c:\\foo\\bar")));
    BOOST_TEST_EQ(
        std::string("file://localhost/c:/foo/bar"),
        file_path_to_url(path("\\\\localhost\\c:\\foo\\bar")));

    // Really not sure what to do with these examples.
    // Maybe an error?
    BOOST_TEST_EQ(std::string("file:///c:"), file_path_to_url(path("c:")));
    BOOST_TEST_EQ(
        std::string("file:///c:foo/bar"), file_path_to_url(path("c:foo\\bar")));
#endif
}

void dir_path_to_url_tests()
{
    using boost::filesystem::path;
    using quickbook::dir_path_to_url;

    BOOST_TEST_EQ(std::string("./"), dir_path_to_url(path()));
    BOOST_TEST_EQ(std::string("./"), dir_path_to_url(path(".")));
    BOOST_TEST_EQ(std::string("./"), dir_path_to_url(path("./")));
    BOOST_TEST_EQ(std::string("a/b/"), dir_path_to_url(path("a/b")));
    BOOST_TEST_EQ(std::string("a/b/"), dir_path_to_url(path("a/b/")));
    BOOST_TEST_EQ(std::string("./a/b/"), dir_path_to_url(path("./a/./././b")));
    BOOST_TEST_EQ(std::string("../a/b/"), dir_path_to_url(path("../a/b")));
    BOOST_TEST_EQ(
        std::string("A%20B%2bC%2520/"), dir_path_to_url(path("A B+C%20")));
    BOOST_TEST_EQ(std::string("file:///"), dir_path_to_url(path("/")));
    BOOST_TEST_EQ(std::string("file:///a/b/"), dir_path_to_url(path("/a/b")));
    BOOST_TEST_EQ(std::string("file:///a/b/"), dir_path_to_url(path("/a/b/")));
    BOOST_TEST_EQ(
        std::string("file://hello/a/b/"), dir_path_to_url(path("//hello/a/b")));

#if BOOST_OS_WINDOWS || BOOST_OS_CYGWIN
    // Should this be file:///c:/x/ ?
    BOOST_TEST_EQ(
        std::string("file://?/a:/x/"), dir_path_to_url(path("\\\\?\\a:\\x")));
    BOOST_TEST_EQ(std::string("file:///a/"), dir_path_to_url(path("\\a")));
    BOOST_TEST_EQ(std::string("file:///c:/"), dir_path_to_url(path("c:\\")));
    BOOST_TEST_EQ(
        std::string("file:///c:/foo/bar/"),
        dir_path_to_url(path("c:\\foo\\bar")));
    BOOST_TEST_EQ(
        std::string("file://localhost/c:/foo/bar/"),
        dir_path_to_url(path("\\\\localhost\\c:\\foo\\bar")));

    // Really not sure what to do with these examples.
    // Maybe an error?
    BOOST_TEST_EQ(std::string("file:///c:"), dir_path_to_url(path("c:")));
    BOOST_TEST_EQ(
        std::string("file:///c:foo/bar/"), dir_path_to_url(path("c:foo\\bar")));
#endif
}

void path_difference_tests()
{
    using boost::filesystem::current_path;
    using boost::filesystem::path;
    using quickbook::path_difference;

    BOOST_TEST(path(".") == path_difference(path(""), path("")));
    BOOST_TEST(path(".") == path_difference(path("a"), path("a")));
    BOOST_TEST(path(".") == path_difference(path("a/../b"), path("b")));
    BOOST_TEST(path(".") == path_difference(current_path(), current_path()));
    BOOST_TEST(path("..") == path_difference(path("a"), path("")));
    BOOST_TEST(
        path("..") == path_difference(current_path() / "a", current_path()));
    BOOST_TEST(path("a") == path_difference(path(""), path("a")));
    BOOST_TEST(
        path("a") == path_difference(current_path(), current_path() / "a"));
    BOOST_TEST(path("b") == path_difference(path("a"), path("a/b")));
    BOOST_TEST(
        path("b") ==
        path_difference(current_path() / "a", current_path() / "a" / "b"));
    BOOST_TEST(path("../a/b") == path_difference(path("c"), path("a/b")));
    BOOST_TEST(
        path("../a/b") ==
        path_difference(current_path() / "c", current_path() / "a" / "b"));
    BOOST_TEST(path("..") == path_difference(path(""), path("..")));
    BOOST_TEST(
        path("..") ==
        path_difference(current_path(), current_path().parent_path()));
    BOOST_TEST(path("b") == path_difference(path("a/c/.."), path("a/b")));
    BOOST_TEST(path("b") == path_difference(path("b/c/../../a"), path("a/b")));
    BOOST_TEST(
        path("b") ==
        path_difference(path("b/c/../../a"), path("d/f/../../a/b")));
    BOOST_TEST(
        path("../../x/a/b") ==
        path_difference(path("b/c/../../a"), path("d/f/../../../x/a/b")));

    // path_difference to a file, try to include the filename in the result,
    // although not always possible. Maybe nonsense calls should be an error?
    //
    // Commented out cases are wrong because path_difference resolves the paths
    // to the current working directory. In use this doesn't matter as it's
    // always called with the full path, but it'd be nice to get this right.
    // Or maybe just add the pre-condition to path_difference?
    std::cout << path_difference(path(""), path(""), true) << std::endl;
    // BOOST_TEST(path(".") == path_difference(path(""), path(""), true));
    BOOST_TEST(path("../a") == path_difference(path("a"), path("a"), true));
    BOOST_TEST(
        path("../../a") == path_difference(path("a/b"), path("a"), true));
    BOOST_TEST(
        path("../b") == path_difference(path("a/../b"), path("b"), true));
    BOOST_TEST(
        ".." / current_path().filename() ==
        path_difference(current_path(), current_path(), true));
    // BOOST_TEST(path("..") == path_difference(path("a"), path(""), true));
    BOOST_TEST(
        "../.." / current_path().filename() ==
        path_difference(current_path() / "a", current_path(), true));
    BOOST_TEST(path("a") == path_difference(path(""), path("a"), true));
    BOOST_TEST(
        path("a") ==
        path_difference(current_path(), current_path() / "a", true));
    BOOST_TEST(path("b") == path_difference(path("a"), path("a/b"), true));
    BOOST_TEST(
        path("b") ==
        path_difference(
            current_path() / "a", current_path() / "a" / "b", true));
    BOOST_TEST(path("../a/b") == path_difference(path("c"), path("a/b"), true));
    BOOST_TEST(
        path("../a/b") ==
        path_difference(
            current_path() / "c", current_path() / "a" / "b", true));
    // BOOST_TEST(path("..") == path_difference(path(""), path(".."), true));
    BOOST_TEST(
        "../.." / current_path().parent_path().filename() ==
        path_difference(current_path(), current_path().parent_path(), true));
    BOOST_TEST(path("b") == path_difference(path("a/c/.."), path("a/b"), true));
    BOOST_TEST(
        path("b") == path_difference(path("b/c/../../a"), path("a/b"), true));
    BOOST_TEST(
        path("b") ==
        path_difference(path("b/c/../../a"), path("d/f/../../a/b"), true));
    BOOST_TEST(
        path("../../x/a/b") ==
        path_difference(path("b/c/../../a"), path("d/f/../../../x/a/b"), true));
}

int main()
{
    file_path_to_url_tests();
    dir_path_to_url_tests();
    path_difference_tests();
    return boost::report_errors();
}