X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a9e994615409e36d441135e4fa0761cf0398a2aa..bd42a07c7a528c607e7327dcd194b739fb242da2:/tests/regex/regextest.cpp diff --git a/tests/regex/regextest.cpp b/tests/regex/regextest.cpp index 261435e8ed..c31b639834 100644 --- a/tests/regex/regextest.cpp +++ b/tests/regex/regextest.cpp @@ -4,7 +4,7 @@ // Author: Mike Wetherell // RCS-ID: $Id$ // Copyright: (c) 2004 Mike Wetherell -// Licence: wxWidgets licence +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// // @@ -15,44 +15,51 @@ // // To run all the regex tests: // test regex -// +// // Some tests must be skipped since they use features which we do not make // available through wxRegEx. To see the list of tests that have been skipped // turn on verbose logging, e.g.: // test --verbose regex -// +// // The tests here are for the builtin library, tests for wxRegEx in general // 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'. -// +// // For compilers that support precompilation, includes "wx/wx.h". -#include "wx/wxprec.h" +#include "testprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif +#if wxUSE_REGEX + // for all others, include the necessary headers #ifndef WX_PRECOMP #include "wx/wx.h" #endif -#include "wx/regex.h" -#include "wx/cppunit.h" -#include - -using namespace std; -using namespace CppUnit; // many of the tests are specific to the builtin regex lib, so only attempts // to do them when using the builtin regex lib. // #ifdef wxHAS_REGEX_ADVANCED +#include "wx/regex.h" +#include +#include + +using CppUnit::Test; +using CppUnit::TestCase; +using CppUnit::TestSuite; +using CppUnit::Exception; + +using std::string; +using std::vector; /////////////////////////////////////////////////////////////////////////////// // The test case - an instance represents a single test @@ -80,7 +87,7 @@ private: void parseFlags(const wxString& flags); void doTest(int flavor); static wxString quote(const wxString& arg); - const wxChar *convError() const { return _T(""); } + const wxChar *convError() const { return wxT(""); } // assertions - adds some information about the test that failed void fail(const wxString& msg) const; @@ -127,20 +134,21 @@ RegExTestCase::RegExTestCase( m_advanced(false) { bool badconv = m_pattern == convError() || m_data == convError(); - vector::const_iterator it; + //RN: Removing the std:: here will break MSVC6 compilation + std::vector::const_iterator it; for (it = expected.begin(); it != expected.end(); ++it) { m_expected.push_back(Conv(*it)); badconv = badconv || *m_expected.rbegin() == convError(); } - failIf(badconv, _T("cannot convert to default character encoding")); - + failIf(badconv, wxT("cannot convert to default character encoding")); + // the flags need further parsing... parseFlags(m_flags); #ifndef wxHAS_REGEX_ADVANCED - failIf(!m_basic && !m_extended, _T("advanced regexs not available")); + failIf(!m_basic && !m_extended, wxT("advanced regexs not available")); #endif } @@ -148,11 +156,11 @@ 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); + + return memcmp(s1, s2, nLen1*sizeof(wchar_t)); } // convert a string from UTF8 to the internal encoding @@ -164,16 +172,17 @@ wxString RegExTestCase::Conv(const char *str) if (!buf || wxWcscmp(wxConvCurrent->cWX2WC(buf), wstr) != 0) return convError(); - else - return buf; + + return buf; } // Parse flags // void RegExTestCase::parseFlags(const wxString& flags) { - for (const wxChar *p = flags; *p; p++) { - switch (*p) { + for ( wxString::const_iterator p = flags.begin(); p != flags.end(); ++p ) + { + switch ( (*p).GetValue() ) { // noop case '-': break; @@ -203,7 +212,7 @@ void RegExTestCase::parseFlags(const wxString& flags) // anything else we must skip the test default: fail(wxString::Format( - _T("requires unsupported flag '%c'"), *p)); + wxT("requires unsupported flag '%c'"), *p)); } } } @@ -221,7 +230,7 @@ void RegExTestCase::runTest() doTest(wxRE_ADVANCED); #endif } - + // Try the test for a single flavour of expression // void RegExTestCase::doTest(int flavor) @@ -230,54 +239,63 @@ void RegExTestCase::doTest(int flavor) // 'e' - test that the pattern fails to compile if (m_mode == 'e') { - failIf(re.IsValid(), _T("compile succeeded (should fail)")); + failIf(re.IsValid(), wxT("compile succeeded (should fail)")); return; } - failIf(!re.IsValid(), _T("compile failed")); + failIf(!re.IsValid(), wxT("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 succeeded (should fail)")); + failIf(matches, wxT("match succeeded (should fail)")); return; } // otherwise 'm' or 'i' - test the pattern does match - failIf(!matches, _T("match failed")); + failIf(!matches, wxT("match failed")); 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; + wxString msg; + msg << wxT("GetMatchCount() == ") << re.GetMatchCount() + << wxT(", expected ") << m_expected.size(); + failIf(m_expected.size() != re.GetMatchCount(), msg); for (size_t i = 0; i < m_expected.size(); i++) { - failIf(!re.GetMatch(&start, &len, i), wxString::Format( - _T("wxRegEx::GetMatch failed for match %d"), i)); + wxString result; + size_t start, len; + + msg.clear(); + msg << wxT("wxRegEx::GetMatch failed for match ") << i; + failIf(!re.GetMatch(&start, &len, i), msg); // m - check the match returns the strings given if (m_mode == 'm') + { if (start < INT_MAX) result = m_data.substr(start, len); else - result = _T(""); + result = wxT(""); + } // i - check the match returns the offsets given else if (m_mode == 'i') - if (start < INT_MAX) - result = wxString::Format(_T("%d %d"), start, start + len - 1); + { + if (start > INT_MAX) + result = wxT("-1 -1"); + else if (start + len > 0) + result << start << wxT(" ") << start + len - 1; else - result = _T("-1 -1"); + result << start << wxT(" -1"); + } - failIf(result != m_expected[i], wxString::Format( - _T("match(%d) == %s, expected == %s"), i, - quote(result).c_str(), quote(m_expected[i]).c_str())); + msg.clear(); + msg << wxT("match(") << i << wxT(") == ") << quote(result) + << wxT(", expected == ") << quote(m_expected[i]); + failIf(result != m_expected[i], msg); } } @@ -288,16 +306,16 @@ void RegExTestCase::fail(const wxString& msg) const wxString str; wxArrayString::const_iterator it; - str << (wxChar)m_mode << _T(" ") << m_id << _T(" ") << m_flags << _T(" ") - << quote(m_pattern) << _T(" ") << quote(m_data); + str << (wxChar)m_mode << wxT(" ") << m_id << wxT(" ") << m_flags << wxT(" ") + << quote(m_pattern) << wxT(" ") << quote(m_data); for (it = m_expected.begin(); it != m_expected.end(); ++it) - str << _T(" ") << quote(*it); - + str << wxT(" ") << quote(*it); + if (str.length() > 77) - str = str.substr(0, 74) + _T("..."); + str = str.substr(0, 74) + wxT("..."); - str << _T("\n ") << msg; + str << wxT("\n ") << msg; // no lossy convs so using utf8 CPPUNIT_FAIL(string(str.mb_str(wxConvUTF8))); @@ -307,24 +325,24 @@ void RegExTestCase::fail(const wxString& msg) const // wxString RegExTestCase::quote(const wxString& arg) { - const wxChar *needEscape = _T("\a\b\t\n\v\f\r\"\\"); - const wxChar *escapes = _T("abtnvfr\"\\"); + const wxChar *needEscape = wxT("\a\b\t\n\v\f\r\"\\"); + const wxChar *escapes = wxT("abtnvfr\"\\"); wxString str; for (size_t i = 0; i < arg.length(); i++) { - wxUChar ch = arg[i]; + wxChar ch = (wxChar)arg[i]; const wxChar *p = wxStrchr(needEscape, ch); - + if (p) - str += wxString::Format(_T("\\%c"), escapes[p - needEscape]); + str += wxString::Format(wxT("\\%c"), escapes[p - needEscape]); else if (wxIscntrl(ch)) - str += wxString::Format(_T("\\%03o"), ch); + str += wxString::Format(wxT("\\%03o"), ch); else - str += ch; + str += (wxChar)ch; } return str.length() == arg.length() && str.find(' ') == wxString::npos ? - str : _T("\"") + str + _T("\""); + str : wxT("\"") + str + wxT("\""); } @@ -358,13 +376,13 @@ void RegExTestSuite::add( expected_results.push_back(expected); va_end(ap); - + try { addTest(new RegExTestCase( name, mode, id, flags, pattern, data, expected_results)); } catch (Exception& e) { - wxLogInfo(wxString::Format(_T("skipping: %s\n %s\n"), + wxLogInfo(wxString::Format(wxT("skipping: %s\n %s\n"), wxString(name.c_str(), wxConvUTF8).c_str(), wxString(e.what(), wxConvUTF8).c_str())); } @@ -377,3 +395,5 @@ void RegExTestSuite::add( #endif // wxHAS_REGEX_ADVANCED + +#endif // wxUSE_REGEX