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