GlobTest.cpp 14 KB

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