// test --verbose regex
//
// The tests here are for the builtin library, tests for wxRegEx in general
-// should go in another module.
+// should go in wxregex.cpp
//
// The tests are generated from Henry Spencer's reg.test, additional test
// can be added in wxreg.test. These test files are then turned into a C++
// include file 'regex.inc' (included below) using a script 'regex.pl'.
//
-#if defined(__GNUG__) && !defined(__APPLE__)
- #pragma implementation
- #pragma interface
-#endif
-
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#include "wx/regex.h"
#include "wx/cppunit.h"
-#include <iomanip>
#include <stdexcept>
using namespace std;
wxString Conv(const char *str);
void parseFlags(const wxString& flags);
void doTest(int flavor);
- static size_t matchCount(const wxString& expr, int flags);
static wxString quote(const wxString& arg);
const wxChar *convError() const { return _T("<cannot convert>"); }
#endif
}
+int wxWcscmp(const wchar_t* s1, const wchar_t* s2)
+{
+ size_t nLen1 = wxWcslen(s1);
+ size_t nLen2 = wxWcslen(s2);
+
+ if (nLen1 != nLen2)
+ return nLen1 - nLen2;
+
+ return wxMemcmp(s1, s2, nLen1);
+}
+
// convert a string from UTF8 to the internal encoding
//
wxString RegExTestCase::Conv(const char *str)
const wxWCharBuffer wstr = wxConvUTF8.cMB2WC(str);
const wxWC2WXbuf buf = wxConvCurrent->cWC2WX(wstr);
- if (!buf || wcscmp(wxConvCurrent->cWX2WC(buf), wstr) != 0)
+ if (!buf || wxWcscmp(wxConvCurrent->cWX2WC(buf), wstr) != 0)
return convError();
else
return buf;
wxRegEx re(m_pattern, m_compileFlags | flavor);
// 'e' - test that the pattern fails to compile
- if (m_mode == 'e')
- {
- failIf(re.IsValid(), _T("compile suceeded (should fail)"));
+ if (m_mode == 'e') {
+ failIf(re.IsValid(), _T("compile succeeded (should fail)"));
return;
}
failIf(!re.IsValid(), _T("compile failed"));
bool matches = re.Matches(m_data, m_matchFlags);
// 'f' or 'p' - test that the pattern does not match
- if (m_mode == 'f' || m_mode == 'p')
- {
- failIf(matches, _T("match suceeded (should fail)"));
+ if (m_mode == 'f' || m_mode == 'p') {
+ failIf(matches, _T("match succeeded (should fail)"));
return;
}
// otherwise 'm' or 'i' - test the pattern does match
failIf(!matches, _T("match failed"));
- // Check that wxRegEx is going to allocate a large enough array for the
- // results we are supposed to get
- failIf(m_expected.size() > matchCount(m_pattern, m_compileFlags | flavor),
- _T("wxRegEx has not allocated a large enough array for the ")
- _T("number of results expected"));
+ if (m_compileFlags & wxRE_NOSUB)
+ return;
+
+ // check wxRegEx has correctly counted the number of subexpressions
+ failIf(m_expected.size() != re.GetMatchCount(),
+ wxString::Format(_T("GetMatchCount() == %d, expected %d"),
+ re.GetMatchCount(), m_expected.size()));
wxString result;
size_t start, len;
str : _T("\"") + str + _T("\"");
}
-// Count the number of subexpressions (taken from wxRegExImpl::Compile)
-//
-size_t RegExTestCase::matchCount(const wxString& expr, int flags)
-{
- // there is always one for the whole expression
- size_t nMatches = 1;
-
- // and some more for bracketed subexperessions
- for ( const wxChar *cptr = expr; *cptr; cptr++ )
- {
- if ( *cptr == _T('\\') )
- {
- // in basic RE syntax groups are inside \(...\)
- if ( *++cptr == _T('(') && (flags & wxRE_BASIC) )
- {
- nMatches++;
- }
- }
- else if ( *cptr == _T('(') && !(flags & wxRE_BASIC) )
- {
- // we know that the previous character is not an unquoted
- // backslash because it would have been eaten above, so we
- // have a bar '(' and this indicates a group start for the
- // extended syntax
- nMatches++;
- }
- }
-
- return nMatches;
-}
-
///////////////////////////////////////////////////////////////////////////////
// Test suite
-//
-// In a non-unicode build the regex is affected by the current locale, so
-// this derived TestSuite is used. It sets the locale in it's run() method
-// for the duration of the regex tests.
class RegExTestSuite : public TestSuite
{
public:
- RegExTestSuite(string name);
- void run(TestResult *result);
+ RegExTestSuite(string name) : TestSuite(name) { }
void add(const char *mode, const char *id, const char *flags,
const char *pattern, const char *data, const char *expected, ...);
};
-// constructor, sets the locale so that it is set when the tests are added
-//
-RegExTestSuite::RegExTestSuite(string name) : TestSuite(name)
-{
- setlocale(LC_ALL, "");
-}
-
-// run the test suite, sets the locale again since it may have been changed
-// by another test since this suite was crated
-//
-void RegExTestSuite::run(TestResult *result)
-{
- setlocale(LC_ALL, "");
- TestSuite::run(result);
-}
-
// Add a testcase to the suite
//
void RegExTestSuite::add(