]> git.tdb.fi Git - libs/core.git/blob - tests/path.cpp
Add move semantics to Variant
[libs/core.git] / tests / path.cpp
1 #include <msp/fs/path.h>
2 #include <msp/test/test.h>
3
4 using namespace std;
5 using namespace Msp;
6
7 class PathTests: public Test::RegisteredTest<PathTests>
8 {
9 public:
10         PathTests();
11
12         static const char *get_name() { return "Path"; }
13
14 private:
15         void empty();
16         void normalization();
17         void concatenation();
18         void iterator_abs();
19         void iterator_rel();
20         void iterator_special();
21         void invalid_iterator();
22         void indexing();
23         void invalid_index();
24 };
25
26 PathTests::PathTests()
27 {
28         add(&PathTests::empty, "Emptiness");
29         add(&PathTests::normalization, "Normalization");
30         add(&PathTests::concatenation, "Concatenation");
31         add(&PathTests::iterator_abs, "Iterator (absolute path)");
32         add(&PathTests::iterator_rel, "Iterator (relative path)");
33         add(&PathTests::iterator_special, "Iterator (special cases)");
34         add(&PathTests::indexing, "Indexing");
35         add(&PathTests::invalid_index, "Invalid index").expect_throw<invalid_argument>();
36 }
37
38 void PathTests::empty()
39 {
40         EXPECT(FS::Path().empty());
41         EXPECT(FS::Path("").empty());
42         EXPECT(!FS::Path(".").empty());
43 }
44
45 void PathTests::normalization()
46 {
47         FS::Path path("foo");
48         EXPECT_EQUAL(path.str(), "./foo");
49         path = "foo/";
50         EXPECT_EQUAL(path.str(), "./foo");
51         path = "foo/.";
52         EXPECT_EQUAL(path.str(), "./foo");
53         path = "foo/..";
54         EXPECT_EQUAL(path.str(), ".");
55         path = "/foo/..";
56         EXPECT_EQUAL(path.str(), "/");
57         path = "//foo";
58         EXPECT_EQUAL(path.str(), "/foo");
59         path = "/..";
60         EXPECT_EQUAL(path.str(), "/");
61 }
62
63 void PathTests::concatenation()
64 {
65         FS::Path path(".");
66         path /= "foo";
67         EXPECT_EQUAL(path.str(), "./foo");
68         path /= ".";
69         EXPECT_EQUAL(path.str(), "./foo");
70         path /= "bar";
71         EXPECT_EQUAL(path.str(), "./foo/bar");
72         path /= "../../quux";
73         EXPECT_EQUAL(path.str(), "./quux");
74         path /= "/";
75         EXPECT_EQUAL(path.str(), "/");
76         path /= "..";
77         EXPECT_EQUAL(path.str(), "/");
78 }
79
80 void PathTests::iterator_abs()
81 {
82         FS::Path path("/foo/bar");
83         FS::Path::Iterator iter = path.begin();
84         EXPECT(iter==path.begin());
85         EXPECT_EQUAL(*iter, "/");
86         ++iter;
87         EXPECT_EQUAL(*iter, "foo");
88         ++iter;
89         EXPECT_EQUAL(*iter, "bar");
90         ++iter;
91         EXPECT(iter==path.end());
92 }
93
94 void PathTests::iterator_rel()
95 {
96         FS::Path path("./foo");
97         FS::Path::Iterator iter = path.begin();
98         EXPECT(iter==path.begin());
99         EXPECT_EQUAL(*iter, ".");
100         ++iter;
101         EXPECT_EQUAL(*iter, "foo");
102         ++iter;
103         EXPECT(iter==path.end());
104 }
105
106 void PathTests::iterator_special()
107 {
108         FS::Path path;
109         EXPECT(path.begin()==path.end());
110
111         path = ".";
112         FS::Path::Iterator iter = path.begin();
113         EXPECT_EQUAL(*iter, ".");
114         ++iter;
115         EXPECT(iter==path.end());
116
117         path = "/";
118         iter = path.begin();
119         EXPECT_EQUAL(*iter, "/");
120         ++iter;
121         EXPECT(iter==path.end());
122 }
123
124 void PathTests::indexing()
125 {
126         FS::Path path("/foo/bar");
127         EXPECT_EQUAL(path.size(), 3);
128         EXPECT_EQUAL(path[0], "/");
129         EXPECT_EQUAL(path[1], "foo");
130         EXPECT_EQUAL(path[-1], "bar");
131 }
132
133 void PathTests::invalid_index()
134 {
135         FS::Path path("foo");
136         path[3];
137 }