1 //===- PathTest.cpp -------------------------------------------------------===//
2 //
3 // The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "PathTest.h"
10 #include "mcld/Support/FileSystem.h"
11 #include <string>
12
13 //
14 using namespace mcld;
15 using namespace mcld::sys::fs;
16 using namespace mcldtest;
17
18 // Constructor can do set-up work for all test here.
PathTest()19 PathTest::PathTest() {
20 // create testee. modify it if need
21 m_pTestee = new Path();
22 }
23
24 // Destructor can do clean-up work that doesn't throw exceptions here.
~PathTest()25 PathTest::~PathTest() {
26 delete m_pTestee;
27 }
28
29 // SetUp() will be called immediately before each test.
SetUp()30 void PathTest::SetUp() {
31 }
32
33 // TearDown() will be called immediately after each test.
TearDown()34 void PathTest::TearDown() {
35 }
36
37 //==========================================================================//
38 // Testcases
39 //
TEST_F(PathTest,should_exist)40 TEST_F(PathTest, should_exist) {
41 std::string root(TOPDIR);
42 root += "/test/lit.cfg";
43 m_pTestee->assign(root);
44 EXPECT_TRUE(exists(*m_pTestee));
45
46 delete m_pTestee;
47 m_pTestee = new Path(root);
48 EXPECT_TRUE(exists(*m_pTestee));
49 }
50
TEST_F(PathTest,should_not_exist)51 TEST_F(PathTest, should_not_exist) {
52 const std::string root = "/luck";
53 m_pTestee->assign(root);
54 EXPECT_FALSE(exists(*m_pTestee));
55
56 delete m_pTestee;
57 m_pTestee = new Path(root);
58 EXPECT_FALSE(exists(*m_pTestee));
59 }
60
TEST_F(PathTest,should_is_directory)61 TEST_F(PathTest, should_is_directory) {
62 const std::string root = "../././..";
63 m_pTestee->assign(root);
64 EXPECT_TRUE(exists(*m_pTestee));
65 EXPECT_TRUE(is_directory(*m_pTestee));
66 delete m_pTestee;
67 m_pTestee = new Path(root);
68 EXPECT_TRUE(exists(*m_pTestee));
69 EXPECT_TRUE(is_directory(*m_pTestee));
70 }
71
TEST_F(PathTest,should_not_is_directory)72 TEST_F(PathTest, should_not_is_directory) {
73 const std::string root = "/luck";
74 m_pTestee->assign(root);
75 EXPECT_FALSE(exists(*m_pTestee));
76 EXPECT_FALSE(is_directory(*m_pTestee));
77 delete m_pTestee;
78 m_pTestee = new Path(root);
79 EXPECT_FALSE(exists(*m_pTestee));
80 EXPECT_FALSE(is_directory(*m_pTestee));
81 }
82
TEST_F(PathTest,should_equal)83 TEST_F(PathTest, should_equal) {
84 const std::string root = "aaa/bbb/../../ccc/";
85 m_pTestee->assign(root);
86
87 Path* p2 = new Path("ccc///////");
88
89 EXPECT_TRUE(*m_pTestee == *p2);
90
91 delete m_pTestee;
92 m_pTestee = new Path(root);
93 EXPECT_TRUE(*m_pTestee == *m_pTestee);
94 delete p2;
95 }
96
TEST_F(PathTest,should_not_equal)97 TEST_F(PathTest, should_not_equal) {
98 const std::string root = "aa/";
99 Path* p2 = new Path("aaa//");
100 // p2->assign(root);
101 m_pTestee->assign(root);
102 EXPECT_TRUE(*m_pTestee != *p2);
103
104 delete m_pTestee;
105 m_pTestee = new Path(root);
106 EXPECT_TRUE(*m_pTestee != *p2);
107 delete p2;
108 }
109
TEST_F(PathTest,append_success)110 TEST_F(PathTest, append_success) {
111 const std::string root = "aa/";
112 m_pTestee->assign(root);
113 m_pTestee->append("aaa");
114 std::string a("aa/aaa");
115 EXPECT_TRUE(m_pTestee->native() == "aa/aaa");
116 delete m_pTestee;
117 m_pTestee = new Path("aa/");
118 m_pTestee->append("/aaa");
119 EXPECT_TRUE(m_pTestee->native() == "aa/aaa");
120 delete m_pTestee;
121 m_pTestee = new Path("aa");
122 m_pTestee->append("/aaa");
123 EXPECT_TRUE(m_pTestee->native() == "aa/aaa");
124 delete m_pTestee;
125 m_pTestee = new Path("aa");
126 m_pTestee->append("aaa");
127 EXPECT_TRUE(m_pTestee->native() == "aa/aaa");
128 }
129
TEST_F(PathTest,should_become_generic_string)130 TEST_F(PathTest, should_become_generic_string) {
131 m_pTestee->assign("/etc/../dev/../usr//lib//");
132 EXPECT_STREQ("/usr/lib/", m_pTestee->generic_string().c_str());
133 }
134
TEST_F(PathTest,parent_path)135 TEST_F(PathTest, parent_path) {
136 m_pTestee->assign("aa/bb/cc/dd");
137 EXPECT_STREQ("aa/bb/cc", m_pTestee->parent_path().c_str());
138 delete m_pTestee;
139 m_pTestee = new Path("/aa/bb/");
140 EXPECT_STREQ("/aa/bb", m_pTestee->parent_path().c_str());
141 delete m_pTestee;
142 m_pTestee = new Path("/aa/bb");
143 EXPECT_STREQ("/aa", m_pTestee->parent_path().c_str());
144 delete m_pTestee;
145 m_pTestee = new Path("aa/");
146 EXPECT_STREQ("aa", m_pTestee->parent_path().c_str());
147 delete m_pTestee;
148 m_pTestee = new Path("aa");
149 EXPECT_TRUE(m_pTestee->parent_path().empty());
150 }
151
TEST_F(PathTest,filename)152 TEST_F(PathTest, filename) {
153 m_pTestee->assign("aa/bb/cc");
154 EXPECT_STREQ("cc", m_pTestee->filename().c_str());
155
156 m_pTestee->assign("aa/bb/");
157 EXPECT_STREQ("", m_pTestee->filename().c_str());
158
159 m_pTestee->assign("aa");
160 EXPECT_STREQ("aa", m_pTestee->filename().c_str());
161 }
162