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