]> git.saurik.com Git - wxWidgets.git/blob - tests/filename/filename.cpp
added SetPath() test
[wxWidgets.git] / tests / filename / filename.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 "wx/wxprec.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 #include "wx/cppunit.h"
26
27 // ----------------------------------------------------------------------------
28 // test data
29 // ----------------------------------------------------------------------------
30
31 static struct FileNameInfo
32 {
33 const wxChar *fullname;
34 const wxChar *volume;
35 const wxChar *path;
36 const wxChar *name;
37 const wxChar *ext;
38 bool isAbsolute;
39 wxPathFormat format;
40 } filenames[] =
41 {
42 // Unix file names
43 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX },
44 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX },
45 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX },
46 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX },
47 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX },
48 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX },
49 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX },
50 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX },
51 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX },
52
53 // Windows file names
54 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
55 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS },
56 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
57 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
58 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS },
59 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
60 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS },
61
62 // wxFileName support for Mac file names is broken currently
63 #if 0
64 // Mac file names
65 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC },
66 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC },
67 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC },
68 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC },
69 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
70 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
71 #endif // 0
72
73 // VMS file names
74 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS },
75 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS },
76 };
77
78 // ----------------------------------------------------------------------------
79 // test class
80 // ----------------------------------------------------------------------------
81
82 class FileNameTestCase : public CppUnit::TestCase
83 {
84 public:
85 FileNameTestCase() { }
86
87 private:
88 CPPUNIT_TEST_SUITE( FileNameTestCase );
89 CPPUNIT_TEST( TestConstruction );
90 CPPUNIT_TEST( TestSplit );
91 CPPUNIT_TEST( TestSetPath );
92 CPPUNIT_TEST_SUITE_END();
93
94 void TestConstruction();
95 void TestSplit();
96 void TestSetPath();
97
98 DECLARE_NO_COPY_CLASS(FileNameTestCase)
99 };
100
101 // register in the unnamed registry so that these tests are run by default
102 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase );
103
104 // also include in it's own registry so that these tests can be run alone
105 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" );
106
107 void FileNameTestCase::TestConstruction()
108 {
109 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
110 {
111 const FileNameInfo& fni = filenames[n];
112
113 wxFileName fn(fni.fullname, fni.format);
114
115 wxString fullname = fn.GetFullPath(fni.format);
116 CPPUNIT_ASSERT( fullname == fni.fullname );
117
118 CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) );
119 }
120 }
121
122 void FileNameTestCase::TestSplit()
123 {
124 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
125 {
126 const FileNameInfo& fni = filenames[n];
127 wxString volume, path, name, ext;
128 wxFileName::SplitPath(fni.fullname,
129 &volume, &path, &name, &ext, fni.format);
130
131 CPPUNIT_ASSERT( volume == fni.volume );
132 CPPUNIT_ASSERT( path == fni.path );
133 CPPUNIT_ASSERT( name == fni.name );
134 CPPUNIT_ASSERT( ext == fni.ext );
135 }
136 }
137
138 void FileNameTestCase::TestSetPath()
139 {
140 wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS);
141 fn.SetPath(_T("c:\\temp"));
142 CPPUNIT_ASSERT( fn == _T("c:\\temp\\foo.bar") );
143
144 fn = _T("/usr/bin/ls");
145 fn.SetPath(_T("/usr/local/bin"));
146 CPPUNIT_ASSERT( fn == _T("/usr/local/bin/ls") );
147 }
148