]> git.tdb.fi Git - libs/core.git/blob - tests/path.cpp
Invent a value for argv[0] if not present
[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(), "/foo");
57         path = "/..";
58         EXPECT_EQUAL(path.str(), "/");
59 }
60
61 void PathTests::concatenation()
62 {
63         FS::Path path(".");
64         path /= "foo";
65         EXPECT_EQUAL(path.str(), "./foo");
66         path /= ".";
67         EXPECT_EQUAL(path.str(), "./foo");
68         path /= "bar";
69         EXPECT_EQUAL(path.str(), "./foo/bar");
70         path /= "../../quux";
71         EXPECT_EQUAL(path.str(), "./quux");
72         path /= "/";
73         EXPECT_EQUAL(path.str(), "/");
74         path /= "..";
75         EXPECT_EQUAL(path.str(), "/");
76 }
77
78 void PathTests::iterator_abs()
79 {
80         FS::Path path("/foo/bar");
81         FS::Path::Iterator iter = path.begin();
82         EXPECT(iter==path.begin());
83         EXPECT_EQUAL(*iter, "/");
84         ++iter;
85         EXPECT_EQUAL(*iter, "foo");
86         ++iter;
87         EXPECT_EQUAL(*iter, "bar");
88         ++iter;
89         EXPECT(iter==path.end());
90 }
91
92 void PathTests::iterator_rel()
93 {
94         FS::Path path("./foo");
95         FS::Path::Iterator iter = path.begin();
96         EXPECT(iter==path.begin());
97         EXPECT_EQUAL(*iter, ".");
98         ++iter;
99         EXPECT_EQUAL(*iter, "foo");
100         ++iter;
101         EXPECT(iter==path.end());
102 }
103
104 void PathTests::iterator_special()
105 {
106         FS::Path path;
107         EXPECT(path.begin()==path.end());
108
109         path = ".";
110         FS::Path::Iterator iter = path.begin();
111         EXPECT_EQUAL(*iter, ".");
112         ++iter;
113         EXPECT(iter==path.end());
114
115         path = "/";
116         iter = path.begin();
117         EXPECT_EQUAL(*iter, "/");
118         ++iter;
119         EXPECT(iter==path.end());
120 }
121
122 void PathTests::indexing()
123 {
124         FS::Path path("/foo/bar");
125         EXPECT_EQUAL(path.size(), 3);
126         EXPECT_EQUAL(path[0], "/");
127         EXPECT_EQUAL(path[1], "foo");
128         EXPECT_EQUAL(path[-1], "bar");
129 }
130
131 void PathTests::invalid_index()
132 {
133         FS::Path path("foo");
134         path[3];
135 }