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