// what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
// test, define it to 1 to do all tests.
-#define TEST_ALL 0
-
+#define TEST_ALL 1
#if TEST_ALL
- #define TEST_DIR
-#else // #if TEST_ALL
#define TEST_DATETIME
#define TEST_VOLUME
#define TEST_STDPATHS
#define TEST_INFO_FUNCTIONS
#define TEST_MIME
#define TEST_DYNLIB
+#else // #if TEST_ALL
#endif
// some tests are interactive, define this to run them
// implementation
// ============================================================================
-// ----------------------------------------------------------------------------
-// wxDir
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_DIR
-
-#include "wx/dir.h"
-
-#ifdef __UNIX__
- static const wxChar *ROOTDIR = wxT("/");
- static const wxChar *TESTDIR = wxT("/usr/local/share");
-#elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
- static const wxChar *ROOTDIR = wxT("c:\\");
- static const wxChar *TESTDIR = wxT("d:\\");
-#else
- #error "don't know where the root directory is"
-#endif
-
-static void TestDirEnumHelper(wxDir& dir,
- int flags = wxDIR_DEFAULT,
- const wxString& filespec = wxEmptyString)
-{
- wxString filename;
-
- if ( !dir.IsOpened() )
- return;
-
- bool cont = dir.GetFirst(&filename, filespec, flags);
- while ( cont )
- {
- wxPrintf(wxT("\t%s\n"), filename.c_str());
-
- cont = dir.GetNext(&filename);
- }
-
- wxPuts(wxEmptyString);
-}
-
-#if TEST_ALL
-
-static void TestDirEnum()
-{
- wxPuts(wxT("*** Testing wxDir::GetFirst/GetNext ***"));
-
- wxString cwd = wxGetCwd();
- if ( !wxDir::Exists(cwd) )
- {
- wxPrintf(wxT("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str());
- return;
- }
-
- wxDir dir(cwd);
- if ( !dir.IsOpened() )
- {
- wxPrintf(wxT("ERROR: failed to open current directory '%s'.\n"), cwd.c_str());
- return;
- }
-
- wxPuts(wxT("Enumerating everything in current directory:"));
- TestDirEnumHelper(dir);
-
- wxPuts(wxT("Enumerating really everything in current directory:"));
- TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
-
- wxPuts(wxT("Enumerating object files in current directory:"));
- TestDirEnumHelper(dir, wxDIR_DEFAULT, wxT("*.o*"));
-
- wxPuts(wxT("Enumerating directories in current directory:"));
- TestDirEnumHelper(dir, wxDIR_DIRS);
-
- wxPuts(wxT("Enumerating files in current directory:"));
- TestDirEnumHelper(dir, wxDIR_FILES);
-
- wxPuts(wxT("Enumerating files including hidden in current directory:"));
- TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
-
- dir.Open(ROOTDIR);
-
- wxPuts(wxT("Enumerating everything in root directory:"));
- TestDirEnumHelper(dir, wxDIR_DEFAULT);
-
- wxPuts(wxT("Enumerating directories in root directory:"));
- TestDirEnumHelper(dir, wxDIR_DIRS);
-
- wxPuts(wxT("Enumerating files in root directory:"));
- TestDirEnumHelper(dir, wxDIR_FILES);
-
- wxPuts(wxT("Enumerating files including hidden in root directory:"));
- TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
-
- wxPuts(wxT("Enumerating files in non existing directory:"));
- wxDir dirNo(wxT("nosuchdir"));
- TestDirEnumHelper(dirNo);
-}
-
-#endif // TEST_ALL
-
-class DirPrintTraverser : public wxDirTraverser
-{
-public:
- virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
- {
- return wxDIR_CONTINUE;
- }
-
- virtual wxDirTraverseResult OnDir(const wxString& dirname)
- {
- wxString path, name, ext;
- wxFileName::SplitPath(dirname, &path, &name, &ext);
-
- if ( !ext.empty() )
- name << wxT('.') << ext;
-
- wxString indent;
- for ( const wxChar *p = path.c_str(); *p; p++ )
- {
- if ( wxIsPathSeparator(*p) )
- indent += wxT(" ");
- }
-
- wxPrintf(wxT("%s%s\n"), indent.c_str(), name.c_str());
-
- return wxDIR_CONTINUE;
- }
-};
-
-static void TestDirTraverse()
-{
- wxPuts(wxT("*** Testing wxDir::Traverse() ***"));
-
- // enum all files
- wxArrayString files;
- size_t n = wxDir::GetAllFiles(TESTDIR, &files);
- wxPrintf(wxT("There are %u files under '%s'\n"), n, TESTDIR);
- if ( n > 1 )
- {
- wxPrintf(wxT("First one is '%s'\n"), files[0u].c_str());
- wxPrintf(wxT(" last one is '%s'\n"), files[n - 1].c_str());
- }
-
- // enum again with custom traverser
- wxPuts(wxT("Now enumerating directories:"));
- wxDir dir(TESTDIR);
- DirPrintTraverser traverser;
- dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
-}
-
-#if TEST_ALL
-
-static void TestDirExists()
-{
- wxPuts(wxT("*** Testing wxDir::Exists() ***"));
-
- static const wxChar *dirnames[] =
- {
- wxT("."),
-#if defined(__WXMSW__)
- wxT("c:"),
- wxT("c:\\"),
- wxT("\\\\share\\file"),
- wxT("c:\\dos"),
- wxT("c:\\dos\\"),
- wxT("c:\\dos\\\\"),
- wxT("c:\\autoexec.bat"),
-#elif defined(__UNIX__)
- wxT("/"),
- wxT("//"),
- wxT("/usr/bin"),
- wxT("/usr//bin"),
- wxT("/usr///bin"),
-#endif
- };
-
- for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ )
- {
- wxPrintf(wxT("%-40s: %s\n"),
- dirnames[n],
- wxDir::Exists(dirnames[n]) ? wxT("exists")
- : wxT("doesn't exist"));
- }
-}
-
-#endif // TEST_ALL
-
-#endif // TEST_DIR
-
// ----------------------------------------------------------------------------
// wxDllLoader
// ----------------------------------------------------------------------------
return 1;
#endif // TEST_SNGLINST
-#ifdef TEST_DIR
- #if TEST_ALL
- TestDirExists();
- TestDirEnum();
- #endif
- TestDirTraverse();
-#endif // TEST_DIR
-
#ifdef TEST_DYNLIB
TestDllListLoaded();
#endif // TEST_DYNLIB
test_stopwatch.o \
test_timertest.o \
test_exec.o \
+ test_dir.o \
test_filefn.o \
test_filetest.o \
test_filekind.o \
test_exec.o: $(srcdir)/exec/exec.cpp $(TEST_ODEP)
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/exec/exec.cpp
+test_dir.o: $(srcdir)/file/dir.cpp $(TEST_ODEP)
+ $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/file/dir.cpp
+
test_filefn.o: $(srcdir)/file/filefn.cpp $(TEST_ODEP)
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/file/filefn.cpp
--- /dev/null
+///////////////////////////////////////////////////////////////////////////////
+// Name: tests/file/dir.cpp
+// Purpose: wxDir unit test
+// Author: Francesco Montorsi (extracted from console sample)
+// Created: 2010-06-19
+// RCS-ID: $Id$
+// Copyright: (c) 2010 wxWidgets team
+///////////////////////////////////////////////////////////////////////////////
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+#include "testprec.h"
+
+#ifdef __BORLANDC__
+ #pragma hdrstop
+#endif
+
+#include "wx/dir.h"
+#include "wx/filename.h"
+
+#define DIRTEST_FOLDER wxString("dirTest_folder")
+#define SEP wxFileName::GetPathSeparator()
+
+// ----------------------------------------------------------------------------
+// test class
+// ----------------------------------------------------------------------------
+
+class DirTestCase : public CppUnit::TestCase
+{
+public:
+ DirTestCase() { }
+
+ virtual void setUp();
+ virtual void tearDown();
+
+private:
+ CPPUNIT_TEST_SUITE( DirTestCase );
+ CPPUNIT_TEST( DirExists );
+ CPPUNIT_TEST( Traverse );
+ CPPUNIT_TEST( Enum );
+ CPPUNIT_TEST_SUITE_END();
+
+ void DirExists();
+ void Traverse();
+ void Enum();
+
+ void CreateTempFile(const wxString& path);
+ wxArrayString DirEnumHelper(wxDir& dir,
+ int flags = wxDIR_DEFAULT,
+ const wxString& filespec = wxEmptyString);
+
+ wxDECLARE_NO_COPY_CLASS(DirTestCase);
+};
+
+// ----------------------------------------------------------------------------
+// CppUnit macros
+// ----------------------------------------------------------------------------
+
+CPPUNIT_TEST_SUITE_REGISTRATION( DirTestCase );
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DirTestCase, "DirTestCase" );
+
+// ----------------------------------------------------------------------------
+// tests implementation
+// ----------------------------------------------------------------------------
+
+void DirTestCase::CreateTempFile(const wxString& path)
+{
+ wxFile f(path, wxFile::write);
+ f.Write("dummy test file");
+ f.Close();
+}
+
+void DirTestCase::setUp()
+{
+ // create a test directory hierarchy
+ wxDir::Make(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder1", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
+ wxDir::Make(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
+ wxDir::Make(DIRTEST_FOLDER + SEP + "folder2", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
+ wxDir::Make(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
+
+ CreateTempFile(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy");
+ CreateTempFile(DIRTEST_FOLDER + SEP + "dummy");
+}
+
+void DirTestCase::tearDown()
+{
+ wxRemove(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy");
+ wxRemove(DIRTEST_FOLDER + SEP + "dummy");
+ wxDir::Remove(DIRTEST_FOLDER, wxPATH_RMDIR_RECURSIVE);
+}
+
+wxArrayString DirTestCase::DirEnumHelper(wxDir& dir,
+ int flags,
+ const wxString& filespec)
+{
+ wxArrayString ret;
+ CPPUNIT_ASSERT( dir.IsOpened() );
+
+ wxString filename;
+ bool cont = dir.GetFirst(&filename, filespec, flags);
+ while ( cont )
+ {
+ ret.push_back(filename);
+ cont = dir.GetNext(&filename);
+ }
+
+ return ret;
+}
+
+void DirTestCase::Enum()
+{
+ wxDir dir(DIRTEST_FOLDER);
+ CPPUNIT_ASSERT( dir.IsOpened() );
+
+ // enumerating everything in test directory
+ CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir).size());
+
+ // enumerating really everything in test directory recursively
+ CPPUNIT_ASSERT_EQUAL(6, DirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT).size());
+
+ // enumerating object files in test directory
+ CPPUNIT_ASSERT_EQUAL(0, DirEnumHelper(dir, wxDIR_DEFAULT, "*.o*").size());
+
+ // enumerating directories in test directory
+ CPPUNIT_ASSERT_EQUAL(3, DirEnumHelper(dir, wxDIR_DIRS).size());
+
+ // enumerating files in test directory
+ CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES).size());
+
+ // enumerating files including hidden in test directory
+ CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN).size());
+
+ // enumerating files and folders in test directory
+ CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir, wxDIR_FILES | wxDIR_DIRS).size());
+}
+
+class TestDirTraverser : public wxDirTraverser
+{
+public:
+ wxArrayString dirs;
+
+ virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
+ {
+ return wxDIR_CONTINUE;
+ }
+
+ virtual wxDirTraverseResult OnDir(const wxString& dirname)
+ {
+ dirs.push_back(dirname);
+ return wxDIR_CONTINUE;
+ }
+};
+
+void DirTestCase::Traverse()
+{
+ // enum all files
+ wxArrayString files;
+ CPPUNIT_ASSERT_EQUAL(2, wxDir::GetAllFiles(DIRTEST_FOLDER, &files));
+
+ // enum again with custom traverser
+ wxDir dir(DIRTEST_FOLDER);
+ TestDirTraverser traverser;
+ dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
+ CPPUNIT_ASSERT_EQUAL(6, traverser.dirs.size());
+}
+
+void DirTestCase::DirExists()
+{
+ struct
+ {
+ const char *dirname;
+ bool shouldExist;
+ } testData[] =
+ {
+ { ".", true },
+ { "..", true },
+#if defined(__WXMSW__)
+ { "..\\..", true },
+ { "..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..", /*false*/ true },
+ // FIXME: should fail but it doesn't... looks like a bug in GetFileAttributes() win API
+ { "c:", true },
+ { "c:\\", true },
+ { "c:\\\\", true },
+ { "\\\\share\\file", false },
+ { "c:\\a\\directory\\which\\does\\not\\exist", false },
+ { "c:\\a\\directory\\which\\does\\not\\exist\\", false },
+ { "c:\\a\\directory\\which\\does\\not\\exist\\\\", false },
+ { "test.exe", false } // not a directory!
+#elif defined(__UNIX__)
+ { "../..", true },
+ { "../../../../../../../../../../../../../../../../../../../..", false },
+ { "/", true },
+ { "//", true },
+ { "/usr/bin", true },
+ { "/usr//bin", false },
+ { "/usr///bin", false }
+#endif
+ };
+
+ for ( size_t n = 0; n < WXSIZEOF(testData); n++ )
+ {
+ wxString errDesc = wxString::Format("failed on directory '%s'", testData[n].dirname);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(errDesc.ToStdString(), testData[n].shouldExist, wxDir::Exists(testData[n].dirname));
+
+ if (!testData[n].shouldExist)
+ {
+ wxDir d(testData[n].dirname);
+ CPPUNIT_ASSERT(!d.IsOpened());
+ }
+ }
+
+ CPPUNIT_ASSERT( wxDir::Exists(wxGetCwd()) );
+}
+
$(OBJS)\test_stopwatch.obj \\r
$(OBJS)\test_timertest.obj \\r
$(OBJS)\test_exec.obj \\r
+ $(OBJS)\test_dir.obj \\r
$(OBJS)\test_filefn.obj \\r
$(OBJS)\test_filetest.obj \\r
$(OBJS)\test_filekind.obj \\r
$(OBJS)\test_exec.obj: .\exec\exec.cpp\r
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\exec\exec.cpp\r
\r
+$(OBJS)\test_dir.obj: .\file\dir.cpp\r
+ $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\file\dir.cpp\r
+\r
$(OBJS)\test_filefn.obj: .\file\filefn.cpp\r
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\file\filefn.cpp\r
\r
$(OBJS)\test_stopwatch.o \\r
$(OBJS)\test_timertest.o \\r
$(OBJS)\test_exec.o \\r
+ $(OBJS)\test_dir.o \\r
$(OBJS)\test_filefn.o \\r
$(OBJS)\test_filetest.o \\r
$(OBJS)\test_filekind.o \\r
$(OBJS)\test_exec.o: ./exec/exec.cpp\r
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<\r
\r
+$(OBJS)\test_dir.o: ./file/dir.cpp\r
+ $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<\r
+\r
$(OBJS)\test_filefn.o: ./file/filefn.cpp\r
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<\r
\r
$(OBJS)\test_stopwatch.obj \\r
$(OBJS)\test_timertest.obj \\r
$(OBJS)\test_exec.obj \\r
+ $(OBJS)\test_dir.obj \\r
$(OBJS)\test_filefn.obj \\r
$(OBJS)\test_filetest.obj \\r
$(OBJS)\test_filekind.obj \\r
$(OBJS)\test_exec.obj: .\exec\exec.cpp\r
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\exec\exec.cpp\r
\r
+$(OBJS)\test_dir.obj: .\file\dir.cpp\r
+ $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\file\dir.cpp\r
+\r
$(OBJS)\test_filefn.obj: .\file\filefn.cpp\r
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\file\filefn.cpp\r
\r
$(OBJS)\test_stopwatch.obj &\r
$(OBJS)\test_timertest.obj &\r
$(OBJS)\test_exec.obj &\r
+ $(OBJS)\test_dir.obj &\r
$(OBJS)\test_filefn.obj &\r
$(OBJS)\test_filetest.obj &\r
$(OBJS)\test_filekind.obj &\r
$(OBJS)\test_exec.obj : .AUTODEPEND .\exec\exec.cpp\r
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<\r
\r
+$(OBJS)\test_dir.obj : .AUTODEPEND .\file\dir.cpp\r
+ $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<\r
+\r
$(OBJS)\test_filefn.obj : .AUTODEPEND .\file\filefn.cpp\r
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<\r
\r
events/stopwatch.cpp
events/timertest.cpp
exec/exec.cpp
+ file/dir.cpp
file/filefn.cpp
file/filetest.cpp
filekind/filekind.cpp
# End Source File\r
# Begin Source File\r
\r
+SOURCE=.\file\dir.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
SOURCE=.\dummy.cpp\r
# ADD BASE CPP /Yc"testprec.h"\r
# ADD CPP /Yc"testprec.h"\r
<File\r
RelativePath=".\datetime\datetimetest.cpp">\r
</File>\r
+ <File\r
+ RelativePath=".\file\dir.cpp">\r
+ </File>\r
<File\r
RelativePath=".\dummy.cpp">\r
<FileConfiguration\r
RelativePath=".\datetime\datetimetest.cpp"\r
>\r
</File>\r
+ <File\r
+ RelativePath=".\file\dir.cpp"\r
+ >\r
+ </File>\r
<File\r
RelativePath=".\dummy.cpp"\r
>\r
RelativePath=".\datetime\datetimetest.cpp"\r
>\r
</File>\r
+ <File\r
+ RelativePath=".\file\dir.cpp"\r
+ >\r
+ </File>\r
<File\r
RelativePath=".\dummy.cpp"\r
>\r