diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 58ab5fd3..15a9fce7 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -878,6 +878,21 @@ class App { ///@} /// @name Parsing ///@{ + // + /// Reset the parsed data + void clear() { + + parsed_ = false; + missing_.clear(); + parsed_subcommands_.clear(); + + for(const Option_p &opt : options_) { + opt->clear(); + } + for(const App_p &app : subcommands_) { + app->clear(); + } + } /// Parses the command line - throws errors /// This must be called after the options are in but before the rest of the program. @@ -895,6 +910,10 @@ class App { /// The real work is done here. Expects a reversed vector. /// Changes the vector to the remaining options. void parse(std::vector &args) { + // Clear if parsed + if(parsed_) + clear(); + _validate(); _parse(args); run_callback(); @@ -930,21 +949,6 @@ class App { return e.get_exit_code(); } - /// Reset the parsed data - void reset() { - - parsed_ = false; - missing_.clear(); - parsed_subcommands_.clear(); - - for(const Option_p &opt : options_) { - opt->clear(); - } - for(const App_p &app : subcommands_) { - app->reset(); - } - } - ///@} /// @name Post parsing ///@{ diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index ee4d45dc..482a7022 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -152,13 +152,10 @@ TEST_F(TApp, BoolAndIntFlags) { EXPECT_EQ(1, iflag); EXPECT_EQ((unsigned int)1, uflag); - app.reset(); - args = {"-b", "-b"}; - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_TRUE(bflag); - app.reset(); bflag = false; args = {"-iiiuu"}; @@ -173,11 +170,9 @@ TEST_F(TApp, BoolOnlyFlag) { app.add_flag("-b", bflag)->multi_option_policy(CLI::MultiOptionPolicy::Throw); args = {"-b"}; - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_TRUE(bflag); - app.reset(); - args = {"-b", "-b"}; EXPECT_THROW(run(), CLI::ConversionError); } @@ -331,8 +326,7 @@ TEST_F(TApp, MissingValueNonRequiredOpt) { args = {"-c"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); - - app.reset(); + app.clear(); args = {"--count"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); @@ -346,8 +340,7 @@ TEST_F(TApp, MissingValueMoreThan) { args = {"-v", "2"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); - - app.reset(); + app.clear(); args = {"--vals", "4"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); @@ -363,8 +356,6 @@ TEST_F(TApp, NoMissingValueMoreThan) { run(); EXPECT_EQ(vals1, std::vector({2, 3, 4})); - app.reset(); - args = {"--vals", "2", "3", "4"}; run(); EXPECT_EQ(vals2, std::vector({2, 3, 4})); @@ -419,7 +410,7 @@ TEST_F(TApp, RequiredOptsDouble) { EXPECT_THROW(run(), CLI::ArgumentMismatch); - app.reset(); + app.clear(); args = {"--str", "one", "two"}; run(); @@ -435,8 +426,7 @@ TEST_F(TApp, RequiredOptsDoubleShort) { args = {"-s", "one"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); - - app.reset(); + app.clear(); args = {"-s", "one", "-s", "one", "-s", "one"}; @@ -450,20 +440,15 @@ TEST_F(TApp, RequiredOptsDoubleNeg) { args = {"-s", "one"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); - - app.reset(); + app.clear(); args = {"-s", "one", "two", "-s", "three"}; - EXPECT_NO_THROW(run()); - + ASSERT_NO_THROW(run()); EXPECT_EQ(strs, std::vector({"one", "two", "three"})); - app.reset(); args = {"-s", "one", "two"}; - - EXPECT_NO_THROW(run()); - + ASSERT_NO_THROW(run()); EXPECT_EQ(strs, std::vector({"one", "two"})); } @@ -483,7 +468,6 @@ TEST_F(TApp, PositionalNoSpace) { EXPECT_EQ(options.size(), (size_t)1); EXPECT_EQ(options.at(0), "Test"); - app.reset(); args = {"-OTest", "param1", "param2"}; run(); @@ -505,7 +489,6 @@ TEST_F(TApp, PositionalNoSpaceLong) { EXPECT_EQ(options.size(), (size_t)1); EXPECT_EQ(options.at(0), "Test"); - app.reset(); args = {"--option=Test", "param1", "param2"}; run(); @@ -520,25 +503,22 @@ TEST_F(TApp, RequiredOptsUnlimited) { args = {"--str"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); + app.clear(); - app.reset(); args = {"--str", "one", "--str", "two"}; run(); EXPECT_EQ(strs, std::vector({"one", "two"})); - app.reset(); args = {"--str", "one", "two"}; run(); EXPECT_EQ(strs, std::vector({"one", "two"})); // It's better to feed a hungry option than to feed allow_extras - app.reset(); app.allow_extras(); run(); EXPECT_EQ(strs, std::vector({"one", "two"})); EXPECT_EQ(app.remaining(), std::vector({})); - app.reset(); app.allow_extras(false); std::vector remain; app.add_option("positional", remain); @@ -546,14 +526,12 @@ TEST_F(TApp, RequiredOptsUnlimited) { EXPECT_EQ(strs, std::vector({"one", "two"})); EXPECT_EQ(remain, std::vector()); - app.reset(); args = {"--str", "one", "--", "two"}; run(); EXPECT_EQ(strs, std::vector({"one"})); EXPECT_EQ(remain, std::vector({"two"})); - app.reset(); args = {"one", "--str", "two"}; run(); @@ -568,25 +546,22 @@ TEST_F(TApp, RequiredOptsUnlimitedShort) { args = {"-s"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); + app.clear(); - app.reset(); args = {"-s", "one", "-s", "two"}; run(); EXPECT_EQ(strs, std::vector({"one", "two"})); - app.reset(); args = {"-s", "one", "two"}; run(); EXPECT_EQ(strs, std::vector({"one", "two"})); // It's better to feed a hungry option than to feed allow_extras - app.reset(); app.allow_extras(); run(); EXPECT_EQ(strs, std::vector({"one", "two"})); EXPECT_EQ(app.remaining(), std::vector({})); - app.reset(); app.allow_extras(false); std::vector remain; app.add_option("positional", remain); @@ -594,14 +569,12 @@ TEST_F(TApp, RequiredOptsUnlimitedShort) { EXPECT_EQ(strs, std::vector({"one", "two"})); EXPECT_EQ(remain, std::vector()); - app.reset(); args = {"-s", "one", "--", "two"}; run(); EXPECT_EQ(strs, std::vector({"one"})); EXPECT_EQ(remain, std::vector({"two"})); - app.reset(); args = {"one", "-s", "two"}; run(); @@ -635,7 +608,6 @@ TEST_F(TApp, RequireOptPriority) { EXPECT_EQ(strs, std::vector({"one"})); EXPECT_EQ(remain, std::vector({"two", "three"})); - app.reset(); args = {"two", "three", "--str", "one", "four"}; run(); @@ -656,7 +628,6 @@ TEST_F(TApp, RequireOptPriorityShort) { EXPECT_EQ(strs, std::vector({"one"})); EXPECT_EQ(remain, std::vector({"two", "three"})); - app.reset(); args = {"two", "three", "-s", "one", "four"}; run(); @@ -689,17 +660,16 @@ TEST_F(TApp, RequiredFlags) { app.add_flag("-b")->mandatory(); // Alternate term EXPECT_THROW(run(), CLI::RequiredError); - - app.reset(); + app.clear(); args = {"-a"}; EXPECT_THROW(run(), CLI::RequiredError); + app.clear(); - app.reset(); args = {"-b"}; EXPECT_THROW(run(), CLI::RequiredError); + app.clear(); - app.reset(); args = {"-a", "-b"}; run(); } @@ -715,12 +685,10 @@ TEST_F(TApp, CallbackFlags) { run(); EXPECT_EQ(value, (size_t)0); - app.reset(); args = {"-v"}; run(); EXPECT_EQ(value, (size_t)1); - app.reset(); args = {"-vv"}; run(); EXPECT_EQ(value, (size_t)2); @@ -740,12 +708,10 @@ TEST_F(TApp, CallbackFlagsAuto) { run(); EXPECT_EQ(value, (size_t)0); - app.reset(); args = {"-v"}; run(); EXPECT_EQ(value, (size_t)1); - app.reset(); args = {"-vv"}; run(); EXPECT_EQ(value, (size_t)2); @@ -782,8 +748,6 @@ TEST_F(TApp, ForcedPositional) { EXPECT_TRUE(one->count()); EXPECT_EQ(answers1, posit); - app.reset(); - args = {"--", "--one", "two", "three"}; std::vector answers2 = {"--one", "two", "three"}; run(); @@ -818,8 +782,6 @@ TEST_F(TApp, BigPositional) { run(); EXPECT_EQ(args, vec); - app.reset(); - args = {"one", "two"}; run(); @@ -840,7 +802,7 @@ TEST_F(TApp, Reset) { EXPECT_EQ((size_t)1, app.count("-d")); EXPECT_DOUBLE_EQ(1.2, doub); - app.reset(); + app.clear(); EXPECT_EQ((size_t)0, app.count("--simple")); EXPECT_EQ((size_t)0, app.count("-d")); @@ -866,7 +828,7 @@ TEST_F(TApp, RemoveOption) { TEST_F(TApp, FileNotExists) { std::string myfile{"TestNonFileNotUsed.txt"}; - EXPECT_NO_THROW(CLI::NonexistentPath(myfile)); + ASSERT_NO_THROW(CLI::NonexistentPath(myfile)); std::string filename; app.add_option("--file", filename)->check(CLI::NonexistentPath); @@ -875,8 +837,6 @@ TEST_F(TApp, FileNotExists) { run(); EXPECT_EQ(myfile, filename); - app.reset(); - bool ok = static_cast(std::ofstream(myfile.c_str()).put('a')); // create file EXPECT_TRUE(ok); EXPECT_THROW(run(), CLI::ValidationError); @@ -894,8 +854,7 @@ TEST_F(TApp, FileExists) { args = {"--file", myfile}; EXPECT_THROW(run(), CLI::ValidationError); - - app.reset(); + app.clear(); bool ok = static_cast(std::ofstream(myfile.c_str()).put('a')); // create file EXPECT_TRUE(ok); @@ -916,8 +875,6 @@ TEST_F(TApp, InSet) { run(); EXPECT_EQ("two", choice); - app.reset(); - args = {"--quick", "four"}; EXPECT_THROW(run(), CLI::ConversionError); } @@ -929,15 +886,12 @@ TEST_F(TApp, InSetWithDefault) { run(); EXPECT_EQ("one", choice); - app.reset(); args = {"--quick", "two"}; run(); EXPECT_EQ("two", choice); - app.reset(); - args = {"--quick", "four"}; EXPECT_THROW(run(), CLI::ConversionError); } @@ -949,15 +903,12 @@ TEST_F(TApp, InCaselessSetWithDefault) { run(); EXPECT_EQ("one", choice); - app.reset(); args = {"--quick", "tWo"}; run(); EXPECT_EQ("two", choice); - app.reset(); - args = {"--quick", "four"}; EXPECT_THROW(run(), CLI::ConversionError); } @@ -972,8 +923,6 @@ TEST_F(TApp, InIntSet) { run(); EXPECT_EQ(2, choice); - app.reset(); - args = {"--quick", "4"}; EXPECT_THROW(run(), CLI::ConversionError); } @@ -985,8 +934,7 @@ TEST_F(TApp, FailSet) { args = {"--quick", "3", "--quick=2"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); - - app.reset(); + app.clear(); args = {"--quick=hello"}; EXPECT_THROW(run(), CLI::ConversionError); @@ -1001,8 +949,8 @@ TEST_F(TApp, FailLValueSet) { args = {"--quick=hello"}; EXPECT_THROW(run(), CLI::ConversionError); + app.clear(); - app.reset(); args = {"--slow=hello"}; EXPECT_THROW(run(), CLI::ConversionError); } @@ -1016,21 +964,18 @@ TEST_F(TApp, InSetIgnoreCase) { run(); EXPECT_EQ("one", choice); - app.reset(); args = {"--quick", "two"}; run(); EXPECT_EQ("Two", choice); // Keeps caps from set - app.reset(); args = {"--quick", "ThrEE"}; run(); EXPECT_EQ("THREE", choice); // Keeps caps from set - app.reset(); args = {"--quick", "four"}; EXPECT_THROW(run(), CLI::ConversionError); + app.clear(); - app.reset(); args = {"--quick=one", "--quick=two"}; EXPECT_THROW(run(), CLI::ArgumentMismatch); } @@ -1073,7 +1018,6 @@ TEST_F(TApp, VectorUnlimString) { EXPECT_EQ((size_t)3, app.count("--string")); EXPECT_EQ(answer, strvec); - app.reset(); args = {"-s", "mystring", "mystring2", "mystring3"}; run(); EXPECT_EQ((size_t)3, app.count("--string")); @@ -1092,11 +1036,10 @@ TEST_F(TApp, VectorFancyOpts) { EXPECT_EQ((size_t)3, app.count("--string")); EXPECT_EQ(answer, strvec); - app.reset(); args = {"one", "two"}; EXPECT_THROW(run(), CLI::RequiredError); + app.clear(); - app.reset(); EXPECT_THROW(run(), CLI::ParseError); } @@ -1122,15 +1065,12 @@ TEST_F(TApp, NeedsFlags) { run(); - app.reset(); args = {"-s"}; run(); - app.reset(); args = {"-s", "--both"}; run(); - app.reset(); args = {"--both"}; EXPECT_THROW(run(), CLI::RequiresError); } @@ -1141,19 +1081,16 @@ TEST_F(TApp, ExcludesFlags) { run(); - app.reset(); args = {"-s"}; run(); - app.reset(); args = {"--nostr"}; run(); - app.reset(); args = {"--nostr", "-s"}; EXPECT_THROW(run(), CLI::ExcludesError); + app.clear(); - app.reset(); args = {"--string", "--nostr"}; EXPECT_THROW(run(), CLI::ExcludesError); } @@ -1166,19 +1103,16 @@ TEST_F(TApp, ExcludesMixedFlags) { run(); - app.reset(); args = {"--no"}; run(); - app.reset(); args = {"--opt2"}; run(); - app.reset(); args = {"--no", "--opt1"}; EXPECT_THROW(run(), CLI::ExcludesError); + app.clear(); - app.reset(); args = {"--no", "--opt2"}; EXPECT_THROW(run(), CLI::ExcludesError); } @@ -1191,27 +1125,24 @@ TEST_F(TApp, NeedsMultiFlags) { run(); - app.reset(); args = {"--opt1"}; run(); - app.reset(); args = {"--opt2"}; run(); - app.reset(); args = {"--optall"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--optall", "--opt1"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--optall", "--opt2", "--opt1"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--optall", "--opt1", "--opt2", "--opt3"}; run(); } @@ -1224,27 +1155,24 @@ TEST_F(TApp, NeedsMixedFlags) { run(); - app.reset(); args = {"--opt1"}; run(); - app.reset(); args = {"--opt2"}; run(); - app.reset(); args = {"--optall"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--optall", "--opt1"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--optall", "--opt2", "--opt1"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--optall", "--opt1", "--opt2", "--opt3"}; run(); } @@ -1256,31 +1184,28 @@ TEST_F(TApp, NeedsChainedFlags) { run(); - app.reset(); args = {"--opt1"}; run(); - app.reset(); args = {"--opt2"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--opt3"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--opt3", "--opt2"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--opt3", "--opt1"}; EXPECT_THROW(run(), CLI::RequiresError); + app.clear(); - app.reset(); args = {"--opt2", "--opt1"}; run(); - app.reset(); args = {"--opt1", "--opt2", "--opt3"}; run(); } @@ -1297,11 +1222,9 @@ TEST_F(TApp, Env) { EXPECT_EQ(2, val); EXPECT_EQ((size_t)1, vopt->count()); - app.reset(); vopt->required(); run(); - app.reset(); unset_env("CLI11_TEST_ENV_TMP"); EXPECT_THROW(run(), CLI::RequiredError); } @@ -1312,20 +1235,18 @@ TEST_F(TApp, RangeInt) { args = {"--one=1"}; EXPECT_THROW(run(), CLI::ValidationError); + app.clear(); - app.reset(); args = {"--one=7"}; EXPECT_THROW(run(), CLI::ValidationError); + app.clear(); - app.reset(); args = {"--one=3"}; run(); - app.reset(); args = {"--one=5"}; run(); - app.reset(); args = {"--one=6"}; run(); } @@ -1338,20 +1259,18 @@ TEST_F(TApp, RangeDouble) { args = {"--one=1"}; EXPECT_THROW(run(), CLI::ValidationError); + app.clear(); - app.reset(); args = {"--one=7"}; EXPECT_THROW(run(), CLI::ValidationError); + app.clear(); - app.reset(); args = {"--one=3"}; run(); - app.reset(); args = {"--one=5"}; run(); - app.reset(); args = {"--one=6"}; run(); } @@ -1367,7 +1286,7 @@ TEST_F(TApp, AllowExtras) { args = {"-x", "-f"}; - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_TRUE(val); EXPECT_EQ(app.remaining(), std::vector({"-x"})); } @@ -1377,9 +1296,8 @@ TEST_F(TApp, AllowExtrasOrder) { app.allow_extras(); args = {"-x", "-f"}; - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_EQ(app.remaining(), std::vector({"-x", "-f"})); - app.reset(); std::vector left_over = app.remaining(); app.parse(left_over); @@ -1462,14 +1380,12 @@ TEST_F(TApp, ThrowingTransform) { auto m = app.add_option("-m,--mess", val); m->transform([](std::string) -> std::string { throw CLI::ValidationError("My Message"); }); - EXPECT_NO_THROW(run()); - app.reset(); + ASSERT_NO_THROW(run()); args = {"-mone"}; ASSERT_THROW(run(), CLI::ValidationError); - - app.reset(); + app.clear(); try { run(); @@ -1533,17 +1449,15 @@ TEST_F(TApp, AddRemoveSetItems) { items.erase("TYPE1"); items.erase("TYPE2"); - app.reset(); args = {"--type1", "TYPE6", "--type2", "TYPE7"}; run(); EXPECT_EQ(type1, "TYPE6"); EXPECT_EQ(type2, "TYPE7"); - app.reset(); args = {"--type1", "TYPE1"}; EXPECT_THROW(run(), CLI::ConversionError); + app.clear(); - app.reset(); args = {"--type2", "TYPE2"}; EXPECT_THROW(run(), CLI::ConversionError); } @@ -1567,17 +1481,15 @@ TEST_F(TApp, AddRemoveSetItemsNoCase) { items.erase("TYPE1"); items.erase("TYPE2"); - app.reset(); args = {"--type1", "TyPE6", "--type2", "tYPE7"}; run(); EXPECT_EQ(type1, "TYPE6"); EXPECT_EQ(type2, "TYPE7"); - app.reset(); args = {"--type1", "TYPe1"}; EXPECT_THROW(run(), CLI::ConversionError); + app.clear(); - app.reset(); args = {"--type2", "TYpE2"}; EXPECT_THROW(run(), CLI::ConversionError); } @@ -1588,10 +1500,9 @@ TEST_F(TApp, RepeatingMultiArgumentOptions) { app.add_option("--entry", entries, "set a key and value")->type_name("KEY VALUE")->type_size(-2); args = {"--entry", "key1", "value1", "--entry", "key2", "value2"}; - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_EQ(entries, std::vector({"key1", "value1", "key2", "value2"})); - app.reset(); args.pop_back(); ASSERT_THROW(run(), CLI::ArgumentMismatch); } diff --git a/tests/HelpTest.cpp b/tests/HelpTest.cpp index e611839e..c48c81ad 100644 --- a/tests/HelpTest.cpp +++ b/tests/HelpTest.cpp @@ -326,14 +326,14 @@ TEST(THelp, OnlyOneAllHelp) { std::vector input{"--help-all"}; EXPECT_THROW(app.parse(input), CLI::ExtrasError); + app.clear(); - app.reset(); std::vector input2{"--yelp"}; EXPECT_THROW(app.parse(input2), CLI::CallForAllHelp); + app.clear(); // Remove the flag app.set_help_all_flag(); - app.reset(); std::vector input3{"--yelp"}; EXPECT_THROW(app.parse(input3), CLI::ExtrasError); } diff --git a/tests/IniTest.cpp b/tests/IniTest.cpp index 09637a6e..71ce22d4 100644 --- a/tests/IniTest.cpp +++ b/tests/IniTest.cpp @@ -193,7 +193,6 @@ TEST_F(TApp, IniNotRequired) { EXPECT_EQ(99, two); EXPECT_EQ(3, three); - app.reset(); one = two = three = 0; args = {"--one=1", "--two=2"}; @@ -238,7 +237,7 @@ TEST_F(TApp, IniGetRemainingOption) { int two = 0; app.add_option("--two", two); - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); std::vector ExpectedRemaining = {ExtraOption}; EXPECT_EQ(app.remaining(), ExpectedRemaining); } @@ -256,7 +255,7 @@ TEST_F(TApp, IniGetNoRemaining) { int two = 0; app.add_option("--two", two); - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_EQ(app.remaining().size(), (size_t)0); } @@ -291,7 +290,6 @@ TEST_F(TApp, IniNotRequiredNotDefault) { EXPECT_EQ(99, two); EXPECT_EQ(3, three); - app.reset(); args = {"--config", tmpini2}; run(); @@ -360,18 +358,16 @@ TEST_F(TApp, IniRequired) { run(); - app.reset(); one = two = three = 0; args = {"--one=1", "--two=2"}; run(); - app.reset(); args = {}; EXPECT_THROW(run(), CLI::RequiredError); + app.clear(); - app.reset(); args = {"--two=2"}; EXPECT_THROW(run(), CLI::RequiredError); @@ -458,7 +454,7 @@ TEST_F(TApp, IniConfigurable) { out << "val=1" << std::endl; } - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_TRUE(value); } @@ -538,7 +534,7 @@ TEST_F(TApp, IniFlagNumbers) { out << "flag=3" << std::endl; } - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_TRUE(boo); } diff --git a/tests/OptionalTest.cpp b/tests/OptionalTest.cpp index 6c519e1f..5d68c724 100644 --- a/tests/OptionalTest.cpp +++ b/tests/OptionalTest.cpp @@ -11,13 +11,11 @@ TEST_F(TApp, StdOptionalTest) { run(); EXPECT_FALSE(opt); - app.reset(); args = {"-c", "1"}; run(); EXPECT_TRUE(opt); EXPECT_EQ(*opt, 1); - app.reset(); args = {"--count", "3"}; run(); EXPECT_TRUE(opt); @@ -33,13 +31,11 @@ TEST_F(TApp, ExperimentalOptionalTest) { run(); EXPECT_FALSE(opt); - app.reset(); args = {"-c", "1"}; run(); EXPECT_TRUE(opt); EXPECT_EQ(*opt, 1); - app.reset(); args = {"--count", "3"}; run(); EXPECT_TRUE(opt); @@ -55,13 +51,11 @@ TEST_F(TApp, BoostOptionalTest) { run(); EXPECT_FALSE(opt); - app.reset(); args = {"-c", "1"}; run(); EXPECT_TRUE(opt); EXPECT_EQ(*opt, 1); - app.reset(); args = {"--count", "3"}; run(); EXPECT_TRUE(opt); diff --git a/tests/SubcommandTest.cpp b/tests/SubcommandTest.cpp index ce8f658a..98f8387e 100644 --- a/tests/SubcommandTest.cpp +++ b/tests/SubcommandTest.cpp @@ -21,12 +21,12 @@ TEST_F(TApp, BasicSubcommands) { run(); EXPECT_EQ((size_t)0, app.get_subcommands().size()); - app.reset(); args = {"sub1"}; run(); EXPECT_EQ(sub1, app.get_subcommands().at(0)); + EXPECT_EQ((size_t)1, app.get_subcommands().size()); - app.reset(); + app.clear(); EXPECT_EQ((size_t)0, app.get_subcommands().size()); args = {"sub2"}; @@ -34,19 +34,18 @@ TEST_F(TApp, BasicSubcommands) { EXPECT_EQ((size_t)1, app.get_subcommands().size()); EXPECT_EQ(sub2, app.get_subcommands().at(0)); - app.reset(); args = {"SUb2"}; EXPECT_THROW(run(), CLI::ExtrasError); + app.clear(); - app.reset(); args = {"SUb2"}; try { run(); } catch(const CLI::ExtrasError &e) { EXPECT_THAT(e.what(), HasSubstr("SUb2")); } + app.clear(); - app.reset(); args = {"sub1", "extra"}; try { run(); @@ -72,22 +71,19 @@ TEST_F(TApp, MultiSubFallthrough) { EXPECT_TRUE(app.got_subcommand(sub2)); EXPECT_TRUE(*sub2); - app.reset(); app.require_subcommand(); run(); - app.reset(); app.require_subcommand(2); run(); - app.reset(); app.require_subcommand(1); EXPECT_THROW(run(), CLI::ExtrasError); + app.clear(); - app.reset(); args = {"sub1"}; run(); @@ -109,24 +105,21 @@ TEST_F(TApp, RequiredAndSubcoms) { // #23 auto bar = app.add_subcommand("bar"); args = {"bar", "foo"}; - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_TRUE(*foo); EXPECT_FALSE(*bar); EXPECT_EQ(baz, "bar"); - app.reset(); args = {"foo"}; - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_FALSE(*foo); EXPECT_EQ(baz, "foo"); - app.reset(); args = {"foo", "foo"}; - EXPECT_NO_THROW(run()); + ASSERT_NO_THROW(run()); EXPECT_TRUE(*foo); EXPECT_EQ(baz, "foo"); - app.reset(); args = {"foo", "other"}; EXPECT_THROW(run(), CLI::ExtrasError); } @@ -144,7 +137,6 @@ TEST_F(TApp, RequiredAndSubcomFallthrough) { EXPECT_TRUE(bar); EXPECT_EQ(baz, "other"); - app.reset(); args = {"bar", "other2"}; EXPECT_THROW(run(), CLI::ExtrasError); } @@ -164,7 +156,6 @@ TEST_F(TApp, FooFooProblem) { EXPECT_EQ(baz_str, ""); EXPECT_EQ(other_str, "foo"); - app.reset(); baz_str = ""; other_str = ""; baz->required(); @@ -198,19 +189,18 @@ TEST_F(TApp, RuntimeErrorInCallback) { args = {"sub1"}; EXPECT_THROW(run(), CLI::RuntimeError); - app.reset(); args = {"sub1"}; try { run(); } catch(const CLI::RuntimeError &e) { EXPECT_EQ(1, e.get_exit_code()); } + app.clear(); - app.reset(); args = {"sub2"}; EXPECT_THROW(run(), CLI::RuntimeError); + app.clear(); - app.reset(); args = {"sub2"}; try { run(); @@ -316,7 +306,6 @@ TEST_F(TApp, CallbackOrdering) { EXPECT_EQ(2, val); EXPECT_EQ(2, sub_val); - app.reset(); args = {"--val=2", "sub"}; run(); EXPECT_EQ(2, val); @@ -330,11 +319,9 @@ TEST_F(TApp, RequiredSubCom) { app.require_subcommand(); EXPECT_THROW(run(), CLI::RequiredError); - - app.reset(); + app.clear(); args = {"sub1"}; - run(); } @@ -347,22 +334,16 @@ TEST_F(TApp, SubComExtras) { EXPECT_EQ(app.remaining(), std::vector({"extra"})); EXPECT_EQ(sub->remaining(), std::vector()); - app.reset(); - args = {"extra1", "extra2", "sub"}; run(); EXPECT_EQ(app.remaining(), std::vector({"extra1", "extra2"})); EXPECT_EQ(sub->remaining(), std::vector()); - app.reset(); - args = {"sub", "extra1", "extra2"}; run(); EXPECT_EQ(app.remaining(), std::vector()); EXPECT_EQ(sub->remaining(), std::vector({"extra1", "extra2"})); - app.reset(); - args = {"extra1", "extra2", "sub", "extra3", "extra4"}; run(); EXPECT_EQ(app.remaining(), std::vector({"extra1", "extra2"})); @@ -377,12 +358,11 @@ TEST_F(TApp, Required1SubCom) { app.add_subcommand("sub3"); EXPECT_THROW(run(), CLI::RequiredError); + app.clear(); - app.reset(); args = {"sub1"}; run(); - app.reset(); args = {"sub1", "sub2"}; EXPECT_THROW(run(), CLI::ExtrasError); } @@ -522,16 +502,14 @@ TEST_F(SubcommandProgram, MultipleArgs) { TEST_F(SubcommandProgram, CaseCheck) { args = {"Start"}; EXPECT_THROW(run(), CLI::ExtrasError); + app.clear(); - app.reset(); args = {"start"}; run(); - app.reset(); start->ignore_case(); run(); - app.reset(); args = {"Start"}; run(); } @@ -546,12 +524,12 @@ TEST_F(TApp, SubcomInheritCaseCheck) { EXPECT_EQ((size_t)2, app.get_subcommands({}).size()); EXPECT_EQ((size_t)1, app.get_subcommands([](const CLI::App *s) { return s->get_name() == "sub1"; }).size()); - app.reset(); args = {"SuB1"}; run(); EXPECT_EQ(sub1, app.get_subcommands().at(0)); + EXPECT_EQ((size_t)1, app.get_subcommands().size()); - app.reset(); + app.clear(); EXPECT_EQ((size_t)0, app.get_subcommands().size()); args = {"sUb2"}; @@ -577,8 +555,6 @@ TEST_F(SubcommandProgram, Callbacks) { run(); - app.reset(); - args = {"start"}; EXPECT_THROW(run(), CLI::Success); @@ -606,15 +582,15 @@ TEST_F(SubcommandProgram, ExtrasErrors) { args = {"one", "two", "start", "three", "four"}; EXPECT_THROW(run(), CLI::ExtrasError); - app.reset(); + app.clear(); args = {"start", "three", "four"}; EXPECT_THROW(run(), CLI::ExtrasError); - app.reset(); + app.clear(); args = {"one", "two"}; EXPECT_THROW(run(), CLI::ExtrasError); - app.reset(); + app.clear(); } TEST_F(SubcommandProgram, OrderedExtras) { @@ -622,7 +598,7 @@ TEST_F(SubcommandProgram, OrderedExtras) { app.allow_extras(); args = {"one", "two", "start", "three", "four"}; EXPECT_THROW(run(), CLI::ExtrasError); - app.reset(); + app.clear(); start->allow_extras(); @@ -632,7 +608,6 @@ TEST_F(SubcommandProgram, OrderedExtras) { EXPECT_EQ(start->remaining(), std::vector({"three", "four"})); EXPECT_EQ(app.remaining(true), std::vector({"one", "two", "three", "four"})); - app.reset(); args = {"one", "two", "start", "three", "--", "four"}; run(); @@ -656,7 +631,6 @@ TEST_F(SubcommandProgram, MixedOrderExtras) { EXPECT_EQ(stop->remaining(), std::vector({"five", "six"})); EXPECT_EQ(app.remaining(true), std::vector({"one", "two", "three", "four", "five", "six"})); - app.reset(); args = {"one", "two", "stop", "three", "four", "start", "five", "six"}; run(); @@ -675,7 +649,6 @@ TEST_F(SubcommandProgram, CallbackOrder) { run(); EXPECT_EQ(callback_order, std::vector({1, 2})); - app.reset(); callback_order.clear(); args = {"stop", "start"}; @@ -728,7 +701,6 @@ TEST_F(ManySubcommands, Required1Fuzzy) { run(); EXPECT_EQ(sub1->remaining(), vs_t({"sub2", "sub3"})); - app.reset(); app.require_subcommand(-1); run(); @@ -742,7 +714,6 @@ TEST_F(ManySubcommands, Required2Fuzzy) { EXPECT_EQ(sub2->remaining(), vs_t({"sub3"})); EXPECT_EQ(app.remaining(true), vs_t({"sub3"})); - app.reset(); app.require_subcommand(-2); run(); @@ -753,13 +724,11 @@ TEST_F(ManySubcommands, Unlimited) { run(); EXPECT_EQ(app.remaining(true), vs_t()); - app.reset(); app.require_subcommand(); run(); EXPECT_EQ(app.remaining(true), vs_t()); - app.reset(); app.require_subcommand(2, 0); // 2 or more run();