1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/cmdline/cmdlinetest.cpp
3 // Purpose: wxCmdLineParser unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
22 #include "wx/cmdline.h"
23 #include "wx/msgout.h"
24 #include "wx/scopeguard.h"
26 // --------------------------------------------------------------------------
28 // --------------------------------------------------------------------------
30 class CmdLineTestCase
: public CppUnit::TestCase
36 CPPUNIT_TEST_SUITE( CmdLineTestCase
);
37 CPPUNIT_TEST( ConvertStringTestCase
);
38 CPPUNIT_TEST( ParseSwitches
);
39 CPPUNIT_TEST( Usage
);
40 CPPUNIT_TEST_SUITE_END();
42 void ConvertStringTestCase();
46 DECLARE_NO_COPY_CLASS(CmdLineTestCase
)
49 // register in the unnamed registry so that these tests are run by default
50 CPPUNIT_TEST_SUITE_REGISTRATION( CmdLineTestCase
);
52 // also include in its own registry so that these tests can be run alone
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase
, "CmdLineTestCase" );
55 // ============================================================================
57 // ============================================================================
59 void CmdLineTestCase::ConvertStringTestCase()
61 #define WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
64 argsDOS(wxCmdLineParser::ConvertStringToArgs(args, \
65 wxCMD_LINE_SPLIT_DOS)); \
66 WX_ASSERT_STRARRAY_EQUAL(s, argsDOS); \
69 #define WX_ASSERT_UNIX_ARGS_EQUAL(s, args) \
72 argsUnix(wxCmdLineParser::ConvertStringToArgs(args, \
73 wxCMD_LINE_SPLIT_UNIX)); \
74 WX_ASSERT_STRARRAY_EQUAL(s, argsUnix); \
77 #define WX_ASSERT_ARGS_EQUAL(s, args) \
78 WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
79 WX_ASSERT_UNIX_ARGS_EQUAL(s, args)
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\"" )
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 \\" )
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" );
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\"" );
106 // check for more sane Unix semantics: backslash does escape spaces and
108 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo bar\\ baz" );
109 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar\"baz", "foo \"bar\\\"baz\"" );
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" )
118 #undef WX_ASSERT_DOS_ARGS_EQUAL
119 #undef WX_ASSERT_UNIX_ARGS_EQUAL
120 #undef WX_ASSERT_ARGS_EQUAL
123 void CmdLineTestCase::ParseSwitches()
125 // install a dummy message output object just suppress error messages from
126 // wxCmdLineParser::Parse()
127 class NoMessageOutput
: public wxMessageOutput
130 virtual void Output(const wxString
& WXUNUSED(str
)) { }
133 wxMessageOutput
* const old
= wxMessageOutput::Set(&noMessages
);
134 wxON_BLOCK_EXIT1( wxMessageOutput::Set
, old
);
141 p
.AddSwitch("n", "neg", "Switch that can be negated",
142 wxCMD_LINE_SWITCH_NEGATABLE
);
145 CPPUNIT_ASSERT_EQUAL(0, p
.Parse(false) );
146 CPPUNIT_ASSERT( !p
.Found("a") );
149 CPPUNIT_ASSERT( p
.Parse(false) != 0 );
152 CPPUNIT_ASSERT_EQUAL(0, p
.Parse(false) );
153 CPPUNIT_ASSERT( p
.Found("a") );
154 CPPUNIT_ASSERT( !p
.Found("b") );
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") );
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") );
170 p
.SetCmdLine("-abdz");
171 CPPUNIT_ASSERT( p
.Parse(false) != 0 );
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") );
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") );
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") );
193 CPPUNIT_ASSERT_EQUAL(0, p
.Parse(false) );
194 CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_OFF
, p
.FoundSwitch("neg") );
196 p
.SetCmdLine("--neg");
197 CPPUNIT_ASSERT_EQUAL(0, p
.Parse(false) );
198 CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_ON
, p
.FoundSwitch("neg") );
200 p
.SetCmdLine("--neg-");
201 CPPUNIT_ASSERT_EQUAL(0, p
.Parse(false) );
202 CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_OFF
, p
.FoundSwitch("n") );
205 void CmdLineTestCase::Usage()
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
[] =
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" },
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
},
221 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file", },
223 { wxCMD_LINE_USAGE_TEXT
, NULL
, NULL
, "\nEven more usage text" },
227 wxCmdLineParser
p(desc
);
228 const wxArrayString usageLines
= wxSplit(p
.GetUsageString(), '\n');
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
]);