]>
Commit | Line | Data |
---|---|---|
5769cf0f VZ |
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" | |
758f356c VZ |
24 | #include "wx/msgout.h" |
25 | #include "wx/scopeguard.h" | |
5769cf0f VZ |
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 ); | |
758f356c | 39 | CPPUNIT_TEST( ParseSwitches ); |
e559d790 | 40 | CPPUNIT_TEST( Usage ); |
5769cf0f VZ |
41 | CPPUNIT_TEST_SUITE_END(); |
42 | ||
43 | void ConvertStringTestCase(); | |
758f356c | 44 | void ParseSwitches(); |
e559d790 | 45 | void Usage(); |
5769cf0f VZ |
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 it's 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 | { | |
a4761b4c VZ |
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) \ | |
5769cf0f | 71 | { \ |
a4761b4c VZ |
72 | const wxArrayString \ |
73 | argsUnix(wxCmdLineParser::ConvertStringToArgs(args, \ | |
74 | wxCMD_LINE_SPLIT_UNIX)); \ | |
75 | WX_ASSERT_STRARRAY_EQUAL(s, argsUnix); \ | |
5769cf0f VZ |
76 | } |
77 | ||
a4761b4c VZ |
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 | ||
5769cf0f VZ |
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 \"" ) | |
a4761b4c VZ |
95 | WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar|\\", "foo bar \\" ) |
96 | WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar|", "foo bar \\" ) | |
5769cf0f | 97 | |
a4761b4c VZ |
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" ); | |
5769cf0f | 101 | |
a4761b4c | 102 | // check for (broken) Windows semantics: backslash doesn't escape spaces |
28d4f49b | 103 | WX_ASSERT_DOS_ARGS_EQUAL( "\\\\foo\\\\|/bar", "\"\\\\foo\\\\\" /bar" ); |
a4761b4c VZ |
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 | |
5769cf0f VZ |
121 | #undef WX_ASSERT_ARGS_EQUAL |
122 | } | |
e559d790 | 123 | |
758f356c VZ |
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 | ||
143 | p.SetCmdLine(""); | |
144 | CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); | |
145 | CPPUNIT_ASSERT( !p.Found("a") ); | |
146 | ||
147 | p.SetCmdLine("-z"); | |
148 | CPPUNIT_ASSERT( p.Parse(false) != 0 ); | |
149 | ||
150 | p.SetCmdLine("-a"); | |
151 | CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); | |
152 | CPPUNIT_ASSERT( p.Found("a") ); | |
153 | CPPUNIT_ASSERT( !p.Found("b") ); | |
154 | ||
155 | p.SetCmdLine("-a -d"); | |
156 | CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); | |
157 | CPPUNIT_ASSERT( p.Found("a") ); | |
158 | CPPUNIT_ASSERT( !p.Found("b") ); | |
159 | CPPUNIT_ASSERT( !p.Found("c") ); | |
160 | CPPUNIT_ASSERT( p.Found("d") ); | |
161 | ||
162 | p.SetCmdLine("-abd"); | |
163 | CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); | |
164 | CPPUNIT_ASSERT( p.Found("a") ); | |
165 | CPPUNIT_ASSERT( p.Found("b") ); | |
166 | CPPUNIT_ASSERT( !p.Found("c") ); | |
167 | CPPUNIT_ASSERT( p.Found("d") ); | |
168 | ||
169 | p.SetCmdLine("-abdz"); | |
170 | CPPUNIT_ASSERT( p.Parse(false) != 0 ); | |
171 | ||
172 | p.SetCmdLine("-ab -cd"); | |
173 | CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); | |
174 | CPPUNIT_ASSERT( p.Found("a") ); | |
175 | CPPUNIT_ASSERT( p.Found("b") ); | |
176 | CPPUNIT_ASSERT( p.Found("c") ); | |
177 | CPPUNIT_ASSERT( p.Found("d") ); | |
178 | ||
179 | p.SetCmdLine("-da"); | |
180 | CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); | |
181 | CPPUNIT_ASSERT( p.Found("a") ); | |
182 | CPPUNIT_ASSERT( !p.Found("b") ); | |
183 | CPPUNIT_ASSERT( !p.Found("c") ); | |
184 | CPPUNIT_ASSERT( p.Found("d") ); | |
185 | } | |
186 | ||
e559d790 VZ |
187 | void CmdLineTestCase::Usage() |
188 | { | |
189 | // check that Usage() returns roughly what we expect (don't check all the | |
190 | // details, its format can change in the future) | |
191 | static const wxCmdLineEntryDesc desc[] = | |
192 | { | |
193 | { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "Verbosity options" }, | |
194 | { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" }, | |
195 | { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" }, | |
196 | ||
197 | { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "Output options" }, | |
198 | { wxCMD_LINE_OPTION, "o", "output", "output file" }, | |
199 | { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER }, | |
200 | { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE }, | |
201 | { wxCMD_LINE_OPTION, "f", "double", "output double", wxCMD_LINE_VAL_DOUBLE }, | |
202 | ||
203 | { wxCMD_LINE_PARAM, NULL, NULL, "input file", }, | |
204 | ||
205 | { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "\nEven more usage text" }, | |
206 | { wxCMD_LINE_NONE } | |
207 | }; | |
208 | ||
209 | wxCmdLineParser p(desc); | |
210 | const wxArrayString usageLines = wxSplit(p.GetUsageString(), '\n'); | |
211 | ||
212 | enum | |
213 | { | |
214 | Line_Synopsis, | |
215 | Line_Text_Verbosity, | |
216 | Line_Verbose, | |
217 | Line_Quiet, | |
218 | Line_Text_Output, | |
219 | Line_Output_File, | |
220 | Line_Output_Size, | |
221 | Line_Output_Date, | |
222 | Line_Output_Double, | |
223 | Line_Text_Dummy1, | |
224 | Line_Text_Dummy2, | |
225 | Line_Last, | |
226 | Line_Max | |
227 | }; | |
228 | ||
1de532f5 VZ |
229 | CPPUNIT_ASSERT_EQUAL(Line_Max, usageLines.size()); |
230 | CPPUNIT_ASSERT_EQUAL("Verbosity options", usageLines[Line_Text_Verbosity]); | |
231 | CPPUNIT_ASSERT_EQUAL("", usageLines[Line_Text_Dummy1]); | |
232 | CPPUNIT_ASSERT_EQUAL("Even more usage text", usageLines[Line_Text_Dummy2]); | |
233 | CPPUNIT_ASSERT_EQUAL("", usageLines[Line_Last]); | |
e559d790 | 234 | } |