X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9630954d5d9541f2bd9fd84beb427dc59c9ded31..bbd55ff9564dcaf1a5d2ee93d789c4d656baabef:/tests/filename/filenametest.cpp diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index ee65dc83e3..dee7cd081c 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -23,20 +23,13 @@ #include "wx/filename.h" #include "wx/filefn.h" +#include "wx/stdpaths.h" #ifdef __WXMSW__ #include "wx/msw/registry.h" #endif // __WXMSW__ -// ---------------------------------------------------------------------------- -// local functions -// ---------------------------------------------------------------------------- - -// define stream inserter for wxFileName to use it in CPPUNIT_ASSERT_EQUAL() -inline std::ostream& operator<<(std::ostream& o, const wxFileName& fn) -{ - return o << fn.GetFullPath(); -} +#include "testfile.h" // ---------------------------------------------------------------------------- // test data @@ -65,6 +58,8 @@ static struct TestFileNameInfo { "../../foo", "", "../..", "foo", "", false, wxPATH_UNIX }, { "foo.bar", "", "", "foo", "bar", false, wxPATH_UNIX }, { "~/foo.bar", "", "~", "foo", "bar", true, wxPATH_UNIX }, + { "~user/foo.bar", "", "~user", "foo", "bar", true, wxPATH_UNIX }, + { "~user/", "", "~user", "", "", true, wxPATH_UNIX }, { "/foo", "", "/", "foo", "", true, wxPATH_UNIX }, { "Mahogany-0.60/foo.bar", "", "Mahogany-0.60", "foo", "bar", false, wxPATH_UNIX }, { "/tmp/wxwin.tar.bz", "", "/tmp", "wxwin.tar", "bz", true, wxPATH_UNIX }, @@ -75,6 +70,10 @@ static struct TestFileNameInfo { "c:foo.bar", "c", "", "foo", "bar", false, wxPATH_DOS }, { "c:\\foo.bar", "c", "\\", "foo", "bar", true, wxPATH_DOS }, { "c:\\Windows\\command.com", "c", "\\Windows", "command", "com", true, wxPATH_DOS }, + { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", + "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\", "", "", true, wxPATH_DOS }, + { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\Program Files\\setup.exe", + "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\Program Files", "setup", "exe", true, wxPATH_DOS }, #if 0 // NB: when using the wxFileName::GetLongPath() function on these two @@ -127,9 +126,14 @@ private: CPPUNIT_TEST( TestStrip ); CPPUNIT_TEST( TestNormalize ); CPPUNIT_TEST( TestReplace ); + CPPUNIT_TEST( TestGetHumanReadable ); #ifdef __WINDOWS__ CPPUNIT_TEST( TestShortLongPath ); #endif // __WINDOWS__ + CPPUNIT_TEST( TestUNC ); + CPPUNIT_TEST( TestVolumeUniqueName ); + CPPUNIT_TEST( TestCreateTempFileName ); + CPPUNIT_TEST( TestGetTimes ); CPPUNIT_TEST_SUITE_END(); void TestConstruction(); @@ -139,9 +143,14 @@ private: void TestStrip(); void TestNormalize(); void TestReplace(); + void TestGetHumanReadable(); #ifdef __WINDOWS__ void TestShortLongPath(); #endif // __WINDOWS__ + void TestUNC(); + void TestVolumeUniqueName(); + void TestCreateTempFileName(); + void TestGetTimes(); DECLARE_NO_COPY_CLASS(FileNameTestCase) }; @@ -313,11 +322,13 @@ void FileNameTestCase::TestNormalize() // test wxPATH_NORM_DOTS { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, + { "./", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, + { "b/../", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, - // test wxPATH_NORM_TILDE - // NB: do the tilde expansion also under Windows to test if it works there too + // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially + // when it is the first character in the file name { "/a/b/~", wxPATH_NORM_TILDE, "/a/b/~", wxPATH_UNIX }, - { "/~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX }, + { "/~/a/b", wxPATH_NORM_TILDE, "/~/a/b", wxPATH_UNIX }, { "~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX }, // test wxPATH_NORM_CASE @@ -474,6 +485,45 @@ void FileNameTestCase::TestReplace() fn.GetFullPath(wxPATH_UNIX) ); } +void FileNameTestCase::TestGetHumanReadable() +{ + static const struct TestData + { + const char *result; + int size; + int prec; + wxSizeConvention conv; + } testData[] = + { + { "NA", 0, 1, wxSIZE_CONV_TRADITIONAL }, + { "2.0 KB", 2000, 1, wxSIZE_CONV_TRADITIONAL }, + { "1.953 KiB", 2000, 3, wxSIZE_CONV_IEC }, + { "2.000 KB", 2000, 3, wxSIZE_CONV_SI }, + { "297 KB", 304351, 0, wxSIZE_CONV_TRADITIONAL }, + { "304 KB", 304351, 0, wxSIZE_CONV_SI }, + }; + + CLocaleSetter loc; // we want to use "C" locale for LC_NUMERIC + // so that regardless of the system's locale + // the decimal point used by GetHumanReadableSize() + // is always '.' + for ( unsigned n = 0; n < WXSIZEOF(testData); n++ ) + { + const TestData& td = testData[n]; + + // take care of using the decimal point for the current locale before + // the actual comparison + CPPUNIT_ASSERT_EQUAL + ( + td.result, + wxFileName::GetHumanReadableSize(td.size, "NA", td.prec, td.conv) + ); + } + + // also test the default convention value + CPPUNIT_ASSERT_EQUAL( "1.4 MB", wxFileName::GetHumanReadableSize(1512993, "") ); +} + void FileNameTestCase::TestStrip() { CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") ); @@ -497,3 +547,100 @@ void FileNameTestCase::TestShortLongPath() } #endif // __WINDOWS__ + +void FileNameTestCase::TestUNC() +{ + wxFileName fn("//share/path/name.ext", wxPATH_DOS); + CPPUNIT_ASSERT_EQUAL( "share", fn.GetVolume() ); + CPPUNIT_ASSERT_EQUAL( "\\path", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); + + fn.Assign("\\\\share2\\path2\\name.ext", wxPATH_DOS); + CPPUNIT_ASSERT_EQUAL( "share2", fn.GetVolume() ); + CPPUNIT_ASSERT_EQUAL( "\\path2", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); +} + +void FileNameTestCase::TestVolumeUniqueName() +{ + wxFileName fn("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", + wxPATH_DOS); + CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", + fn.GetVolume() ); + CPPUNIT_ASSERT_EQUAL( "\\", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); + CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", + fn.GetFullPath(wxPATH_DOS) ); + + fn.Assign("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\" + "Program Files\\setup.exe", wxPATH_DOS); + CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", + fn.GetVolume() ); + CPPUNIT_ASSERT_EQUAL( "\\Program Files", + fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); + CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\" + "Program Files\\setup.exe", + fn.GetFullPath(wxPATH_DOS) ); +} + +void FileNameTestCase::TestCreateTempFileName() +{ + static const struct TestData + { + const char *prefix; + const char *expectedFolder; + bool shouldSucceed; + } testData[] = + { + { "", "$SYSTEM_TEMP", true }, + { "foo", "$SYSTEM_TEMP", true }, + { "..", "$SYSTEM_TEMP", true }, + { "../bar", "..", true }, +#ifdef __WXMSW__ + { "$USER_DOCS_DIR\\", "$USER_DOCS_DIR", true }, + { "c:\\a\\directory\\which\\does\\not\\exist", "", false }, +#elif defined( __UNIX__ ) + { "$USER_DOCS_DIR/", "$USER_DOCS_DIR", true }, + { "/tmp/foo", "/tmp", true }, + { "/tmp/a/directory/which/does/not/exist", "", false }, +#endif // __UNIX__ + }; + + for ( size_t n = 0; n < WXSIZEOF(testData); n++ ) + { + wxString prefix = testData[n].prefix; + prefix.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir()); + + std::string errDesc = wxString::Format("failed on prefix '%s'", prefix).ToStdString(); + + wxString path = wxFileName::CreateTempFileName(prefix); + CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc, !testData[n].shouldSucceed, path.empty() ); + + if (testData[n].shouldSucceed) + { + errDesc += "; path is " + path.ToStdString(); + + // test the place where the temp file has been created + wxString expected = testData[n].expectedFolder; + expected.Replace("$SYSTEM_TEMP", wxStandardPaths::Get().GetTempDir()); + expected.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir()); + CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc, expected, wxFileName(path).GetPath() ); + + // the temporary file is created with full permissions for the current process + // so we should always be able to remove it: + CPPUNIT_ASSERT_MESSAGE( errDesc, wxRemoveFile(path) ); + } + } +} + +void FileNameTestCase::TestGetTimes() +{ + wxFileName fn(wxFileName::CreateTempFileName("filenametest")); + CPPUNIT_ASSERT( fn.IsOk() ); + + wxDateTime dtAccess, dtMod, dtCreate; + CPPUNIT_ASSERT( fn.GetTimes(&dtAccess, &dtMod, &dtCreate) ); + + // make sure all retrieved dates are equal to the current date&time + // with an accuracy up to 1 minute + CPPUNIT_ASSERT(dtCreate.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); + CPPUNIT_ASSERT(dtMod.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); + CPPUNIT_ASSERT(dtAccess.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); +}