Correct the expected result of directory existence check.
[wxWidgets.git] / tests / file / dir.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/file/dir.cpp
3 // Purpose: wxDir unit test
4 // Author: Francesco Montorsi (extracted from console sample)
5 // Created: 2010-06-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/dir.h"
21 #include "wx/filename.h"
22
23 #define DIRTEST_FOLDER wxString("dirTest_folder")
24 #define SEP wxFileName::GetPathSeparator()
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class DirTestCase : public CppUnit::TestCase
31 {
32 public:
33 DirTestCase() { }
34
35 virtual void setUp();
36 virtual void tearDown();
37
38 private:
39 CPPUNIT_TEST_SUITE( DirTestCase );
40 CPPUNIT_TEST( DirExists );
41 CPPUNIT_TEST( Traverse );
42 CPPUNIT_TEST( Enum );
43 CPPUNIT_TEST_SUITE_END();
44
45 void DirExists();
46 void Traverse();
47 void Enum();
48
49 void CreateTempFile(const wxString& path);
50 wxArrayString DirEnumHelper(wxDir& dir,
51 int flags = wxDIR_DEFAULT,
52 const wxString& filespec = wxEmptyString);
53
54 wxDECLARE_NO_COPY_CLASS(DirTestCase);
55 };
56
57 // ----------------------------------------------------------------------------
58 // CppUnit macros
59 // ----------------------------------------------------------------------------
60
61 CPPUNIT_TEST_SUITE_REGISTRATION( DirTestCase );
62 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DirTestCase, "DirTestCase" );
63
64 // ----------------------------------------------------------------------------
65 // tests implementation
66 // ----------------------------------------------------------------------------
67
68 void DirTestCase::CreateTempFile(const wxString& path)
69 {
70 wxFile f(path, wxFile::write);
71 f.Write("dummy test file");
72 f.Close();
73 }
74
75 void DirTestCase::setUp()
76 {
77 // create a test directory hierarchy
78 wxDir::Make(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder1", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
79 wxDir::Make(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
80 wxDir::Make(DIRTEST_FOLDER + SEP + "folder2", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
81 wxDir::Make(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
82
83 CreateTempFile(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy");
84 CreateTempFile(DIRTEST_FOLDER + SEP + "dummy");
85 }
86
87 void DirTestCase::tearDown()
88 {
89 wxRemove(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy");
90 wxRemove(DIRTEST_FOLDER + SEP + "dummy");
91 wxDir::Remove(DIRTEST_FOLDER, wxPATH_RMDIR_RECURSIVE);
92 }
93
94 wxArrayString DirTestCase::DirEnumHelper(wxDir& dir,
95 int flags,
96 const wxString& filespec)
97 {
98 wxArrayString ret;
99 CPPUNIT_ASSERT( dir.IsOpened() );
100
101 wxString filename;
102 bool cont = dir.GetFirst(&filename, filespec, flags);
103 while ( cont )
104 {
105 ret.push_back(filename);
106 cont = dir.GetNext(&filename);
107 }
108
109 return ret;
110 }
111
112 void DirTestCase::Enum()
113 {
114 wxDir dir(DIRTEST_FOLDER);
115 CPPUNIT_ASSERT( dir.IsOpened() );
116
117 // enumerating everything in test directory
118 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir).size());
119
120 // enumerating really everything in test directory recursively
121 CPPUNIT_ASSERT_EQUAL(6, DirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT).size());
122
123 // enumerating object files in test directory
124 CPPUNIT_ASSERT_EQUAL(0, DirEnumHelper(dir, wxDIR_DEFAULT, "*.o*").size());
125
126 // enumerating directories in test directory
127 CPPUNIT_ASSERT_EQUAL(3, DirEnumHelper(dir, wxDIR_DIRS).size());
128
129 // enumerating files in test directory
130 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES).size());
131
132 // enumerating files including hidden in test directory
133 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN).size());
134
135 // enumerating files and folders in test directory
136 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir, wxDIR_FILES | wxDIR_DIRS).size());
137 }
138
139 class TestDirTraverser : public wxDirTraverser
140 {
141 public:
142 wxArrayString dirs;
143
144 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
145 {
146 return wxDIR_CONTINUE;
147 }
148
149 virtual wxDirTraverseResult OnDir(const wxString& dirname)
150 {
151 dirs.push_back(dirname);
152 return wxDIR_CONTINUE;
153 }
154 };
155
156 void DirTestCase::Traverse()
157 {
158 // enum all files
159 wxArrayString files;
160 CPPUNIT_ASSERT_EQUAL(2, wxDir::GetAllFiles(DIRTEST_FOLDER, &files));
161
162 // enum again with custom traverser
163 wxDir dir(DIRTEST_FOLDER);
164 TestDirTraverser traverser;
165 dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
166 CPPUNIT_ASSERT_EQUAL(6, traverser.dirs.size());
167 }
168
169 void DirTestCase::DirExists()
170 {
171 struct
172 {
173 const char *dirname;
174 bool shouldExist;
175 } testData[] =
176 {
177 { ".", true },
178 { "..", true },
179 #if defined(__WXMSW__)
180 { "..\\..", true },
181 { "..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..", true },
182 { "c:", true },
183 { "c:\\", true },
184 { "c:\\\\", true },
185 { "\\\\share\\file", false },
186 { "c:\\a\\directory\\which\\does\\not\\exist", false },
187 { "c:\\a\\directory\\which\\does\\not\\exist\\", false },
188 { "c:\\a\\directory\\which\\does\\not\\exist\\\\", false },
189 { "test.exe", false } // not a directory!
190 #elif defined(__UNIX__)
191 { "../..", true },
192 { "../../../../../../../../../../../../../../../../../../../..", true },
193 { "/", true },
194 { "//", true },
195 { "/usr/bin", true },
196 { "/usr//bin", false },
197 { "/usr///bin", false }
198 #endif
199 };
200
201 for ( size_t n = 0; n < WXSIZEOF(testData); n++ )
202 {
203 wxString errDesc = wxString::Format("failed on directory '%s'", testData[n].dirname);
204 CPPUNIT_ASSERT_EQUAL_MESSAGE(errDesc.ToStdString(), testData[n].shouldExist, wxDir::Exists(testData[n].dirname));
205
206 if (!testData[n].shouldExist)
207 {
208 wxDir d(testData[n].dirname);
209 CPPUNIT_ASSERT(!d.IsOpened());
210 }
211 }
212
213 CPPUNIT_ASSERT( wxDir::Exists(wxGetCwd()) );
214 }
215