]> git.saurik.com Git - wxWidgets.git/blob - tests/cmdline/cmdlinetest.cpp
Removing more CodeWarrior project files.
[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 // 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"
24
25 // --------------------------------------------------------------------------
26 // test class
27 // --------------------------------------------------------------------------
28
29 class CmdLineTestCase : public CppUnit::TestCase
30 {
31 public:
32 CmdLineTestCase() {}
33
34 private:
35 CPPUNIT_TEST_SUITE( CmdLineTestCase );
36 CPPUNIT_TEST( ConvertStringTestCase );
37 CPPUNIT_TEST( Usage );
38 CPPUNIT_TEST_SUITE_END();
39
40 void ConvertStringTestCase();
41 void Usage();
42
43 DECLARE_NO_COPY_CLASS(CmdLineTestCase)
44 };
45
46 // register in the unnamed registry so that these tests are run by default
47 CPPUNIT_TEST_SUITE_REGISTRATION( CmdLineTestCase );
48
49 // also include in it's own registry so that these tests can be run alone
50 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase, "CmdLineTestCase" );
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 void CmdLineTestCase::ConvertStringTestCase()
57 {
58 #define WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
59 { \
60 const wxArrayString \
61 argsDOS(wxCmdLineParser::ConvertStringToArgs(args, \
62 wxCMD_LINE_SPLIT_DOS)); \
63 WX_ASSERT_STRARRAY_EQUAL(s, argsDOS); \
64 }
65
66 #define WX_ASSERT_UNIX_ARGS_EQUAL(s, args) \
67 { \
68 const wxArrayString \
69 argsUnix(wxCmdLineParser::ConvertStringToArgs(args, \
70 wxCMD_LINE_SPLIT_UNIX)); \
71 WX_ASSERT_STRARRAY_EQUAL(s, argsUnix); \
72 }
73
74 #define WX_ASSERT_ARGS_EQUAL(s, args) \
75 WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
76 WX_ASSERT_UNIX_ARGS_EQUAL(s, args)
77
78 // normal cases
79 WX_ASSERT_ARGS_EQUAL( "foo", "foo" )
80 WX_ASSERT_ARGS_EQUAL( "foo bar", "\"foo bar\"" )
81 WX_ASSERT_ARGS_EQUAL( "foo|bar", "foo bar" )
82 WX_ASSERT_ARGS_EQUAL( "foo|bar|baz", "foo bar baz" )
83 WX_ASSERT_ARGS_EQUAL( "foo|bar baz", "foo \"bar baz\"" )
84
85 // special cases
86 WX_ASSERT_ARGS_EQUAL( "", "" )
87 WX_ASSERT_ARGS_EQUAL( "foo", "foo " )
88 WX_ASSERT_ARGS_EQUAL( "foo", "foo \t " )
89 WX_ASSERT_ARGS_EQUAL( "foo|bar", "foo bar " )
90 WX_ASSERT_ARGS_EQUAL( "foo|bar|", "foo bar \"" )
91 WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar|\\", "foo bar \\" )
92 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar|", "foo bar \\" )
93
94 WX_ASSERT_ARGS_EQUAL( "12 34", "1\"2 3\"4" );
95 WX_ASSERT_ARGS_EQUAL( "1|2 34", "1 \"2 3\"4" );
96 WX_ASSERT_ARGS_EQUAL( "1|2 3|4", "1 \"2 3\" 4" );
97
98 // check for (broken) Windows semantics: backslash doesn't escape spaces
99 WX_ASSERT_DOS_ARGS_EQUAL( "\\\\foo\\\\|/bar", "\"\\\\foo\\\\\" /bar" );
100 WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar\\|baz", "foo bar\\ baz" );
101 WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar\\\"baz", "foo \"bar\\\"baz\"" );
102
103 // check for more sane Unix semantics: backslash does escape spaces and
104 // quotes
105 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo bar\\ baz" );
106 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar\"baz", "foo \"bar\\\"baz\"" );
107
108 // check that single quotes work too with Unix semantics
109 WX_ASSERT_UNIX_ARGS_EQUAL( "foo bar", "'foo bar'" )
110 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo 'bar baz'" )
111 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo 'bar baz'" )
112 WX_ASSERT_UNIX_ARGS_EQUAL( "O'Henry", "\"O'Henry\"" )
113 WX_ASSERT_UNIX_ARGS_EQUAL( "O'Henry", "O\\'Henry" )
114
115 #undef WX_ASSERT_DOS_ARGS_EQUAL
116 #undef WX_ASSERT_UNIX_ARGS_EQUAL
117 #undef WX_ASSERT_ARGS_EQUAL
118 }
119
120 void CmdLineTestCase::Usage()
121 {
122 // check that Usage() returns roughly what we expect (don't check all the
123 // details, its format can change in the future)
124 static const wxCmdLineEntryDesc desc[] =
125 {
126 { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "Verbosity options" },
127 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
128 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
129
130 { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "Output options" },
131 { wxCMD_LINE_OPTION, "o", "output", "output file" },
132 { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER },
133 { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
134 { wxCMD_LINE_OPTION, "f", "double", "output double", wxCMD_LINE_VAL_DOUBLE },
135
136 { wxCMD_LINE_PARAM, NULL, NULL, "input file", },
137
138 { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "\nEven more usage text" },
139 { wxCMD_LINE_NONE }
140 };
141
142 wxCmdLineParser p(desc);
143 const wxArrayString usageLines = wxSplit(p.GetUsageString(), '\n');
144
145 enum
146 {
147 Line_Synopsis,
148 Line_Text_Verbosity,
149 Line_Verbose,
150 Line_Quiet,
151 Line_Text_Output,
152 Line_Output_File,
153 Line_Output_Size,
154 Line_Output_Date,
155 Line_Output_Double,
156 Line_Text_Dummy1,
157 Line_Text_Dummy2,
158 Line_Last,
159 Line_Max
160 };
161
162 CPPUNIT_ASSERT_EQUAL(Line_Max, usageLines.size());
163 CPPUNIT_ASSERT_EQUAL("Verbosity options", usageLines[Line_Text_Verbosity]);
164 CPPUNIT_ASSERT_EQUAL("", usageLines[Line_Text_Dummy1]);
165 CPPUNIT_ASSERT_EQUAL("Even more usage text", usageLines[Line_Text_Dummy2]);
166 CPPUNIT_ASSERT_EQUAL("", usageLines[Line_Last]);
167 }