-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.cpp
289 lines (243 loc) · 10.3 KB
/
test.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/******************************************************************************
* Copyright (c) 2013 Dan Lecocq
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <algorithm>
/* Internal libraries */
#include "path.hpp"
using namespace apathy;
TEST_CASE("path", "Path functionality works as advertised") {
SECTION("cwd", "And equivalent vs ==") {
Path cwd(Path::cwd());
Path empty("");
REQUIRE(cwd != empty);
REQUIRE(cwd.equivalent(empty));
REQUIRE(empty.equivalent(cwd));
REQUIRE(cwd.is_absolute());
REQUIRE(!empty.is_absolute());
REQUIRE(empty.absolute() == cwd);
REQUIRE(Path() == "");
}
SECTION("operator=", "Make sure assignment works as expected") {
Path cwd(Path::cwd());
Path empty("");
REQUIRE(cwd != empty);
empty = cwd;
REQUIRE(cwd == empty);
}
SECTION("operator+=", "Make sure operator<< works correctly") {
Path root("/");
root << "hello" << "how" << "are" << "you";
REQUIRE(root.string() == "/hello/how/are/you");
/* It also needs to be able to accept things like floats, ints, etc. */
root = Path("/");
root << "hello" << 5 << "how" << 3.14 << "are";
REQUIRE(root.string() == "/hello/5/how/3.14/are");
}
SECTION("operator+", "Make sure operator+ works correctly") {
Path root("foo/bar");
REQUIRE((root + "baz").string() == "foo/bar/baz");
}
SECTION("trim", "Make sure trim actually strips off separators") {
Path root("/hello/how/are/you////");
REQUIRE(root.trim().string() == "/hello/how/are/you");
root = Path("/hello/how/are/you");
REQUIRE(root.trim().string() == "/hello/how/are/you");
root = Path("/hello/how/are/you/");
REQUIRE(root.trim().string() == "/hello/how/are/you");
}
SECTION("directory", "Make sure we can make paths into directories") {
Path root("/hello/how/are/you");
REQUIRE(root.directory().string() == "/hello/how/are/you/");
root = Path("/hello/how/are/you/");
REQUIRE(root.directory().string() == "/hello/how/are/you/");
root = Path("/hello/how/are/you//");
REQUIRE(root.directory().string() == "/hello/how/are/you/");
}
SECTION("relative", "Evaluates relative urls correctly") {
Path a("/hello/how/are/you");
Path b("foo");
REQUIRE(a.relative(b).string() == "/hello/how/are/you/foo");
a = Path("/hello/how/are/you/");
REQUIRE(a.relative(b).string() == "/hello/how/are/you/foo");
b = Path("/fine/thank/you");
REQUIRE(a.relative(b).string() == "/fine/thank/you");
}
SECTION("parent", "Make sure we can find the parent directory") {
Path a("/hello/how/are/you");
REQUIRE(a.parent().string() == "/hello/how/are/");
a = Path("/hello/how/are/you");
REQUIRE(a.parent().parent().string() == "/hello/how/");
/* / is its own parent, at least according to bash:
*
* cd / && cd ..
*/
a = Path("/");
REQUIRE(a.parent().string() == "/");
a = Path("");
REQUIRE(a.parent() != Path::cwd().parent());
REQUIRE(a.parent().equivalent(Path::cwd().parent()));
a = Path("foo/bar");
REQUIRE(a.parent().parent() == "");
a = Path("foo/../bar/baz/a/../");
REQUIRE(a.parent() == "bar/");
}
SECTION("makedirs", "Make sure we recursively make directories") {
Path path("foo");
REQUIRE(!path.exists());
path << "bar" << "baz" << "whiz";
Path::makedirs(path);
REQUIRE(path.exists());
REQUIRE(path.is_directory());
/* Now, we should remove the directories, make sure it's gone. */
REQUIRE(Path::rmdirs("foo"));
REQUIRE(!Path("foo").exists());
}
SECTION("listdirs", "Make sure we can list directories") {
Path path("foo");
path << "bar" << "baz" << "whiz";
Path::makedirs(path);
REQUIRE(path.exists());
/* Now touch some files in this area */
Path::touch(Path(path).append("a"));
Path::touch(Path(path).append("b"));
Path::touch(Path(path).append("c"));
/* Now list that directory */
std::vector<Path> files = Path::listdir(path);
REQUIRE(files.size() == 3);
/* listdir doesn't enforce any ordering */
REQUIRE((std::find(files.begin(), files.end(),
Path(path).absolute().append("a").string()) != files.end()));
REQUIRE((std::find(files.begin(), files.end(),
Path(path).absolute().append("b").string()) != files.end()));
REQUIRE((std::find(files.begin(), files.end(),
Path(path).absolute().append("c").string()) != files.end()));
REQUIRE(Path::rmdirs("foo"));
REQUIRE(!Path("foo").exists());
}
SECTION("rm", "Make sure we can remove files we create") {
REQUIRE(!Path("foo").exists());
Path::touch("foo");
REQUIRE( Path("foo").exists());
Path::rm("foo");
REQUIRE(!Path("foo").exists());
}
SECTION("move", "Make sure we can move files / directories") {
/* We should be able to move it in the most basic case */
Path source("foo");
Path dest("bar");
REQUIRE(!source.exists());
REQUIRE(! dest.exists());
Path::touch(source);
REQUIRE(Path::move(source, dest));
REQUIRE(!source.exists());
REQUIRE( dest.exists());
REQUIRE(Path::rm(dest));
REQUIRE(!source.exists());
REQUIRE(! dest.exists());
/* And now, when the directory doesn't exist */
dest = "bar/baz";
REQUIRE(!dest.parent().exists());
Path::touch(source);
REQUIRE(!Path::move(source, dest));
REQUIRE( Path::move(source, dest, true));
REQUIRE(!source.exists());
REQUIRE( dest.exists());
Path::rmdirs("bar");
REQUIRE(!Path("bar").exists());
}
SECTION("sanitize", "Make sure we can sanitize a path") {
Path path("foo///bar/a/b/../c");
REQUIRE(path.sanitize() == "foo/bar/a/c");
path = "../foo///bar/a/b/../c";
REQUIRE(path.sanitize() == "../foo/bar/a/c");
path = "../../a/b////c";
REQUIRE(path.sanitize() == "../../a/b/c");
path = "/../../a/b////c";
REQUIRE(path.sanitize() == "/a/b/c");
path = "/./././a/./b/../../c";
REQUIRE(path.sanitize() == "/c");
path = "././a/b/c/";
REQUIRE(path.sanitize() == "a/b/c/");
}
SECTION("equivalent", "Make sure equivalent paths work") {
Path a("foo////a/b/../c/");
Path b("foo/a/c/");
REQUIRE(a.equivalent(b));
a = "../foo/bar/";
b = Path::cwd().parent().append("foo").append("bar").directory();
REQUIRE(a.equivalent(b));
}
SECTION("split", "Make sure we can get segments out") {
Path a("foo/bar/baz");
std::vector<Path::Segment> segments(a.split());
REQUIRE(segments.size() == 3);
REQUIRE(segments[0].segment == "foo");
REQUIRE(segments[1].segment == "bar");
REQUIRE(segments[2].segment == "baz");
a = Path("foo/bar/baz/");
REQUIRE(a.split().size() == 4);
a = Path("/foo/bar/baz/");
REQUIRE(a.split().size() == 5);
}
SECTION("extension", "Make sure we can accurately get th file extension") {
/* Works in a basic way */
REQUIRE(Path("foo/bar/baz.out").extension() == "out");
/* Gets the outermost extension */
REQUIRE(Path("foo/bar.baz.out").extension() == "out");
/* Doesn't take extensions from directories */
REQUIRE(Path("foo/bar.baz/out").extension() == "");
}
SECTION("stem", "Make sure we can get the path stem") {
/* Works in a basic way */
REQUIRE(Path("foo/bar/baz.out").stem() == Path("foo/bar/baz"));
/* Gets the outermost extension */
REQUIRE(Path("foo/bar.baz.out").stem() == Path("foo/bar.baz"));
/* Doesn't take extensions from directories */
REQUIRE(Path("foo/bar.baz/out").stem() == Path("foo/bar.baz/out"));
/* Can be used to successively pop off the extension */
Path a("foo.bar.baz.out");
a = a.stem(); REQUIRE(a == Path("foo.bar.baz"));
a = a.stem(); REQUIRE(a == Path("foo.bar"));
a = a.stem(); REQUIRE(a == Path("foo"));
a = a.stem(); REQUIRE(a == Path("foo"));
}
SECTION("glob", "Make sure glob works") {
/* We'll touch a bunch of files to work with */
Path::makedirs("foo");
Path::touch("foo/bar");
Path::touch("foo/bar2");
Path::touch("foo/bar3");
Path::touch("foo/baz");
Path::touch("foo/bazzy");
Path::touch("foo/foo");
/* Make sure we can get it to work in a few basic ways */
REQUIRE(Path::glob("foo/*" ).size() == 6);
REQUIRE(Path::glob("foo/b*" ).size() == 5);
REQUIRE(Path::glob("foo/baz*").size() == 2);
REQUIRE(Path::glob("foo/ba?" ).size() == 2);
/* Now, we should remove the directories, make sure it's gone. */
REQUIRE(Path::rmdirs("foo"));
REQUIRE(!Path("foo").exists());
}
}