]>
git.saurik.com Git - wxWidgets.git/blob - tests/regex/regextest.cpp
bd0895085990dcc2118f9e01b16d3997eb4a6b89
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/regex/regex.cpp
3 // Purpose: Test the built-in regex lib and wxRegEx
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
13 // To run just one section, say wx_1, do this:
16 // To run all the regex tests:
19 // Some tests must be skipped since they use features which we do not make
20 // available through wxRegEx. To see the list of tests that have been skipped
21 // turn on verbose logging, e.g.:
22 // test --verbose regex
24 // The tests here are for the builtin library, tests for wxRegEx in general
25 // should go in wxregex.cpp
27 // The tests are generated from Henry Spencer's reg.test, additional test
28 // can be added in wxreg.test. These test files are then turned into a C++
29 // include file 'regex.inc' (included below) using a script 'regex.pl'.
32 // For compilers that support precompilation, includes "wx/wx.h".
39 // for all others, include the necessary headers
45 // many of the tests are specific to the builtin regex lib, so only attempts
46 // to do them when using the builtin regex lib.
48 #ifdef wxHAS_REGEX_ADVANCED
55 using CppUnit::TestCase
;
56 using CppUnit::TestSuite
;
57 using CppUnit::Exception
;
62 ///////////////////////////////////////////////////////////////////////////////
63 // The test case - an instance represents a single test
65 class RegExTestCase
: public TestCase
68 // constructor - create a single testcase
76 const vector
<const char *>& expected
);
84 wxString
Conv(const char *str
);
85 void parseFlags(const wxString
& flags
);
86 void doTest(int flavor
);
87 static wxString
quote(const wxString
& arg
);
88 const wxChar
*convError() const { return _T("<cannot convert>"); }
90 // assertions - adds some information about the test that failed
91 void fail(const wxString
& msg
) const;
92 void failIf(bool condition
, const wxString
& msg
) const
93 { if (condition
) fail(msg
); }
95 // mode, id, flags, pattern, test data, expected results...
101 wxArrayString m_expected
;
111 // constructor - throws Exception on failure
113 RegExTestCase::RegExTestCase(
120 const vector
<const char *>& expected
)
125 m_flags(Conv(flags
)),
126 m_pattern(Conv(pattern
)),
134 bool badconv
= m_pattern
== convError() || m_data
== convError();
135 //RN: Removing the std:: here will break MSVC6 compilation
136 std::vector
<const char *>::const_iterator it
;
138 for (it
= expected
.begin(); it
!= expected
.end(); ++it
) {
139 m_expected
.push_back(Conv(*it
));
140 badconv
= badconv
|| *m_expected
.rbegin() == convError();
143 failIf(badconv
, _T("cannot convert to default character encoding"));
145 // the flags need further parsing...
148 #ifndef wxHAS_REGEX_ADVANCED
149 failIf(!m_basic
&& !m_extended
, _T("advanced regexs not available"));
153 int wxWcscmp(const wchar_t* s1
, const wchar_t* s2
)
155 size_t nLen1
= wxWcslen(s1
);
156 size_t nLen2
= wxWcslen(s2
);
159 return nLen1
- nLen2
;
161 return memcmp(s1
, s2
, nLen1
*sizeof(wchar_t));
164 // convert a string from UTF8 to the internal encoding
166 wxString
RegExTestCase::Conv(const char *str
)
168 const wxWCharBuffer wstr
= wxConvUTF8
.cMB2WC(str
);
169 const wxWC2WXbuf buf
= wxConvCurrent
->cWC2WX(wstr
);
171 if (!buf
|| wxWcscmp(wxConvCurrent
->cWX2WC(buf
), wstr
) != 0)
179 void RegExTestCase::parseFlags(const wxString
& flags
)
181 for ( wxString::const_iterator p
= flags
.begin(); p
!= flags
.end(); ++p
)
183 switch ( (*p
).GetValue() ) {
187 // we don't fully support these flags, but they don't stop us
188 // checking for success of failure of the match, so treat as noop
189 case 'A': case 'B': case 'E': case 'H':
190 case 'I': case 'L': case 'M': case 'N':
191 case 'P': case 'Q': case 'R': case 'S':
192 case 'T': case 'U': case '%':
196 case '^': m_matchFlags
|= wxRE_NOTBOL
; break;
197 case '$': m_matchFlags
|= wxRE_NOTEOL
; break;
202 case '&': m_advanced
= m_basic
= true; break;
203 case 'b': m_basic
= true; break;
204 case 'e': m_extended
= true; break;
205 case 'i': m_compileFlags
|= wxRE_ICASE
; break;
206 case 'o': m_compileFlags
|= wxRE_NOSUB
; break;
207 case 'n': m_compileFlags
|= wxRE_NEWLINE
; break;
208 case 't': if (strchr("ep", m_mode
)) break; // else fall through...
210 // anything else we must skip the test
212 fail(wxString::Format(
213 _T("requires unsupported flag '%c'"), *p
));
218 // Try test for all flavours of expression specified
220 void RegExTestCase::runTest()
225 doTest(wxRE_EXTENDED
);
226 #ifdef wxHAS_REGEX_ADVANCED
227 if (m_advanced
|| (!m_basic
&& !m_extended
))
228 doTest(wxRE_ADVANCED
);
232 // Try the test for a single flavour of expression
234 void RegExTestCase::doTest(int flavor
)
236 wxRegEx
re(m_pattern
, m_compileFlags
| flavor
);
238 // 'e' - test that the pattern fails to compile
240 failIf(re
.IsValid(), _T("compile succeeded (should fail)"));
243 failIf(!re
.IsValid(), _T("compile failed"));
245 bool matches
= re
.Matches(m_data
, m_matchFlags
);
247 // 'f' or 'p' - test that the pattern does not match
248 if (m_mode
== 'f' || m_mode
== 'p') {
249 failIf(matches
, _T("match succeeded (should fail)"));
253 // otherwise 'm' or 'i' - test the pattern does match
254 failIf(!matches
, _T("match failed"));
256 if (m_compileFlags
& wxRE_NOSUB
)
259 // check wxRegEx has correctly counted the number of subexpressions
261 msg
<< _T("GetMatchCount() == ") << re
.GetMatchCount()
262 << _T(", expected ") << m_expected
.size();
263 failIf(m_expected
.size() != re
.GetMatchCount(), msg
);
265 for (size_t i
= 0; i
< m_expected
.size(); i
++) {
270 msg
<< _T("wxRegEx::GetMatch failed for match ") << i
;
271 failIf(!re
.GetMatch(&start
, &len
, i
), msg
);
273 // m - check the match returns the strings given
276 result
= m_data
.substr(start
, len
);
280 // i - check the match returns the offsets given
281 else if (m_mode
== 'i')
283 result
= _T("-1 -1");
284 else if (start
+ len
> 0)
285 result
<< start
<< _T(" ") << start
+ len
- 1;
287 result
<< start
<< _T(" -1");
290 msg
<< _T("match(") << i
<< _T(") == ") << quote(result
)
291 << _T(", expected == ") << quote(m_expected
[i
]);
292 failIf(result
!= m_expected
[i
], msg
);
296 // assertion - adds some information about the test that failed
298 void RegExTestCase::fail(const wxString
& msg
) const
301 wxArrayString::const_iterator it
;
303 str
<< (wxChar
)m_mode
<< _T(" ") << m_id
<< _T(" ") << m_flags
<< _T(" ")
304 << quote(m_pattern
) << _T(" ") << quote(m_data
);
306 for (it
= m_expected
.begin(); it
!= m_expected
.end(); ++it
)
307 str
<< _T(" ") << quote(*it
);
309 if (str
.length() > 77)
310 str
= str
.substr(0, 74) + _T("...");
312 str
<< _T("\n ") << msg
;
314 // no lossy convs so using utf8
315 CPPUNIT_FAIL(string(str
.mb_str(wxConvUTF8
)));
318 // quote a string so that it can be displayed (static)
320 wxString
RegExTestCase::quote(const wxString
& arg
)
322 const wxChar
*needEscape
= _T("\a\b\t\n\v\f\r\"\\");
323 const wxChar
*escapes
= _T("abtnvfr\"\\");
326 for (size_t i
= 0; i
< arg
.length(); i
++) {
327 wxChar ch
= (wxChar
)arg
[i
];
328 const wxChar
*p
= wxStrchr(needEscape
, ch
);
331 str
+= wxString::Format(_T("\\%c"), escapes
[p
- needEscape
]);
332 else if (wxIscntrl(ch
))
333 str
+= wxString::Format(_T("\\%03o"), ch
);
338 return str
.length() == arg
.length() && str
.find(' ') == wxString::npos
?
339 str
: _T("\"") + str
+ _T("\"");
343 ///////////////////////////////////////////////////////////////////////////////
346 class RegExTestSuite
: public TestSuite
349 RegExTestSuite(string name
) : TestSuite(name
) { }
350 void add(const char *mode
, const char *id
, const char *flags
,
351 const char *pattern
, const char *data
, const char *expected
, ...);
354 // Add a testcase to the suite
356 void RegExTestSuite::add(
362 const char *expected
, ...)
364 string name
= getName() + "." + id
;
366 vector
<const char *> expected_results
;
369 for (va_start(ap
, expected
); expected
; expected
= va_arg(ap
, const char *))
370 expected_results
.push_back(expected
);
375 addTest(new RegExTestCase(
376 name
, mode
, id
, flags
, pattern
, data
, expected_results
));
378 catch (Exception
& e
) {
379 wxLogInfo(wxString::Format(_T("skipping: %s\n %s\n"),
380 wxString(name
.c_str(), wxConvUTF8
).c_str(),
381 wxString(e
.what(), wxConvUTF8
).c_str()));
386 // Include the generated tests
391 #endif // wxHAS_REGEX_ADVANCED