Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Tests / CMakeLib / testArgumentParser.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3
4 #include <functional>
5 #include <initializer_list>
6 #include <iostream>
7 #include <map>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include <cm/optional>
13 #include <cm/string_view>
14 #include <cmext/string_view>
15
16 #include "cmArgumentParser.h"
17 #include "cmArgumentParserTypes.h"
18
19 namespace {
20
21 struct Result : public ArgumentParser::ParseResult
22 {
23   bool Option1 = false;
24   bool Option2 = false;
25
26   std::string String1;
27   cm::optional<std::string> String2;
28   cm::optional<std::string> String3;
29   ArgumentParser::Maybe<std::string> String4;
30   ArgumentParser::NonEmpty<std::string> String5;
31   ArgumentParser::NonEmpty<std::string> String6;
32
33   ArgumentParser::NonEmpty<std::vector<std::string>> List1;
34   ArgumentParser::NonEmpty<std::vector<std::string>> List2;
35   cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List3;
36   cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List4;
37   cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> List5;
38   cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> List6;
39
40   std::vector<std::vector<std::string>> Multi1;
41   std::vector<std::vector<std::string>> Multi2;
42   cm::optional<std::vector<std::vector<std::string>>> Multi3;
43   cm::optional<std::vector<std::vector<std::string>>> Multi4;
44
45   cm::optional<std::string> Pos0;
46   cm::optional<std::string> Pos1;
47   cm::optional<std::string> Pos2;
48
49   bool Func0_ = false;
50   ArgumentParser::Continue Func0(cm::string_view)
51   {
52     Func0_ = true;
53     return ArgumentParser::Continue::No;
54   }
55
56   std::string Func1_;
57   ArgumentParser::Continue Func1(cm::string_view arg)
58   {
59     Func1_ = std::string(arg);
60     return ArgumentParser::Continue::No;
61   }
62
63   std::map<std::string, std::vector<std::string>> Func2_;
64   ArgumentParser::Continue Func2(cm::string_view key, cm::string_view arg)
65   {
66     Func2_[std::string(key)].emplace_back(arg);
67     return key == "FUNC_2b" ? ArgumentParser::Continue::Yes
68                             : ArgumentParser::Continue::No;
69   }
70
71   std::vector<std::string> Func3_;
72   ArgumentParser::Continue Func3(cm::string_view arg)
73   {
74     Func3_.emplace_back(arg);
75     return ArgumentParser::Continue::Yes;
76   }
77
78   std::map<std::string, std::vector<std::string>> Func4_;
79   ArgumentParser::Continue Func4(cm::string_view key, cm::string_view arg)
80   {
81     Func4_[std::string(key)].emplace_back(arg);
82     return key == "FUNC_4b" ? ArgumentParser::Continue::Yes
83                             : ArgumentParser::Continue::No;
84   }
85
86   ArgumentParser::Maybe<std::string> UnboundMaybe{ 'u', 'n', 'b', 'o',
87                                                    'u', 'n', 'd' };
88   ArgumentParser::MaybeEmpty<std::vector<std::string>> UnboundMaybeEmpty{
89     1, "unbound"
90   };
91   ArgumentParser::NonEmpty<std::vector<std::string>> UnboundNonEmpty{
92     1, "unbound"
93   };
94   ArgumentParser::NonEmpty<std::string> UnboundNonEmptyStr{ 'u', 'n', 'b', 'o',
95                                                             'u', 'n', 'd' };
96
97   std::vector<cm::string_view> ParsedKeywords;
98 };
99
100 std::initializer_list<cm::string_view> const args = {
101   /* clang-format off */
102   "pos0",                    // position index 0
103   "OPTION_1",                // option
104   "pos2",                    // position index 2, ignored because after keyword
105   // "OPTION_2",             // option that is not present
106   "STRING_1",                // string arg missing value
107   "STRING_2", "foo", "bar",  // string arg + unparsed value, presence captured
108   // "STRING_3",             // string arg that is not present
109   "STRING_4",                // string arg allowed to be missing value
110   "STRING_5", "foo",         // string arg that is not empty
111   "STRING_6", "",            // string arg that is empty
112   "LIST_1",                  // list arg missing values
113   "LIST_2", "foo", "bar",    // list arg with 2 elems
114   "LIST_3", "bar",           // list arg ...
115   "LIST_3", "foo",           // ... with continuation
116   "LIST_4",                  // list arg missing values, presence captured
117   // "LIST_5",               // list arg that is not present
118   "LIST_6",                  // list arg allowed to be empty
119   "MULTI_2",                 // multi list with 0 lists
120   "MULTI_3", "foo", "bar",   // multi list with first list with two elems
121   "MULTI_3", "bar", "foo",   // multi list with second list with two elems
122   // "MULTI_4",              // multi list arg that is not present
123   "FUNC_0",                  // callback arg missing value
124   "FUNC_1", "foo", "ign1",   // callback with one arg + unparsed value
125   "FUNC_2a", "foo", "ign2",  // callback with keyword-dependent arg count
126   "FUNC_2b", "bar", "zot",   // callback with keyword-dependent arg count
127   "FUNC_3", "foo", "bar",    // callback with list arg ...
128   "FUNC_4a", "foo", "ign4",  // callback with keyword-dependent arg count
129   "FUNC_4b", "bar", "zot",   // callback with keyword-dependent arg count
130   /* clang-format on */
131 };
132
133 bool verifyResult(Result const& result,
134                   std::vector<std::string> const& unparsedArguments)
135 {
136   static std::vector<std::string> const foobar = { "foo", "bar" };
137   static std::vector<std::string> const barfoo = { "bar", "foo" };
138   static std::vector<std::string> const unbound = { "unbound" };
139   static std::vector<cm::string_view> const parsedKeywords = {
140     /* clang-format off */
141     "OPTION_1",
142     "STRING_1",
143     "STRING_2",
144     "STRING_4",
145     "STRING_5",
146     "STRING_6",
147     "LIST_1",
148     "LIST_2",
149     "LIST_3",
150     "LIST_3",
151     "LIST_4",
152     "LIST_6",
153     "MULTI_2",
154     "MULTI_3",
155     "MULTI_3",
156     "FUNC_0",
157     "FUNC_1",
158     "FUNC_2a",
159     "FUNC_2b",
160     "FUNC_3",
161     "FUNC_4a",
162     "FUNC_4b",
163     /* clang-format on */
164   };
165   static std::map<std::string, std::vector<std::string>> const func2map = {
166     { "FUNC_2a", { "foo" } }, { "FUNC_2b", { "bar", "zot" } }
167   };
168   static std::map<std::string, std::vector<std::string>> const func4map = {
169     { "FUNC_4a", { "foo" } }, { "FUNC_4b", { "bar", "zot" } }
170   };
171   static std::map<cm::string_view, std::string> const keywordErrors = {
172     { "STRING_1"_s, "  missing required value\n" },
173     { "STRING_6"_s, "  empty string not allowed\n" },
174     { "LIST_1"_s, "  missing required value\n" },
175     { "LIST_4"_s, "  missing required value\n" },
176     { "FUNC_0"_s, "  missing required value\n" }
177   };
178   static std::vector<std::string> const unparsed = { "pos2", "bar", "ign1",
179                                                      "ign2", "ign4" };
180
181 #define ASSERT_TRUE(x)                                                        \
182   do {                                                                        \
183     if (!(x)) {                                                               \
184       std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
185       return false;                                                           \
186     }                                                                         \
187   } while (false)
188
189   ASSERT_TRUE(!result);
190
191   ASSERT_TRUE(result.Option1);
192   ASSERT_TRUE(!result.Option2);
193
194   ASSERT_TRUE(result.String1.empty());
195   ASSERT_TRUE(result.String2);
196   ASSERT_TRUE(*result.String2 == "foo");
197   ASSERT_TRUE(!result.String3);
198   ASSERT_TRUE(result.String4.empty());
199   ASSERT_TRUE(result.String5 == "foo");
200   ASSERT_TRUE(result.String6.empty());
201
202   ASSERT_TRUE(result.List1.empty());
203   ASSERT_TRUE(result.List2 == foobar);
204   ASSERT_TRUE(result.List3);
205   ASSERT_TRUE(*result.List3 == barfoo);
206   ASSERT_TRUE(result.List4);
207   ASSERT_TRUE(result.List4->empty());
208   ASSERT_TRUE(!result.List5);
209   ASSERT_TRUE(result.List6);
210   ASSERT_TRUE(result.List6->empty());
211
212   ASSERT_TRUE(result.Multi1.empty());
213   ASSERT_TRUE(result.Multi2.size() == 1);
214   ASSERT_TRUE(result.Multi2[0].empty());
215   ASSERT_TRUE(result.Multi3);
216   ASSERT_TRUE((*result.Multi3).size() == 2);
217   ASSERT_TRUE((*result.Multi3)[0] == foobar);
218   ASSERT_TRUE((*result.Multi3)[1] == barfoo);
219   ASSERT_TRUE(!result.Multi4);
220
221   ASSERT_TRUE(result.Pos0 == "pos0");
222   ASSERT_TRUE(!result.Pos1);
223   ASSERT_TRUE(!result.Pos2);
224
225   ASSERT_TRUE(result.Func0_ == false);
226   ASSERT_TRUE(result.Func1_ == "foo");
227   ASSERT_TRUE(result.Func2_ == func2map);
228   ASSERT_TRUE(result.Func3_ == foobar);
229   ASSERT_TRUE(result.Func4_ == func4map);
230
231   ASSERT_TRUE(unparsedArguments == unparsed);
232
233   ASSERT_TRUE(result.UnboundMaybe == "unbound");
234   ASSERT_TRUE(result.UnboundMaybeEmpty == unbound);
235   ASSERT_TRUE(result.UnboundNonEmpty == unbound);
236   ASSERT_TRUE(result.UnboundNonEmptyStr == "unbound");
237
238   ASSERT_TRUE(result.ParsedKeywords == parsedKeywords);
239
240   ASSERT_TRUE(result.GetKeywordErrors().size() == keywordErrors.size());
241   for (auto const& ke : result.GetKeywordErrors()) {
242     auto const ki = keywordErrors.find(ke.first);
243     ASSERT_TRUE(ki != keywordErrors.end());
244     ASSERT_TRUE(ke.second == ki->second);
245   }
246
247   return true;
248 }
249
250 bool testArgumentParserDynamic()
251 {
252   Result result;
253   std::vector<std::string> unparsedArguments;
254
255   std::function<ArgumentParser::Continue(cm::string_view, cm::string_view)>
256     func4 = [&result](cm::string_view key,
257                       cm::string_view arg) -> ArgumentParser::Continue {
258     return result.Func4(key, arg);
259   };
260
261   static_cast<ArgumentParser::ParseResult&>(result) =
262     cmArgumentParser<void>{}
263       .Bind(0, result.Pos0)
264       .Bind(1, result.Pos1)
265       .Bind(2, result.Pos2)
266       .Bind("OPTION_1"_s, result.Option1)
267       .Bind("OPTION_2"_s, result.Option2)
268       .Bind("STRING_1"_s, result.String1)
269       .Bind("STRING_2"_s, result.String2)
270       .Bind("STRING_3"_s, result.String3)
271       .Bind("STRING_4"_s, result.String4)
272       .Bind("STRING_5"_s, result.String5)
273       .Bind("STRING_6"_s, result.String6)
274       .Bind("LIST_1"_s, result.List1)
275       .Bind("LIST_2"_s, result.List2)
276       .Bind("LIST_3"_s, result.List3)
277       .Bind("LIST_4"_s, result.List4)
278       .Bind("LIST_5"_s, result.List5)
279       .Bind("LIST_6"_s, result.List6)
280       .Bind("MULTI_1"_s, result.Multi1)
281       .Bind("MULTI_2"_s, result.Multi2)
282       .Bind("MULTI_3"_s, result.Multi3)
283       .Bind("MULTI_4"_s, result.Multi4)
284       .Bind("FUNC_0"_s,
285             [&result](cm::string_view arg) -> ArgumentParser::Continue {
286               return result.Func0(arg);
287             })
288       .Bind("FUNC_1"_s,
289             [&result](cm::string_view arg) -> ArgumentParser::Continue {
290               return result.Func1(arg);
291             })
292       .Bind("FUNC_2a"_s,
293             [&result](cm::string_view key, cm::string_view arg)
294               -> ArgumentParser::Continue { return result.Func2(key, arg); })
295       .Bind("FUNC_2b"_s,
296             [&result](cm::string_view key, cm::string_view arg)
297               -> ArgumentParser::Continue { return result.Func2(key, arg); })
298       .Bind("FUNC_3"_s,
299             [&result](cm::string_view arg) -> ArgumentParser::Continue {
300               return result.Func3(arg);
301             })
302       .Bind("FUNC_4a"_s, func4)
303       .Bind("FUNC_4b"_s, func4)
304       .BindParsedKeywords(result.ParsedKeywords)
305       .Parse(args, &unparsedArguments);
306
307   return verifyResult(result, unparsedArguments);
308 }
309
310 static auto const parserStaticFunc4 =
311   [](Result& result, cm::string_view key,
312      cm::string_view arg) -> ArgumentParser::Continue {
313   return result.Func4(key, arg);
314 };
315 static auto const parserStatic = //
316   cmArgumentParser<Result>{}
317     .Bind(0, &Result::Pos0)
318     .Bind(1, &Result::Pos1)
319     .Bind(2, &Result::Pos2)
320     .Bind("OPTION_1"_s, &Result::Option1)
321     .Bind("OPTION_2"_s, &Result::Option2)
322     .Bind("STRING_1"_s, &Result::String1)
323     .Bind("STRING_2"_s, &Result::String2)
324     .Bind("STRING_3"_s, &Result::String3)
325     .Bind("STRING_4"_s, &Result::String4)
326     .Bind("STRING_5"_s, &Result::String5)
327     .Bind("STRING_6"_s, &Result::String6)
328     .Bind("LIST_1"_s, &Result::List1)
329     .Bind("LIST_2"_s, &Result::List2)
330     .Bind("LIST_3"_s, &Result::List3)
331     .Bind("LIST_4"_s, &Result::List4)
332     .Bind("LIST_5"_s, &Result::List5)
333     .Bind("LIST_6"_s, &Result::List6)
334     .Bind("MULTI_1"_s, &Result::Multi1)
335     .Bind("MULTI_2"_s, &Result::Multi2)
336     .Bind("MULTI_3"_s, &Result::Multi3)
337     .Bind("MULTI_4"_s, &Result::Multi4)
338     .Bind("FUNC_0"_s, &Result::Func0)
339     .Bind("FUNC_1"_s, &Result::Func1)
340     .Bind("FUNC_2a"_s, &Result::Func2)
341     .Bind("FUNC_2b"_s, &Result::Func2)
342     .Bind("FUNC_3"_s,
343           [](Result& result, cm::string_view arg) -> ArgumentParser::Continue {
344             return result.Func3(arg);
345           })
346     .Bind("FUNC_4a"_s, parserStaticFunc4)
347     .Bind("FUNC_4b"_s, parserStaticFunc4)
348     .BindParsedKeywords(&Result::ParsedKeywords)
349   /* keep semicolon on own line */;
350
351 bool testArgumentParserStatic()
352 {
353   std::vector<std::string> unparsedArguments;
354   Result const result = parserStatic.Parse(args, &unparsedArguments);
355   return verifyResult(result, unparsedArguments);
356 }
357
358 bool testArgumentParserStaticBool()
359 {
360   std::vector<std::string> unparsedArguments;
361   Result result;
362   ASSERT_TRUE(parserStatic.Parse(result, args, &unparsedArguments) == false);
363   return verifyResult(result, unparsedArguments);
364 }
365
366 } // namespace
367
368 int testArgumentParser(int /*unused*/, char* /*unused*/ [])
369 {
370   if (!testArgumentParserDynamic()) {
371     std::cout << "While executing testArgumentParserDynamic().\n";
372     return -1;
373   }
374
375   if (!testArgumentParserStatic()) {
376     std::cout << "While executing testArgumentParserStatic().\n";
377     return -1;
378   }
379
380   if (!testArgumentParserStaticBool()) {
381     std::cout << "While executing testArgumentParserStaticBool().\n";
382     return -1;
383   }
384
385   return 0;
386 }