#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
{ "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
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();
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)
};
// 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()
// test wxPATH_NORM_DOTS
{ "a/.././b/c/../../", 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
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("") );
}
#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)));
+}