]>
git.saurik.com Git - wxWidgets.git/blob - tests/cmdline/cmdlinetest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/cmdline/cmdlinetest.cpp
3 // Purpose: wxCmdLineParser unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
23 #include "wx/cmdline.h"
24 #include "wx/msgout.h"
25 #include "wx/scopeguard.h"
27 // --------------------------------------------------------------------------
29 // --------------------------------------------------------------------------
31 class CmdLineTestCase
: public CppUnit::TestCase
37 CPPUNIT_TEST_SUITE( CmdLineTestCase
);
38 CPPUNIT_TEST( ConvertStringTestCase
);
39 CPPUNIT_TEST( ParseSwitches
);
40 CPPUNIT_TEST( Usage
);
41 CPPUNIT_TEST_SUITE_END();
43 void ConvertStringTestCase();
47 DECLARE_NO_COPY_CLASS(CmdLineTestCase
)
50 // register in the unnamed registry so that these tests are run by default
51 CPPUNIT_TEST_SUITE_REGISTRATION( CmdLineTestCase
);
53 // also include in it's own registry so that these tests can be run alone
54 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase
, "CmdLineTestCase" );
56 // ============================================================================
58 // ============================================================================
60 void CmdLineTestCase::ConvertStringTestCase()
62 #define WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
65 argsDOS(wxCmdLineParser::ConvertStringToArgs(args, \
66 wxCMD_LINE_SPLIT_DOS)); \
67 WX_ASSERT_STRARRAY_EQUAL(s, argsDOS); \
70 #define WX_ASSERT_UNIX_ARGS_EQUAL(s, args) \
73 argsUnix(wxCmdLineParser::ConvertStringToArgs(args, \
74 wxCMD_LINE_SPLIT_UNIX)); \
75 WX_ASSERT_STRARRAY_EQUAL(s, argsUnix); \
78 #define WX_ASSERT_ARGS_EQUAL(s, args) \
79 WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
80 WX_ASSERT_UNIX_ARGS_EQUAL(s, args)
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\"" )
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 \\" )
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" );
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\"" );
107 // check for more sane Unix semantics: backslash does escape spaces and
109 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo bar\\ baz" );
110 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar\"baz", "foo \"bar\\\"baz\"" );
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" )
119 #undef WX_ASSERT_DOS_ARGS_EQUAL
120 #undef WX_ASSERT_UNIX_ARGS_EQUAL
121 #undef WX_ASSERT_ARGS_EQUAL
124 void CmdLineTestCase::ParseSwitches()
126 // install a dummy message output object just suppress error messages from
127 // wxCmdLineParser::Parse()
128 class NoMessageOutput
: public wxMessageOutput
131 virtual void Output(const wxString
& WXUNUSED(str
)) { }
134 wxMessageOutput
* const old
= wxMessageOutput::Set(&noMessages
);
135 wxON_BLOCK_EXIT1( wxMessageOutput::Set
, old
);
144 CPPUNIT_ASSERT_EQUAL(0, p
.Parse(false) );
145 CPPUNIT_ASSERT( !p
.Found("a") );
148 CPPUNIT_ASSERT( p
.Parse(false) != 0 );
151 CPPUNIT_ASSERT_EQUAL(0, p
.Parse(false) );
152 CPPUNIT_ASSERT( p
.Found("a") );
153 CPPUNIT_ASSERT( !p
.Found("b") );
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") );
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") );
169 p
.SetCmdLine("-abdz");
170 CPPUNIT_ASSERT( p
.Parse(false) != 0 );
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") );
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") );
187 void CmdLineTestCase::Usage()
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
[] =
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" },
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
},
203 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file", },
205 { wxCMD_LINE_USAGE_TEXT
, NULL
, NULL
, "\nEven more usage text" },
209 wxCmdLineParser
p(desc
);
210 const wxArrayString usageLines
= wxSplit(p
.GetUsageString(), '\n');
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
]);