]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/cmdline/cmdlinetest.cpp
update version check for glib 2.32
[wxWidgets.git] / tests / cmdline / cmdlinetest.cpp
index 388166ffaf4377fc2a8346f71e8c7d6ca0887735..82f6548e65e61e36a5b2044fc6b0dddacba89c38 100644 (file)
@@ -21,6 +21,8 @@
 #endif // WX_PRECOMP
 
 #include "wx/cmdline.h"
+#include "wx/msgout.h"
+#include "wx/scopeguard.h"
 
 // --------------------------------------------------------------------------
 // test class
@@ -34,10 +36,12 @@ public:
 private:
     CPPUNIT_TEST_SUITE( CmdLineTestCase );
         CPPUNIT_TEST( ConvertStringTestCase );
+        CPPUNIT_TEST( ParseSwitches );
         CPPUNIT_TEST( Usage );
     CPPUNIT_TEST_SUITE_END();
 
     void ConvertStringTestCase();
+    void ParseSwitches();
     void Usage();
 
     DECLARE_NO_COPY_CLASS(CmdLineTestCase)
@@ -46,7 +50,7 @@ private:
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( CmdLineTestCase );
 
-// also include in it's own registry so that these tests can be run alone
+// also include in its own registry so that these tests can be run alone
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase, "CmdLineTestCase" );
 
 // ============================================================================
@@ -55,12 +59,26 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase, "CmdLineTestCase" );
 
 void CmdLineTestCase::ConvertStringTestCase()
 {
-    #define WX_ASSERT_ARGS_EQUAL(s, args)                                     \
+    #define WX_ASSERT_DOS_ARGS_EQUAL(s, args)                                 \
+        {                                                                     \
+            const wxArrayString                                               \
+                argsDOS(wxCmdLineParser::ConvertStringToArgs(args,            \
+                                            wxCMD_LINE_SPLIT_DOS));           \
+            WX_ASSERT_STRARRAY_EQUAL(s, argsDOS);                             \
+        }
+
+    #define WX_ASSERT_UNIX_ARGS_EQUAL(s, args)                                \
         {                                                                     \
-            const wxArrayString a(wxCmdLineParser::ConvertStringToArgs(args));\
-            WX_ASSERT_STRARRAY_EQUAL(s, a);                                   \
+            const wxArrayString                                               \
+                argsUnix(wxCmdLineParser::ConvertStringToArgs(args,           \
+                                            wxCMD_LINE_SPLIT_UNIX));          \
+            WX_ASSERT_STRARRAY_EQUAL(s, argsUnix);                            \
         }
 
+    #define WX_ASSERT_ARGS_EQUAL(s, args)                                     \
+        WX_ASSERT_DOS_ARGS_EQUAL(s, args)                                     \
+        WX_ASSERT_UNIX_ARGS_EQUAL(s, args)
+
     // normal cases
     WX_ASSERT_ARGS_EQUAL( "foo", "foo" )
     WX_ASSERT_ARGS_EQUAL( "foo bar", "\"foo bar\"" )
@@ -74,15 +92,117 @@ void CmdLineTestCase::ConvertStringTestCase()
     WX_ASSERT_ARGS_EQUAL( "foo", "foo \t   " )
     WX_ASSERT_ARGS_EQUAL( "foo|bar", "foo bar " )
     WX_ASSERT_ARGS_EQUAL( "foo|bar|", "foo bar \"" )
-    WX_ASSERT_ARGS_EQUAL( "foo|bar|\\", "foo bar \\" )
+    WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar|\\", "foo bar \\" )
+    WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar|", "foo bar \\" )
 
-    // check for (broken) Windows semantics: backslash doesn't escape spaces
-    WX_ASSERT_ARGS_EQUAL( "foo|bar\\|baz", "foo bar\\ baz" );
-    WX_ASSERT_ARGS_EQUAL( "foo|bar\\\"baz", "foo \"bar\\\"baz\"" );
+    WX_ASSERT_ARGS_EQUAL( "12 34", "1\"2 3\"4" );
+    WX_ASSERT_ARGS_EQUAL( "1|2 34", "1 \"2 3\"4" );
+    WX_ASSERT_ARGS_EQUAL( "1|2 3|4", "1 \"2 3\" 4" );
 
+    // check for (broken) Windows semantics: backslash doesn't escape spaces
+    WX_ASSERT_DOS_ARGS_EQUAL( "\\\\foo\\\\|/bar", "\"\\\\foo\\\\\" /bar" );
+    WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar\\|baz", "foo bar\\ baz" );
+    WX_ASSERT_DOS_ARGS_EQUAL( "foo|bar\\\"baz", "foo \"bar\\\"baz\"" );
+
+    // check for more sane Unix semantics: backslash does escape spaces and
+    // quotes
+    WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo bar\\ baz" );
+    WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar\"baz", "foo \"bar\\\"baz\"" );
+
+    // check that single quotes work too with Unix semantics
+    WX_ASSERT_UNIX_ARGS_EQUAL( "foo bar", "'foo bar'" )
+    WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo 'bar baz'" )
+    WX_ASSERT_UNIX_ARGS_EQUAL( "foo|bar baz", "foo 'bar baz'" )
+    WX_ASSERT_UNIX_ARGS_EQUAL( "O'Henry", "\"O'Henry\"" )
+    WX_ASSERT_UNIX_ARGS_EQUAL( "O'Henry", "O\\'Henry" )
+
+    #undef WX_ASSERT_DOS_ARGS_EQUAL
+    #undef WX_ASSERT_UNIX_ARGS_EQUAL
     #undef WX_ASSERT_ARGS_EQUAL
 }
 
