]> git.saurik.com Git - wxWidgets.git/blob - tests/filename/filename.cpp
added wxFileName 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_SUITE_END();
92
93 void TestConstruction();
94 void TestSplit();
95 void TestSetPath();
96
97 DECLARE_NO_COPY_CLASS(FileNameTestCase)
98 };
99
100 // register in the unnamed registry so that these tests are run by default
101 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase );
102
103 // also include in it's own registry so that these tests can be run alone
104 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" );
105
106 void FileNameTestCase::TestConstruction()
107 {
108 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
109 {
110 const FileNameInfo& fni = filenames[n];
111
112 wxFileName fn(fni.fullname, fni.format);
113
114 wxString fullname = fn.GetFullPath(fni.format);
115 CPPUNIT_ASSERT( fullname == fni.fullname );
116
117 CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) );
118 }
119 }
120
121 void FileNameTestCase::TestSplit()
122 {
123 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
124 {
125 const FileNameInfo& fni = filenames[n];
126 wxString volume, path, name, ext;
127 wxFileName::SplitPath(fni.fullname,
128 &volume, &path, &name, &ext, fni.format);
129
130 CPPUNIT_ASSERT( volume == fni.volume );
131 CPPUNIT_ASSERT( path == fni.path );
132 CPPUNIT_ASSERT( name == fni.name );
133 CPPUNIT_ASSERT( ext == fni.ext );
134 }
135 }
136