]>
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"
25 // --------------------------------------------------------------------------
27 // --------------------------------------------------------------------------
29 class CmdLineTestCase
: public CppUnit::TestCase
35 CPPUNIT_TEST_SUITE( CmdLineTestCase
);
36 CPPUNIT_TEST( ConvertStringTestCase
);
37 CPPUNIT_TEST( Usage
);
38 CPPUNIT_TEST_SUITE_END();
40 void ConvertStringTestCase();
43 DECLARE_NO_COPY_CLASS(CmdLineTestCase
)
46 // register in the unnamed registry so that these tests are run by default
47 CPPUNIT_TEST_SUITE_REGISTRATION( CmdLineTestCase
);
49 // also include in it's own registry so that these tests can be run alone
50 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase
, "CmdLineTestCase" );
52 // ============================================================================
54 // ============================================================================
56 void CmdLineTestCase::ConvertStringTestCase()
58 #define WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
61 argsDOS(wxCmdLineParser::ConvertStringToArgs(args, \
62 wxCMD_LINE_SPLIT_DOS)); \
63 WX_ASSERT_STRARRAY_EQUAL(s, argsDOS); \
66 #define WX_ASSERT_UNIX_ARGS_EQUAL(s, args) \
69 argsUnix(wxCmdLineParser::ConvertStringToArgs(args, \
70 wxCMD_LINE_SPLIT_UNIX)); \
71 WX_ASSERT_STRARRAY_EQUAL(s, argsUnix); \
74 #define WX_ASSERT_ARGS_EQUAL(s, args) \
75 WX_ASSERT_DOS_ARGS_EQUAL(s, args) \
76 WX_ASSERT_UNIX_ARGS_EQUAL(s, args)
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\"" )
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 \\" )
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" );
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\"" );
103 // check for more sane Unix semantics: backslash does escape spaces and
105 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo bar\\ baz" );
106 WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar\"baz", "foo \"bar\\\"baz\"" );
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" )
115 #undef WX_ASSERT_DOS_ARGS_EQUAL
116 #undef WX_ASSERT_UNIX_ARGS_EQUAL
117 #undef WX_ASSERT_ARGS_EQUAL
120 void CmdLineTestCase::Usage()
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
[] =
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" },
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
},
136 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file", },
138 { wxCMD_LINE_USAGE_TEXT
, NULL
, NULL
, "\nEven more usage text" },
142 wxCmdLineParser
p(desc
);
143 const wxArrayString usageLines
= wxSplit(p
.GetUsageString(), '\n');
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
]);