File: wxregextest.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (338 lines) | stat: -rw-r--r-- 9,855 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/regex/wxregex.cpp
// Purpose:     Test wxRegEx
// Author:      Vadim Zeitlin, Mike Wetherell
// Copyright:   Vadim Zeitlin, Mike Wetherell
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#include "testprec.h"

#ifdef __BORLANDC__
#   pragma hdrstop
#endif

#ifndef WX_PRECOMP
#   include "wx/wx.h"
#endif

#if wxUSE_REGEX

#include "wx/regex.h"
#include "wx/tokenzr.h"
#include <string>

using CppUnit::Test;
using CppUnit::TestCase;
using CppUnit::TestSuite;
using std::string;


///////////////////////////////////////////////////////////////////////////////
// Compile Test

class RegExCompileTestCase : public TestCase
{
public:
    RegExCompileTestCase(const char *name, const wxString& pattern,
                         bool correct, int flags)
    :   TestCase(name),
        m_pattern(pattern),
        m_correct(correct),
        m_flags(flags)
    { }

protected:
    void runTest();

private:
    wxString m_pattern;
    bool m_correct;
    int m_flags;
};

void RegExCompileTestCase::runTest()
{
    wxRegEx re;
    bool ok = re.Compile(m_pattern, m_flags);

    if (m_correct)
        CPPUNIT_ASSERT_MESSAGE("compile failed", ok);
    else
        CPPUNIT_ASSERT_MESSAGE("compile succeeded (should fail)", !ok);
}


///////////////////////////////////////////////////////////////////////////////
// Match Test

class RegExMatchTestCase : public TestCase
{
public:
    RegExMatchTestCase(const char *name, const wxString& pattern,
                       const wxString& text, const char *expected,
                       int flags)
    :   TestCase(name),
        m_pattern(pattern),
        m_text(text),
        m_expected(expected),
        m_flags(flags)
    { }

protected:
    void runTest();

private:
    wxString m_pattern;
    wxString m_text;
    const char *m_expected;
    int m_flags;
};

void RegExMatchTestCase::runTest()
{
    int compileFlags = m_flags & ~(wxRE_NOTBOL | wxRE_NOTEOL);
    int matchFlags = m_flags & (wxRE_NOTBOL | wxRE_NOTEOL);

    wxRegEx re(m_pattern, compileFlags);
    CPPUNIT_ASSERT_MESSAGE("compile failed", re.IsValid());

    bool ok = re.Matches(m_text, matchFlags);

    if (m_expected) {
        CPPUNIT_ASSERT_MESSAGE("match failed", ok);

        wxStringTokenizer tkz(wxString(m_expected, *wxConvCurrent),
                              wxT("\t"), wxTOKEN_RET_EMPTY);
        size_t i;

        for (i = 0; i < re.GetMatchCount() && tkz.HasMoreTokens(); i++) {
            wxString expected = tkz.GetNextToken();
            wxString result = re.GetMatch(m_text, i);

            wxString msgstr;
            msgstr.Printf(wxT("\\%d == '%s' (expected '%s')"),
                          (int)i, result.c_str(), expected.c_str());

            CPPUNIT_ASSERT_MESSAGE((const char*)msgstr.mb_str(),
                                   result == expected);
        }

        if ((m_flags & wxRE_NOSUB) == 0)
            CPPUNIT_ASSERT(re.GetMatchCount() == i);
    }
    else {
        CPPUNIT_ASSERT_MESSAGE("match succeeded (should fail)", !ok);
    }
}


///////////////////////////////////////////////////////////////////////////////
// Replacement Test

class RegExReplaceTestCase : public TestCase
{
public:
    RegExReplaceTestCase(const char *name, const wxString& pattern,
                         const wxString& text, const wxString& repl,
                         const wxString& expected, size_t count, int flags)
    :   TestCase(name),
        m_pattern(pattern),
        m_text(text),
        m_repl(repl),
        m_expected(expected),
        m_count(count),
        m_flags(flags)
    { }

protected:
    void runTest();

private:
    wxString m_pattern;
    wxString m_text;
    wxString m_repl;
    wxString m_expected;
    size_t m_count;
    int m_flags;
};

void RegExReplaceTestCase::runTest()
{
    wxRegEx re(m_pattern, m_flags);

    wxString text(m_text);
    size_t nRepl = re.Replace(&text, m_repl);

    wxString msgstr;
    msgstr.Printf(wxT("returns '%s' (expected '%s')"), text.c_str(), m_expected.c_str());
    CPPUNIT_ASSERT_MESSAGE((const char*)msgstr.mb_str(), text == m_expected);

    msgstr.Printf(wxT("matches %d times (expected %d)"), (int)nRepl, (int)m_count);
    CPPUNIT_ASSERT_MESSAGE((const char*)msgstr.mb_str(), nRepl == m_count);
}


///////////////////////////////////////////////////////////////////////////////
// The suite

class wxRegExTestSuite : public TestSuite
{
public:
    wxRegExTestSuite() : TestSuite("wxRegExTestSuite") { }
    static Test *suite();

private:
    void add(const char *pattern, bool correct, int flags = wxRE_DEFAULT);
    void add(const char *pattern, const char *text,
             const char *expected = NULL, int flags = wxRE_DEFAULT);
    void add(const char *pattern, const char *text, const char *replacement,
             const char *expected, size_t count, int flags = wxRE_DEFAULT);

    static wxString FlagStr(int flags);
    static wxString Conv(const char *str) { return wxString(str, *wxConvCurrent); }
};

