]>
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" | |
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 ); | |
e559d790 | 37 | CPPUNIT_TEST( Usage ); |
5769cf0f VZ |
38 | CPPUNIT_TEST_SUITE_END(); |
39 | ||
40 | void ConvertStringTestCase(); | |
e559d790 | 41 | void Usage(); |
5769cf0f VZ |
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_ARGS_EQUAL(s, args) \ | |
59 | { \ | |
60 | const wxArrayString a(wxCmdLineParser::ConvertStringToArgs(args));\ | |
61 | WX_ASSERT_STRARRAY_EQUAL(s, a); \ | |
62 | } | |
63 | ||
64 | // normal cases | |
65 | WX_ASSERT_ARGS_EQUAL( "foo", "foo" ) | |
66 | WX_ASSERT_ARGS_EQUAL( "foo bar", "\"foo bar\"" ) | |
67 | WX_ASSERT_ARGS_EQUAL( "foo|bar", "foo bar" ) | |
68 | WX_ASSERT_ARGS_EQUAL( "foo|bar|baz", "foo bar baz" ) | |
69 | WX_ASSERT_ARGS_EQUAL( "foo|bar baz", "foo \"bar baz\"" ) | |
70 | ||
71 | // special cases | |
72 | WX_ASSERT_ARGS_EQUAL( "", "" ) | |
73 | WX_ASSERT_ARGS_EQUAL( "foo", "foo " ) | |
74 | WX_ASSERT_ARGS_EQUAL( "foo", "foo \t " ) | |
75 | WX_ASSERT_ARGS_EQUAL( "foo|bar", "foo bar " ) | |
76 | WX_ASSERT_ARGS_EQUAL( "foo|bar|", "foo bar \"" ) | |
77 | WX_ASSERT_ARGS_EQUAL( "foo|bar|\\", "foo bar \\" ) | |
78 | ||
79 | // check for (broken) Windows semantics: backslash doesn't escape spaces | |
80 | WX_ASSERT_ARGS_EQUAL( "foo|bar\\|baz", "foo bar\\ baz" ); | |
81 | WX_ASSERT_ARGS_EQUAL( "foo|bar\\\"baz", "foo \"bar\\\"baz\"" ); | |
82 | ||
83 | #undef WX_ASSERT_ARGS_EQUAL | |
84 | } | |
e559d790 VZ |
85 | |
86 | void CmdLineTestCase::Usage() | |
87 | { | |
88 | // check that Usage() returns roughly what we expect (don't check all the | |
89 | // details, its format can change in the future) | |
90 | static const wxCmdLineEntryDesc desc[] = | |
91 | { | |
92 | { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "Verbosity options" }, | |
93 | { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" }, | |
94 | { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" }, | |
95 | ||
96 | { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "Output options" }, | |
97 | { wxCMD_LINE_OPTION, "o", "output", "output file" }, | |
98 | { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER }, | |
99 | { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE }, | |
100 | { wxCMD_LINE_OPTION, "f", "double", "output double", wxCMD_LINE_VAL_DOUBLE }, | |
101 | ||
102 | { wxCMD_LINE_PARAM, NULL, NULL, "input file", }, | |
103 | ||
104 | { wxCMD_LINE_USAGE_TEXT, NULL, NULL, "\nEven more usage text" }, | |
105 | { wxCMD_LINE_NONE } | |
106 | }; | |
107 | ||
108 | wxCmdLineParser p(desc); | |
109 | const wxArrayString usageLines = wxSplit(p.GetUsageString(), '\n'); | |
110 | ||
111 | enum | |
112 | { | |
113 | Line_Synopsis, | |
114 | Line_Text_Verbosity, | |
115 | Line_Verbose, | |
116 | Line_Quiet, | |
117 | Line_Text_Output, | |
118 | Line_Output_File, | |
119 | Line_Output_Size, | |
120 | Line_Output_Date, | |
121 | Line_Output_Double, | |
122 | Line_Text_Dummy1, | |
123 | Line_Text_Dummy2, | |
124 | Line_Last, | |
125 | Line_Max | |
126 | }; | |
127 | ||
128 | WX_ASSERT_SIZET_EQUAL( Line_Max, usageLines.size() ); | |
129 | WX_ASSERT_STR_EQUAL("Verbosity options", usageLines[Line_Text_Verbosity]); | |
130 | WX_ASSERT_STR_EQUAL("", usageLines[Line_Text_Dummy1]); | |
131 | WX_ASSERT_STR_EQUAL("Even more usage text", usageLines[Line_Text_Dummy2]); | |
132 | WX_ASSERT_STR_EQUAL("", usageLines[Line_Last]); | |
133 | } |