ctkCommandLineParserTest1.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // Qt includes
  2. #include <QDebug>
  3. // CTK includes
  4. #include "ctkCommandLineParser.h"
  5. // STD includes
  6. #include <cstdlib>
  7. int ctkCommandLineParserTest1(int, char*[])
  8. {
  9. // Test1 - Check if unparsedArguments() worked as expected
  10. QStringList arguments1;
  11. arguments1 << "ctkCommandLineParserTest1";
  12. arguments1 << "--test-bool";
  13. arguments1 << "--test-string";
  14. arguments1 << "ctkrocks";
  15. ctkCommandLineParser parser1;
  16. if (!parser1.parseArguments(arguments1))
  17. {
  18. qCritical() << "Test1 - Failed to parse arguments";
  19. return EXIT_FAILURE;
  20. }
  21. QStringList expectedUnparsedArguments1;
  22. expectedUnparsedArguments1 << "--test-bool" << "--test-string" << "ctkrocks";
  23. if (parser1.unparsedArguments() != expectedUnparsedArguments1)
  24. {
  25. qCritical() << "unparsedArguments:" << parser1.unparsedArguments();
  26. qCritical() << "expectedUnparsedArguments1:" << expectedUnparsedArguments1;
  27. return EXIT_FAILURE;
  28. }
  29. // Test2 - Check if addBooleanArgument() worked as expected
  30. QStringList arguments2;
  31. arguments2 << "ctkCommandLineParserTest1";
  32. arguments2 << "--test-bool";
  33. arguments2 << "--test-string";
  34. arguments2 << "ctkrocks";
  35. bool testBool = false;
  36. ctkCommandLineParser parser2;
  37. parser2.addBooleanArgument("--test-bool", 0, &testBool, "This is a test bool", false);
  38. if (!parser2.parseArguments(arguments2))
  39. {
  40. qCritical() << "Test2 - Failed to parse arguments";
  41. return EXIT_FAILURE;
  42. }
  43. QStringList expectedUnparsedArguments2;
  44. expectedUnparsedArguments2 << "--test-string" << "ctkrocks";
  45. if (parser2.unparsedArguments() != expectedUnparsedArguments2)
  46. {
  47. qCritical() << "Test2 - Failed - unparsedArguments:" << parser2.unparsedArguments()
  48. << ", expectedUnparsedArguments2:" << expectedUnparsedArguments2;
  49. return EXIT_FAILURE;
  50. }
  51. if (!testBool)
  52. {
  53. qCritical() << "Test2 - Failed to parse --test-bool";
  54. return EXIT_FAILURE;
  55. }
  56. // Test3 - check if addStringArgument(), addIntegerArgument() and addStringListArgument() works
  57. QStringList arguments3;
  58. arguments3 << "ctkCommandLineParserTest1";
  59. arguments3 << "--test-string" << "TestingIsGood";
  60. arguments3 << "--test-string2"<< "CTKSuperRocks";
  61. arguments3 << "--test-integer"<< "-3";
  62. arguments3 << "--test-stringlist"<< "item1" << "item2" << "item3";
  63. QString testString;
  64. QString testString2;
  65. int testInteger = 1;
  66. QStringList testStringlist;
  67. ctkCommandLineParser parser3;
  68. parser3.addStringArgument("--test-string", 0, &testString, "This is a test string");
  69. parser3.addStringArgument("--test-string2", 0, &testString2, "This is a test string2", "CTKGood");
  70. parser3.addIntegerArgument("--test-integer", 0, &testInteger, "This is a test integer");
  71. parser3.addStringListArgument("--test-stringlist", 0, &testStringlist,
  72. "This is a test stringlist");
  73. if (!parser3.parseArguments(arguments3))
  74. {
  75. qCritical() << "Test3 - Failed to parse arguments";
  76. return EXIT_FAILURE;
  77. }
  78. QString expectedTestString = "TestingIsGood";
  79. if (testString != expectedTestString)
  80. {
  81. qCritical() << "Test3 - Failed - testString" << testString
  82. << ", expectedTestString" << expectedTestString;
  83. return EXIT_FAILURE;
  84. }
  85. QString expectedTestString2 = "CTKSuperRocks";
  86. if (testString2 != expectedTestString2)
  87. {
  88. qCritical() << "Test3 - Failed - testString2" << testString2
  89. << ", expectedTestString2" << expectedTestString2;
  90. return EXIT_FAILURE;
  91. }
  92. int expectedTestInteger = -3;
  93. if (testInteger != expectedTestInteger)
  94. {
  95. qCritical() << "Test3 - Failed - testInteger" << testInteger
  96. << ", expectedTestInteger" << expectedTestInteger;
  97. return EXIT_FAILURE;
  98. }
  99. QStringList expectedTestStringlist;
  100. expectedTestStringlist << "item1" << "item2" << "item3";
  101. if (testStringlist != expectedTestStringlist)
  102. {
  103. qCritical() << "Test3 - Failed - testStringlist" << testStringlist
  104. << ", expectedTestStringlist" << expectedTestStringlist;
  105. return EXIT_FAILURE;
  106. }
  107. // Test4 - check if helpText() works as expected
  108. ctkCommandLineParser parser4;
  109. QString helpString;
  110. QString helpString1;
  111. QString helpString2;
  112. QString helpString3;
  113. parser4.addStringArgument("--help-string", 0, &helpString, "This is an help string");
  114. parser4.addStringArgument("--help-string-med", 0, &helpString1, "");
  115. parser4.addStringArgument("--help-string-long", "-hs2", &helpString2, "This is an help string too !");
  116. parser4.addStringArgument(0, "-hs3", &helpString3, "This is an help string too for sure !?");
  117. QString expectedHelpString;
  118. QTextStream streamExpectedHelpString(&expectedHelpString);
  119. streamExpectedHelpString << " --help-string..........This is an help string\n"
  120. << " --help-string-med\n"
  121. << " -hs2\n"
  122. << " --help-string-long.....This is an help string too !\n"
  123. << " -hs3...................This is an help string too for sure !?\n";
  124. if (expectedHelpString != parser4.helpText('.'))
  125. {
  126. qCritical() << "Test4 - Problem with helpText('.') - helpText:\n" << parser4.helpText('.')
  127. << ", expectedHelpString:\n" << expectedHelpString;
  128. return EXIT_FAILURE;
  129. }
  130. QString expectedHelpString2;
  131. QTextStream streamExpectedHelpString2(&expectedHelpString2);
  132. streamExpectedHelpString2 << " --help-string This is an help string\n"
  133. << " --help-string-med\n"
  134. << " -hs2\n"
  135. << " --help-string-long This is an help string too !\n"
  136. << " -hs3 This is an help string too for sure !?\n";
  137. if (expectedHelpString2 != parser4.helpText())
  138. {
  139. qCritical() << "Test4 - Problem with helpText() - helpText:\n" << parser4.helpText()
  140. << ", expectedHelpString2:\n" << expectedHelpString2;
  141. return EXIT_FAILURE;
  142. }
  143. // Test5 - check if setExactMatchRegularExpression() works as expected
  144. ctkCommandLineParser parser5;
  145. if (parser5.setExactMatchRegularExpression("--unknown",".*", "invalid"))
  146. {
  147. qCritical() << "Test5 - Problem with setExactMatchRegularExpression(shortOrLongArg) - "
  148. "The function should return false if an invalid argument is passed";
  149. return EXIT_FAILURE;
  150. }
  151. bool test5Bool = false;
  152. QString test5String;
  153. int test5Int = 0;
  154. QStringList test5List;
  155. parser5.addStringListArgument("--list", 0, &test5List, "Test5 list");
  156. if (!parser5.setExactMatchRegularExpression("--list","item[0-9]",
  157. "Element of the form item[0-9] are expected."))
  158. {
  159. qCritical() << "Test5 - Problem with setExactMatchRegularExpression(StringListArg)";
  160. return EXIT_FAILURE;
  161. }
  162. parser5.addStringArgument("--string", 0, &test5String, "Test5 string");
  163. if (!parser5.setExactMatchRegularExpression("--string","ctkStop|ctkStart",
  164. "ctkStop or ctkStart is expected."))
  165. {
  166. qCritical() << "Test5 - Problem with setExactMatchRegularExpression(StringArg)";
  167. return EXIT_FAILURE;
  168. }
  169. parser5.addBooleanArgument("--bool", 0, &test5Bool, "Test5 bool");
  170. if (parser5.setExactMatchRegularExpression("--bool",".*", "invalid"))
  171. {
  172. qCritical() << "Test5 - Problem with setExactMatchRegularExpression(BooleanArg) - "
  173. "The function should return false if a boolean argument is passed";
  174. return EXIT_FAILURE;
  175. }
  176. parser5.addIntegerArgument("--int", 0, &test5Int, "Test5 int");
  177. if (!parser5.setExactMatchRegularExpression("--int","[1-3]",
  178. "Value 1, 2 or 3 is expected."))
  179. {
  180. qCritical() << "Test5 - Problem with setExactMatchRegularExpression(IntegerArg)";
  181. return EXIT_FAILURE;
  182. }
  183. QStringList arguments5;
  184. arguments5 << "ctkCommandLineParserTest1";
  185. arguments5 << "--string"<< "ctkStop";
  186. arguments5 << "--int"<< "2";
  187. arguments5 << "--list"<< "item2" << "item3";
  188. if (!parser5.parseArguments(arguments5))
  189. {
  190. qCritical() << "Test5 - Failed to parse arguments";
  191. return EXIT_FAILURE;
  192. }
  193. arguments5.clear();
  194. arguments5 << "ctkCommandLineParserTest1";
  195. arguments5 << "--string"<< "ctkStopp";
  196. arguments5 << "--int"<< "2";
  197. arguments5 << "--list"<< "item2" << "item3";
  198. if (parser5.parseArguments(arguments5))
  199. {
  200. qCritical() << "Test5 - parseArguments() should return False - 'ctkStopp' isn't a valid string";
  201. return EXIT_FAILURE;
  202. }
  203. QString expectedErrorString =
  204. "Value(s) associated with argument --string are incorrect."
  205. " ctkStop or ctkStart is expected.";
  206. if(expectedErrorString != parser5.errorString())
  207. {
  208. qCritical() << "Test5 - Failed - expectedErrorString" << expectedErrorString
  209. << ", parser5.errorString()" << parser5.errorString();
  210. return EXIT_FAILURE;
  211. }
  212. arguments5.clear();
  213. arguments5 << "ctkCommandLineParserTest1";
  214. arguments5 << "--string"<< "ctkStop";
  215. arguments5 << "--int"<< "4";
  216. arguments5 << "--list"<< "item2" << "item3";
  217. if (parser5.parseArguments(arguments5))
  218. {
  219. qCritical() << "Test5 - parseArguments() should return False - '4' isn't a valid int";
  220. return EXIT_FAILURE;
  221. }
  222. QString expectedErrorString2 =
  223. "Value(s) associated with argument --int are incorrect."
  224. " Value 1, 2 or 3 is expected.";
  225. if(expectedErrorString2 != parser5.errorString())
  226. {
  227. qCritical() << "Test5 - Failed - expectedErrorString2" << expectedErrorString2
  228. << ", parser5.errorString()" << parser5.errorString();
  229. return EXIT_FAILURE;
  230. }
  231. arguments5.clear();
  232. arguments5 << "ctkCommandLineParserTest1";
  233. arguments5 << "--string"<< "ctkStop";
  234. arguments5 << "--int"<< "2";
  235. arguments5 << "--list"<< "item2" << "item10";
  236. if (parser5.parseArguments(arguments5))
  237. {
  238. qCritical() << "Test5 - parseArguments() should return False "
  239. "- 'item10' isn't a valid list element";
  240. return EXIT_FAILURE;
  241. }
  242. QString expectedErrorString3 =
  243. "Value(s) associated with argument --list are incorrect."
  244. " Element of the form item[0-9] are expected.";
  245. if(expectedErrorString3 != parser5.errorString())
  246. {
  247. qCritical() << "Test5 - Failed - expectedErrorString3" << expectedErrorString3
  248. << ", parser5.errorString()" << parser5.errorString();
  249. return EXIT_FAILURE;
  250. }
  251. // Test6 - Check if the parser handle the case when value of parameter is omitted
  252. ctkCommandLineParser parser6;
  253. QString test6String;
  254. bool test6Bool = false;
  255. parser6.addStringArgument("--string", 0, &test6String, "This is a string");
  256. parser6.addBooleanArgument("--bool", 0, &test6Bool, "This is a bool");
  257. QStringList arguments6;
  258. arguments6 << "ctkCommandLineParserTest1"
  259. << "--string";
  260. // since --string is missing a parameter, parseArgument is expected to fail
  261. if (parser6.parseArguments(arguments6))
  262. {
  263. qCritical() << "Test6 - Problem with parseArguments()";
  264. return EXIT_FAILURE;
  265. }
  266. // Expected ignore argument for Test7, 8. 9 and 10
  267. QStringList expectedUnparsedArguments;
  268. expectedUnparsedArguments << "--ignoreint"<< "2" << "--ignorelist"<< "item1" << "item2";
  269. // Test7 - Check if addBooleanArgument and ignore_rest=true works as expected
  270. ctkCommandLineParser parser7;
  271. bool test7Bool = false;
  272. parser7.addBooleanArgument("--bool", 0, &test7Bool, "This is a boolean",
  273. false /*defaultValue*/, true /* ignoreRest*/);
  274. QStringList arguments7;
  275. arguments7 << "ctkCommandLineParserTest1";
  276. arguments7 << "--bool";
  277. arguments7 << expectedUnparsedArguments;
  278. if (!parser7.parseArguments(arguments7))
  279. {
  280. qCritical() << "Test7 - Failed to parse arguments";
  281. return EXIT_FAILURE;
  282. }
  283. bool expectedTest7Bool = true;
  284. if (test7Bool != expectedTest7Bool)
  285. {
  286. qCritical() << "Test7 - Failed - test7Bool" << test7Bool
  287. << ", expectedTest7Bool" << expectedTest7Bool;
  288. return EXIT_FAILURE;
  289. }
  290. if (parser7.unparsedArguments() != expectedUnparsedArguments)
  291. {
  292. qCritical() << "Test7 - Failed - expectedUnparsedArguments " << expectedUnparsedArguments
  293. << ", parser7.unparsedArguments" << parser7.unparsedArguments();
  294. return EXIT_FAILURE;
  295. }
  296. // Test8 - Check if addStringArgument and ignore_rest=true works as expected
  297. ctkCommandLineParser parser8;
  298. QString test8String;
  299. parser8.addStringArgument("--string", 0, &test8String, "This is a string",
  300. QString() /*defaultValue*/, true /* ignoreRest*/);
  301. QStringList arguments8;
  302. arguments8 << "ctkCommandLineParserTest1";
  303. arguments8 << "--string" << "ctk";
  304. arguments8 << expectedUnparsedArguments;
  305. if (!parser8.parseArguments(arguments8))
  306. {
  307. qCritical() << "Test8 - Failed to parse arguments";
  308. return EXIT_FAILURE;
  309. }
  310. QString expectedTest8String = "ctk";
  311. if (test8String != expectedTest8String)
  312. {
  313. qCritical() << "Test8 - Failed - test8String" << test8String
  314. << ", expectedTest8String" << expectedTest8String;
  315. return EXIT_FAILURE;
  316. }
  317. if (parser8.unparsedArguments() != expectedUnparsedArguments)
  318. {
  319. qCritical() << "Test8 - Failed - expectedUnparsedArguments " << expectedUnparsedArguments
  320. << ", parser8.unparsedArguments" << parser8.unparsedArguments();
  321. return EXIT_FAILURE;
  322. }
  323. // Test9 - Check if addIntegerArgument and ignore_rest=true works as expected
  324. ctkCommandLineParser parser9;
  325. int test9Int = 0;
  326. parser9.addIntegerArgument("--integer", 0, &test9Int, "This is an integer",
  327. 0 /*defaultValue*/, true /* ignoreRest*/);
  328. QStringList arguments9;
  329. arguments9 << "ctkCommandLineParserTest1";
  330. arguments9 << "--integer" << "74";
  331. arguments9 << expectedUnparsedArguments;
  332. if (!parser9.parseArguments(arguments9))
  333. {
  334. qCritical() << "Test9 - Failed to parse arguments";
  335. return EXIT_FAILURE;
  336. }
  337. int expectedTest9Int = 74;
  338. if (test9Int != expectedTest9Int)
  339. {
  340. qCritical() << "Test9 - Failed - test9Int" << test9Int
  341. << ", expectedTest9Int" << expectedTest9Int;
  342. return EXIT_FAILURE;
  343. }
  344. if (parser9.unparsedArguments() != expectedUnparsedArguments)
  345. {
  346. qCritical() << "Test9 - Failed - expectedUnparsedArguments " << expectedUnparsedArguments
  347. << ", parser9.unparsedArguments" << parser9.unparsedArguments();
  348. return EXIT_FAILURE;
  349. }
  350. // Test10 - Check if argumentParsed works as expected
  351. ctkCommandLineParser parser10;
  352. bool test10bool = false;
  353. parser10.addBooleanArgument("--bool", 0, &test10bool, "This is a boolean");
  354. // Since parseArguments hasn't been called, argumentParsed should return False
  355. if(parser10.argumentParsed("--bool"))
  356. {
  357. qCritical() << "Test10 - Problem with argumentParsed() - Should return False";
  358. return EXIT_FAILURE;
  359. }
  360. QStringList arguments10;
  361. arguments10 << "ctkCommandLineParserTest1";
  362. arguments10 << "--bool";
  363. if (!parser10.parseArguments(arguments10))
  364. {
  365. qCritical() << "Test10 - Failed to parse arguments.";
  366. return EXIT_FAILURE;
  367. }
  368. if(parser10.argumentParsed("--bool-notadded"))
  369. {
  370. qCritical() << "Test10 - Problem with argumentParsed() - "
  371. "Should return False since argument '--bool-notadded' hasn't been added.";
  372. return EXIT_FAILURE;
  373. }
  374. if(!parser10.argumentParsed("--bool"))
  375. {
  376. qCritical() << "Test10 - Problem with argumentParsed() - Should return True";
  377. return EXIT_FAILURE;
  378. }
  379. return EXIT_SUCCESS;
  380. }