+void CmdLineTestCase::ParseSwitches()
+{
+    // install a dummy message output object just suppress error messages from
+    // wxCmdLineParser::Parse()
+    class NoMessageOutput : public wxMessageOutput
+    {
+    public:
+        virtual void Output(const wxString& WXUNUSED(str)) { }
+    } noMessages;
+
+    wxMessageOutput * const old = wxMessageOutput::Set(&noMessages);
+    wxON_BLOCK_EXIT1( wxMessageOutput::Set, old );
+
+    wxCmdLineParser p;
+    p.AddSwitch("a");
+    p.AddSwitch("b");
+    p.AddSwitch("c");
+    p.AddSwitch("d");
+    p.AddSwitch("n", "neg", "Switch that can be negated",
+                wxCMD_LINE_SWITCH_NEGATABLE);
+
+    p.SetCmdLine("");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT( !p.Found("a") );
+
+    p.SetCmdLine("-z");
+    CPPUNIT_ASSERT( p.Parse(false) != 0 );
+
+    p.SetCmdLine("-a");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT( p.Found("a") );
+    CPPUNIT_ASSERT( !p.Found("b") );
+
+    p.SetCmdLine("-a -d");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT( p.Found("a") );
+    CPPUNIT_ASSERT( !p.Found("b") );
+    CPPUNIT_ASSERT( !p.Found("c") );
+    CPPUNIT_ASSERT( p.Found("d") );
+
+    p.SetCmdLine("-abd");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT( p.Found("a") );
+    CPPUNIT_ASSERT( p.Found("b") );
+    CPPUNIT_ASSERT( !p.Found("c") );
+    CPPUNIT_ASSERT( p.Found("d") );
+
+    p.SetCmdLine("-abdz");
+    CPPUNIT_ASSERT( p.Parse(false) != 0 );
+
+    p.SetCmdLine("-ab -cd");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT( p.Found("a") );
+    CPPUNIT_ASSERT( p.Found("b") );
+    CPPUNIT_ASSERT( p.Found("c") );
+    CPPUNIT_ASSERT( p.Found("d") );
+
+    p.SetCmdLine("-da");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT( p.Found("a") );
+    CPPUNIT_ASSERT( !p.Found("b") );
+    CPPUNIT_ASSERT( !p.Found("c") );
+    CPPUNIT_ASSERT( p.Found("d") );
+
+    p.SetCmdLine("-n");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_NOT_FOUND, p.FoundSwitch("a") );
+    CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_ON, p.FoundSwitch("n") );
+
+    p.SetCmdLine("-n-");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_OFF, p.FoundSwitch("neg") );
+
+    p.SetCmdLine("--neg");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_ON, p.FoundSwitch("neg") );
+
+    p.SetCmdLine("--neg-");
+    CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) );
+    CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_OFF, p.FoundSwitch("n") );
+}
+
 void CmdLineTestCase::Usage()
 {
     // check that Usage() returns roughly what we expect (don't check all the
@@ -125,9 +245,9 @@ void CmdLineTestCase::Usage()
         Line_Max
     };
 
-    WX_ASSERT_SIZET_EQUAL( Line_Max, usageLines.size() );
-    WX_ASSERT_STR_EQUAL("Verbosity options", usageLines[Line_Text_Verbosity]);
-    WX_ASSERT_STR_EQUAL("", usageLines[Line_Text_Dummy1]);
-    WX_ASSERT_STR_EQUAL("Even more usage text", usageLines[Line_Text_Dummy2]);
-    WX_ASSERT_STR_EQUAL("", usageLines[Line_Last]);
+    CPPUNIT_ASSERT_EQUAL(Line_Max, usageLines.size());
+    CPPUNIT_ASSERT_EQUAL("Verbosity options", usageLines[Line_Text_Verbosity]);
+    CPPUNIT_ASSERT_EQUAL("", usageLines[Line_Text_Dummy1]);
+    CPPUNIT_ASSERT_EQUAL("Even more usage text", usageLines[Line_Text_Dummy2]);
+    CPPUNIT_ASSERT_EQUAL("", usageLines[Line_Last]);
 }