// Use this macro to compare a size_t with a literal integer
#define WX_ASSERT_SIZET_EQUAL(n, m) CPPUNIT_ASSERT_EQUAL(((size_t)n), m)
+// Use this macro to compare a wxArrayString with the pipe-separated elements
+// of the given string
+//
+// NB: it's a macro and not a function to have the correct line numbers in the
+// test failure messages
+#define WX_ASSERT_STRARRAY_EQUAL(s, a) \
+ { \
+ wxArrayString expected(wxSplit(s, '|', '\0')); \
+ \
+ CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \
+ \
+ for ( size_t n = 0; n < a.size(); n++ ) \
+ { \
+ CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \
+ } \
+ }
// Use this macro to assert with the given formatted message (it should contain
// the format string and arguments in a separate pair of parentheses)
bool isInsideQuotes = false;
+ const wxString::const_iterator end = cmdline.end();
wxString::const_iterator p = cmdline.begin();
for ( ;; )
{
// skip white space
- while ( p != cmdline.end() && (*p == _T(' ') || *p == _T('\t')) )
+ while ( p != end && (*p == ' ' || *p == '\t') )
++p;
// anything left?
- if ( p == cmdline.end() )
+ if ( p == end )
break;
// parse this parameter
- bool endParam = false;
bool lastBS = false;
- for ( arg.clear(); !endParam; p++ )
+ for ( arg.clear(); p != end; ++p )
{
- switch ( (*p).GetValue() )
+ const wxChar ch = *p;
+ if ( ch == '"' )
{
- case _T('"'):
- if ( !lastBS )
- {
- isInsideQuotes = !isInsideQuotes;
-
- // don't put quote in arg
- continue;
- }
- //else: quote has no special meaning but the backslash
- // still remains -- makes no sense but this is what
- // Windows does
- break;
-
- case _T(' '):
- case _T('\t'):
- // backslash does *not* quote the space, only quotes do
- if ( isInsideQuotes )
- {
- // skip assignment below
- break;
- }
- // fall through
-
- case _T('\0'):
- endParam = true;
+ if ( !lastBS )
+ {
+ isInsideQuotes = !isInsideQuotes;
- break;
+ // don't put quote in arg
+ continue;
+ }
+ //else: quote has no special meaning but the backslash
+ // still remains -- makes no sense but this is what
+ // Windows does
}
-
- if ( endParam )
+ // note that backslash does *not* quote the space, only quotes do
+ else if ( !isInsideQuotes && (ch == ' ' || ch == '\t') )
{
+ ++p; // skip this space anyhow
break;
}
- lastBS = *p == _T('\\');
+ lastBS = ch == '\\';
- arg += *p;
+ arg += ch;
}
args.push_back(arg);
test_tartest.o \
test_arrays.o \
test_base64.o \
+ test_cmdlinetest.o \
test_fileconf.o \
test_datetimetest.o \
test_filekind.o \
test_base64.o: $(srcdir)/base64/base64.cpp $(TEST_ODEP)
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/base64/base64.cpp
+test_cmdlinetest.o: $(srcdir)/cmdline/cmdlinetest.cpp $(TEST_ODEP)
+ $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/cmdline/cmdlinetest.cpp
+
test_fileconf.o: $(srcdir)/config/fileconf.cpp $(TEST_ODEP)
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/config/fileconf.cpp
--- /dev/null
+///////////////////////////////////////////////////////////////////////////////
+// Name: tests/cmdline/cmdlinetest.cpp
+// Purpose: wxCmdLineParser unit test
+// Author: Vadim Zeitlin
+// Created: 2008-04-12
+// RCS-ID: $Id$
+// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
+///////////////////////////////////////////////////////////////////////////////
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+#include "testprec.h"
+
+#ifdef __BORLANDC__
+ #pragma hdrstop
+#endif
+
+#ifndef WX_PRECOMP
+#endif // WX_PRECOMP
+
+#include "wx/cmdline.h"
+
+// --------------------------------------------------------------------------
+// test class
+// --------------------------------------------------------------------------
+
+class CmdLineTestCase : public CppUnit::TestCase
+{
+public:
+ CmdLineTestCase() {}
+
+private:
+ CPPUNIT_TEST_SUITE( CmdLineTestCase );
+ CPPUNIT_TEST( ConvertStringTestCase );
+ CPPUNIT_TEST_SUITE_END();
+
+ void ConvertStringTestCase();
+
+ DECLARE_NO_COPY_CLASS(CmdLineTestCase)
+};
+
+// 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
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CmdLineTestCase, "CmdLineTestCase" );
+
+// ============================================================================
+// implementation
+// ============================================================================
+
+void CmdLineTestCase::ConvertStringTestCase()
+{
+ #define WX_ASSERT_ARGS_EQUAL(s, args) \
+ { \
+ const wxArrayString a(wxCmdLineParser::ConvertStringToArgs(args));\
+ WX_ASSERT_STRARRAY_EQUAL(s, a); \
+ }
+
+ // normal cases
+ WX_ASSERT_ARGS_EQUAL( "foo", "foo" )
+ WX_ASSERT_ARGS_EQUAL( "foo bar", "\"foo bar\"" )
+ WX_ASSERT_ARGS_EQUAL( "foo|bar", "foo bar" )
+ WX_ASSERT_ARGS_EQUAL( "foo|bar|baz", "foo bar baz" )
+ WX_ASSERT_ARGS_EQUAL( "foo|bar baz", "foo \"bar baz\"" )
+
+ // special cases
+ WX_ASSERT_ARGS_EQUAL( "", "" )
+ WX_ASSERT_ARGS_EQUAL( "foo", "foo " )
+ 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 \\" )
+
+ // 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\"" );
+
+ #undef WX_ASSERT_ARGS_EQUAL
+}
$(OBJS)\test_tartest.obj \
$(OBJS)\test_arrays.obj \
$(OBJS)\test_base64.obj \
+ $(OBJS)\test_cmdlinetest.obj \
$(OBJS)\test_fileconf.obj \
$(OBJS)\test_datetimetest.obj \
$(OBJS)\test_filekind.obj \
$(OBJS)\test_base64.obj: .\base64\base64.cpp
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\base64\base64.cpp
+$(OBJS)\test_cmdlinetest.obj: .\cmdline\cmdlinetest.cpp
+ $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\cmdline\cmdlinetest.cpp
+
$(OBJS)\test_fileconf.obj: .\config\fileconf.cpp
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\config\fileconf.cpp
$(OBJS)\test_tartest.o \
$(OBJS)\test_arrays.o \
$(OBJS)\test_base64.o \
+ $(OBJS)\test_cmdlinetest.o \
$(OBJS)\test_fileconf.o \
$(OBJS)\test_datetimetest.o \
$(OBJS)\test_filekind.o \
$(OBJS)\test_base64.o: ./base64/base64.cpp
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
+$(OBJS)\test_cmdlinetest.o: ./cmdline/cmdlinetest.cpp
+ $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
+
$(OBJS)\test_fileconf.o: ./config/fileconf.cpp
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\test_tartest.obj \
$(OBJS)\test_arrays.obj \
$(OBJS)\test_base64.obj \
+ $(OBJS)\test_cmdlinetest.obj \
$(OBJS)\test_fileconf.obj \
$(OBJS)\test_datetimetest.obj \
$(OBJS)\test_filekind.obj \
$(OBJS)\test_base64.obj: .\base64\base64.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\base64\base64.cpp
+$(OBJS)\test_cmdlinetest.obj: .\cmdline\cmdlinetest.cpp
+ $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\cmdline\cmdlinetest.cpp
+
$(OBJS)\test_fileconf.obj: .\config\fileconf.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\config\fileconf.cpp
$(OBJS)\test_tartest.obj &
$(OBJS)\test_arrays.obj &
$(OBJS)\test_base64.obj &
+ $(OBJS)\test_cmdlinetest.obj &
$(OBJS)\test_fileconf.obj &
$(OBJS)\test_datetimetest.obj &
$(OBJS)\test_filekind.obj &
$(OBJS)\test_base64.obj : .AUTODEPEND .\base64\base64.cpp
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
+$(OBJS)\test_cmdlinetest.obj : .AUTODEPEND .\cmdline\cmdlinetest.cpp
+ $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
+
$(OBJS)\test_fileconf.obj : .AUTODEPEND .\config\fileconf.cpp
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
archive/tartest.cpp
arrays/arrays.cpp
base64/base64.cpp
+ cmdline/cmdlinetest.cpp
config/fileconf.cpp
datetime/datetimetest.cpp
filekind/filekind.cpp
# End Source File\r
# Begin Source File\r
\r
+SOURCE=.\cmdline\cmdlinetest.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
SOURCE=.\mbconv\convautotest.cpp\r
# End Source File\r
# Begin Source File\r
RelativePath=".\base64\base64.cpp"/>\r
<File\r
RelativePath=".\streams\bstream.cpp"/>\r
+ <File\r
+ RelativePath=".\cmdline\cmdlinetest.cpp"/>\r
<File\r
RelativePath=".\mbconv\convautotest.cpp"/>\r
<File\r
<File\r
RelativePath=".\streams\bstream.cpp"\r
/>\r
+ <File\r
+ RelativePath=".\cmdline\cmdlinetest.cpp"\r
+ />\r
<File\r
RelativePath=".\mbconv\convautotest.cpp"\r
/>\r