[ 1068188 ] Precompiled header for the test program [Modified a bit]
[wxWidgets.git] / tests / filename / filenametest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/filename/filename.cpp
3 // Purpose: wxFileName unit test
4 // Author: Vadim Zeitlin
5 // Created: 2004-07-25
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #endif // WX_PRECOMP
22
23 #include "wx/filename.h"
24
25 // ----------------------------------------------------------------------------
26 // test data
27 // ----------------------------------------------------------------------------
28
29 static struct FileNameInfo
30 {
31 const wxChar *fullname;
32 const wxChar *volume;
33 const wxChar *path;
34 const wxChar *name;
35 const wxChar *ext;
36 bool isAbsolute;
37 wxPathFormat format;
38 } filenames[] =
39 {
40 // Unix file names
41 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX },
42 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX },
43 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX },
44 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX },
45 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX },
46 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX },
47 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX },
48 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX },
49 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX },
50
51 // Windows file names
52 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
53 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS },
54 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
55 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
56 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS },
57 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
58 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS },
59
60 // wxFileName support for Mac file names is broken currently
61 #if 0
62 // Mac file names
63 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC },
64 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC },
65 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC },
66 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC },
67 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
68 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
69 #endif // 0
70
71 // VMS file names
72 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS },
73 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS },
74 };
75
76 // ----------------------------------------------------------------------------
77 // test class
78 // ----------------------------------------------------------------------------
79
80 class FileNameTestCase : public CppUnit::TestCase
81 {
82 public:
83 FileNameTestCase() { }
84
85 private:
86 CPPUNIT_TEST_SUITE( FileNameTestCase );
87 CPPUNIT_TEST( TestConstruction );
88 CPPUNIT_TEST( TestSplit );
89 CPPUNIT_TEST( TestSetPath );
90 CPPUNIT_TEST_SUITE_END();
91
92 void TestConstruction();
93 void TestSplit();
94 void TestSetPath();
95
96 DECLARE_NO_COPY_CLASS(FileNameTestCase)
97 };
98
99 // register in the unnamed registry so that these tests are run by default
100 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase );
101
102 // also include in it's own registry so that these tests can be run alone
103 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" );
104
105 void FileNameTestCase::TestConstruction()
106 {
107 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
108 {
109 const FileNameInfo& fni = filenames[n];
110
111 wxFileName fn(fni.fullname, fni.format);
112
113 wxString fullname = fn.GetFullPath(fni.format);
114 CPPUNIT_ASSERT( fullname == fni.fullname );
115
116 CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) );
117
118 if ( *fni.volume && *fni.path )
119 {
120 // check that specifying the volume separately or as part of the
121 // path doesn't make any difference
122 wxString pathWithVolume = fni.volume;
123 pathWithVolume += wxFileName::GetVolumeSeparator(fni.format);
124 pathWithVolume += fni.path;
125
126 CPPUNIT_ASSERT( fn == wxFileName(pathWithVolume,
127 fni.name,
128 fni.ext,
129 fni.format) );
130 }
131 }
132 }
133
134 void FileNameTestCase::TestSplit()
135 {
136 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
137 {
138 const FileNameInfo& fni = filenames[n];
139 wxString volume, path, name, ext;
140 wxFileName::SplitPath(fni.fullname,
141 &volume, &path, &name, &ext, fni.format);
142
143 CPPUNIT_ASSERT( volume == fni.volume );
144 CPPUNIT_ASSERT( path == fni.path );
145 CPPUNIT_ASSERT( name == fni.name );
146 CPPUNIT_ASSERT( ext == fni.ext );
147 }
148 }
149
150 void FileNameTestCase::TestSetPath()
151 {
152 wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS);
153 fn.SetPath(_T("c:\\temp"), wxPATH_DOS);
154 CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS)) );
155
156 fn = wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX);
157 fn.SetPath(_T("/usr/local/bin"), wxPATH_UNIX);
158 CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX)) );
159 }
160