1 #include <msp/fs/utils.h>
2 #include <msp/test/test.h>
7 class FSUtilTests: public Test::RegisteredTest<FSUtilTests>
12 static const char *get_name() { return "fsutils"; }
21 FSUtilTests::FSUtilTests()
23 add(&FSUtilTests::pathsplit, "Path splitting");
24 add(&FSUtilTests::fnsplit, "Filename splitting");
25 add(&FSUtilTests::hierarchy, "Hierarchy");
26 add(&FSUtilTests::bad_hierarchy, "Bad hierarchy").expect_throw<invalid_argument>();
29 void FSUtilTests::pathsplit()
31 FS::Path path("/foo/bar/baz");
32 FS::Path dir = FS::dirname(path);
33 EXPECT_EQUAL(dir.str(), "/foo/bar");
34 string base = FS::basename(path);
35 EXPECT_EQUAL(base, "baz");
38 void FSUtilTests::fnsplit()
40 string filename = "file.png";
41 string base = FS::basepart(filename);
42 EXPECT_EQUAL(base, "file");
43 string ext = FS::extpart(filename);
44 EXPECT_EQUAL(ext, ".png");
46 filename = "file.tar.gz";
47 base = FS::basepart(filename);
48 EXPECT_EQUAL(base, "file.tar");
49 ext = FS::extpart(filename);
50 EXPECT_EQUAL(ext, ".gz");
53 void FSUtilTests::hierarchy()
55 FS::Path path1 = "/foo/bar";
56 FS::Path path2 = "/foo/bar/quux/baz";
57 int depth = FS::descendant_depth(path2, path1);
58 EXPECT_EQUAL(depth, 2);
59 FS::Path rel = FS::relative(path2, path1);
60 EXPECT_EQUAL(rel.str(), "./quux/baz");
62 path2 = "/foo/quux/baz";
63 depth = FS::descendant_depth(path2, path1);
64 EXPECT_EQUAL(depth, -1);
65 rel = FS::relative(path2, path1);
66 EXPECT_EQUAL(rel.str(), "../quux/baz");
67 FS::Path anc = FS::common_ancestor(path1, path2);
68 EXPECT_EQUAL(anc.str(), "/foo");
71 anc = FS::common_ancestor(path1, path2);
75 void FSUtilTests::bad_hierarchy()
77 FS::Path path1 = "/foo";
78 FS::Path path2 = "bar";
79 FS::relative(path2, path1);