1//
2// RegularExpressionTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "RegularExpressionTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/RegularExpression.h"
15#include "Poco/Exception.h"
16
17
18using Poco::RegularExpression;
19using Poco::RegularExpressionException;
20
21
22RegularExpressionTest::RegularExpressionTest(const std::string& rName): CppUnit::TestCase(rName)
23{
24}
25
26
27RegularExpressionTest::~RegularExpressionTest()
28{
29}
30
31
32void RegularExpressionTest::testIndex()
33{
34 RegularExpression re("[0-9]+");
35 RegularExpression::Match match;
36 assertTrue (re.match("", 0, match) == 0);
37 assertTrue (re.match("123", 3, match) == 0);
38}
39
40
41void RegularExpressionTest::testMatch1()
42{
43 RegularExpression re("([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T\\s]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?(\\17[0-5]\\d([\\.,]\\d+)?)?([zZ]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?");//("[0-9]+");
44 assertTrue (re.match("2005-01-08T12:30:00Z"));
45 /*assertTrue (re.match("123"));
46 assertTrue (!re.match("123cd"));
47 assertTrue (!re.match("abcde"));
48 assertTrue (re.match("ab123", 2));*/
49}
50
51
52void RegularExpressionTest::testMatch2()
53{
54 RegularExpression re("[0-9]+");
55 RegularExpression::Match match;
56 assertTrue (re.match("123", 0, match) == 1);
57 assertTrue (match.offset == 0);
58 assertTrue (match.length == 3);
59
60 assertTrue (re.match("abc123def", 0, match) == 1);
61 assertTrue (match.offset == 3);
62 assertTrue (match.length == 3);
63
64 assertTrue (re.match("abcdef", 0, match) == 0);
65 assertTrue (match.offset == std::string::npos);
66 assertTrue (match.length == 0);
67
68 assertTrue (re.match("abc123def", 3, match) == 1);
69 assertTrue (match.offset == 3);
70 assertTrue (match.length == 3);
71}
72
73
74void RegularExpressionTest::testMatch3()
75{
76 RegularExpression re("[0-9]+");
77 RegularExpression::MatchVec match;
78 assertTrue (re.match("123", 0, match) == 1);
79 assertTrue (match.size() == 1);
80 assertTrue (match[0].offset == 0);
81 assertTrue (match[0].length == 3);
82
83 assertTrue (re.match("abc123def", 0, match) == 1);
84 assertTrue (match.size() == 1);
85 assertTrue (match[0].offset == 3);
86 assertTrue (match[0].length == 3);
87
88 assertTrue (re.match("abcdef", 0, match) == 0);
89 assertTrue (match.size() == 0);
90
91 assertTrue (re.match("abc123def", 3, match) == 1);
92 assertTrue (match.size() == 1);
93 assertTrue (match[0].offset == 3);
94 assertTrue (match[0].length == 3);
95}
96
97
98void RegularExpressionTest::testMatch4()
99{
100 RegularExpression re("([0-9]+) ([0-9]+)");
101 RegularExpression::MatchVec matches;
102 assertTrue (re.match("123 456", 0, matches) == 3);
103 assertTrue (matches.size() == 3);
104 assertTrue (matches[0].offset == 0);
105 assertTrue (matches[0].length == 7);
106 assertTrue (matches[1].offset == 0);
107 assertTrue (matches[1].length == 3);
108 assertTrue (matches[2].offset == 4);
109 assertTrue (matches[2].length == 3);
110
111 assertTrue (re.match("abc123 456def", 0, matches) == 3);
112 assertTrue (matches.size() == 3);
113 assertTrue (matches[0].offset == 3);
114 assertTrue (matches[0].length == 7);
115 assertTrue (matches[1].offset == 3);
116 assertTrue (matches[1].length == 3);
117 assertTrue (matches[2].offset == 7);
118 assertTrue (matches[2].length == 3);
119}
120
121
122void RegularExpressionTest::testMatch5()
123{
124 std::string digits = "0123";
125 assertTrue (RegularExpression::match(digits, "[0-9]+"));
126 std::string alphas = "abcd";
127 assertTrue (!RegularExpression::match(alphas, "[0-9]+"));
128}
129
130
131void RegularExpressionTest::testMatch6()
132{
133 RegularExpression expr("^([a-z]*)?$");
134 assertTrue (expr.match("", 0, 0));
135 assertTrue (expr.match("abcde", 0, 0));
136 assertTrue (!expr.match("123", 0, 0));
137}
138
139
140void RegularExpressionTest::testExtract()
141{
142 RegularExpression re("[0-9]+");
143 std::string str;
144 assertTrue (re.extract("123", str) == 1);
145 assertTrue (str == "123");
146
147 assertTrue (re.extract("abc123def", 0, str) == 1);
148 assertTrue (str == "123");
149
150 assertTrue (re.extract("abcdef", 0, str) == 0);
151 assertTrue (str == "");
152
153 assertTrue (re.extract("abc123def", 3, str) == 1);
154 assertTrue (str == "123");
155}
156
157
158void RegularExpressionTest::testSplit1()
159{
160 RegularExpression re("[0-9]+");
161 std::vector<std::string> strings;
162 assertTrue (re.split("123", 0, strings) == 1);
163 assertTrue (strings.size() == 1);
164 assertTrue (strings[0] == "123");
165
166 assertTrue (re.split("abc123def", 0, strings) == 1);
167 assertTrue (strings.size() == 1);
168 assertTrue (strings[0] == "123");
169
170 assertTrue (re.split("abcdef", 0, strings) == 0);
171 assertTrue (strings.empty());
172
173 assertTrue (re.split("abc123def", 3, strings) == 1);
174 assertTrue (strings.size() == 1);
175 assertTrue (strings[0] == "123");
176}
177
178
179void RegularExpressionTest::testSplit2()
180{
181 RegularExpression re("([0-9]+) ([0-9]+)");
182 std::vector<std::string> strings;
183 assertTrue (re.split("123 456", 0, strings) == 3);
184 assertTrue (strings.size() == 3);
185 assertTrue (strings[0] == "123 456");
186 assertTrue (strings[1] == "123");
187 assertTrue (strings[2] == "456");
188
189 assertTrue (re.split("abc123 456def", 0, strings) == 3);
190 assertTrue (strings.size() == 3);
191 assertTrue (strings[0] == "123 456");
192 assertTrue (strings[1] == "123");
193 assertTrue (strings[2] == "456");
194}
195
196
197void RegularExpressionTest::testSubst1()
198{
199 RegularExpression re("[0-9]+");
200 std::string s = "123";
201 assertTrue (re.subst(s, "ABC") == 1);
202 assertTrue (s == "ABC");
203 assertTrue (re.subst(s, "123") == 0);
204
205 s = "123";
206 assertTrue (re.subst(s, "AB$0CD") == 1);
207 assertTrue (s == "AB123CD");
208
209 s = "123";
210 assertTrue (re.subst(s, "AB$1CD") == 1);
211 assertTrue (s == "ABCD");
212
213 s = "123";
214 assertTrue (re.subst(s, "AB$2CD") == 1);
215 assertTrue (s == "ABCD");
216
217 s = "123";
218 assertTrue (re.subst(s, "AB$$CD") == 1);
219 assertTrue (s == "AB$$CD");
220
221 s = "123";
222 assertTrue (re.subst(s, "AB$0CD", RegularExpression::RE_NO_VARS) == 1);
223 assertTrue (s == "AB$0CD");
224}
225
226
227void RegularExpressionTest::testSubst2()
228{
229 RegularExpression re("([0-9]+) ([0-9]+)");
230 std::string s = "123 456";
231 assertTrue (re.subst(s, "$2-$1") == 1);
232 assertTrue (s == "456-123");
233}
234
235
236void RegularExpressionTest::testSubst3()
237{
238 RegularExpression re("[0-9]+");
239 std::string s = "123 456 789";
240 assertTrue (re.subst(s, "n", RegularExpression::RE_GLOBAL) == 3);
241 assertTrue (s == "n n n");
242}
243
244
245void RegularExpressionTest::testSubst4()
246{
247 RegularExpression re("[0-9]+");
248 std::string s = "ABC 123 456 789 DEF";
249 assertTrue (re.subst(s, "n", RegularExpression::RE_GLOBAL) == 3);
250 assertTrue (s == "ABC n n n DEF");
251}
252
253
254void RegularExpressionTest::testError()
255{
256 try
257 {
258 RegularExpression re("(0-9]");
259 failmsg("bad regexp - must throw exception");
260 }
261 catch (RegularExpressionException&)
262 {
263 }
264}
265
266void RegularExpressionTest::testGroup()
267{
268 RegularExpression::MatchVec matches;
269 RegularExpression re("(?P<group1>[a-z]+) (?P<group2>[0-9]+)");
270 assertTrue (re.match("abcd 1234", 0, matches) == 3);
271 assertTrue (matches[0].name == "");
272 assertTrue (matches[1].name == "group1");
273 assertTrue (matches[2].name == "group2");
274}
275
276
277void RegularExpressionTest::setUp()
278{
279}
280
281
282void RegularExpressionTest::tearDown()
283{
284}
285
286
287CppUnit::Test* RegularExpressionTest::suite()
288{
289 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RegularExpressionTest");
290
291 CppUnit_addTest(pSuite, RegularExpressionTest, testIndex);
292 CppUnit_addTest(pSuite, RegularExpressionTest, testMatch1);
293 CppUnit_addTest(pSuite, RegularExpressionTest, testMatch2);
294 CppUnit_addTest(pSuite, RegularExpressionTest, testMatch3);
295 CppUnit_addTest(pSuite, RegularExpressionTest, testMatch4);
296 CppUnit_addTest(pSuite, RegularExpressionTest, testMatch5);
297 CppUnit_addTest(pSuite, RegularExpressionTest, testMatch6);
298 CppUnit_addTest(pSuite, RegularExpressionTest, testExtract);
299 CppUnit_addTest(pSuite, RegularExpressionTest, testSplit1);
300 CppUnit_addTest(pSuite, RegularExpressionTest, testSplit2);
301 CppUnit_addTest(pSuite, RegularExpressionTest, testSubst1);
302 CppUnit_addTest(pSuite, RegularExpressionTest, testSubst2);
303 CppUnit_addTest(pSuite, RegularExpressionTest, testSubst3);
304 CppUnit_addTest(pSuite, RegularExpressionTest, testSubst4);
305 CppUnit_addTest(pSuite, RegularExpressionTest, testError);
306 CppUnit_addTest(pSuite, RegularExpressionTest, testGroup);
307
308 return pSuite;
309}
310