PathTest.cpp revision cedee4b38f4786845183be7f5916dd520a170ae0
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//
14using namespace mcld;
15using namespace mcld::sys::fs;
16using namespace mcldtest;
17
18
19// Constructor can do set-up work for all test here.
20PathTest::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.
27PathTest::~PathTest()
28{
29	delete m_pTestee;
30}
31
32// SetUp() will be called immediately before each test.
33void PathTest::SetUp()
34{
35}
36
37// TearDown() will be called immediately after each test.
38void PathTest::TearDown()
39{
40}
41
42//==========================================================================//
43// Testcases
44//
45TEST_F( PathTest, should_exist ) {
46  const std::string root = "/etc/hosts";
47  m_pTestee->assign(root);
48  EXPECT_TRUE(exists(*m_pTestee));
49
50  delete m_pTestee;
51  m_pTestee = new Path(root);
52  EXPECT_TRUE(exists(*m_pTestee));
53}
54
55TEST_F( PathTest, should_not_exist ) {
56  const std::string root = "/luck";
57  m_pTestee->assign(root);
58  EXPECT_FALSE(exists(*m_pTestee));
59
60  delete m_pTestee;
61  m_pTestee = new Path(root);
62  EXPECT_FALSE(exists(*m_pTestee));
63}
64
65TEST_F( PathTest, should_is_directory ) {
66//  const std::string root = "/proj/mtk03931/temp/pndk-luba/../";
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
77TEST_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
88TEST_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
102TEST_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
115TEST_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->string()=="aa/aaa");
126  delete m_pTestee;
127  m_pTestee = new Path("aa");
128  m_pTestee->append("/aaa");
129  EXPECT_TRUE(m_pTestee->string()=="aa/aaa");
130  delete m_pTestee;
131  m_pTestee = new Path("aa");
132  m_pTestee->append("aaa");
133  EXPECT_TRUE(m_pTestee->string()=="aa/aaa");
134}
135
136TEST_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
141TEST_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
158