testArgumentParser.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <initializer_list>
  4. #include <iostream>
  5. #include <map>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include <cm/optional>
  10. #include <cm/string_view>
  11. #include <cmext/string_view>
  12. #include "cmArgumentParser.h"
  13. #include "cmArgumentParserTypes.h"
  14. namespace {
  15. struct Result : public ArgumentParser::ParseResult
  16. {
  17. bool Option1 = false;
  18. bool Option2 = false;
  19. std::string String1;
  20. cm::optional<std::string> String2;
  21. cm::optional<std::string> String3;
  22. ArgumentParser::Maybe<std::string> String4;
  23. ArgumentParser::NonEmpty<std::vector<std::string>> List1;
  24. ArgumentParser::NonEmpty<std::vector<std::string>> List2;
  25. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List3;
  26. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List4;
  27. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List5;
  28. cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> List6;
  29. std::vector<std::vector<std::string>> Multi1;
  30. std::vector<std::vector<std::string>> Multi2;
  31. cm::optional<std::vector<std::vector<std::string>>> Multi3;
  32. cm::optional<std::vector<std::vector<std::string>>> Multi4;
  33. };
  34. std::initializer_list<cm::string_view> const args = {
  35. /* clang-format off */
  36. "OPTION_1", // option
  37. // "OPTION_2", // option that is not present
  38. "STRING_1", // string arg missing value
  39. "STRING_2", "foo", "bar", // string arg + unparsed value, presence captured
  40. // "STRING_3", // string arg that is not present
  41. "STRING_4", // string arg allowed to be missing value
  42. "LIST_1", // list arg missing values
  43. "LIST_2", "foo", "bar", // list arg with 2 elems
  44. "LIST_3", "bar", // list arg ...
  45. "LIST_3", "foo", // ... with continuation
  46. "LIST_4", // list arg missing values, presence captured
  47. // "LIST_5", // list arg that is not present
  48. "LIST_6", // list arg allowed to be empty
  49. "MULTI_2", // multi list with 0 lists
  50. "MULTI_3", "foo", "bar", // multi list with first list with two elems
  51. "MULTI_3", "bar", "foo", // multi list with second list with two elems
  52. // "MULTI_4", // multi list arg that is not present
  53. /* clang-format on */
  54. };
  55. bool verifyResult(Result const& result,
  56. std::vector<std::string> const& unparsedArguments)
  57. {
  58. static std::vector<std::string> const foobar = { "foo", "bar" };
  59. static std::vector<std::string> const barfoo = { "bar", "foo" };
  60. static std::map<cm::string_view, std::string> const keywordErrors = {
  61. { "STRING_1"_s, " missing required value\n" },
  62. { "LIST_1"_s, " missing required value\n" },
  63. { "LIST_4"_s, " missing required value\n" }
  64. };
  65. #define ASSERT_TRUE(x) \
  66. do { \
  67. if (!(x)) { \
  68. std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
  69. return false; \
  70. } \
  71. } while (false)
  72. ASSERT_TRUE(!result);
  73. ASSERT_TRUE(result.Option1);
  74. ASSERT_TRUE(!result.Option2);
  75. ASSERT_TRUE(result.String1.empty());
  76. ASSERT_TRUE(result.String2);
  77. ASSERT_TRUE(*result.String2 == "foo");
  78. ASSERT_TRUE(!result.String3);
  79. ASSERT_TRUE(result.String4.empty());
  80. ASSERT_TRUE(result.List1.empty());
  81. ASSERT_TRUE(result.List2 == foobar);
  82. ASSERT_TRUE(result.List3);
  83. ASSERT_TRUE(*result.List3 == barfoo);
  84. ASSERT_TRUE(result.List4);
  85. ASSERT_TRUE(result.List4->empty());
  86. ASSERT_TRUE(!result.List5);
  87. ASSERT_TRUE(result.List6);
  88. ASSERT_TRUE(result.List6->empty());
  89. ASSERT_TRUE(result.Multi1.empty());
  90. ASSERT_TRUE(result.Multi2.size() == 1);
  91. ASSERT_TRUE(result.Multi2[0].empty());
  92. ASSERT_TRUE(result.Multi3);
  93. ASSERT_TRUE((*result.Multi3).size() == 2);
  94. ASSERT_TRUE((*result.Multi3)[0] == foobar);
  95. ASSERT_TRUE((*result.Multi3)[1] == barfoo);
  96. ASSERT_TRUE(!result.Multi4);
  97. ASSERT_TRUE(unparsedArguments.size() == 1);
  98. ASSERT_TRUE(unparsedArguments[0] == "bar");
  99. ASSERT_TRUE(result.GetKeywordErrors().size() == keywordErrors.size());
  100. for (auto const& ke : result.GetKeywordErrors()) {
  101. auto const ki = keywordErrors.find(ke.first);
  102. ASSERT_TRUE(ki != keywordErrors.end());
  103. ASSERT_TRUE(ke.second == ki->second);
  104. }
  105. return true;
  106. }
  107. bool testArgumentParserDynamic()
  108. {
  109. Result result;
  110. std::vector<std::string> unparsedArguments;
  111. static_cast<ArgumentParser::ParseResult&>(result) =
  112. cmArgumentParser<void>{}
  113. .Bind("OPTION_1"_s, result.Option1)
  114. .Bind("OPTION_2"_s, result.Option2)
  115. .Bind("STRING_1"_s, result.String1)
  116. .Bind("STRING_2"_s, result.String2)
  117. .Bind("STRING_3"_s, result.String3)
  118. .Bind("STRING_4"_s, result.String4)
  119. .Bind("LIST_1"_s, result.List1)
  120. .Bind("LIST_2"_s, result.List2)
  121. .Bind("LIST_3"_s, result.List3)
  122. .Bind("LIST_4"_s, result.List4)
  123. .Bind("LIST_5"_s, result.List5)
  124. .Bind("LIST_6"_s, result.List6)
  125. .Bind("MULTI_1"_s, result.Multi1)
  126. .Bind("MULTI_2"_s, result.Multi2)
  127. .Bind("MULTI_3"_s, result.Multi3)
  128. .Bind("MULTI_4"_s, result.Multi4)
  129. .Parse(args, &unparsedArguments);
  130. return verifyResult(result, unparsedArguments);
  131. }
  132. static auto const parserStatic = //
  133. cmArgumentParser<Result>{}
  134. .Bind("OPTION_1"_s, &Result::Option1)
  135. .Bind("OPTION_2"_s, &Result::Option2)
  136. .Bind("STRING_1"_s, &Result::String1)
  137. .Bind("STRING_2"_s, &Result::String2)
  138. .Bind("STRING_3"_s, &Result::String3)
  139. .Bind("STRING_4"_s, &Result::String4)
  140. .Bind("LIST_1"_s, &Result::List1)
  141. .Bind("LIST_2"_s, &Result::List2)
  142. .Bind("LIST_3"_s, &Result::List3)
  143. .Bind("LIST_4"_s, &Result::List4)
  144. .Bind("LIST_5"_s, &Result::List5)
  145. .Bind("LIST_6"_s, &Result::List6)
  146. .Bind("MULTI_1"_s, &Result::Multi1)
  147. .Bind("MULTI_2"_s, &Result::Multi2)
  148. .Bind("MULTI_3"_s, &Result::Multi3)
  149. .Bind("MULTI_4"_s, &Result::Multi4)
  150. /* keep semicolon on own line */;
  151. bool testArgumentParserStatic()
  152. {
  153. std::vector<std::string> unparsedArguments;
  154. Result const result = parserStatic.Parse(args, &unparsedArguments);
  155. return verifyResult(result, unparsedArguments);
  156. }
  157. bool testArgumentParserStaticBool()
  158. {
  159. std::vector<std::string> unparsedArguments;
  160. Result result;
  161. ASSERT_TRUE(parserStatic.Parse(result, args, &unparsedArguments) == false);
  162. return verifyResult(result, unparsedArguments);
  163. }
  164. } // namespace
  165. int testArgumentParser(int /*unused*/, char* /*unused*/ [])
  166. {
  167. if (!testArgumentParserDynamic()) {
  168. std::cout << "While executing testArgumentParserDynamic().\n";
  169. return -1;
  170. }
  171. if (!testArgumentParserStatic()) {
  172. std::cout << "While executing testArgumentParserStatic().\n";
  173. return -1;
  174. }
  175. if (!testArgumentParserStaticBool()) {
  176. std::cout << "While executing testArgumentParserStaticBool().\n";
  177. return -1;
  178. }
  179. return 0;
  180. }