]> git.saurik.com Git - wxWidgets.git/blob - tests/cmdline/cmdlinetest.cpp
adding constants for newer OSX versions to make sure our conditional expressions...
[wxWidgets.git] / tests / cmdline / cmdlinetest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/cmdline/cmdlinetest.cpp
3 // Purpose: wxCmdLineParser unit test
4 // Author: Vadim Zeitlin
5 // Created: 2008-04-12
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
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/cmdline.h"
24 #include "wx/msgout.h"
25 #include "wx/scopeguard.h"
26
27 // --------------------------------------------------------------------------
28 // test class
29 // --------------------------------------------------------------------------
30
31 class CmdLineTestCase : public CppUnit::TestCase
32 {
33 public:
34 CmdLineTestCase() {}
35
36 private:
37 CPPUNIT_TEST_SUITE( CmdLineTestCase );
38 CPPUNIT_TEST( ConvertStringTestCase );
39 CPPUNIT_TEST( ParseSwitches );
40 CPPUNIT_TEST( Usage );
41 CPPUNIT_TEST_SUITE_END();
42
43 void ConvertStringTestCase();
44 void ParseSwitches();
45 void Usage();
46
47 DECLARE_NO_COPY_CLASS(CmdLineTestCase)
48 };
49
50 // register in the unnamed registry so that these tests are run by default
51 CPPUNIT_TEST_SUITE_REGISTRATION( CmdLineTestCase );
52
53 // also include in its own registry so that these tests can be run alone
54 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase, "CmdLineTestCase" );
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 void CmdLineTestCase::ConvertStringTestCase()
61 {
62 #define WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
63 { \
64 const wxArrayString \
65 argsDOS(wxCmdLineParser::ConvertStringToArgs(args, \
66 wxCMD_LINE_SPLIT_DOS)); \
67 WX_ASSERT_STRARRAY_EQUAL(s, argsDOS); \
68 }
69
70 #define WX_ASSERT_UNIX_ARGS_EQUAL(s, args) \
71 { \
72 const wxArrayString \
73 argsUnix(wxCmdLineParser::ConvertStringToArgs(args, \
74 wxCMD_LINE_SPLIT_UNIX)); \
75 WX_ASSERT_STRARRAY_EQUAL(s, argsUnix); \
76 }
77
78 #define WX_ASSERT_ARGS_EQUAL(s, args) \
79 WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
80 WX_ASSERT_UNIX_ARGS_EQUAL(s, args)
81
82 // normal cases
83 WX_ASSERT_ARGS_EQUAL( "foo", "foo" )
84 WX_ASSERT_ARGS_EQUAL( "foo bar", "\"foo bar\"" )
85 WX_ASSERT_ARGS_EQUAL( "foo|bar", "foo bar" )
86 WX_ASSERT_ARGS_EQUAL( "foo|bar|baz", "foo bar baz" )
87 WX_ASSERT_ARGS_EQUAL( "foo|bar baz", "foo \"bar baz\"" )
88
89 // special cases
90 WX_ASSERT_ARGS_EQUAL( "", "" )
91 WX_ASSERT_ARGS_EQUAL( "foo", "foo " )
92 WX_ASSERT_ARGS_EQUAL( "foo", "foo \t " )
93 WX_ASSERT_ARGS_EQUAL( "foo|bar", "foo bar " )
94 WX_ASSERT_ARGS_EQUAL( "foo|bar|", "foo bar \"" )
95 WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar|\\", "foo bar \\" )
96 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar|", "foo bar \\" )
97
98 WX_ASSERT_ARGS_EQUAL( "12 34", "1\"2 3\"4" );
99 WX_ASSERT_ARGS_EQUAL( "1|2 34", "1 \"2 3\"4" );
100 WX_ASSERT_ARGS_EQUAL( "1|2 3|4", "1 \"2 3\" 4" );
101
102 // check for (broken) Windows semantics: backslash doesn't escape spaces
103 WX_ASSERT_DOS_ARGS_EQUAL( "\\\\foo\\\\|/bar", "\"\\\\foo\\\\\" /bar" );
104 WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar\\|baz", "foo bar\\ baz" );
105 WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar\\\"baz", "foo \"bar\\\"baz\"" );
106
107 // check for more sane Unix semantics: backslash does escape spaces and
108 // quotes
109 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo bar\\ baz" );
110 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar\"baz", "foo \"bar\\\"baz\"" );
111
112 // check that single quotes work too with Unix semantics
113 WX_ASSERT_UNIX_ARGS_EQUAL( "foo bar", "'foo bar'" )
114 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo 'bar baz'" )
115 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo 'bar baz'" )
116 WX_ASSERT_UNIX_ARGS_EQUAL( "O'Henry", "\"O'Henry\"" )
117 WX_ASSERT_UNIX_ARGS_EQUAL( "O'Henry", "O\\'Henry" )
118
119 #undef WX_ASSERT_DOS_ARGS_EQUAL
120 #undef WX_ASSERT_UNIX_ARGS_EQUAL
121 #undef WX_ASSERT_ARGS_EQUAL
122 }
123
124 void CmdLineTestCase::ParseSwitches()
125 {
126 // install a dummy message output object just suppress error messages from
127 // wxCmdLineParser::Parse()
128 class NoMessageOutput : public wxMessageOutput
129 {
130 public:
131 virtual void Output(const wxString& WXUNUSED(str)) { }
132 } noMessages;
133
134 wxMessageOutput * const old = wxMessageOutput::Set(&noMessages);
135 wxON_BLOCK_EXIT1( wxMessageOutput::Set, old );
136
137 wxCmdLineParser p;
138 p.AddSwitch("a");
139 p.AddSwitch("b");
140 p.AddSwitch("c");
141 p.AddSwitch("d");
142 p.AddSwitch("n", "neg", "Switch that can be negated",
143 wxCMD_LINE_SWITCH_NEGATABLE);
144
145 p.SetCmdLine("");
146 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
147 CPPUNIT_ASSERT( !p.Found("a") );
148
149 p.SetCmdLine("-z");
150 CPPUNIT_ASSERT( p.Parse(false) != 0 );
151
152 p.SetCmdLine("-a");
153 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
154 CPPUNIT_ASSERT( p.Found("a") );
155 CPPUNIT_ASSERT( !p.Found("b") );
156
157 p.SetCmdLine("-a -d");
158 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
159 CPPUNIT_ASSERT( p.Found("a") );
160 CPPUNIT_ASSERT( !p.Found("b") );
161 CPPUNIT_ASSERT( !p.Found("c") );
162 CPPUNIT_ASSERT( p.Found("d") );
163
164 p.SetCmdLine("-abd");
165 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
166 CPPUNIT_ASSERT( p.Found("a") );
167 CPPUNIT_ASSERT( p.Found("b") );
168 CPPUNIT_ASSERT( !p.Found("c") );
169 CPPUNIT_ASSERT( p.Found("d") );
170
171 p.SetCmdLine("-abdz");
172 CPPUNIT_ASSERT( p.Parse(false) != 0 );
173
174 p.SetCmdLine("-ab -cd");
175 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
176 CPPUNIT_ASSERT( p.Found("a") );
177 CPPUNIT_ASSERT( p.Found("b") );
178 CPPUNIT_ASSERT( p.Found("c") );
179 CPPUNIT_ASSERT( p.Found("d") );
180
181 p.SetCmdLine("-da");
182 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
183 CPPUNIT_ASSERT( p.Found("a") );
184 CPPUNIT_ASSERT( !p.Found("b") );
185 CPPUNIT_ASSERT( !p.Found("c") );
186 CPPUNIT_ASSERT( p.Found("d") );
187
188 p.SetCmdLine("-n");
189 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
190 CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_NOT_FOUND, p.FoundSwitch("a") );
191 CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_ON, p.FoundSwitch("n") );
192
193 p.SetCmdLine("-n-");
194 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
195 CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_OFF, p.FoundSwitch("neg") );
196
197 p.SetCmdLine("--neg");
198 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
199 CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_ON, p.FoundSwitch("neg") );
200
201 p.SetCmdLine("--neg-");
202 CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
203 CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_OFF, p.FoundSwitch("n") );
204 }
205
206 void CmdLineTestCase::Usage()
207 {
208 // check that Usage() returns roughly what we expect (don't check all the
209 // details, its format can change in the future)
210 static const wxCmdLineEntryDesc desc[] =
211 {
212 { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "Verbosity options" },
213 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
214 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
215
216 { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "Output options" },
217 { wxCMD_LINE_OPTION, "o", "output", "output file" },
218 { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER },
219 { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
220 { wxCMD_LINE_OPTION, "f", "double", "output double", wxCMD_LINE_VAL_DOUBLE },
221
222 { wxCMD_LINE_PARAM, NULL, NULL, "input file", },
223
224 { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "\nEven more usage text" },
225 { wxCMD_LINE_NONE }
226 };
227
228 wxCmdLineParser p(desc);
229 const wxArrayString usageLines = wxSplit(p.GetUsageString(), '\n');
230
231 enum
232 {
233 Line_Synopsis,
234 Line_Text_Verbosity,
235 Line_Verbose,
236 Line_Quiet,
237 Line_Text_Output,
238 Line_Output_File,
239 Line_Output_Size,
240 Line_Output_Date,
241 Line_Output_Double,
242 Line_Text_Dummy1,
243 Line_Text_Dummy2,
244 Line_Last,
245 Line_Max
246 };
247
248 CPPUNIT_ASSERT_EQUAL(Line_Max, usageLines.size());
249 CPPUNIT_ASSERT_EQUAL("Verbosity options", usageLines[Line_Text_Verbosity]);
250 CPPUNIT_ASSERT_EQUAL("", usageLines[Line_Text_Dummy1]);
251 CPPUNIT_ASSERT_EQUAL("Even more usage text", usageLines[Line_Text_Dummy2]);
252 CPPUNIT_ASSERT_EQUAL("", usageLines[Line_Last]);
253 }