]> git.saurik.com Git - wxWidgets.git/blame - tests/cmdline/cmdlinetest.cpp
Use RIAA wrapper for wxSpinCtrl event disabling in wxGTK.
[wxWidgets.git] / tests / cmdline / cmdlinetest.cpp
CommitLineData
5769cf0f
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/cmdline/cmdlinetest.cpp
3// Purpose: wxCmdLineParser unit test
4// Author: Vadim Zeitlin
5// Created: 2008-04-12
5769cf0f
VZ
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"
758f356c
VZ
23#include "wx/msgout.h"
24#include "wx/scopeguard.h"
5769cf0f
VZ
25
26// --------------------------------------------------------------------------
27// test class
28// --------------------------------------------------------------------------
29
30class CmdLineTestCase : public CppUnit::TestCase
31{
32public:
33 CmdLineTestCase() {}
34
35private:
36 CPPUNIT_TEST_SUITE( CmdLineTestCase );
37 CPPUNIT_TEST( ConvertStringTestCase );
758f356c 38 CPPUNIT_TEST( ParseSwitches );
e559d790 39 CPPUNIT_TEST( Usage );
5769cf0f
VZ
40 CPPUNIT_TEST_SUITE_END();
41
42 void ConvertStringTestCase();
758f356c 43 void ParseSwitches();
e559d790 44 void Usage();
5769cf0f
VZ
45
46 DECLARE_NO_COPY_CLASS(CmdLineTestCase)
47};
48
49// register in the unnamed registry so that these tests are run by default
50CPPUNIT_TEST_SUITE_REGISTRATION( CmdLineTestCase );
51
e3778b4d 52// also include in its own registry so that these tests can be run alone
5769cf0f
VZ
53CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase, "CmdLineTestCase" );
54
55// ============================================================================
56// implementation
57// ============================================================================
58
59void CmdLineTestCase::ConvertStringTestCase()
60{
a4761b4c
VZ
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) \
5769cf0f 70 { \
a4761b4c
VZ
71 const wxArrayString \
72 argsUnix(wxCmdLineParser::ConvertStringToArgs(args, \
73 wxCMD_LINE_SPLIT_UNIX)); \
74 WX_ASSERT_STRARRAY_EQUAL(s, argsUnix); \
5769cf0f
VZ
75 }
76
a4761b4c
VZ
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
5769cf0f
VZ
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 \"" )
a4761b4c
VZ
94 WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar|\\", "foo bar \\" )
95 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar|", "foo bar \\" )
5769cf0f 96
a4761b4c
VZ
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" );
5769cf0f 100
a4761b4c 101 // check for (broken) Windows semantics: backslash doesn't escape spaces
28d4f49b 102 WX_ASSERT_DOS_ARGS_EQUAL( "\\\\foo\\\\|/bar", "\"\\\\foo\\\\\" /bar" );
a4761b4c
VZ
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
5769cf0f
VZ
120 #undef WX_ASSERT_ARGS_EQUAL
121}
e559d790 122
758f356c
VZ
123void 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");
1a63798c
VZ
141 p.AddSwitch("n", "neg", "Switch that can be negated",
142 wxCMD_LINE_SWITCH_NEGATABLE);
758f356c
VZ
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") );
1a63798c
VZ
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") );
758f356c
VZ
203}
204
e559d790
VZ
205void 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
1de532f5
VZ
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]);
e559d790 252}