Commit f93cb653c7ebcc4a818ee6ce9a31935dfbf8dd02

Authored by Henry Fredrick Schreiner
1 parent 09afb243

Adding tests for App, may have found an issue with expected(-1)

Showing 1 changed file with 72 additions and 1 deletions
tests/AppTest.cpp
... ... @@ -39,7 +39,6 @@ TEST_F(TApp, DashedOptions) {
39 39  
40 40 }
41 41  
42   -
43 42 TEST_F(TApp, OneFlagRef) {
44 43 int ref;
45 44 app.add_flag("-c,--count", ref);
... ... @@ -111,6 +110,22 @@ TEST_F(TApp, DefaultStringAgain) {
111 110 EXPECT_EQ(str, "previous");
112 111 }
113 112  
  113 +TEST_F(TApp, DualOptions) {
  114 +
  115 + std::string str = "previous";
  116 + std::vector<std::string> vstr = {"previous"};
  117 + std::vector<std::string> ans = {"one", "two"};
  118 + app.add_option("-s,--string", str);
  119 + app.add_option("-v,--vector", vstr);
  120 +
  121 + args = {"--vector=one", "--vector=two"};
  122 + run();
  123 + EXPECT_EQ(ans, vstr);
  124 +
  125 + args = {"--string=one", "--string=two"};
  126 + EXPECT_THROW(run(), CLI::ConversionError);
  127 +}
  128 +
114 129 TEST_F(TApp, LotsOfFlags) {
115 130  
116 131 app.add_flag("-a");
... ... @@ -229,6 +244,30 @@ TEST_F(TApp, Positionals) {
229 244 EXPECT_EQ("thing2", posit2);
230 245 }
231 246  
  247 +
  248 +TEST_F(TApp, ForcedPositional) {
  249 + std::vector<std::string> posit;
  250 + auto one = app.add_flag("--one");
  251 + auto pos = app.add_option("posit", posit)->expected(2); // Expected -1 broken?
  252 +
  253 + args = {"--one", "two", "three"};
  254 + run();
  255 + std::vector<std::string> answers1 = {"two", "three"};
  256 + EXPECT_TRUE(one->count());
  257 + EXPECT_EQ(answers1, posit);
  258 +
  259 + app.reset();
  260 +
  261 + args = {"--", "--one", "two", "three"};
  262 + std::vector<std::string> answers2 = {"--one", "two", "three"};
  263 + pos->expected(3);
  264 + run();
  265 +
  266 + EXPECT_FALSE(one->count());
  267 + EXPECT_EQ(answers2, posit);
  268 +}
  269 +
  270 +
232 271 TEST_F(TApp, MixedPositionals) {
233 272  
234 273 int positional_int;
... ... @@ -274,6 +313,18 @@ TEST_F(TApp, Reset) {
274 313 }
275 314  
276 315  
  316 +TEST_F(TApp, RemoveOption) {
  317 + app.add_flag("--one");
  318 + auto opt = app.add_flag("--two");
  319 +
  320 + EXPECT_TRUE(app.remove_option(opt));
  321 + EXPECT_FALSE(app.remove_option(opt));
  322 +
  323 + args = {"--two"};
  324 +
  325 + EXPECT_THROW(run(), CLI::ExtrasError);
  326 +}
  327 +
277 328 TEST_F(TApp, FileNotExists) {
278 329 std::string myfile{"TestNonFileNotUsed.txt"};
279 330 EXPECT_TRUE(CLI::NonexistentPath(myfile));
... ... @@ -349,6 +400,21 @@ TEST_F(TApp, InIntSet) {
349 400 EXPECT_THROW(run(), CLI::ConversionError);
350 401 }
351 402  
  403 +TEST_F(TApp, FailSet) {
  404 +
  405 + int choice;
  406 + app.add_set("-q,--quick", choice, {1, 2, 3});
  407 +
  408 + args = {"--quick", "3", "--quick=2"};
  409 + EXPECT_THROW(run(), CLI::ConversionError);
  410 +
  411 + app.reset();
  412 +
  413 + args = {"--quick=hello"};
  414 + EXPECT_THROW(run(), CLI::ConversionError);
  415 +}
  416 +
  417 +
352 418 TEST_F(TApp, InSetIgnoreCase) {
353 419  
354 420 std::string choice;
... ... @@ -372,6 +438,11 @@ TEST_F(TApp, InSetIgnoreCase) {
372 438 app.reset();
373 439 args = {"--quick", "four"};
374 440 EXPECT_THROW(run(), CLI::ConversionError);
  441 +
  442 + app.reset();
  443 + args = {"--quick=one", "--quick=two"};
  444 + EXPECT_THROW(run(), CLI::ConversionError);
  445 +
375 446 }
376 447  
377 448 TEST_F(TApp, VectorFixedString) {
... ...