]> git.tdb.fi Git - libs/core.git/blob - tests/path.cpp
Make FS::Path::Iterator assignable
[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 indexing();
21 };
22
23 PathTests::PathTests()
24 {
25         add(&PathTests::empty, "Emptiness");
26         add(&PathTests::normalization, "Normalization");
27         add(&PathTests::concatenation, "Concatenation");
28         add(&PathTests::iterator_abs, "Iterator (absolute path)");
29         add(&PathTests::iterator_rel, "Iterator (relative path)");
30         add(&PathTests::indexing, "Indexing");
31 }
32
33 void PathTests::empty()
34 {
35         EXPECT(FS::Path().empty());
36         EXPECT(!FS::Path(".").empty());
37 }
38
39 void PathTests::normalization()
40 {
41         FS::Path path("foo");
42         EXPECT_EQUAL(path.str(), "./foo");
43         path = "foo/";
44         EXPECT_EQUAL(path.str(), "./foo");
45         path = "foo/.";
46         EXPECT_EQUAL(path.str(), "./foo");
47         path = "foo/..";
48         EXPECT_EQUAL(path.str(), ".");
49         path = "//foo";
50         EXPECT_EQUAL(path.str(), "/foo");
51         path = "/..";
52         EXPECT_EQUAL(path.str(), "/");
53 }
54
55 void PathTests::concatenation()
56 {
57         FS::Path path(".");
58         path /= "foo";
59         EXPECT_EQUAL(path.str(), "./foo");
60         path /= ".";
61         EXPECT_EQUAL(path.str(), "./foo");
62         path /= "bar";
63         EXPECT_EQUAL(path.str(), "./foo/bar");
64         path /= "../../quux";
65         EXPECT_EQUAL(path.str(), "./quux");
66         path /= "/";
67         EXPECT_EQUAL(path.str(), "/");
68         path /= "..";
69         EXPECT_EQUAL(path.str(), "/");
70 }
71
72 void PathTests::iterator_abs()
73 {
74         FS::Path path("/foo/bar");
75         FS::Path::Iterator iter = path.begin();
76         EXPECT(iter==path.begin());
77         EXPECT_EQUAL(*iter, "/");
78         ++iter;
79         EXPECT_EQUAL(*iter, "foo");
80         ++iter;
81         EXPECT_EQUAL(*iter, "bar");
82         ++iter;
83         EXPECT(iter==path.end());
84 }
85
86 void PathTests::iterator_rel()
87 {
88         FS::Path path("./foo");
89         FS::Path::Iterator iter = path.begin();
90         EXPECT(iter==path.begin());
91         EXPECT_EQUAL(*iter, ".");
92         ++iter;
93         EXPECT_EQUAL(*iter, "foo");
94         ++iter;
95         EXPECT(iter==path.end());
96 }
97
98 void PathTests::indexing()
99 {
100         FS::Path path("/foo/bar");
101         EXPECT_EQUAL(path.size(), 3);
102         EXPECT_EQUAL(path[0], "/");
103         EXPECT_EQUAL(path[1], "foo");
104         EXPECT_EQUAL(path[-1], "bar");
105 }