]>
git.saurik.com Git - wxWidgets.git/blob - tests/regex/wxregex.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/regex/wxregex.cpp
3 // Purpose: Test wxRegEx
4 // Author: Vadim Zeitlin, Mike Wetherell
6 // Copyright: Vadim Zeitlin, Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
23 #include "wx/cppunit.h"
24 #include "wx/tokenzr.h"
28 using namespace CppUnit
;
31 ///////////////////////////////////////////////////////////////////////////////
34 class RegExCompileTestCase
: public TestCase
37 RegExCompileTestCase(const char *name
, const wxString
& pattern
,
38 bool correct
, int flags
)
54 void RegExCompileTestCase::runTest()
57 bool ok
= re
.Compile(m_pattern
, m_flags
);
60 CPPUNIT_ASSERT_MESSAGE("compile failed", ok
);
62 CPPUNIT_ASSERT_MESSAGE("compile succeeded (should fail)", !ok
);
66 ///////////////////////////////////////////////////////////////////////////////
69 class RegExMatchTestCase
: public TestCase
72 RegExMatchTestCase(const char *name
, const wxString
& pattern
,
73 const wxString
& text
, const char *expected
,
88 const char *m_expected
;
92 void RegExMatchTestCase::runTest()
94 int compileFlags
= m_flags
& ~(wxRE_NOTBOL
| wxRE_NOTEOL
);
95 int matchFlags
= m_flags
& (wxRE_NOTBOL
| wxRE_NOTEOL
);
97 wxRegEx
re(m_pattern
, compileFlags
);
98 CPPUNIT_ASSERT_MESSAGE("compile failed", re
.IsValid());
100 bool ok
= re
.Matches(m_text
, matchFlags
);
103 CPPUNIT_ASSERT_MESSAGE("match failed", ok
);
105 wxStringTokenizer
tkz(wxString(m_expected
, *wxConvCurrent
),
106 _T("\t"), wxTOKEN_RET_EMPTY
);
109 for (i
= 0; i
< re
.GetMatchCount() && tkz
.HasMoreTokens(); i
++) {
110 wxString expected
= tkz
.GetNextToken();
111 wxString result
= re
.GetMatch(m_text
, i
);
114 msgstr
.Printf(_T("\\%d == '%s' (expected '%s')"),
115 (int)i
, result
.c_str(), expected
.c_str());
116 const char *msg
= msgstr
.mb_str();
118 CPPUNIT_ASSERT_MESSAGE(msg
, result
== expected
);
121 if ((m_flags
& wxRE_NOSUB
) == 0)
122 CPPUNIT_ASSERT(re
.GetMatchCount() == i
);
125 CPPUNIT_ASSERT_MESSAGE("match succeeded (should fail)", !ok
);
130 ///////////////////////////////////////////////////////////////////////////////
133 class RegExReplaceTestCase
: public TestCase
136 RegExReplaceTestCase(const char *name
, const wxString
& pattern
,
137 const wxString
& text
, const wxString
& repl
,
138 const wxString
& expected
, size_t count
, int flags
)
143 m_expected(expected
),
160 void RegExReplaceTestCase::runTest()
162 wxRegEx
re(m_pattern
, m_flags
);
164 wxString
text(m_text
);
165 size_t nRepl
= re
.Replace(&text
, m_repl
);
168 msgstr
.Printf(_T("returns '%s' (expected '%s')"), text
.c_str(), m_expected
.c_str());
169 const char *msg
= msgstr
.mb_str();
170 CPPUNIT_ASSERT_MESSAGE(msg
, text
== m_expected
);
172 msgstr
.Printf(_T("matches %d times (expected %d)"), nRepl
, m_count
);
173 msg
= msgstr
.mb_str();
174 CPPUNIT_ASSERT_MESSAGE(msg
, nRepl
== m_count
);
178 ///////////////////////////////////////////////////////////////////////////////
181 class wxRegExTestSuite
: public TestSuite
184 wxRegExTestSuite() : TestSuite("wxRegExTestSuite") { }
185 static Test
*suite();
188 void add(const char *pattern
, bool correct
, int flags
= wxRE_DEFAULT
);
189 void add(const char *pattern
, const char *text
,
190 const char *expected
= NULL
, int flags
= wxRE_DEFAULT
);
191 void add(const char *pattern
, const char *text
, const char *replacement
,
192 const char *expected
, size_t count
, int flags
= wxRE_DEFAULT
);
194 static wxString
FlagStr(int flags
);
195 static wxString
Conv(const char *str
) { return wxString(str
, *wxConvCurrent
); }
198 // Build the suite (static)
200 Test
*wxRegExTestSuite::suite()
202 wxRegExTestSuite
*suite
= new wxRegExTestSuite
;
205 // pattern, expected result
206 suite
->add("foo", true);
207 suite
->add("foo(", false);
208 suite
->add("foo(bar", false);
209 suite
->add("foo(bar)", true);
210 suite
->add("foo[", false);
211 suite
->add("foo[bar", false);
212 suite
->add("foo[bar]", true);
213 suite
->add("foo{1", false);
214 suite
->add("foo{1}", true);
215 suite
->add("foo{1,2}", true);
216 suite
->add("foo*", true);
217 suite
->add("foo+", true);
218 suite
->add("foo?", true);
221 // pattern, text, expected results (match, followed by submatches
222 // tab separated, or NULL for no match expected)
223 suite
->add("foo", "bar");
224 suite
->add("foo", "foobar", "foo");
225 suite
->add("^foo", "foobar", "foo");
226 suite
->add("^foo", "barfoo");
227 suite
->add("bar$", "barbar", "bar");
228 suite
->add("bar$", "barbar ");
229 suite
->add("OoBa", "FoObAr", "oObA", wxRE_ICASE
);
230 suite
->add("^[A-Z].*$", "AA\nbb\nCC", "AA\nbb\nCC");
231 suite
->add("^[A-Z].*$", "AA\nbb\nCC", "AA", wxRE_NEWLINE
);
232 suite
->add("^[a-z].*$", "AA\nbb\nCC", "bb", wxRE_NEWLINE
);
233 suite
->add("^[A-Z].*$", "AA\nbb\nCC", "CC", wxRE_NEWLINE
| wxRE_NOTBOL
);
234 suite
->add("^[A-Z].*$", "AA\nbb\nCC", NULL
, wxRE_NEWLINE
| wxRE_NOTBOL
| wxRE_NOTEOL
);
235 suite
->add("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).* ([[:digit:]]+)$",
236 "Fri Jul 13 18:37:52 CEST 2001",
237 "Fri Jul 13 18:37:52 CEST 2001\tFri\tJul\t13\t2001");
240 // pattern, text, replacement, expected result and number of matches
241 const char *patn
= "([a-z]+)[^0-9]*([0-9]+)";
242 suite
->add(patn
, "foo123", "bar", "bar", 1);
243 suite
->add(patn
, "foo123", "\\2\\1", "123foo", 1);
244 suite
->add(patn
, "foo_123", "\\2\\1", "123foo", 1);
245 suite
->add(patn
, "123foo", "bar", "123foo", 0);
246 suite
->add(patn
, "123foo456foo", "&&", "123foo456foo456foo", 1);
247 suite
->add(patn
, "123foo456foo", "\\0\\0", "123foo456foo456foo", 1);
248 suite
->add(patn
, "foo123foo123", "bar", "barbar", 2);
249 suite
->add(patn
, "foo123_foo456_foo789", "bar", "bar_bar_bar", 3);
254 // Add a compile test
256 void wxRegExTestSuite::add(
259 int flags
/*=wxRE_DEFAULT*/)
261 addTest(new RegExCompileTestCase(
262 (_T("/") + Conv(pattern
) + _T("/") + FlagStr(flags
)).mb_str(),
263 Conv(pattern
), correct
, flags
));
268 void wxRegExTestSuite::add(
271 const char *expected
/*=NULL*/,
272 int flags
/*=wxRE_DEFAULT*/)
276 name
<< _T("'") << Conv(text
) << _T("' =~ /") << Conv(pattern
) << _T("/")
278 name
.Replace(_T("\n"), _T("\\n"));
280 addTest(new RegExMatchTestCase(name
.mb_str(), Conv(pattern
),
281 Conv(text
), expected
, flags
));
284 // Add a replace test
286 void wxRegExTestSuite::add(
289 const char *replacement
,
290 const char *expected
,
292 int flags
/*=wxRE_DEFAULT*/)
296 name
<< _T("'") << Conv(text
) << _T("' =~ s/") << Conv(pattern
) << _T("/")
297 << Conv(replacement
) << _T("/g") << FlagStr(flags
);
298 name
.Replace(_T("\n"), _T("\\n"));
300 addTest(new RegExReplaceTestCase(
301 name
.mb_str(), Conv(pattern
), Conv(text
),
302 Conv(replacement
), Conv(expected
), count
, flags
));
305 // Display string for the flags
307 wxString
wxRegExTestSuite::FlagStr(int flags
)
314 for (int i
= 0; (unsigned)flags
>> i
; i
++) {
315 switch (flags
& (1 << i
)) {
317 #ifdef wxHAS_REGEX_ADVANCED
318 case wxRE_ADVANCED
: str
+= _T(" | wxRE_ADVANCED"); break;
320 case wxRE_BASIC
: str
+= _T(" | wxRE_BASIC"); break;
321 case wxRE_ICASE
: str
+= _T(" | wxRE_ICASE"); break;
322 case wxRE_NOSUB
: str
+= _T(" | wxRE_NOSUB"); break;
323 case wxRE_NEWLINE
: str
+= _T(" | wxRE_NEWLINE"); break;
324 case wxRE_NOTBOL
: str
+= _T(" | wxRE_NOTBOL"); break;
325 case wxRE_NOTEOL
: str
+= _T(" | wxRE_NOTEOL"); break;
326 default: wxFAIL
; break;
330 return _T(" (") + str
.Mid(3) + _T(")");
333 // register in the unnamed registry so that these tests are run by default
334 CPPUNIT_TEST_SUITE_REGISTRATION(wxRegExTestSuite
);
336 // also include in it's own registry so that these tests can be run alone
337 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(wxRegExTestSuite
, "wxRegExTestSuite");
340 #endif // wxUSE_REGEX