]> git.saurik.com Git - wxWidgets.git/blob - tests/regex/regex.cpp
Minor manual fixes.
[wxWidgets.git] / 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
5 // RCS-ID: $Id$
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 //
11 // Notes:
12 //
13 // To run just one section, say wx_1, do this:
14 // test regex.wx_1
15 //
16 // To run all the regex tests:
17 // test regex
18 //
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
23 //
24 // The tests here are for the builtin library, tests for wxRegEx in general
25 // should go in wxregex.cpp
26 //
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'.
30 //
31
32 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
34
35 #ifdef __BORLANDC__
36 #pragma hdrstop
37 #endif
38
39 // for all others, include the necessary headers
40 #ifndef WX_PRECOMP
41 #include "wx/wx.h"
42 #endif
43
44 #include "wx/regex.h"
45 #include "wx/cppunit.h"
46 #include <stdexcept>
47
48 using namespace std;
49 using namespace CppUnit;
50
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.
53 //
54 #ifdef wxHAS_REGEX_ADVANCED
55
56
57 ///////////////////////////////////////////////////////////////////////////////
58 // The test case - an instance represents a single test
59
60 class RegExTestCase : public TestCase
61 {
62 public:
63 // constructor - create a single testcase
64 RegExTestCase(
65 const string& name,
66 const char *mode,
67 const char *id,
68 const char *flags,
69 const char *pattern,
70 const char *data,
71 const vector<const char *>& expected);
72
73 protected:
74 // run this testcase
75 void runTest();
76
77 private:
78 // workers
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>"); }
84
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); }
89
90 // mode, id, flags, pattern, test data, expected results...
91 int m_mode;
92 wxString m_id;
93 wxString m_flags;
94 wxString m_pattern;
95 wxString m_data;
96 wxArrayString m_expected;
97
98 // the flag decoded
99 int m_compileFlags;
100 int m_matchFlags;
101 bool m_basic;
102 bool m_extended;
103 bool m_advanced;
104 };
105
106 // constructor - throws Exception on failure
107 //
108 RegExTestCase::RegExTestCase(
109 const string& name,
110 const char *mode,
111 const char *id,
112 const char *flags,
113 const char *pattern,
114 const char *data,
115 const vector<const char *>& expected)
116 :
117 TestCase(name),
118 m_mode(mode[0]),
119 m_id(Conv(id)),
120 m_flags(Conv(flags)),
121 m_pattern(Conv(pattern)),
122 m_data(Conv(data)),
123 m_compileFlags(0),
124 m_matchFlags(0),
125 m_basic(false),
126 m_extended(false),
127 m_advanced(false)
128 {
129 bool badconv = m_pattern == convError() || m_data == convError();
130 vector<const char *>::const_iterator it;
131
132 for (it = expected.begin(); it != expected.end(); ++it) {
133 m_expected.push_back(Conv(*it));
134 badconv = badconv || *m_expected.rbegin() == convError();
135 }
136
137 failIf(badconv, _T("cannot convert to default character encoding"));
138
139 // the flags need further parsing...
140 parseFlags(m_flags);
141
142 #ifndef wxHAS_REGEX_ADVANCED
143 failIf(!m_basic && !m_extended, _T("advanced regexs not available"));
144 #endif
145 }
146
147 int wxWcscmp(const wchar_t* s1, const wchar_t* s2)
148 {
149 size_t nLen1 = wxWcslen(s1);
150 size_t nLen2 = wxWcslen(s2);
151
152 if (nLen1 != nLen2)
153 return nLen1 - nLen2;
154
155 return wxMemcmp(s1, s2, nLen1);
156 }
157
158 // convert a string from UTF8 to the internal encoding
159 //
160 wxString RegExTestCase::Conv(const char *str)
161 {
162 const wxWCharBuffer wstr = wxConvUTF8.cMB2WC(str);
163 const wxWC2WXbuf buf = wxConvCurrent->cWC2WX(wstr);
164
165 if (!buf || wxWcscmp(wxConvCurrent->cWX2WC(buf), wstr) != 0)
166 return convError();
167 else
168 return buf;
169 }
170
171 // Parse flags
172 //
173 void RegExTestCase::parseFlags(const wxString& flags)
174 {
175 for (const wxChar *p = flags; *p; p++) {
176 switch (*p) {
177 // noop
178 case '-': break;
179
180 // we don't fully support these flags, but they don't stop us
181 // checking for success of failure of the match, so treat as noop
182 case 'A': case 'B': case 'E': case 'H':
183 case 'I': case 'L': case 'M': case 'N':
184 case 'P': case 'Q': case 'R': case 'S':
185 case 'T': case 'U': case '%':
186 break;
187
188 // match options
189 case '^': m_matchFlags |= wxRE_NOTBOL; break;
190 case '$': m_matchFlags |= wxRE_NOTEOL; break;
191 #if wxUSE_UNICODE
192 case '*': break;
193 #endif
194 // compile options
195 case '&': m_advanced = m_basic = true; break;
196 case 'b': m_basic = true; break;
197 case 'e': m_extended = true; break;
198 case 'i': m_compileFlags |= wxRE_ICASE; break;
199 case 'o': m_compileFlags |= wxRE_NOSUB; break;
200 case 'n': m_compileFlags |= wxRE_NEWLINE; break;
201 case 't': if (strchr("ep", m_mode)) break; // else fall through...
202
203 // anything else we must skip the test
204 default:
205 fail(wxString::Format(
206 _T("requires unsupported flag '%c'"), *p));
207 }
208 }
209 }
210
211 // Try test for all flavours of expression specified
212 //
213 void RegExTestCase::runTest()
214 {
215 if (m_basic)
216 doTest(wxRE_BASIC);
217 if (m_extended)
218 doTest(wxRE_EXTENDED);
219 #ifdef wxHAS_REGEX_ADVANCED
220 if (m_advanced || (!m_basic && !m_extended))
221 doTest(wxRE_ADVANCED);
222 #endif
223 }
224
225 // Try the test for a single flavour of expression
226 //
227 void RegExTestCase::doTest(int flavor)
228 {
229 wxRegEx re(m_pattern, m_compileFlags | flavor);
230
231 // 'e' - test that the pattern fails to compile
232 if (m_mode == 'e') {
233 failIf(re.IsValid(), _T("compile succeeded (should fail)"));
234 return;
235 }
236 failIf(!re.IsValid(), _T("compile failed"));
237
238 bool matches = re.Matches(m_data, m_matchFlags);
239
240 // 'f' or 'p' - test that the pattern does not match
241 if (m_mode == 'f' || m_mode == 'p') {
242 failIf(matches, _T("match succeeded (should fail)"));
243 return;
244 }
245
246 // otherwise 'm' or 'i' - test the pattern does match
247 failIf(!matches, _T("match failed"));
248
249 if (m_compileFlags & wxRE_NOSUB)
250 return;
251
252 // check wxRegEx has correctly counted the number of subexpressions
253 failIf(m_expected.size() != re.GetMatchCount(),
254 wxString::Format(_T("GetMatchCount() == %d, expected %d"),
255 re.GetMatchCount(), m_expected.size()));
256
257 wxString result;
258 size_t start, len;
259
260 for (size_t i = 0; i < m_expected.size(); i++) {
261 failIf(!re.GetMatch(&start, &len, i), wxString::Format(
262 _T("wxRegEx::GetMatch failed for match %d"), i));
263
264 // m - check the match returns the strings given
265 if (m_mode == 'm')
266 if (start < INT_MAX)
267 result = m_data.substr(start, len);
268 else
269 result = _T("");
270
271 // i - check the match returns the offsets given
272 else if (m_mode == 'i')
273 if (start < INT_MAX)
274 result = wxString::Format(_T("%d %d"), start, start + len - 1);
275 else
276 result = _T("-1 -1");
277
278 failIf(result != m_expected[i], wxString::Format(
279 _T("match(%d) == %s, expected == %s"), i,
280 quote(result).c_str(), quote(m_expected[i]).c_str()));
281 }
282 }
283
284 // assertion - adds some information about the test that failed
285 //
286 void RegExTestCase::fail(const wxString& msg) const
287 {
288 wxString str;
289 wxArrayString::const_iterator it;
290
291 str << (wxChar)m_mode << _T(" ") << m_id << _T(" ") << m_flags << _T(" ")
292 << quote(m_pattern) << _T(" ") << quote(m_data);
293
294 for (it = m_expected.begin(); it != m_expected.end(); ++it)
295 str << _T(" ") << quote(*it);
296
297 if (str.length() > 77)
298 str = str.substr(0, 74) + _T("...");
299
300 str << _T("\n ") << msg;
301
302 // no lossy convs so using utf8
303 CPPUNIT_FAIL(string(str.mb_str(wxConvUTF8)));
304 }
305
306 // quote a string so that it can be displayed (static)
307 //
308 wxString RegExTestCase::quote(const wxString& arg)
309 {
310 const wxChar *needEscape = _T("\a\b\t\n\v\f\r\"\\");
311 const wxChar *escapes = _T("abtnvfr\"\\");
312 wxString str;
313
314 for (size_t i = 0; i < arg.length(); i++) {
315 wxUChar ch = arg[i];
316 const wxChar *p = wxStrchr(needEscape, ch);
317
318 if (p)
319 str += wxString::Format(_T("\\%c"), escapes[p - needEscape]);
320 else if (wxIscntrl(ch))
321 str += wxString::Format(_T("\\%03o"), ch);
322 else
323 str += ch;
324 }
325
326 return str.length() == arg.length() && str.find(' ') == wxString::npos ?
327 str : _T("\"") + str + _T("\"");
328 }
329
330
331 ///////////////////////////////////////////////////////////////////////////////
332 // Test suite
333
334 class RegExTestSuite : public TestSuite
335 {
336 public:
337 RegExTestSuite(string name) : TestSuite(name) { }
338 void add(const char *mode, const char *id, const char *flags,
339 const char *pattern, const char *data, const char *expected, ...);
340 };
341
342 // Add a testcase to the suite
343 //
344 void RegExTestSuite::add(
345 const char *mode,
346 const char *id,
347 const char *flags,
348 const char *pattern,
349 const char *data,
350 const char *expected, ...)
351 {
352 string name = getName() + "." + id;
353
354 vector<const char *> expected_results;
355 va_list ap;
356
357 for (va_start(ap, expected); expected; expected = va_arg(ap, const char *))
358 expected_results.push_back(expected);
359
360 va_end(ap);
361
362 try {
363 addTest(new RegExTestCase(
364 name, mode, id, flags, pattern, data, expected_results));
365 }
366 catch (Exception& e) {
367 wxLogInfo(wxString::Format(_T("skipping: %s\n %s\n"),
368 wxString(name.c_str(), wxConvUTF8).c_str(),
369 wxString(e.what(), wxConvUTF8).c_str()));
370 }
371 }
372
373
374 // Include the generated tests
375 //
376 #include "regex.inc"
377
378
379 #endif // wxHAS_REGEX_ADVANCED