1 | // |
2 | // GlobTest.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 "GlobTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Glob.h" |
15 | #include "Poco/File.h" |
16 | #include "Poco/Path.h" |
17 | #include <fstream> |
18 | |
19 | |
20 | using Poco::Glob; |
21 | using Poco::File; |
22 | using Poco::Path; |
23 | |
24 | |
25 | GlobTest::GlobTest(const std::string& rName): CppUnit::TestCase(rName) |
26 | { |
27 | } |
28 | |
29 | |
30 | GlobTest::~GlobTest() |
31 | { |
32 | } |
33 | |
34 | |
35 | void GlobTest::testMatchChars() |
36 | { |
37 | Glob g1("a" ); |
38 | assertTrue (g1.match("a" )); |
39 | assertTrue (!g1.match("b" )); |
40 | assertTrue (!g1.match("aa" )); |
41 | assertTrue (!g1.match("" )); |
42 | |
43 | Glob g2("ab" ); |
44 | assertTrue (g2.match("ab" )); |
45 | assertTrue (!g2.match("aab" )); |
46 | assertTrue (!g2.match("abab" )); |
47 | } |
48 | |
49 | |
50 | void GlobTest::testMatchQM() |
51 | { |
52 | Glob g1("?" ); |
53 | assertTrue (g1.match("a" )); |
54 | assertTrue (g1.match("b" )); |
55 | assertTrue (!g1.match("aa" )); |
56 | assertTrue (g1.match("." )); |
57 | |
58 | Glob g2("\\?" ); |
59 | assertTrue (g2.match("?" )); |
60 | assertTrue (!g2.match("a" )); |
61 | assertTrue (!g2.match("ab" )); |
62 | |
63 | Glob g3("a?" ); |
64 | assertTrue (g3.match("aa" )); |
65 | assertTrue (g3.match("az" )); |
66 | assertTrue (!g3.match("a" )); |
67 | assertTrue (!g3.match("aaa" )); |
68 | |
69 | Glob g4("??" ); |
70 | assertTrue (g4.match("aa" )); |
71 | assertTrue (g4.match("ab" )); |
72 | assertTrue (!g4.match("a" )); |
73 | assertTrue (!g4.match("abc" )); |
74 | |
75 | Glob g5("?a?" ); |
76 | assertTrue (g5.match("aaa" )); |
77 | assertTrue (g5.match("bac" )); |
78 | assertTrue (!g5.match("bbc" )); |
79 | assertTrue (!g5.match("ba" )); |
80 | assertTrue (!g5.match("ab" )); |
81 | |
82 | Glob g6("a\\?" ); |
83 | assertTrue (g6.match("a?" )); |
84 | assertTrue (!g6.match("az" )); |
85 | assertTrue (!g6.match("a" )); |
86 | |
87 | Glob g7("?" , Glob::GLOB_DOT_SPECIAL); |
88 | assertTrue (g7.match("a" )); |
89 | assertTrue (g7.match("b" )); |
90 | assertTrue (!g7.match("aa" )); |
91 | assertTrue (!g7.match("." )); |
92 | } |
93 | |
94 | |
95 | void GlobTest::testMatchAsterisk() |
96 | { |
97 | Glob g1("*" ); |
98 | assertTrue (g1.match("" )); |
99 | assertTrue (g1.match("a" )); |
100 | assertTrue (g1.match("ab" )); |
101 | assertTrue (g1.match("abc" )); |
102 | assertTrue (g1.match("." )); |
103 | |
104 | Glob g2("a*" ); |
105 | assertTrue (g2.match("a" )); |
106 | assertTrue (g2.match("aa" )); |
107 | assertTrue (g2.match("abc" )); |
108 | assertTrue (!g2.match("b" )); |
109 | assertTrue (!g2.match("ba" )); |
110 | |
111 | Glob g3("ab*" ); |
112 | assertTrue (g3.match("ab" )); |
113 | assertTrue (g3.match("abc" )); |
114 | assertTrue (g3.match("abab" )); |
115 | assertTrue (!g3.match("ac" )); |
116 | assertTrue (!g3.match("baab" )); |
117 | |
118 | Glob g4("*a" ); |
119 | assertTrue (g4.match("a" )); |
120 | assertTrue (g4.match("ba" )); |
121 | assertTrue (g4.match("aa" )); |
122 | assertTrue (g4.match("aaaaaa" )); |
123 | assertTrue (g4.match("bbbbba" )); |
124 | assertTrue (!g4.match("b" )); |
125 | assertTrue (!g4.match("ab" )); |
126 | assertTrue (!g4.match("aaab" )); |
127 | |
128 | Glob g5("a*a" ); |
129 | assertTrue (g5.match("aa" )); |
130 | assertTrue (g5.match("aba" )); |
131 | assertTrue (g5.match("abba" )); |
132 | assertTrue (!g5.match("aab" )); |
133 | assertTrue (!g5.match("aaab" )); |
134 | assertTrue (!g5.match("baaaa" )); |
135 | |
136 | Glob g6("a*b*c" ); |
137 | assertTrue (g6.match("abc" )); |
138 | assertTrue (g6.match("aabbcc" )); |
139 | assertTrue (g6.match("abcbbc" )); |
140 | assertTrue (g6.match("aaaabbbbcccc" )); |
141 | assertTrue (!g6.match("aaaabbbcb" )); |
142 | |
143 | Glob g7("a*b*" ); |
144 | assertTrue (g7.match("aaabbb" )); |
145 | assertTrue (g7.match("abababab" )); |
146 | assertTrue (g7.match("ab" )); |
147 | assertTrue (g7.match("aaaaab" )); |
148 | assertTrue (!g7.match("a" )); |
149 | assertTrue (!g7.match("aa" )); |
150 | assertTrue (!g7.match("aaa" )); |
151 | |
152 | Glob g8("**" ); |
153 | assertTrue (g1.match("" )); |
154 | assertTrue (g1.match("a" )); |
155 | assertTrue (g1.match("ab" )); |
156 | assertTrue (g1.match("abc" )); |
157 | |
158 | Glob g9("a\\*" ); |
159 | assertTrue (g9.match("a*" )); |
160 | assertTrue (!g9.match("aa" )); |
161 | assertTrue (!g9.match("a" )); |
162 | |
163 | Glob g10("a*\\*" ); |
164 | assertTrue (g10.match("a*" )); |
165 | assertTrue (g10.match("aaa*" )); |
166 | assertTrue (!g10.match("a" )); |
167 | assertTrue (!g10.match("aa" )); |
168 | |
169 | Glob g11("*" , Glob::GLOB_DOT_SPECIAL); |
170 | assertTrue (g11.match("" )); |
171 | assertTrue (g11.match("a" )); |
172 | assertTrue (g11.match("ab" )); |
173 | assertTrue (g11.match("abc" )); |
174 | assertTrue (!g11.match("." )); |
175 | } |
176 | |
177 | |
178 | void GlobTest::testMatchRange() |
179 | { |
180 | Glob g1("[a]" ); |
181 | assertTrue (g1.match("a" )); |
182 | assertTrue (!g1.match("b" )); |
183 | assertTrue (!g1.match("aa" )); |
184 | |
185 | Glob g2("[ab]" ); |
186 | assertTrue (g2.match("a" )); |
187 | assertTrue (g2.match("b" )); |
188 | assertTrue (!g2.match("c" )); |
189 | assertTrue (!g2.match("ab" )); |
190 | |
191 | Glob g3("[abc]" ); |
192 | assertTrue (g3.match("a" )); |
193 | assertTrue (g3.match("b" )); |
194 | assertTrue (g3.match("c" )); |
195 | assertTrue (!g3.match("ab" )); |
196 | |
197 | Glob g4("[a-z]" ); |
198 | assertTrue (g4.match("a" )); |
199 | assertTrue (g4.match("z" )); |
200 | assertTrue (!g4.match("A" )); |
201 | |
202 | Glob g5("[!a]" ); |
203 | assertTrue (g5.match("b" )); |
204 | assertTrue (g5.match("c" )); |
205 | assertTrue (!g5.match("a" )); |
206 | assertTrue (!g5.match("bb" )); |
207 | |
208 | Glob g6("[!a-z]" ); |
209 | assertTrue (g6.match("A" )); |
210 | assertTrue (!g6.match("a" )); |
211 | assertTrue (!g6.match("z" )); |
212 | |
213 | Glob g7("[0-9a-zA-Z_]" ); |
214 | assertTrue (g7.match("0" )); |
215 | assertTrue (g7.match("1" )); |
216 | assertTrue (g7.match("8" )); |
217 | assertTrue (g7.match("9" )); |
218 | assertTrue (g7.match("a" )); |
219 | assertTrue (g7.match("b" )); |
220 | assertTrue (g7.match("z" )); |
221 | assertTrue (g7.match("A" )); |
222 | assertTrue (g7.match("Z" )); |
223 | assertTrue (g7.match("_" )); |
224 | assertTrue (!g7.match("-" )); |
225 | |
226 | Glob g8("[1-3]" ); |
227 | assertTrue (g8.match("1" )); |
228 | assertTrue (g8.match("2" )); |
229 | assertTrue (g8.match("3" )); |
230 | assertTrue (!g8.match("0" )); |
231 | assertTrue (!g8.match("4" )); |
232 | |
233 | Glob g9("[!1-3]" ); |
234 | assertTrue (g9.match("0" )); |
235 | assertTrue (g9.match("4" )); |
236 | assertTrue (!g9.match("1" )); |
237 | assertTrue (!g9.match("2" )); |
238 | assertTrue (!g9.match("3" )); |
239 | |
240 | Glob g10("[\\!a]" ); |
241 | assertTrue (g10.match("!" )); |
242 | assertTrue (g10.match("a" )); |
243 | assertTrue (!g10.match("x" )); |
244 | |
245 | Glob g11("[a\\-c]" ); |
246 | assertTrue (g11.match("a" )); |
247 | assertTrue (g11.match("c" )); |
248 | assertTrue (g11.match("-" )); |
249 | assertTrue (!g11.match("b" )); |
250 | |
251 | Glob g12("[\\]]" ); |
252 | assertTrue (g12.match("]" )); |
253 | assertTrue (!g12.match("[" )); |
254 | |
255 | Glob g13("[[\\]]" ); |
256 | assertTrue (g13.match("[" )); |
257 | assertTrue (g13.match("]" )); |
258 | assertTrue (!g13.match("x" )); |
259 | |
260 | Glob g14("\\[]" ); |
261 | assertTrue (g14.match("[]" )); |
262 | assertTrue (!g14.match("[[" )); |
263 | |
264 | Glob g15("a[bc]" ); |
265 | assertTrue (g15.match("ab" )); |
266 | assertTrue (g15.match("ac" )); |
267 | assertTrue (!g15.match("a" )); |
268 | assertTrue (!g15.match("aa" )); |
269 | |
270 | Glob g16("[ab]c" ); |
271 | assertTrue (g16.match("ac" )); |
272 | assertTrue (g16.match("bc" )); |
273 | assertTrue (!g16.match("a" )); |
274 | assertTrue (!g16.match("b" )); |
275 | assertTrue (!g16.match("c" )); |
276 | assertTrue (!g16.match("aa" )); |
277 | } |
278 | |
279 | |
280 | void GlobTest::testMisc() |
281 | { |
282 | Glob g1("*.cpp" ); |
283 | assertTrue (g1.match("Glob.cpp" )); |
284 | assertTrue (!g1.match("Glob.h" )); |
285 | |
286 | Glob g2("*.[hc]" ); |
287 | assertTrue (g2.match("foo.c" )); |
288 | assertTrue (g2.match("foo.h" )); |
289 | assertTrue (!g2.match("foo.i" )); |
290 | |
291 | Glob g3("*.*" ); |
292 | assertTrue (g3.match("foo.cpp" )); |
293 | assertTrue (g3.match("foo.h" )); |
294 | assertTrue (g3.match("foo." )); |
295 | assertTrue (!g3.match("foo" )); |
296 | |
297 | Glob g4("File*.?pp" ); |
298 | assertTrue (g4.match("File.hpp" )); |
299 | assertTrue (g4.match("File.cpp" )); |
300 | assertTrue (g4.match("Filesystem.hpp" )); |
301 | assertTrue (!g4.match("File.h" )); |
302 | |
303 | Glob g5("File*.[ch]*" ); |
304 | assertTrue (g5.match("File.hpp" )); |
305 | assertTrue (g5.match("File.cpp" )); |
306 | assertTrue (g5.match("Filesystem.hpp" )); |
307 | assertTrue (g5.match("File.h" )); |
308 | assertTrue (g5.match("Filesystem.cp" )); |
309 | } |
310 | |
311 | |
312 | void GlobTest::testCaseless() |
313 | { |
314 | Glob g1("*.cpp" , Glob::GLOB_CASELESS); |
315 | assertTrue (g1.match("Glob.cpp" )); |
316 | assertTrue (!g1.match("Glob.h" )); |
317 | assertTrue (g1.match("Glob.CPP" )); |
318 | assertTrue (!g1.match("Glob.H" )); |
319 | |
320 | Glob g2("*.[hc]" , Glob::GLOB_CASELESS); |
321 | assertTrue (g2.match("foo.c" )); |
322 | assertTrue (g2.match("foo.h" )); |
323 | assertTrue (!g2.match("foo.i" )); |
324 | assertTrue (g2.match("foo.C" )); |
325 | assertTrue (g2.match("foo.H" )); |
326 | assertTrue (!g2.match("foo.I" )); |
327 | |
328 | Glob g4("File*.?pp" , Glob::GLOB_CASELESS); |
329 | assertTrue (g4.match("file.hpp" )); |
330 | assertTrue (g4.match("FILE.CPP" )); |
331 | assertTrue (g4.match("filesystem.hpp" )); |
332 | assertTrue (g4.match("FILESYSTEM.HPP" )); |
333 | assertTrue (!g4.match("FILE.H" )); |
334 | assertTrue (!g4.match("file.h" )); |
335 | |
336 | Glob g5("File*.[ch]*" , Glob::GLOB_CASELESS); |
337 | assertTrue (g5.match("file.hpp" )); |
338 | assertTrue (g5.match("FILE.HPP" )); |
339 | assertTrue (g5.match("file.cpp" )); |
340 | assertTrue (g5.match("FILE.CPP" )); |
341 | assertTrue (g5.match("filesystem.hpp" )); |
342 | assertTrue (g5.match("FILESYSTEM.HPP" )); |
343 | assertTrue (g5.match("file.h" )); |
344 | assertTrue (g5.match("FILE.H" )); |
345 | assertTrue (g5.match("filesystem.cp" )); |
346 | assertTrue (g5.match("FILESYSTEM.CP" )); |
347 | |
348 | Glob g6("[abc]" , Glob::GLOB_CASELESS); |
349 | assertTrue (g6.match("a" )); |
350 | assertTrue (g6.match("b" )); |
351 | assertTrue (g6.match("c" )); |
352 | assertTrue (g6.match("A" )); |
353 | assertTrue (g6.match("B" )); |
354 | assertTrue (g6.match("C" )); |
355 | |
356 | Glob g7("[a-f]" , Glob::GLOB_CASELESS); |
357 | assertTrue (g7.match("a" )); |
358 | assertTrue (g7.match("b" )); |
359 | assertTrue (g7.match("f" )); |
360 | assertTrue (!g7.match("g" )); |
361 | assertTrue (g7.match("A" )); |
362 | assertTrue (g7.match("B" )); |
363 | assertTrue (g7.match("F" )); |
364 | assertTrue (!g7.match("G" )); |
365 | |
366 | Glob g8("[A-F]" , Glob::GLOB_CASELESS); |
367 | assertTrue (g8.match("a" )); |
368 | assertTrue (g8.match("b" )); |
369 | assertTrue (g8.match("f" )); |
370 | assertTrue (!g8.match("g" )); |
371 | assertTrue (g8.match("A" )); |
372 | assertTrue (g8.match("B" )); |
373 | assertTrue (g8.match("F" )); |
374 | assertTrue (!g8.match("G" )); |
375 | } |
376 | |
377 | |
378 | void GlobTest::testGlob() |
379 | { |
380 | createFile("globtest/Makefile" ); |
381 | createFile("globtest/.hidden" ); |
382 | createFile("globtest/include/one.h" ); |
383 | createFile("globtest/include/two.h" ); |
384 | createFile("globtest/src/one.c" ); |
385 | createFile("globtest/src/two.c" ); |
386 | createFile("globtest/src/main.c" ); |
387 | createFile("globtest/testsuite/src/test.h" ); |
388 | createFile("globtest/testsuite/src/test.c" ); |
389 | createFile("globtest/testsuite/src/main.c" ); |
390 | |
391 | std::set<std::string> files; |
392 | Glob::glob("globtest/*" , files); |
393 | translatePaths(files); |
394 | assertTrue (files.size() == 5); |
395 | assertTrue (files.find("globtest/Makefile" ) != files.end()); |
396 | assertTrue (files.find("globtest/.hidden" ) != files.end()); |
397 | assertTrue (files.find("globtest/include/" ) != files.end()); |
398 | assertTrue (files.find("globtest/src/" ) != files.end()); |
399 | assertTrue (files.find("globtest/testsuite/" ) != files.end()); |
400 | |
401 | files.clear(); |
402 | Glob::glob("GlobTest/*" , files, Glob::GLOB_CASELESS); |
403 | translatePaths(files); |
404 | assertTrue (files.size() == 5); |
405 | assertTrue (files.find("globtest/Makefile" ) != files.end()); |
406 | assertTrue (files.find("globtest/.hidden" ) != files.end()); |
407 | assertTrue (files.find("globtest/include/" ) != files.end()); |
408 | assertTrue (files.find("globtest/src/" ) != files.end()); |
409 | assertTrue (files.find("globtest/testsuite/" ) != files.end()); |
410 | |
411 | files.clear(); |
412 | Glob::glob("globtest/*/*.[hc]" , files); |
413 | translatePaths(files); |
414 | assertTrue (files.size() == 5); |
415 | assertTrue (files.find("globtest/include/one.h" ) != files.end()); |
416 | assertTrue (files.find("globtest/include/two.h" ) != files.end()); |
417 | assertTrue (files.find("globtest/src/one.c" ) != files.end()); |
418 | assertTrue (files.find("globtest/src/one.c" ) != files.end()); |
419 | assertTrue (files.find("globtest/src/main.c" ) != files.end()); |
420 | |
421 | files.clear(); |
422 | Glob::glob("gl?bt?st/*/*/*.c" , files); |
423 | translatePaths(files); |
424 | assertTrue (files.size() == 2); |
425 | assertTrue (files.find("globtest/testsuite/src/test.c" ) != files.end()); |
426 | assertTrue (files.find("globtest/testsuite/src/main.c" ) != files.end()); |
427 | |
428 | files.clear(); |
429 | Glob::glob("Gl?bT?st/*/*/*.C" , files, Glob::GLOB_CASELESS); |
430 | translatePaths(files); |
431 | assertTrue (files.size() == 2); |
432 | assertTrue (files.find("globtest/testsuite/src/test.c" ) != files.end()); |
433 | assertTrue (files.find("globtest/testsuite/src/main.c" ) != files.end()); |
434 | |
435 | files.clear(); |
436 | Glob::glob("globtest/*/src/*" , files); |
437 | translatePaths(files); |
438 | assertTrue (files.size() == 3); |
439 | assertTrue (files.find("globtest/testsuite/src/test.h" ) != files.end()); |
440 | assertTrue (files.find("globtest/testsuite/src/test.c" ) != files.end()); |
441 | assertTrue (files.find("globtest/testsuite/src/main.c" ) != files.end()); |
442 | |
443 | files.clear(); |
444 | Glob::glob("globtest/*/" , files); |
445 | translatePaths(files); |
446 | assertTrue (files.size() == 3); |
447 | assertTrue (files.find("globtest/include/" ) != files.end()); |
448 | assertTrue (files.find("globtest/src/" ) != files.end()); |
449 | assertTrue (files.find("globtest/testsuite/" ) != files.end()); |
450 | |
451 | files.clear(); |
452 | Glob::glob("globtest/testsuite/src/*" , "globtest/testsuite/" , files); |
453 | translatePaths(files); |
454 | assertTrue (files.size() == 3); |
455 | assertTrue (files.find("globtest/testsuite/src/test.h" ) != files.end()); |
456 | assertTrue (files.find("globtest/testsuite/src/test.c" ) != files.end()); |
457 | assertTrue (files.find("globtest/testsuite/src/main.c" ) != files.end()); |
458 | |
459 | #if !defined(_WIN32_WCE) |
460 | // won't work if current directory is root dir |
461 | files.clear(); |
462 | Glob::glob("globtest/../*/testsuite/*/" , files); |
463 | translatePaths(files); |
464 | assertTrue (files.size() == 1); |
465 | #endif |
466 | |
467 | File dir("globtest" ); |
468 | dir.remove(true); |
469 | } |
470 | |
471 | |
472 | void GlobTest::testMatchEmptyPattern() |
473 | { |
474 | // Run the empty pattern against a number of subjects with all different match options |
475 | const std::string empty; |
476 | |
477 | assertTrue (!Glob(empty, Glob::GLOB_DEFAULT).match("subject" )); |
478 | assertTrue (Glob(empty, Glob::GLOB_DEFAULT).match(empty)); |
479 | |
480 | assertTrue (!Glob(empty, Glob::GLOB_DOT_SPECIAL).match("subject" )); |
481 | assertTrue (Glob(empty, Glob::GLOB_DOT_SPECIAL).match(empty)); |
482 | |
483 | assertTrue (!Glob(empty, Glob::GLOB_CASELESS).match("subject" )); |
484 | assertTrue (Glob(empty, Glob::GLOB_CASELESS).match(empty)); |
485 | } |
486 | |
487 | |
488 | void GlobTest::createFile(const std::string& path) |
489 | { |
490 | Path p(path, Path::PATH_UNIX); |
491 | File dir(p.parent()); |
492 | dir.createDirectories(); |
493 | std::ofstream ostr(path.c_str()); |
494 | ostr << path << std::endl; |
495 | } |
496 | |
497 | |
498 | void GlobTest::translatePaths(std::set<std::string>& paths) |
499 | { |
500 | std::set<std::string> translated; |
501 | for (std::set<std::string>::const_iterator it = paths.begin(); it != paths.end(); ++it) |
502 | { |
503 | Path p(*it); |
504 | std::string tp(p.toString(Path::PATH_UNIX)); |
505 | translated.insert(tp); |
506 | } |
507 | paths = translated; |
508 | } |
509 | |
510 | |
511 | void GlobTest::setUp() |
512 | { |
513 | } |
514 | |
515 | |
516 | void GlobTest::tearDown() |
517 | { |
518 | } |
519 | |
520 | |
521 | CppUnit::Test* GlobTest::suite() |
522 | { |
523 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("GlobTest" ); |
524 | |
525 | CppUnit_addTest(pSuite, GlobTest, testMatchChars); |
526 | CppUnit_addTest(pSuite, GlobTest, testMatchQM); |
527 | CppUnit_addTest(pSuite, GlobTest, testMatchAsterisk); |
528 | CppUnit_addTest(pSuite, GlobTest, testMatchRange); |
529 | CppUnit_addTest(pSuite, GlobTest, testMisc); |
530 | CppUnit_addTest(pSuite, GlobTest, testCaseless); |
531 | CppUnit_addTest(pSuite, GlobTest, testGlob); |
532 | CppUnit_addTest(pSuite, GlobTest, testMatchEmptyPattern); |
533 | |
534 | return pSuite; |
535 | } |
536 | |