diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 1d98b5c9be..0d6ed2f169 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -1784,15 +1784,27 @@ bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) m_dirs.Insert(wxT(".."), 0u); } - if ( format == wxPATH_UNIX || format == wxPATH_DOS ) + switch ( GetFormat(format) ) { - // a directory made relative with respect to itself is '.' under Unix - // and DOS, by definition (but we don't have to insert "./" for the - // files) - if ( m_dirs.IsEmpty() && IsDir() ) - { - m_dirs.Add(wxT('.')); - } + case wxPATH_NATIVE: + case wxPATH_MAX: + wxFAIL_MSG( wxS("unreachable") ); + wxFALLTHROUGH; + + case wxPATH_UNIX: + case wxPATH_DOS: + // a directory made relative with respect to itself is '.' under + // Unix and DOS, by definition (but we don't have to insert "./" + // for the files) + if ( m_dirs.IsEmpty() && IsDir() ) + { + m_dirs.Add(wxT('.')); + } + break; + + case wxPATH_MAC: + case wxPATH_VMS: + break; } m_relative = true; diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index 9d4402a776..6e344126cd 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -133,6 +133,7 @@ private: CPPUNIT_TEST( TestSetPath ); CPPUNIT_TEST( TestStrip ); CPPUNIT_TEST( TestNormalize ); + CPPUNIT_TEST( TestRelative ); CPPUNIT_TEST( TestReplace ); CPPUNIT_TEST( TestGetHumanReadable ); #ifdef __WINDOWS__ @@ -158,6 +159,7 @@ private: void TestSplit(); void TestSetPath(); void TestStrip(); + void TestRelative(); void TestNormalize(); void TestReplace(); void TestGetHumanReadable(); @@ -441,6 +443,21 @@ void FileNameTestCase::TestNormalize() #endif // __WINDOWS__ } +void FileNameTestCase::TestRelative() +{ + wxFileName fn("a/b.cpp"); + fn.MakeRelativeTo("a"); + CPPUNIT_ASSERT_EQUAL( "b.cpp", fn.GetFullPath() ); + + fn.AssignDir("a/b"); + fn.MakeRelativeTo("a"); + CPPUNIT_ASSERT_EQUAL( "b/", fn.GetFullPath() ); + + fn.AssignDir("a"); + fn.MakeRelativeTo("a"); + CPPUNIT_ASSERT_EQUAL( "./", fn.GetFullPath() ); +} + void FileNameTestCase::TestReplace() { static const struct FileNameTest