]> git.tdb.fi Git - libs/core.git/blob - tests/path.cpp
Add test cases for path manipulation functions
[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::invalid_iterator, "Invalid iterator").expect_throw<logic_error>();
35         add(&PathTests::indexing, "Indexing");
36         add(&PathTests::invalid_index, "Invalid index").expect_throw<invalid_argument>();
37 }
38
39 void PathTests::empty()
40 {
41         EXPECT(FS::Path().empty());
42         EXPECT(FS::Path("").empty());
43         EXPECT(!FS::Path(".").empty());
44 }
45
46 void PathTests::normalization()
47 {
48         FS::Path path("foo");
49         EXPECT_EQUAL(path.str(), "./foo");
50         path = "foo/";
51         EXPECT_EQUAL(path.str(), "./foo");
52         path = "foo/.";
53         EXPECT_EQUAL(path.str(), "./foo");
54         path = "foo/..";
55         EXPECT_EQUAL(path.str(), ".");
56         path = "//foo";
57         EXPECT_EQUAL(path.str(), "/foo");
58         path = "/..";
59         EXPECT_EQUAL(path.str(), "/");
60 }
61
62 void PathTests::concatenation()
63 {
64         FS::Path path(".");
65         path /= "foo";
66         EXPECT_EQUAL(path.str(), "./foo");
67         path /= ".";
68         EXPECT_EQUAL(path.str(), "./foo");
69         path /= "bar";
70         EXPECT_EQUAL(path.str(), "./foo/bar");
71         path /= "../../quux";
72         EXPECT_EQUAL(path.str(), "./quux");
73         path /= "/";
74         EXPECT_EQUAL(path.str(), "/");
75         path /= "..";
76         EXPECT_EQUAL(path.str(), "/");
77 }
78
79 void PathTests::iterator_abs()
80 {
81         FS::Path path("/foo/bar");
82         FS::Path::Iterator iter = path.begin();
83         EXPECT(iter==path.begin());
84         EXPECT_EQUAL(*iter, "/");
85         ++iter;
86         EXPECT_EQUAL(*iter, "foo");
87         ++iter;
88         EXPECT_EQUAL(*iter, "bar");
89         ++iter;
90         EXPECT(iter==path.end());
91 }
92
93 void PathTests::iterator_rel()
94 {
95         FS::Path path("./foo");
96         FS::Path::Iterator iter = path.begin();
97         EXPECT(iter==path.begin());
98         EXPECT_EQUAL(*iter, ".");
99         ++iter;
100         EXPECT_EQUAL(*iter, "foo");
101         ++iter;
102         EXPECT(iter==path.end());
103 }
104
105 void PathTests::iterator_special()
106 {
107         FS::Path path;
108         EXPECT(path.begin()==path.end());
109
110         path = ".";
111         FS::Path::Iterator iter = path.begin();
112         EXPECT_EQUAL(*iter, ".");
113         ++iter;
114         EXPECT(iter==path.end());
115
116         path = "/";
117         iter = path.begin();
118         EXPECT_EQUAL(*iter, "/");
119         ++iter;
120         EXPECT(iter==path.end());
121 }
122
123 void PathTests::invalid_iterator()
124 {
125         FS::Path path("foo");
126         *path.end();
127 }
128
129 void PathTests::indexing()
130 {
131         FS::Path path("/foo/bar");
132         EXPECT_EQUAL(path.size(), 3);
133         EXPECT_EQUAL(path[0], "/");
134         EXPECT_EQUAL(path[1], "foo");
135         EXPECT_EQUAL(path[-1], "bar");
136 }
137
138 void PathTests::invalid_index()
139 {
140         FS::Path path("foo");
141         path[3];
142 }