]>
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_ARGS_EQUAL(s, args) \
60 const wxArrayString a(wxCmdLineParser::ConvertStringToArgs(args));\
61 WX_ASSERT_STRARRAY_EQUAL(s, a); \
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\"" )
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 \\" )
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\"" );
83 #undef WX_ASSERT_ARGS_EQUAL
86 void CmdLineTestCase::Usage()
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
[] =
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" },
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
},
102 { wxCMD_LINE_PARAM
, NULL
, NULL
, "input file", },
104 { wxCMD_LINE_USAGE_TEXT
, NULL
, NULL
, "\nEven more usage text" },
108 wxCmdLineParser
p(desc
);
109 const wxArrayString usageLines
= wxSplit(p
.GetUsageString(), '\n');
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
]);