X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a07526187ae255bc80a5e1a5195d33e6f98fb58f..c29c95fe24973b94fd724db767193171ca7c513d:/tests/filename/filenametest.cpp diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index a9fab6197c..a1ea735acd 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -23,6 +23,7 @@ #include "wx/filename.h" #include "wx/filefn.h" +#include "wx/stdpaths.h" #ifdef __WXMSW__ #include "wx/msw/registry.h" @@ -131,6 +132,8 @@ private: #endif // __WINDOWS__ CPPUNIT_TEST( TestUNC ); CPPUNIT_TEST( TestVolumeUniqueName ); + CPPUNIT_TEST( TestCreateTempFileName ); + CPPUNIT_TEST( TestGetTimes ); CPPUNIT_TEST_SUITE_END(); void TestConstruction(); @@ -146,6 +149,8 @@ private: #endif // __WINDOWS__ void TestUNC(); void TestVolumeUniqueName(); + void TestCreateTempFileName(); + void TestGetTimes(); DECLARE_NO_COPY_CLASS(FileNameTestCase) }; @@ -153,7 +158,7 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase ); -// also include in it's own registry so that these tests can be run alone +// also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" ); void FileNameTestCase::TestConstruction() @@ -317,8 +322,11 @@ 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 }, + { "", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, + { "./foo", wxPATH_NORM_DOTS, "foo", wxPATH_UNIX }, + { "b/../bar", wxPATH_NORM_DOTS, "bar", wxPATH_UNIX }, + { "c/../../quux", wxPATH_NORM_DOTS, "../quux", wxPATH_UNIX }, + { "/c/../../quux", wxPATH_NORM_DOTS, "/quux", wxPATH_UNIX }, // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially // when it is the first character in the file name @@ -498,10 +506,16 @@ void FileNameTestCase::TestGetHumanReadable() { "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, @@ -568,3 +582,68 @@ void FileNameTestCase::TestVolumeUniqueName() "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))); +}