]>
git.saurik.com Git - wxWidgets.git/blob - tests/regex/wxregextest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/regex/wxregex.cpp
3 // Purpose: Test wxRegEx
4 // Author: Vadim Zeitlin, Mike Wetherell
5 // Copyright: Vadim Zeitlin, Mike Wetherell
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
22 #include "wx/tokenzr.h"
26 using CppUnit::TestCase
;
27 using CppUnit::TestSuite
;
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 wxT("\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(wxT("\\%d == '%s' (expected '%s')"),
115 (int)i
, result
.c_str(), expected
.c_str());
117 CPPUNIT_ASSERT_MESSAGE((const char*)msgstr
.mb_str(),
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(wxT("returns '%s' (expected '%s')"), text
.c_str(), m_expected
.c_str());
169 CPPUNIT_ASSERT_MESSAGE((const char*)msgstr
.mb_str(), text
== m_expected
);
171 msgstr
.Printf(wxT("matches %d times (expected %d)"), (int)nRepl
, (int)m_count
);
172 CPPUNIT_ASSERT_MESSAGE((const char*)msgstr
.mb_str(), nRepl
== m_count
);
176 ///////////////////////////////////////////////////////////////////////////////
179 class wxRegExTestSuite
: public TestSuite
182 wxRegExTestSuite() : TestSuite("wxRegExTestSuite") { }
183 static Test
*suite();
186 void add(const char *pattern
, bool correct
, int flags
= wxRE_DEFAULT
);
187 void add(const char *pattern
, const char *text
,
188 const char *expected
= NULL
, int flags
= wxRE_DEFAULT
);
189 void add(const char *pattern
, const char *text
, const char *replacement
,
190 const char *expected
, size_t count
, int flags
= wxRE_DEFAULT
);
192 static wxString
FlagStr(int flags
);
193 static wxString
Conv(const char *str
) { return wxString(str
, *wxConvCurrent
); }
196 // Build the suite (static)
198 Test
*wxRegExTestSuite::suite()
200 wxRegExTestSuite
*suite
= new wxRegExTestSuite
;
203 // pattern, expected result
204 suite
->add("foo", true);
205 suite
->add("foo(", false);
206 suite
->add("foo(bar", false);
207 suite
->add("foo(bar)", true);
208 suite
->add("foo[", false);
209 suite
->add("foo[bar", false);
210 suite
->add("foo[bar]", true);
211 suite
->add("foo{1", false);
212 suite
->add("foo{1}", true);
213 suite
->add("foo{1,2}", true);
214 suite
->add("foo*", true);
215 suite
->add("foo+", true);
216 suite
->add("foo?", true);
219 // pattern, text, expected results (match, followed by submatches
220 // tab separated, or NULL for no match expected)
221 suite
->add("foo", "bar");
222 suite
->add("foo", "foobar", "foo");
223 suite
->add("^foo", "foobar", "foo");
224 suite
->add("^foo", "barfoo");
225 suite
->add("bar$", "barbar", "bar");
226 suite
->add("bar$", "barbar ");
227 suite
->add("OoBa", "FoObAr", "oObA", wxRE_ICASE
);
228 suite
->add("^[A-Z].*$", "AA\nbb\nCC", "AA\nbb\nCC");
229 suite
->add("^[A-Z].*$", "AA\nbb\nCC", "AA", wxRE_NEWLINE
);
230 suite
->add("^[a-z].*$", "AA\nbb\nCC", "bb", wxRE_NEWLINE
);
231 suite
->add("^[A-Z].*$", "AA\nbb\nCC", "CC", wxRE_NEWLINE
| wxRE_NOTBOL
);
232 suite
->add("^[A-Z].*$", "AA\nbb\nCC", NULL
, wxRE_NEWLINE
| wxRE_NOTBOL
| wxRE_NOTEOL
);
233 suite
->add("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).* ([[:digit:]]+)$",
234 "Fri Jul 13 18:37:52 CEST 2001",
235 "Fri Jul 13 18:37:52 CEST 2001\tFri\tJul\t13\t2001");
238 // pattern, text, replacement, expected result and number of matches
239 const char *patn
= "([a-z]+)[^0-9]*([0-9]+)";
240 suite
->add(patn
, "foo123", "bar", "bar", 1);
241 suite
->add(patn
, "foo123", "\\2\\1", "123foo", 1);
242 suite
->add(patn
, "foo_123", "\\2\\1", "123foo", 1);
243 suite
->add(patn
, "123foo", "bar", "123foo", 0);
244 suite
->add(patn
, "123foo456foo", "&&", "123foo456foo456foo", 1);
245 suite
->add(patn
, "123foo456foo", "\\0\\0", "123foo456foo456foo", 1);
246 suite
->add(patn
, "foo123foo123", "bar", "barbar", 2);
247 suite
->add(patn
, "foo123_foo456_foo789", "bar", "bar_bar_bar", 3);
252 // Add a compile test
254 void wxRegExTestSuite::add(
257 int flags
/*=wxRE_DEFAULT*/)
259 addTest(new RegExCompileTestCase(
260 (wxT("/") + Conv(pattern
) + wxT("/") + FlagStr(flags
)).mb_str(),
261 Conv(pattern
), correct
, flags
));
266 void wxRegExTestSuite::add(
269 const char *expected
/*=NULL*/,
270 int flags
/*=wxRE_DEFAULT*/)
274 name
<< wxT("'") << Conv(text
) << wxT("' =~ /") << Conv(pattern
) << wxT("/")
276 name
.Replace(wxT("\n"), wxT("\\n"));
278 addTest(new RegExMatchTestCase(name
.mb_str(), Conv(pattern
),
279 Conv(text
), expected
, flags
));
282 // Add a replace test
284 void wxRegExTestSuite::add(
287 const char *replacement
,
288 const char *expected
,
290 int flags
/*=wxRE_DEFAULT*/)
294 name
<< wxT("'") << Conv(text
) << wxT("' =~ s/") << Conv(pattern
) << wxT("/")
295 << Conv(replacement
) << wxT("/g") << FlagStr(flags
);
296 name
.Replace(wxT("\n"), wxT("\\n"));
298 addTest(new RegExReplaceTestCase(
299 name
.mb_str(), Conv(pattern
), Conv(text
),
300 Conv(replacement
), Conv(expected
), count
, flags
));
303 // Display string for the flags
305 wxString
wxRegExTestSuite::FlagStr(int flags
)
312 for (int i
= 0; (unsigned)flags
>> i
; i
++) {
313 switch (flags
& (1 << i
)) {
315 #ifdef wxHAS_REGEX_ADVANCED
316 case wxRE_ADVANCED
: str
+= wxT(" | wxRE_ADVANCED"); break;
318 case wxRE_BASIC
: str
+= wxT(" | wxRE_BASIC"); break;
319 case wxRE_ICASE
: str
+= wxT(" | wxRE_ICASE"); break;
320 case wxRE_NOSUB
: str
+= wxT(" | wxRE_NOSUB"); break;
321 case wxRE_NEWLINE
: str
+= wxT(" | wxRE_NEWLINE"); break;
322 case wxRE_NOTBOL
: str
+= wxT(" | wxRE_NOTBOL"); break;
323 case wxRE_NOTEOL
: str
+= wxT(" | wxRE_NOTEOL"); break;
324 default: wxFAIL
; break;
328 return wxT(" (") + str
.Mid(3) + wxT(")");
331 // register in the unnamed registry so that these tests are run by default
332 CPPUNIT_TEST_SUITE_REGISTRATION(wxRegExTestSuite
);
334 // also include in its own registry so that these tests can be run alone
335 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(wxRegExTestSuite
, "wxRegExTestSuite");
338 #endif // wxUSE_REGEX