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