From e7747eb2e0c892abdcea1925dfa71ade77a761a8 Mon Sep 17 00:00:00 2001 From: Francesco Montorsi Date: Sat, 19 Jun 2010 12:32:57 +0000 Subject: [PATCH] Move dir tests from the console sample to DirTestCase git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64635 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/console/console.cpp | 200 +-------------------------------- tests/Makefile.in | 4 + tests/file/dir.cpp | 216 ++++++++++++++++++++++++++++++++++++ tests/makefile.bcc | 4 + tests/makefile.gcc | 4 + tests/makefile.vc | 4 + tests/makefile.wat | 4 + tests/test.bkl | 1 + tests/test_test.dsp | 4 + tests/test_vc7_test.vcproj | 3 + tests/test_vc8_test.vcproj | 4 + tests/test_vc9_test.vcproj | 4 + 12 files changed, 254 insertions(+), 198 deletions(-) create mode 100644 tests/file/dir.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index bc31024863..f1cf85272e 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -101,12 +101,9 @@ // 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 @@ -117,6 +114,7 @@ #define TEST_INFO_FUNCTIONS #define TEST_MIME #define TEST_DYNLIB +#else // #if TEST_ALL #endif // some tests are interactive, define this to run them @@ -132,192 +130,6 @@ // 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 // ---------------------------------------------------------------------------- @@ -1070,14 +882,6 @@ int main(int argc, char **argv) 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 diff --git a/tests/Makefile.in b/tests/Makefile.in index 445d2ccb4d..233fd60181 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -69,6 +69,7 @@ TEST_OBJECTS = \ test_stopwatch.o \ test_timertest.o \ test_exec.o \ + test_dir.o \ test_filefn.o \ test_filetest.o \ test_filekind.o \ @@ -410,6 +411,9 @@ test_timertest.o: $(srcdir)/events/timertest.cpp $(TEST_ODEP) 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 diff --git a/tests/file/dir.cpp b/tests/file/dir.cpp new file mode 100644 index 0000000000..50b5b71052 --- /dev/null +++ b/tests/file/dir.cpp @@ -0,0 +1,216 @@ +/////////////////////////////////////////////////////////////////////////////// +// 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()) ); +} + diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 32e3bb97ea..8d037d6d6d 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -53,6 +53,7 @@ TEST_OBJECTS = \ $(OBJS)\test_stopwatch.obj \ $(OBJS)\test_timertest.obj \ $(OBJS)\test_exec.obj \ + $(OBJS)\test_dir.obj \ $(OBJS)\test_filefn.obj \ $(OBJS)\test_filetest.obj \ $(OBJS)\test_filekind.obj \ @@ -452,6 +453,9 @@ $(OBJS)\test_timertest.obj: .\events\timertest.cpp $(OBJS)\test_exec.obj: .\exec\exec.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\exec\exec.cpp +$(OBJS)\test_dir.obj: .\file\dir.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\file\dir.cpp + $(OBJS)\test_filefn.obj: .\file\filefn.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\file\filefn.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 69c87613f0..444adfe996 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -45,6 +45,7 @@ TEST_OBJECTS = \ $(OBJS)\test_stopwatch.o \ $(OBJS)\test_timertest.o \ $(OBJS)\test_exec.o \ + $(OBJS)\test_dir.o \ $(OBJS)\test_filefn.o \ $(OBJS)\test_filetest.o \ $(OBJS)\test_filekind.o \ @@ -433,6 +434,9 @@ $(OBJS)\test_timertest.o: ./events/timertest.cpp $(OBJS)\test_exec.o: ./exec/exec.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_dir.o: ./file/dir.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_filefn.o: ./file/filefn.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index fa535a3885..0495345c00 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -47,6 +47,7 @@ TEST_OBJECTS = \ $(OBJS)\test_stopwatch.obj \ $(OBJS)\test_timertest.obj \ $(OBJS)\test_exec.obj \ + $(OBJS)\test_dir.obj \ $(OBJS)\test_filefn.obj \ $(OBJS)\test_filetest.obj \ $(OBJS)\test_filekind.obj \ @@ -578,6 +579,9 @@ $(OBJS)\test_timertest.obj: .\events\timertest.cpp $(OBJS)\test_exec.obj: .\exec\exec.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\exec\exec.cpp +$(OBJS)\test_dir.obj: .\file\dir.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\file\dir.cpp + $(OBJS)\test_filefn.obj: .\file\filefn.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\file\filefn.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index aac28188fa..8df3c7d951 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -283,6 +283,7 @@ TEST_OBJECTS = & $(OBJS)\test_stopwatch.obj & $(OBJS)\test_timertest.obj & $(OBJS)\test_exec.obj & + $(OBJS)\test_dir.obj & $(OBJS)\test_filefn.obj & $(OBJS)\test_filetest.obj & $(OBJS)\test_filekind.obj & @@ -490,6 +491,9 @@ $(OBJS)\test_timertest.obj : .AUTODEPEND .\events\timertest.cpp $(OBJS)\test_exec.obj : .AUTODEPEND .\exec\exec.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< +$(OBJS)\test_dir.obj : .AUTODEPEND .\file\dir.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< + $(OBJS)\test_filefn.obj : .AUTODEPEND .\file\filefn.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< diff --git a/tests/test.bkl b/tests/test.bkl index bb1d82192c..491e299b6b 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -44,6 +44,7 @@ events/stopwatch.cpp events/timertest.cpp exec/exec.cpp + file/dir.cpp file/filefn.cpp file/filetest.cpp filekind/filekind.cpp diff --git a/tests/test_test.dsp b/tests/test_test.dsp index 83fef0ef6f..e06a08e2a4 100644 --- a/tests/test_test.dsp +++ b/tests/test_test.dsp @@ -279,6 +279,10 @@ SOURCE=.\datetime\datetimetest.cpp # End Source File # Begin Source File +SOURCE=.\file\dir.cpp +# End Source File +# Begin Source File + SOURCE=.\dummy.cpp # ADD BASE CPP /Yc"testprec.h" # ADD CPP /Yc"testprec.h" diff --git a/tests/test_vc7_test.vcproj b/tests/test_vc7_test.vcproj index b593bc6545..9aa73921ca 100644 --- a/tests/test_vc7_test.vcproj +++ b/tests/test_vc7_test.vcproj @@ -598,6 +598,9 @@ + + + + diff --git a/tests/test_vc9_test.vcproj b/tests/test_vc9_test.vcproj index 4f13922453..77dd6798e2 100644 --- a/tests/test_vc9_test.vcproj +++ b/tests/test_vc9_test.vcproj @@ -843,6 +843,10 @@ RelativePath=".\datetime\datetimetest.cpp" > + + -- 2.47.2