X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e01a788ee05125b4fefa01a5b11f4088915cb54e..c29c95fe24973b94fd724db767193171ca7c513d:/tests/filename/filenametest.cpp diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index c1a9c5e6d4..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" @@ -125,11 +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,11 +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) }; @@ -151,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() @@ -315,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 @@ -478,6 +488,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("") ); @@ -533,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))); +}