]>
git.saurik.com Git - wxWidgets.git/blob - tests/regex/regextest.cpp
c31b63983475f06acc2e7d44468f7bf504e6d3a7
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: wxWindows 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".
41 // for all others, include the necessary headers
47 // many of the tests are specific to the builtin regex lib, so only attempts
48 // to do them when using the builtin regex lib.
50 #ifdef wxHAS_REGEX_ADVANCED
57 using CppUnit::TestCase
;
58 using CppUnit::TestSuite
;
59 using CppUnit::Exception
;
64 ///////////////////////////////////////////////////////////////////////////////
65 // The test case - an instance represents a single test
67 class RegExTestCase
: public TestCase
70 // constructor - create a single testcase
78 const vector
<const char *>& expected
);
86 wxString
Conv(const char *str
);
87 void parseFlags(const wxString
& flags
);
88 void doTest(int flavor
);
89 static wxString
quote(const wxString
& arg
);
90 const wxChar
*convError() const { return wxT("<cannot convert>"); }
92 // assertions - adds some information about the test that failed
93 void fail(const wxString
& msg
) const;
94 void failIf(bool condition
, const wxString
& msg
) const
95 { if (condition
) fail(msg
); }
97 // mode, id, flags, pattern, test data, expected results...
103 wxArrayString m_expected
;
113 // constructor - throws Exception on failure
115 RegExTestCase::RegExTestCase(
122 const vector
<const char *>& expected
)
127 m_flags(Conv(flags
)),
128 m_pattern(Conv(pattern
)),
136 bool badconv
= m_pattern
== convError() || m_data
== convError();
137 //RN: Removing the std:: here will break MSVC6 compilation
138 std::vector
<const char *>::const_iterator it
;
140 for (it
= expected
.begin(); it
!= expected
.end(); ++it
) {
141 m_expected
.push_back(Conv(*it
));
142 badconv
= badconv
|| *m_expected
.rbegin() == convError();
145 failIf(badconv
, wxT("cannot convert to default character encoding"));
147 // the flags need further parsing...
150 #ifndef wxHAS_REGEX_ADVANCED
151 failIf(!m_basic
&& !m_extended
, wxT("advanced regexs not available"));
155 int wxWcscmp(const wchar_t* s1
, const wchar_t* s2
)
157 size_t nLen1
= wxWcslen(s1
);
158 size_t nLen2
= wxWcslen(s2
);
161 return nLen1
- nLen2
;
163 return memcmp(s1
, s2
, nLen1
*sizeof(wchar_t));
166 // convert a string from UTF8 to the internal encoding
168 wxString
RegExTestCase::Conv(const char *str
)
170 const wxWCharBuffer wstr
= wxConvUTF8
.cMB2WC(str
);
171 const wxWC2WXbuf buf
= wxConvCurrent
->cWC2WX(wstr
);
173 if (!buf
|| wxWcscmp(wxConvCurrent
->cWX2WC(buf
), wstr
) != 0)
181 void RegExTestCase::parseFlags(const wxString
& flags
)
183 for ( wxString::const_iterator p
= flags
.begin(); p
!= flags
.end(); ++p
)
185 switch ( (*p
).GetValue() ) {
189 // we don't fully support these flags, but they don't stop us
190 // checking for success of failure of the match, so treat as noop
191 case 'A': case 'B': case 'E': case 'H':
192 case 'I': case 'L': case 'M': case 'N':
193 case 'P': case 'Q': case 'R': case 'S':
194 case 'T': case 'U': case '%':
198 case '^': m_matchFlags
|= wxRE_NOTBOL
; break;
199 case '$': m_matchFlags
|= wxRE_NOTEOL
; break;
204 case '&': m_advanced
= m_basic
= true; break;
205 case 'b': m_basic
= true; break;
206 case 'e': m_extended
= true; break;
207 case 'i': m_compileFlags
|= wxRE_ICASE
; break;
208 case 'o': m_compileFlags
|= wxRE_NOSUB
; break;
209 case 'n': m_compileFlags
|= wxRE_NEWLINE
; break;
210 case 't': if (strchr("ep", m_mode
)) break; // else fall through...
212 // anything else we must skip the test
214 fail(wxString::Format(
215 wxT("requires unsupported flag '%c'"), *p
));
220 // Try test for all flavours of expression specified
222 void RegExTestCase::runTest()
227 doTest(wxRE_EXTENDED
);
228 #ifdef wxHAS_REGEX_ADVANCED
229 if (m_advanced
|| (!m_basic
&& !m_extended
))
230 doTest(wxRE_ADVANCED
);
234 // Try the test for a single flavour of expression
236 void RegExTestCase::doTest(int flavor
)
238 wxRegEx
re(m_pattern
, m_compileFlags
| flavor
);
240 // 'e' - test that the pattern fails to compile
242 failIf(re
.IsValid(), wxT("compile succeeded (should fail)"));
245 failIf(!re
.IsValid(), wxT("compile failed"));
247 bool matches
= re
.Matches(m_data
, m_matchFlags
);
249 // 'f' or 'p' - test that the pattern does not match
250 if (m_mode
== 'f' || m_mode
== 'p') {
251 failIf(matches
, wxT("match succeeded (should fail)"));
255 // otherwise 'm' or 'i' - test the pattern does match
256 failIf(!matches
, wxT("match failed"));
258 if (m_compileFlags
& wxRE_NOSUB
)
261 // check wxRegEx has correctly counted the number of subexpressions
263 msg
<< wxT("GetMatchCount() == ") << re
.GetMatchCount()
264 << wxT(", expected ") << m_expected
.size();
265 failIf(m_expected
.size() != re
.GetMatchCount(), msg
);
267 for (size_t i
= 0; i
< m_expected
.size(); i
++) {
272 msg
<< wxT("wxRegEx::GetMatch failed for match ") << i
;
273 failIf(!re
.GetMatch(&start
, &len
, i
), msg
);
275 // m - check the match returns the strings given
279 result
= m_data
.substr(start
, len
);
284 // i - check the match returns the offsets given
285 else if (m_mode
== 'i')
288 result
= wxT("-1 -1");
289 else if (start
+ len
> 0)
290 result
<< start
<< wxT(" ") << start
+ len
- 1;
292 result
<< start
<< wxT(" -1");
296 msg
<< wxT("match(") << i
<< wxT(") == ") << quote(result
)
297 << wxT(", expected == ") << quote(m_expected
[i
]);
298 failIf(result
!= m_expected
[i
], msg
);
302 // assertion - adds some information about the test that failed
304 void RegExTestCase::fail(const wxString
& msg
) const
307 wxArrayString::const_iterator it
;
309 str
<< (wxChar
)m_mode
<< wxT(" ") << m_id
<< wxT(" ") << m_flags
<< wxT(" ")
310 << quote(m_pattern
) << wxT(" ") << quote(m_data
);
312 for (it
= m_expected
.begin(); it
!= m_expected
.end(); ++it
)
313 str
<< wxT(" ") << quote(*it
);
315 if (str
.length() > 77)
316 str
= str
.substr(0, 74) + wxT("...");
318 str
<< wxT("\n ") << msg
;
320 // no lossy convs so using utf8
321 CPPUNIT_FAIL(string(str
.mb_str(wxConvUTF8
)));
324 // quote a string so that it can be displayed (static)
326 wxString
RegExTestCase::quote(const wxString
& arg
)
328 const wxChar
*needEscape
= wxT("\a\b\t\n\v\f\r\"\\");
329 const wxChar
*escapes
= wxT("abtnvfr\"\\");
332 for (size_t i
= 0; i
< arg
.length(); i
++) {
333 wxChar ch
= (wxChar
)arg
[i
];
334 const wxChar
*p
= wxStrchr(needEscape
, ch
);
337 str
+= wxString::Format(wxT("\\%c"), escapes
[p
- needEscape
]);
338 else if (wxIscntrl(ch
))
339 str
+= wxString::Format(wxT("\\%03o"), ch
);
344 return str
.length() == arg
.length() && str
.find(' ') == wxString::npos
?
345 str
: wxT("\"") + str
+ wxT("\"");
349 ///////////////////////////////////////////////////////////////////////////////
352 class RegExTestSuite
: public TestSuite
355 RegExTestSuite(string name
) : TestSuite(name
) { }
356 void add(const char *mode
, const char *id
, const char *flags
,
357 const char *pattern
, const char *data
, const char *expected
, ...);
360 // Add a testcase to the suite
362 void RegExTestSuite::add(
368 const char *expected
, ...)
370 string name
= getName() + "." + id
;
372 vector
<const char *> expected_results
;
375 for (va_start(ap
, expected
); expected
; expected
= va_arg(ap
, const char *))
376 expected_results
.push_back(expected
);
381 addTest(new RegExTestCase(
382 name
, mode
, id
, flags
, pattern
, data
, expected_results
));
384 catch (Exception
& e
) {
385 wxLogInfo(wxString::Format(wxT("skipping: %s\n %s\n"),
386 wxString(name
.c_str(), wxConvUTF8
).c_str(),
387 wxString(e
.what(), wxConvUTF8
).c_str()));
392 // Include the generated tests
397 #endif // wxHAS_REGEX_ADVANCED
399 #endif // wxUSE_REGEX