]>
git.saurik.com Git - wxWidgets.git/blob - tests/regex/regex.cpp
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".
33 #include "wx/wxprec.h"
39 // for all others, include the necessary headers
45 #include "wx/cppunit.h"
49 using namespace CppUnit
;
51 // many of the tests are specific to the builtin regex lib, so only attempts
52 // to do them when using the builtin regex lib.
54 #ifdef wxHAS_REGEX_ADVANCED
57 ///////////////////////////////////////////////////////////////////////////////
58 // The test case - an instance represents a single test
60 class RegExTestCase
: public TestCase
63 // constructor - create a single testcase
71 const vector
<const char *>& expected
);
79 wxString
Conv(const char *str
);
80 void parseFlags(const wxString
& flags
);
81 void doTest(int flavor
);
82 static wxString
quote(const wxString
& arg
);
83 const wxChar
*convError() const { return _T("<cannot convert>"); }
85 // assertions - adds some information about the test that failed
86 void fail(const wxString
& msg
) const;
87 void failIf(bool condition
, const wxString
& msg
) const
88 { if (condition
) fail(msg
); }
90 // mode, id, flags, pattern, test data, expected results...
96 wxArrayString m_expected
;
106 // constructor - throws Exception on failure
108 RegExTestCase::RegExTestCase(
115 const vector
<const char *>& expected
)
120 m_flags(Conv(flags
)),
121 m_pattern(Conv(pattern
)),
129 bool badconv
= m_pattern
== convError() || m_data
== convError();
130 vector
<const char *>::const_iterator it
;
132 for (it
= expected
.begin(); it
!= expected
.end(); ++it
) {
133 m_expected
.push_back(Conv(*it
));
134 badconv
= badconv
|| *m_expected
.rbegin() == convError();
137 failIf(badconv
, _T("cannot convert to default character encoding"));
139 // the flags need further parsing...
142 #ifndef wxHAS_REGEX_ADVANCED
143 failIf(!m_basic
&& !m_extended
, _T("advanced regexs not available"));
147 // convert a string from UTF8 to the internal encoding
149 wxString
RegExTestCase::Conv(const char *str
)
151 const wxWCharBuffer wstr
= wxConvUTF8
.cMB2WC(str
);
152 const wxWC2WXbuf buf
= wxConvCurrent
->cWC2WX(wstr
);
154 if (!buf
|| wcscmp(wxConvCurrent
->cWX2WC(buf
), wstr
) != 0)
162 void RegExTestCase::parseFlags(const wxString
& flags
)
164 for (const wxChar
*p
= flags
; *p
; p
++) {
169 // we don't fully support these flags, but they don't stop us
170 // checking for success of failure of the match, so treat as noop
171 case 'A': case 'B': case 'E': case 'H':
172 case 'I': case 'L': case 'M': case 'N':
173 case 'P': case 'Q': case 'R': case 'S':
174 case 'T': case 'U': case '%':
178 case '^': m_matchFlags
|= wxRE_NOTBOL
; break;
179 case '$': m_matchFlags
|= wxRE_NOTEOL
; break;
184 case '&': m_advanced
= m_basic
= true; break;
185 case 'b': m_basic
= true; break;
186 case 'e': m_extended
= true; break;
187 case 'i': m_compileFlags
|= wxRE_ICASE
; break;
188 case 'o': m_compileFlags
|= wxRE_NOSUB
; break;
189 case 'n': m_compileFlags
|= wxRE_NEWLINE
; break;
190 case 't': if (strchr("ep", m_mode
)) break; // else fall through...
192 // anything else we must skip the test
194 fail(wxString::Format(
195 _T("requires unsupported flag '%c'"), *p
));
200 // Try test for all flavours of expression specified
202 void RegExTestCase::runTest()
207 doTest(wxRE_EXTENDED
);
208 #ifdef wxHAS_REGEX_ADVANCED
209 if (m_advanced
|| (!m_basic
&& !m_extended
))
210 doTest(wxRE_ADVANCED
);
214 // Try the test for a single flavour of expression
216 void RegExTestCase::doTest(int flavor
)
218 wxRegEx
re(m_pattern
, m_compileFlags
| flavor
);
220 // 'e' - test that the pattern fails to compile
222 failIf(re
.IsValid(), _T("compile succeeded (should fail)"));
225 failIf(!re
.IsValid(), _T("compile failed"));
227 bool matches
= re
.Matches(m_data
, m_matchFlags
);
229 // 'f' or 'p' - test that the pattern does not match
230 if (m_mode
== 'f' || m_mode
== 'p') {
231 failIf(matches
, _T("match succeeded (should fail)"));
235 // otherwise 'm' or 'i' - test the pattern does match
236 failIf(!matches
, _T("match failed"));
238 if (m_compileFlags
& wxRE_NOSUB
)
241 // check wxRegEx has correctly counted the number of subexpressions
242 failIf(m_expected
.size() != re
.GetMatchCount(),
243 wxString::Format(_T("GetMatchCount() == %d, expected %d"),
244 re
.GetMatchCount(), m_expected
.size()));
249 for (size_t i
= 0; i
< m_expected
.size(); i
++) {
250 failIf(!re
.GetMatch(&start
, &len
, i
), wxString::Format(
251 _T("wxRegEx::GetMatch failed for match %d"), i
));
253 // m - check the match returns the strings given
256 result
= m_data
.substr(start
, len
);
260 // i - check the match returns the offsets given
261 else if (m_mode
== 'i')
263 result
= wxString::Format(_T("%d %d"), start
, start
+ len
- 1);
265 result
= _T("-1 -1");
267 failIf(result
!= m_expected
[i
], wxString::Format(
268 _T("match(%d) == %s, expected == %s"), i
,
269 quote(result
).c_str(), quote(m_expected
[i
]).c_str()));
273 // assertion - adds some information about the test that failed
275 void RegExTestCase::fail(const wxString
& msg
) const
278 wxArrayString::const_iterator it
;
280 str
<< (wxChar
)m_mode
<< _T(" ") << m_id
<< _T(" ") << m_flags
<< _T(" ")
281 << quote(m_pattern
) << _T(" ") << quote(m_data
);
283 for (it
= m_expected
.begin(); it
!= m_expected
.end(); ++it
)
284 str
<< _T(" ") << quote(*it
);
286 if (str
.length() > 77)
287 str
= str
.substr(0, 74) + _T("...");
289 str
<< _T("\n ") << msg
;
291 // no lossy convs so using utf8
292 CPPUNIT_FAIL(string(str
.mb_str(wxConvUTF8
)));
295 // quote a string so that it can be displayed (static)
297 wxString
RegExTestCase::quote(const wxString
& arg
)
299 const wxChar
*needEscape
= _T("\a\b\t\n\v\f\r\"\\");
300 const wxChar
*escapes
= _T("abtnvfr\"\\");
303 for (size_t i
= 0; i
< arg
.length(); i
++) {
305 const wxChar
*p
= wxStrchr(needEscape
, ch
);
308 str
+= wxString::Format(_T("\\%c"), escapes
[p
- needEscape
]);
309 else if (wxIscntrl(ch
))
310 str
+= wxString::Format(_T("\\%03o"), ch
);
315 return str
.length() == arg
.length() && str
.find(' ') == wxString::npos
?
316 str
: _T("\"") + str
+ _T("\"");
320 ///////////////////////////////////////////////////////////////////////////////
323 // In a non-unicode build the regex is affected by the current locale, so
324 // this derived TestSuite is used. It sets the locale in it's run() method
325 // for the duration of the regex tests.
327 class RegExTestSuite
: public TestSuite
330 RegExTestSuite(string name
);
331 void run(TestResult
*result
);
332 void add(const char *mode
, const char *id
, const char *flags
,
333 const char *pattern
, const char *data
, const char *expected
, ...);
336 // constructor, sets the locale so that it is set when the tests are added
338 RegExTestSuite::RegExTestSuite(string name
) : TestSuite(name
)
340 setlocale(LC_ALL
, "");
343 // run the test suite, sets the locale again since it may have been changed
344 // by another test since this suite was crated
346 void RegExTestSuite::run(TestResult
*result
)
348 setlocale(LC_ALL
, "");
349 TestSuite::run(result
);
352 // Add a testcase to the suite
354 void RegExTestSuite::add(
360 const char *expected
, ...)
362 string name
= getName() + "." + id
;
364 vector
<const char *> expected_results
;
367 for (va_start(ap
, expected
); expected
; expected
= va_arg(ap
, const char *))
368 expected_results
.push_back(expected
);
373 addTest(new RegExTestCase(
374 name
, mode
, id
, flags
, pattern
, data
, expected_results
));
376 catch (Exception
& e
) {
377 wxLogInfo(wxString::Format(_T("skipping: %s\n %s\n"),
378 wxString(name
.c_str(), wxConvUTF8
).c_str(),
379 wxString(e
.what(), wxConvUTF8
).c_str()));
384 // Include the generated tests
389 #endif // wxHAS_REGEX_ADVANCED