// Build the suite (static)
//
Test *wxRegExTestSuite::suite()
{
    wxRegExTestSuite *suite = new wxRegExTestSuite;

    // Compile tests
    // pattern, expected result
    suite->add("foo", true);
    suite->add("foo(", false);
    suite->add("foo(bar", false);
    suite->add("foo(bar)", true);
    suite->add("foo[", false);
    suite->add("foo[bar", false);
    suite->add("foo[bar]", true);
    suite->add("foo{1", false);
    suite->add("foo{1}", true);
    suite->add("foo{1,2}", true);
    suite->add("foo*", true);
    suite->add("foo+", true);
    suite->add("foo?", true);

    // Match tests
    // pattern, text, expected results (match, followed by submatches
    // tab separated, or NULL for no match expected)
    suite->add("foo", "bar");
    suite->add("foo", "foobar", "foo");
    suite->add("^foo", "foobar", "foo");
    suite->add("^foo", "barfoo");
    suite->add("bar$", "barbar", "bar");
    suite->add("bar$", "barbar ");
    suite->add("OoBa", "FoObAr", "oObA", wxRE_ICASE);
    suite->add("^[A-Z].*$", "AA\nbb\nCC", "AA\nbb\nCC");
    suite->add("^[A-Z].*$", "AA\nbb\nCC", "AA", wxRE_NEWLINE);
    suite->add("^[a-z].*$", "AA\nbb\nCC", "bb", wxRE_NEWLINE);
    suite->add("^[A-Z].*$", "AA\nbb\nCC", "CC", wxRE_NEWLINE | wxRE_NOTBOL);
    suite->add("^[A-Z].*$", "AA\nbb\nCC", NULL, wxRE_NEWLINE | wxRE_NOTBOL | wxRE_NOTEOL);
    suite->add("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).* ([[:digit:]]+)$",
        "Fri Jul 13 18:37:52 CEST 2001",
        "Fri Jul 13 18:37:52 CEST 2001\tFri\tJul\t13\t2001");

    // Replace tests
    // pattern, text, replacement, expected result and number of matches
    const char *patn = "([a-z]+)[^0-9]*([0-9]+)";
    suite->add(patn, "foo123", "bar", "bar", 1);
    suite->add(patn, "foo123", "\\2\\1", "123foo", 1);
    suite->add(patn, "foo_123", "\\2\\1", "123foo", 1);
    suite->add(patn, "123foo", "bar", "123foo", 0);
    suite->add(patn, "123foo456foo", "&&", "123foo456foo456foo", 1);
    suite->add(patn, "123foo456foo", "\\0\\0", "123foo456foo456foo", 1);
    suite->add(patn, "foo123foo123", "bar", "barbar", 2);
    suite->add(patn, "foo123_foo456_foo789", "bar", "bar_bar_bar", 3);

    return suite;
}

// Add a compile test
//
void wxRegExTestSuite::add(
    const char *pattern,
    bool correct,
    int flags /*=wxRE_DEFAULT*/)
{
    addTest(new RegExCompileTestCase(
                (wxT("/") + Conv(pattern) + wxT("/") + FlagStr(flags)).mb_str(),
                Conv(pattern), correct, flags));
}

// Add a match test
//
void wxRegExTestSuite::add(
    const char *pattern,
    const char *text,
    const char *expected /*=NULL*/,
    int flags /*=wxRE_DEFAULT*/)
{
    wxString name;

    name << wxT("'") << Conv(text) << wxT("' =~ /") << Conv(pattern) << wxT("/")
         << FlagStr(flags);
    name.Replace(wxT("\n"), wxT("\\n"));

    addTest(new RegExMatchTestCase(name.mb_str(), Conv(pattern),
                                   Conv(text), expected, flags));
}

// Add a replace test
//
void wxRegExTestSuite::add(
    const char *pattern,
    const char *text,
    const char *replacement,
    const char *expected,
    size_t count,
    int flags /*=wxRE_DEFAULT*/)
{
    wxString name;

    name << wxT("'") << Conv(text) << wxT("' =~ s/") << Conv(pattern) << wxT("/")
         << Conv(replacement) << wxT("/g") << FlagStr(flags);
    name.Replace(wxT("\n"), wxT("\\n"));

    addTest(new RegExReplaceTestCase(
                    name.mb_str(), Conv(pattern), Conv(text),
                    Conv(replacement), Conv(expected), count, flags));
}

// Display string for the flags
//
wxString wxRegExTestSuite::FlagStr(int flags)
{
    wxString str;

    if (!flags)
        return str;

    for (int i = 0; (unsigned)flags >> i; i++) {
        switch (flags & (1 << i)) {
            case 0: break;
#ifdef wxHAS_REGEX_ADVANCED
            case wxRE_ADVANCED: str += wxT(" | wxRE_ADVANCED"); break;
#endif
            case wxRE_BASIC:    str += wxT(" | wxRE_BASIC"); break;
            case wxRE_ICASE:    str += wxT(" | wxRE_ICASE"); break;
            case wxRE_NOSUB:    str += wxT(" | wxRE_NOSUB"); break;
            case wxRE_NEWLINE:  str += wxT(" | wxRE_NEWLINE"); break;
            case wxRE_NOTBOL:   str += wxT(" | wxRE_NOTBOL"); break;
            case wxRE_NOTEOL:   str += wxT(" | wxRE_NOTEOL"); break;
            default: wxFAIL; break;
        }
    }

    return wxT(" (") + str.Mid(3) + wxT(")");
}

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION(wxRegExTestSuite);

// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(wxRegExTestSuite, "wxRegExTestSuite");


#endif // wxUSE_REGEX