diff options
author | nga <nga@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
commit | 1f553f46fb4f3c5eec631352cdd900a0709016af (patch) | |
tree | a231fba2c03b440becaea6c86a2702d0bfb0336e /library/cpp/getopt/ut | |
parent | c4de7efdedc25b49cbea74bd589eecb61b55b60a (diff) | |
download | ydb-1f553f46fb4f3c5eec631352cdd900a0709016af.tar.gz |
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/getopt/ut')
-rw-r--r-- | library/cpp/getopt/ut/last_getopt_ut.cpp | 700 | ||||
-rw-r--r-- | library/cpp/getopt/ut/opt2_ut.cpp | 30 | ||||
-rw-r--r-- | library/cpp/getopt/ut/opt_ut.cpp | 22 | ||||
-rw-r--r-- | library/cpp/getopt/ut/posix_getopt_ut.cpp | 126 |
4 files changed, 439 insertions, 439 deletions
diff --git a/library/cpp/getopt/ut/last_getopt_ut.cpp b/library/cpp/getopt/ut/last_getopt_ut.cpp index c99a1d053d..7d6a29f2a5 100644 --- a/library/cpp/getopt/ut/last_getopt_ut.cpp +++ b/library/cpp/getopt/ut/last_getopt_ut.cpp @@ -2,22 +2,22 @@ #include <library/cpp/colorizer/colors.h> #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/array_size.h> #include <util/string/subst.h> #include <util/string/vector.h> #include <util/string/split.h> -using namespace NLastGetopt; - -namespace { +using namespace NLastGetopt; + +namespace { struct TOptsNoDefault: public TOpts { TOptsNoDefault(const TStringBuf& optstring = TStringBuf()) : TOpts(optstring) { } - }; - + }; + class TOptsParseResultTestWrapper: public TOptsParseResultException { TVector<const char*> Argv_; @@ -30,101 +30,101 @@ namespace { }; using V = TVector<const char*>; -} - -struct TOptsParserTester { - TOptsNoDefault Opts_; +} + +struct TOptsParserTester { + TOptsNoDefault Opts_; TVector<const char*> Argv_; - - THolder<TOptsParser> Parser_; - - void Initialize() { - if (!Parser_) + + THolder<TOptsParser> Parser_; + + void Initialize() { + if (!Parser_) Parser_.Reset(new TOptsParser(&Opts_, (int)Argv_.size(), Argv_.data())); - } - - void Accept() { - Initialize(); - UNIT_ASSERT(Parser_->Next()); - } - - void AcceptOption() { - Accept(); + } + + void Accept() { + Initialize(); + UNIT_ASSERT(Parser_->Next()); + } + + void AcceptOption() { + Accept(); UNIT_ASSERT(!!Parser_->CurOpt()); - } - - void AcceptOption(char c) { - AcceptOption(); + } + + void AcceptOption(char c) { + AcceptOption(); UNIT_ASSERT(Parser_->CurOpt()->CharIs(c)); - } - + } + void AcceptOption(const TString& optName) { - AcceptOption(); + AcceptOption(); UNIT_ASSERT(Parser_->CurOpt()->NameIs(optName)); - } - - template <typename TOpt> + } + + template <typename TOpt> void AcceptOptionWithValue(TOpt optName, const TString& value) { - AcceptOption(optName); + AcceptOption(optName); UNIT_ASSERT_VALUES_EQUAL_C(value, Parser_->CurValStr(), "; option " << optName); - } - - template <typename TOpt> - void AcceptOptionWithoutValue(TOpt optName) { - AcceptOption(optName); + } + + template <typename TOpt> + void AcceptOptionWithoutValue(TOpt optName) { + AcceptOption(optName); UNIT_ASSERT_C(!Parser_->CurVal(), ": opt " << optName << " must have no param"); - } - + } + void AcceptFreeArgInOrder(const TString& expected) { - Accept(); + Accept(); UNIT_ASSERT(!Parser_->CurOpt()); UNIT_ASSERT_VALUES_EQUAL(expected, Parser_->CurValStr()); - } - - size_t Pos_; - - void AcceptEndOfOptions() { - Initialize(); - UNIT_ASSERT(!Parser_->Next()); - Pos_ = Parser_->Pos_; - - // pos must not be changed after last meaningful invocation of Next() - UNIT_ASSERT(!Parser_->Next()); - UNIT_ASSERT_VALUES_EQUAL(Pos_, Parser_->Pos_); - UNIT_ASSERT(!Parser_->Next()); - UNIT_ASSERT_VALUES_EQUAL(Pos_, Parser_->Pos_); - } - - void AcceptError() { - Initialize(); - try { - Parser_->Next(); - UNIT_FAIL("expecting exception"); - } catch (const TUsageException&) { - // expecting - } - } - - void AcceptUnexpectedOption() { - Initialize(); - size_t pos = Parser_->Pos_; - size_t sop = Parser_->Sop_; - AcceptError(); - UNIT_ASSERT_VALUES_EQUAL(pos, Parser_->Pos_); - UNIT_ASSERT_VALUES_EQUAL(sop, Parser_->Sop_); - } - + } + + size_t Pos_; + + void AcceptEndOfOptions() { + Initialize(); + UNIT_ASSERT(!Parser_->Next()); + Pos_ = Parser_->Pos_; + + // pos must not be changed after last meaningful invocation of Next() + UNIT_ASSERT(!Parser_->Next()); + UNIT_ASSERT_VALUES_EQUAL(Pos_, Parser_->Pos_); + UNIT_ASSERT(!Parser_->Next()); + UNIT_ASSERT_VALUES_EQUAL(Pos_, Parser_->Pos_); + } + + void AcceptError() { + Initialize(); + try { + Parser_->Next(); + UNIT_FAIL("expecting exception"); + } catch (const TUsageException&) { + // expecting + } + } + + void AcceptUnexpectedOption() { + Initialize(); + size_t pos = Parser_->Pos_; + size_t sop = Parser_->Sop_; + AcceptError(); + UNIT_ASSERT_VALUES_EQUAL(pos, Parser_->Pos_); + UNIT_ASSERT_VALUES_EQUAL(sop, Parser_->Sop_); + } + void AcceptFreeArg(const TString& expected) { - UNIT_ASSERT(Pos_ < Parser_->Argc_); - UNIT_ASSERT_VALUES_EQUAL(expected, Parser_->Argv_[Pos_]); - ++Pos_; - } - - void AcceptEndOfFreeArgs() { - UNIT_ASSERT_VALUES_EQUAL(Argv_.size(), Pos_); - } -}; - + UNIT_ASSERT(Pos_ < Parser_->Argc_); + UNIT_ASSERT_VALUES_EQUAL(expected, Parser_->Argv_[Pos_]); + ++Pos_; + } + + void AcceptEndOfFreeArgs() { + UNIT_ASSERT_VALUES_EQUAL(Argv_.size(), Pos_); + } +}; + namespace { bool gSimpleFlag = false; void SimpleHander(void) { @@ -134,37 +134,37 @@ namespace { Y_UNIT_TEST_SUITE(TLastGetoptTests) { Y_UNIT_TEST(TestEqual) { - TOptsNoDefault opts; - opts.AddLongOption("from"); - opts.AddLongOption("to"); + TOptsNoDefault opts; + opts.AddLongOption("from"); + opts.AddLongOption("to"); TOptsParseResultTestWrapper r(&opts, V({"copy", "--from=/", "--to=/etc"})); - - UNIT_ASSERT_VALUES_EQUAL("copy", r.GetProgramName()); - UNIT_ASSERT_VALUES_EQUAL("/", r.Get("from")); - UNIT_ASSERT_VALUES_EQUAL("/etc", r.Get("to")); - UNIT_ASSERT_VALUES_EQUAL("/etc", r.GetOrElse("to", "trash")); - UNIT_ASSERT(r.Has("from")); - UNIT_ASSERT(r.Has("to")); + + UNIT_ASSERT_VALUES_EQUAL("copy", r.GetProgramName()); + UNIT_ASSERT_VALUES_EQUAL("/", r.Get("from")); + UNIT_ASSERT_VALUES_EQUAL("/etc", r.Get("to")); + UNIT_ASSERT_VALUES_EQUAL("/etc", r.GetOrElse("to", "trash")); + UNIT_ASSERT(r.Has("from")); + UNIT_ASSERT(r.Has("to")); UNIT_ASSERT_EXCEPTION(r.Get("left"), TException); - } - + } + Y_UNIT_TEST(TestCharOptions) { - TOptsNoDefault opts; - opts.AddCharOption('R', NO_ARGUMENT); - opts.AddCharOption('l', NO_ARGUMENT); - opts.AddCharOption('h', NO_ARGUMENT); + TOptsNoDefault opts; + opts.AddCharOption('R', NO_ARGUMENT); + opts.AddCharOption('l', NO_ARGUMENT); + opts.AddCharOption('h', NO_ARGUMENT); TOptsParseResultTestWrapper r(&opts, V({"cp", "/etc", "-Rl", "/tmp/etc"})); - UNIT_ASSERT(r.Has('R')); - UNIT_ASSERT(r.Has('l')); - UNIT_ASSERT(!r.Has('h')); - + UNIT_ASSERT(r.Has('R')); + UNIT_ASSERT(r.Has('l')); + UNIT_ASSERT(!r.Has('h')); + UNIT_ASSERT_VALUES_EQUAL(2u, r.GetFreeArgs().size()); UNIT_ASSERT_VALUES_EQUAL(2u, r.GetFreeArgCount()); - UNIT_ASSERT_VALUES_EQUAL("/etc", r.GetFreeArgs()[0]); - UNIT_ASSERT_VALUES_EQUAL("/tmp/etc", r.GetFreeArgs()[1]); - } - + UNIT_ASSERT_VALUES_EQUAL("/etc", r.GetFreeArgs()[0]); + UNIT_ASSERT_VALUES_EQUAL("/tmp/etc", r.GetFreeArgs()[1]); + } + Y_UNIT_TEST(TestFreeArgs) { TOptsNoDefault opts; opts.SetFreeArgsNum(1, 3); @@ -185,167 +185,167 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { } Y_UNIT_TEST(TestCharOptionsRequiredOptional) { - TOptsNoDefault opts; - opts.AddCharOption('d', REQUIRED_ARGUMENT); - opts.AddCharOption('e', REQUIRED_ARGUMENT); - opts.AddCharOption('x', REQUIRED_ARGUMENT); - opts.AddCharOption('y', REQUIRED_ARGUMENT); - opts.AddCharOption('l', NO_ARGUMENT); + TOptsNoDefault opts; + opts.AddCharOption('d', REQUIRED_ARGUMENT); + opts.AddCharOption('e', REQUIRED_ARGUMENT); + opts.AddCharOption('x', REQUIRED_ARGUMENT); + opts.AddCharOption('y', REQUIRED_ARGUMENT); + opts.AddCharOption('l', NO_ARGUMENT); TOptsParseResultTestWrapper r(&opts, V({"cmd", "-ld11", "-e", "22", "-lllx33", "-y", "44"})); - UNIT_ASSERT_VALUES_EQUAL("11", r.Get('d')); - UNIT_ASSERT_VALUES_EQUAL("22", r.Get('e')); - UNIT_ASSERT_VALUES_EQUAL("33", r.Get('x')); - UNIT_ASSERT_VALUES_EQUAL("44", r.Get('y')); - } - + UNIT_ASSERT_VALUES_EQUAL("11", r.Get('d')); + UNIT_ASSERT_VALUES_EQUAL("22", r.Get('e')); + UNIT_ASSERT_VALUES_EQUAL("33", r.Get('x')); + UNIT_ASSERT_VALUES_EQUAL("44", r.Get('y')); + } + Y_UNIT_TEST(TestReturnInOrder) { - TOptsParserTester tester; - tester.Opts_.AddLongOption('v', "value"); - tester.Opts_.ArgPermutation_ = RETURN_IN_ORDER; - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("--value=11"); - tester.Argv_.push_back("xx"); - tester.Argv_.push_back("-v12"); - tester.Argv_.push_back("yy"); - tester.Argv_.push_back("--"); - tester.Argv_.push_back("-v13"); - tester.Argv_.push_back("--"); - - tester.AcceptOptionWithValue("value", "11"); - tester.AcceptFreeArgInOrder("xx"); - tester.AcceptOptionWithValue('v', "12"); - tester.AcceptFreeArgInOrder("yy"); - tester.AcceptFreeArgInOrder("-v13"); - tester.AcceptFreeArgInOrder("--"); - tester.AcceptEndOfOptions(); - tester.AcceptEndOfFreeArgs(); - } - + TOptsParserTester tester; + tester.Opts_.AddLongOption('v', "value"); + tester.Opts_.ArgPermutation_ = RETURN_IN_ORDER; + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("--value=11"); + tester.Argv_.push_back("xx"); + tester.Argv_.push_back("-v12"); + tester.Argv_.push_back("yy"); + tester.Argv_.push_back("--"); + tester.Argv_.push_back("-v13"); + tester.Argv_.push_back("--"); + + tester.AcceptOptionWithValue("value", "11"); + tester.AcceptFreeArgInOrder("xx"); + tester.AcceptOptionWithValue('v', "12"); + tester.AcceptFreeArgInOrder("yy"); + tester.AcceptFreeArgInOrder("-v13"); + tester.AcceptFreeArgInOrder("--"); + tester.AcceptEndOfOptions(); + tester.AcceptEndOfFreeArgs(); + } + Y_UNIT_TEST(TestRequireOrder) { - TOptsParserTester tester; - tester.Opts_.ArgPermutation_ = REQUIRE_ORDER; - tester.Opts_.AddLongOption('v', "value"); - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("--value=11"); - tester.Argv_.push_back("xx"); - tester.Argv_.push_back("-v12"); - tester.Argv_.push_back("yy"); - - tester.AcceptOptionWithValue("value", "11"); - tester.AcceptEndOfOptions(); - - tester.AcceptFreeArg("xx"); - tester.AcceptFreeArg("-v12"); - tester.AcceptFreeArg("yy"); - tester.AcceptEndOfFreeArgs(); - } - + TOptsParserTester tester; + tester.Opts_.ArgPermutation_ = REQUIRE_ORDER; + tester.Opts_.AddLongOption('v', "value"); + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("--value=11"); + tester.Argv_.push_back("xx"); + tester.Argv_.push_back("-v12"); + tester.Argv_.push_back("yy"); + + tester.AcceptOptionWithValue("value", "11"); + tester.AcceptEndOfOptions(); + + tester.AcceptFreeArg("xx"); + tester.AcceptFreeArg("-v12"); + tester.AcceptFreeArg("yy"); + tester.AcceptEndOfFreeArgs(); + } + Y_UNIT_TEST(TestPlusForLongOption) { - TOptsParserTester tester; - tester.Opts_.AddLongOption('v', "value"); - tester.Opts_.AllowPlusForLong_ = true; - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("+value=11"); - tester.Argv_.push_back("xx"); - tester.Argv_.push_back("-v12"); - tester.Argv_.push_back("yy"); - - tester.AcceptOptionWithValue("value", "11"); - tester.AcceptOptionWithValue("value", "12"); - tester.AcceptEndOfOptions(); - - tester.AcceptFreeArg("xx"); - tester.AcceptFreeArg("yy"); - tester.AcceptEndOfFreeArgs(); - } - + TOptsParserTester tester; + tester.Opts_.AddLongOption('v', "value"); + tester.Opts_.AllowPlusForLong_ = true; + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("+value=11"); + tester.Argv_.push_back("xx"); + tester.Argv_.push_back("-v12"); + tester.Argv_.push_back("yy"); + + tester.AcceptOptionWithValue("value", "11"); + tester.AcceptOptionWithValue("value", "12"); + tester.AcceptEndOfOptions(); + + tester.AcceptFreeArg("xx"); + tester.AcceptFreeArg("yy"); + tester.AcceptEndOfFreeArgs(); + } + Y_UNIT_TEST(TestBug1) { - TOptsParserTester tester; - tester.Opts_.AddCharOptions("A:b:cd:"); - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("-A"); - tester.Argv_.push_back("aaaa"); - tester.Argv_.push_back("zz"); - tester.Argv_.push_back("-c"); - tester.Argv_.push_back("-d8"); - tester.Argv_.push_back("ww"); - - tester.AcceptOptionWithValue('A', "aaaa"); - tester.AcceptOptionWithoutValue('c'); - tester.AcceptOptionWithValue('d', "8"); - tester.AcceptEndOfOptions(); - - tester.AcceptFreeArg("zz"); - tester.AcceptFreeArg("ww"); - tester.AcceptEndOfFreeArgs(); - } - + TOptsParserTester tester; + tester.Opts_.AddCharOptions("A:b:cd:"); + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("-A"); + tester.Argv_.push_back("aaaa"); + tester.Argv_.push_back("zz"); + tester.Argv_.push_back("-c"); + tester.Argv_.push_back("-d8"); + tester.Argv_.push_back("ww"); + + tester.AcceptOptionWithValue('A', "aaaa"); + tester.AcceptOptionWithoutValue('c'); + tester.AcceptOptionWithValue('d', "8"); + tester.AcceptEndOfOptions(); + + tester.AcceptFreeArg("zz"); + tester.AcceptFreeArg("ww"); + tester.AcceptEndOfFreeArgs(); + } + Y_UNIT_TEST(TestPermuteComplex) { - TOptsParserTester tester; - - tester.Opts_.AddCharOption('x').NoArgument(); - tester.Opts_.AddCharOption('y').RequiredArgument(); - tester.Opts_.AddCharOption('z').NoArgument(); - tester.Opts_.AddCharOption('w').RequiredArgument(); - tester.Opts_.ArgPermutation_ = PERMUTE; - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("-x"); - tester.Argv_.push_back("-y"); - tester.Argv_.push_back("val"); - tester.Argv_.push_back("freearg1"); - tester.Argv_.push_back("-zw"); - tester.Argv_.push_back("val2"); - tester.Argv_.push_back("freearg2"); - - tester.AcceptOptionWithoutValue('x'); - tester.AcceptOptionWithValue('y', "val"); - tester.AcceptOptionWithoutValue('z'); - tester.AcceptOptionWithValue('w', "val2"); - tester.AcceptEndOfOptions(); - tester.AcceptFreeArg("freearg1"); - tester.AcceptFreeArg("freearg2"); - tester.AcceptEndOfFreeArgs(); - } - + TOptsParserTester tester; + + tester.Opts_.AddCharOption('x').NoArgument(); + tester.Opts_.AddCharOption('y').RequiredArgument(); + tester.Opts_.AddCharOption('z').NoArgument(); + tester.Opts_.AddCharOption('w').RequiredArgument(); + tester.Opts_.ArgPermutation_ = PERMUTE; + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("-x"); + tester.Argv_.push_back("-y"); + tester.Argv_.push_back("val"); + tester.Argv_.push_back("freearg1"); + tester.Argv_.push_back("-zw"); + tester.Argv_.push_back("val2"); + tester.Argv_.push_back("freearg2"); + + tester.AcceptOptionWithoutValue('x'); + tester.AcceptOptionWithValue('y', "val"); + tester.AcceptOptionWithoutValue('z'); + tester.AcceptOptionWithValue('w', "val2"); + tester.AcceptEndOfOptions(); + tester.AcceptFreeArg("freearg1"); + tester.AcceptFreeArg("freearg2"); + tester.AcceptEndOfFreeArgs(); + } + Y_UNIT_TEST(TestFinalDashDash) { - TOptsParserTester tester; - tester.Opts_.AddLongOption("size"); - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("--"); - - tester.AcceptEndOfOptions(); - tester.AcceptEndOfFreeArgs(); - } - + TOptsParserTester tester; + tester.Opts_.AddLongOption("size"); + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("--"); + + tester.AcceptEndOfOptions(); + tester.AcceptEndOfFreeArgs(); + } + Y_UNIT_TEST(TestDashDashAfterDashDash) { - TOptsParserTester tester; - tester.Opts_.AddLongOption("size"); - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("--"); - tester.Argv_.push_back("--"); - tester.Argv_.push_back("--"); - - tester.AcceptEndOfOptions(); - tester.AcceptFreeArg("--"); - tester.AcceptFreeArg("--"); - tester.AcceptEndOfFreeArgs(); - } - + TOptsParserTester tester; + tester.Opts_.AddLongOption("size"); + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("--"); + tester.Argv_.push_back("--"); + tester.Argv_.push_back("--"); + + tester.AcceptEndOfOptions(); + tester.AcceptFreeArg("--"); + tester.AcceptFreeArg("--"); + tester.AcceptEndOfFreeArgs(); + } + Y_UNIT_TEST(TestUnexpectedUnknownOption) { - TOptsParserTester tester; - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("-x"); - - tester.AcceptUnexpectedOption(); - } - + TOptsParserTester tester; + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("-x"); + + tester.AcceptUnexpectedOption(); + } + Y_UNIT_TEST(TestDuplicatedOptionCrash) { // this test is broken, cause UNIT_ASSERT(false) always throws return; @@ -364,84 +364,84 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { } Y_UNIT_TEST(TestPositionWhenNoArgs) { - TOptsParserTester tester; - - tester.Argv_.push_back("cmd"); - - tester.Opts_.AddCharOption('c'); - - tester.AcceptEndOfOptions(); - + TOptsParserTester tester; + + tester.Argv_.push_back("cmd"); + + tester.Opts_.AddCharOption('c'); + + tester.AcceptEndOfOptions(); + UNIT_ASSERT_VALUES_EQUAL(1u, tester.Parser_->Pos_); - } - + } + Y_UNIT_TEST(TestExpectedUnknownCharOption) { - TOptsParserTester tester; - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("-x"); - tester.Argv_.push_back("-y"); - tester.Argv_.push_back("val"); - tester.Argv_.push_back("freearg1"); - tester.Argv_.push_back("-zw"); - tester.Argv_.push_back("val2"); - tester.Argv_.push_back("freearg2"); - - tester.Opts_.AllowUnknownCharOptions_ = true; - - tester.AcceptOptionWithoutValue('x'); - tester.AcceptOptionWithValue('y', "val"); - tester.AcceptOptionWithoutValue('z'); - tester.AcceptOptionWithValue('w', "val2"); - tester.AcceptEndOfOptions(); - tester.AcceptFreeArg("freearg1"); - tester.AcceptFreeArg("freearg2"); - tester.AcceptEndOfFreeArgs(); - } - -#if 0 + TOptsParserTester tester; + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("-x"); + tester.Argv_.push_back("-y"); + tester.Argv_.push_back("val"); + tester.Argv_.push_back("freearg1"); + tester.Argv_.push_back("-zw"); + tester.Argv_.push_back("val2"); + tester.Argv_.push_back("freearg2"); + + tester.Opts_.AllowUnknownCharOptions_ = true; + + tester.AcceptOptionWithoutValue('x'); + tester.AcceptOptionWithValue('y', "val"); + tester.AcceptOptionWithoutValue('z'); + tester.AcceptOptionWithValue('w', "val2"); + tester.AcceptEndOfOptions(); + tester.AcceptFreeArg("freearg1"); + tester.AcceptFreeArg("freearg2"); + tester.AcceptEndOfFreeArgs(); + } + +#if 0 Y_UNIT_TEST(TestRequiredParams) { - TOptsParserTester tester; - - tester.Argv_.push_back("cmd"); - tester.Argv_.push_back("--port=1231"); - tester.Argv_.push_back("asas"); - - tester.Opts_.AddLongOption("port"); - tester.Opts_.AddLongOption("home").Required(); - - tester.AcceptOptionWithValue("port", "1231"); - tester.AcceptError(); - } -#endif - + TOptsParserTester tester; + + tester.Argv_.push_back("cmd"); + tester.Argv_.push_back("--port=1231"); + tester.Argv_.push_back("asas"); + + tester.Opts_.AddLongOption("port"); + tester.Opts_.AddLongOption("home").Required(); + + tester.AcceptOptionWithValue("port", "1231"); + tester.AcceptError(); + } +#endif + Y_UNIT_TEST(TestStoreResult) { - TOptsNoDefault opts; + TOptsNoDefault opts; TString data; - int number; + int number; TMaybe<TString> optionalString0, optionalString1; TMaybe<int> optionalNumber0, optionalNumber1; - opts.AddLongOption('d', "data").StoreResult(&data); - opts.AddLongOption('n', "number").StoreResult(&number); + opts.AddLongOption('d', "data").StoreResult(&data); + opts.AddLongOption('n', "number").StoreResult(&number); opts.AddLongOption("optional-string-0").StoreResult(&optionalString0); opts.AddLongOption("optional-number-0").StoreResult(&optionalNumber0); opts.AddLongOption("optional-string-1").StoreResult(&optionalString1); opts.AddLongOption("optional-number-1").StoreResult(&optionalNumber1); TOptsParseResultTestWrapper r(&opts, V({"cmd", "--data=jjhh", "-n", "11", "--optional-number-1=8", "--optional-string-1=os1"})); - UNIT_ASSERT_VALUES_EQUAL("jjhh", data); - UNIT_ASSERT_VALUES_EQUAL(11, number); + UNIT_ASSERT_VALUES_EQUAL("jjhh", data); + UNIT_ASSERT_VALUES_EQUAL(11, number); UNIT_ASSERT(!optionalString0.Defined()); UNIT_ASSERT(!optionalNumber0.Defined()); UNIT_ASSERT_VALUES_EQUAL(*optionalString1, "os1"); UNIT_ASSERT_VALUES_EQUAL(*optionalNumber1, 8); - } - + } + Y_UNIT_TEST(TestStoreValue) { int a = 0, b = 0; size_t c = 0; EHasArg e = NO_ARGUMENT; - TOptsNoDefault opts; + TOptsNoDefault opts; opts.AddLongOption('a', "alpha").NoArgument().StoreValue(&a, 42); opts.AddLongOption('b', "beta").NoArgument().StoreValue(&b, 24); opts.AddLongOption('e', "enum").NoArgument().StoreValue(&e, REQUIRED_ARGUMENT).StoreValue(&c, 12345); @@ -472,17 +472,17 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { } Y_UNIT_TEST(TestDefaultValue) { - TOptsNoDefault opts; - opts.AddLongOption("path").DefaultValue("/etc"); + TOptsNoDefault opts; + opts.AddLongOption("path").DefaultValue("/etc"); int value = 42; opts.AddLongOption("value").StoreResult(&value).DefaultValue(32); TOptsParseResultTestWrapper r(&opts, V({"cmd", "dfdf"})); - UNIT_ASSERT_VALUES_EQUAL("/etc", r.Get("path")); + UNIT_ASSERT_VALUES_EQUAL("/etc", r.Get("path")); UNIT_ASSERT_VALUES_EQUAL(32, value); - } - + } + Y_UNIT_TEST(TestSplitValue) { - TOptsNoDefault opts; + TOptsNoDefault opts; TVector<TString> vals; opts.AddLongOption('s', "split").SplitHandler(&vals, ','); TOptsParseResultTestWrapper r(&opts, V({"prog", "--split=a,b,c"})); @@ -508,17 +508,17 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { } Y_UNIT_TEST(TestParseArgs) { - TOptsNoDefault o("AbCx:y:z::"); - UNIT_ASSERT_EQUAL(o.GetCharOption('A').HasArg_, NO_ARGUMENT); - UNIT_ASSERT_EQUAL(o.GetCharOption('b').HasArg_, NO_ARGUMENT); - UNIT_ASSERT_EQUAL(o.GetCharOption('C').HasArg_, NO_ARGUMENT); - UNIT_ASSERT_EQUAL(o.GetCharOption('x').HasArg_, REQUIRED_ARGUMENT); - UNIT_ASSERT_EQUAL(o.GetCharOption('y').HasArg_, REQUIRED_ARGUMENT); - UNIT_ASSERT_EQUAL(o.GetCharOption('z').HasArg_, OPTIONAL_ARGUMENT); - } - + TOptsNoDefault o("AbCx:y:z::"); + UNIT_ASSERT_EQUAL(o.GetCharOption('A').HasArg_, NO_ARGUMENT); + UNIT_ASSERT_EQUAL(o.GetCharOption('b').HasArg_, NO_ARGUMENT); + UNIT_ASSERT_EQUAL(o.GetCharOption('C').HasArg_, NO_ARGUMENT); + UNIT_ASSERT_EQUAL(o.GetCharOption('x').HasArg_, REQUIRED_ARGUMENT); + UNIT_ASSERT_EQUAL(o.GetCharOption('y').HasArg_, REQUIRED_ARGUMENT); + UNIT_ASSERT_EQUAL(o.GetCharOption('z').HasArg_, OPTIONAL_ARGUMENT); + } + Y_UNIT_TEST(TestRequiredOpts) { - TOptsNoDefault opts; + TOptsNoDefault opts; TOpt& opt_d = opts.AddCharOption('d'); // test 'not required' @@ -549,14 +549,14 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { }; Y_UNIT_TEST(TestHandlers) { { - TOptsNoDefault opts; + TOptsNoDefault opts; bool flag = false; opts.AddLongOption("flag").Handler0(HandlerStoreTrue(&flag)).NoArgument(); TOptsParseResultTestWrapper r(&opts, V({"cmd", "--flag"})); UNIT_ASSERT(flag); } { - TOptsNoDefault opts; + TOptsNoDefault opts; unsigned uval = 5; double fval = 0.0; opts.AddLongOption("flag1").RequiredArgument().StoreResult(&uval); @@ -702,17 +702,17 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { Y_UNIT_TEST(TestAppendTo) { TVector<int> ints; - - TOptsNoDefault opts; - opts.AddLongOption("size").AppendTo(&ints); - + + TOptsNoDefault opts; + opts.AddLongOption("size").AppendTo(&ints); + TOptsParseResultTestWrapper r(&opts, V({"cmd", "--size=17", "--size=19"})); - - UNIT_ASSERT_VALUES_EQUAL(size_t(2), ints.size()); - UNIT_ASSERT_VALUES_EQUAL(17, ints.at(0)); - UNIT_ASSERT_VALUES_EQUAL(19, ints.at(1)); - } - + + UNIT_ASSERT_VALUES_EQUAL(size_t(2), ints.size()); + UNIT_ASSERT_VALUES_EQUAL(17, ints.at(0)); + UNIT_ASSERT_VALUES_EQUAL(19, ints.at(1)); + } + Y_UNIT_TEST(TestEmplaceTo) { TVector<std::tuple<TString>> richPaths; @@ -791,4 +791,4 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { UNIT_ASSERT_VALUES_EQUAL(25, number); UNIT_ASSERT_VALUES_EQUAL(2, r.GetFreeArgCount()); } -} +} diff --git a/library/cpp/getopt/ut/opt2_ut.cpp b/library/cpp/getopt/ut/opt2_ut.cpp index 0e7464747c..facf0a6b0d 100644 --- a/library/cpp/getopt/ut/opt2_ut.cpp +++ b/library/cpp/getopt/ut/opt2_ut.cpp @@ -1,13 +1,13 @@ #include <library/cpp/getopt/opt2.h> #include <library/cpp/testing/unittest/registar.h> - + //using namespace NLastGetopt; - + Y_UNIT_TEST_SUITE(Opt2Test) { Y_UNIT_TEST(TestSimple) { int argc = 8; - char* argv[] = { + char* argv[] = { (char*)"cmd", (char*)"--aaaa=aaaa", (char*)"zz", @@ -16,10 +16,10 @@ Y_UNIT_TEST_SUITE(Opt2Test) { (char*)"-c", (char*)"-d8", (char*)"ww", - }; - + }; + Opt2 opt(argc, argv, "A:b:cd:e:x:", 2, "aaaa=A"); - + const char* edef = "edef"; const char* a = opt.Arg('A', "<var_name> - usage of -A"); int b = opt.Int('b', "<var_name> - usage of -b", 2); @@ -27,21 +27,21 @@ Y_UNIT_TEST_SUITE(Opt2Test) { int d = opt.Int('d', "<var_name> - usage of -d", 13); const char* e = opt.Arg('e', "<unused> - only default is really used", edef); const TVector<const char*>& x = opt.MArg('x', "<var_name> - usage of -x"); - - UNIT_ASSERT(!opt.AutoUsage("<L> <M>")); - UNIT_ASSERT_VALUES_EQUAL("aaaa", a); - UNIT_ASSERT_VALUES_EQUAL(2, b); - UNIT_ASSERT(c); - UNIT_ASSERT_VALUES_EQUAL(8, d); + + UNIT_ASSERT(!opt.AutoUsage("<L> <M>")); + UNIT_ASSERT_VALUES_EQUAL("aaaa", a); + UNIT_ASSERT_VALUES_EQUAL(2, b); + UNIT_ASSERT(c); + UNIT_ASSERT_VALUES_EQUAL(8, d); UNIT_ASSERT_VALUES_EQUAL((void*)edef, e); - + UNIT_ASSERT_VALUES_EQUAL(2u, opt.Pos.size()); UNIT_ASSERT_STRINGS_EQUAL("zz", opt.Pos.at(0)); UNIT_ASSERT_VALUES_EQUAL((void*)argv[2], opt.Pos.at(0)); UNIT_ASSERT_STRINGS_EQUAL("ww", opt.Pos.at(1)); UNIT_ASSERT_STRINGS_EQUAL("1", x.at(0)); UNIT_ASSERT_STRINGS_EQUAL("2", x.at(1)); - } + } Y_UNIT_TEST(TestErrors1) { int argc = 4; @@ -60,4 +60,4 @@ Y_UNIT_TEST_SUITE(Opt2Test) { UNIT_ASSERT(c); UNIT_ASSERT_VALUES_EQUAL((void*)edef, e); } -} +} diff --git a/library/cpp/getopt/ut/opt_ut.cpp b/library/cpp/getopt/ut/opt_ut.cpp index 441aa493a0..66122f6ae3 100644 --- a/library/cpp/getopt/ut/opt_ut.cpp +++ b/library/cpp/getopt/ut/opt_ut.cpp @@ -2,19 +2,19 @@ #include <library/cpp/testing/unittest/registar.h> #include <util/string/vector.h> - + Y_UNIT_TEST_SUITE(OptTest) { Y_UNIT_TEST(TestSimple) { - int argc = 3; - char* argv[] = { + int argc = 3; + char* argv[] = { (char*)"cmd", (char*)"-x"}; - Opt opt(argc, argv, ""); - opt.Err = false; // be quiet - UNIT_ASSERT_VALUES_EQUAL('?', opt.Get()); - UNIT_ASSERT_VALUES_EQUAL(EOF, opt.Get()); - UNIT_ASSERT_VALUES_EQUAL(EOF, opt.Get()); - UNIT_ASSERT_VALUES_EQUAL(EOF, opt.Get()); - } + Opt opt(argc, argv, ""); + opt.Err = false; // be quiet + UNIT_ASSERT_VALUES_EQUAL('?', opt.Get()); + UNIT_ASSERT_VALUES_EQUAL(EOF, opt.Get()); + UNIT_ASSERT_VALUES_EQUAL(EOF, opt.Get()); + UNIT_ASSERT_VALUES_EQUAL(EOF, opt.Get()); + } Y_UNIT_TEST(TestFreeArguments) { Opt::Ion options[] = { @@ -49,4 +49,4 @@ Y_UNIT_TEST_SUITE(OptTest) { UNIT_ASSERT_VALUES_EQUAL(optionValue, "ARG2"); } } -} +} diff --git a/library/cpp/getopt/ut/posix_getopt_ut.cpp b/library/cpp/getopt/ut/posix_getopt_ut.cpp index b6d374bf28..9235e7340d 100644 --- a/library/cpp/getopt/ut/posix_getopt_ut.cpp +++ b/library/cpp/getopt/ut/posix_getopt_ut.cpp @@ -1,119 +1,119 @@ #include <library/cpp/getopt/posix_getopt.h> #include <library/cpp/testing/unittest/registar.h> - -using namespace NLastGetopt; - + +using namespace NLastGetopt; + Y_UNIT_TEST_SUITE(TPosixGetoptTest) { Y_UNIT_TEST(TestSimple) { - int argc = 6; + int argc = 6; const char* argv0[] = {"program", "-b", "-f1", "-f", "2", "zzzz"}; char** const argv = (char**)argv0; - - NLastGetopt::optreset = 1; - UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt(argc, argv, "bf:")); - UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt(argc, argv, "bf:")); + + NLastGetopt::optreset = 1; + UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt(argc, argv, "bf:")); + UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt(argc, argv, "bf:")); UNIT_ASSERT_VALUES_EQUAL(NLastGetopt::optarg, TString("1")); - UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt(argc, argv, "bf:")); + UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt(argc, argv, "bf:")); UNIT_ASSERT_VALUES_EQUAL(NLastGetopt::optarg, TString("2")); - UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt(argc, argv, "bf:")); - - UNIT_ASSERT_VALUES_EQUAL(5, NLastGetopt::optind); - } - + UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt(argc, argv, "bf:")); + + UNIT_ASSERT_VALUES_EQUAL(5, NLastGetopt::optind); + } + Y_UNIT_TEST(TestLong) { - int daggerset = 0; - /* options descriptor */ - const NLastGetopt::option longopts[] = { + int daggerset = 0; + /* options descriptor */ + const NLastGetopt::option longopts[] = { {"buffy", no_argument, nullptr, 'b'}, {"fluoride", required_argument, nullptr, 'f'}, {"daggerset", no_argument, &daggerset, 1}, {nullptr, 0, nullptr, 0}}; - - int argc = 7; + + int argc = 7; const char* argv0[] = {"program", "-b", "--buffy", "-f1", "--fluoride=2", "--daggerset", "zzzz"}; char** const argv = (char**)argv0; - - int longIndex; - - NLastGetopt::optreset = 1; - UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, &longIndex)); - UNIT_ASSERT_VALUES_EQUAL(0, longIndex); + + int longIndex; + + NLastGetopt::optreset = 1; + UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, &longIndex)); + UNIT_ASSERT_VALUES_EQUAL(0, longIndex); UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr)); - UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, &longIndex)); - UNIT_ASSERT_VALUES_EQUAL(1, longIndex); + UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, &longIndex)); + UNIT_ASSERT_VALUES_EQUAL(1, longIndex); UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr)); UNIT_ASSERT_VALUES_EQUAL(0, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr)); UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr)); - - UNIT_ASSERT_VALUES_EQUAL(6, NLastGetopt::optind); - } - + + UNIT_ASSERT_VALUES_EQUAL(6, NLastGetopt::optind); + } + Y_UNIT_TEST(TestLongPermutation) { - int daggerset = 0; - /* options descriptor */ - const NLastGetopt::option longopts[] = { + int daggerset = 0; + /* options descriptor */ + const NLastGetopt::option longopts[] = { {"buffy", no_argument, nullptr, 'b'}, {"fluoride", required_argument, nullptr, 'f'}, {"daggerset", no_argument, &daggerset, 1}, {nullptr, 0, nullptr, 0}}; - - int argc = 7; + + int argc = 7; const char* argv0[] = {"program", "aa", "-b", "bb", "cc", "--buffy", "dd"}; char** const argv = (char**)argv0; - - NLastGetopt::optreset = 1; + + NLastGetopt::optreset = 1; UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr)); UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr)); UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr)); - - UNIT_ASSERT_VALUES_EQUAL(3, NLastGetopt::optind); - } - + + UNIT_ASSERT_VALUES_EQUAL(3, NLastGetopt::optind); + } + Y_UNIT_TEST(TestNoOptionsOptionsWithDoubleDash) { - const NLastGetopt::option longopts[] = { + const NLastGetopt::option longopts[] = { {"buffy", no_argument, nullptr, 'b'}, {"fluoride", no_argument, nullptr, 'f'}, {nullptr, 0, nullptr, 0}}; - - int argc = 2; + + int argc = 2; const char* argv0[] = {"program", "--bf"}; char** const argv = (char**)argv0; - - NLastGetopt::optreset = 1; + + NLastGetopt::optreset = 1; UNIT_ASSERT_VALUES_EQUAL('?', NLastGetopt::getopt_long(argc, argv, "bf", longopts, nullptr)); - } - + } + Y_UNIT_TEST(TestLongOnly) { - const NLastGetopt::option longopts[] = { + const NLastGetopt::option longopts[] = { {"foo", no_argument, nullptr, 'F'}, {"fluoride", no_argument, nullptr, 'f'}, {"ogogo", no_argument, nullptr, 'o'}, {nullptr, 0, nullptr, 0}}; - - int argc = 4; + + int argc = 4; const char* argv0[] = {"program", "--foo", "-foo", "-fo"}; char** const argv = (char**)argv0; - - NLastGetopt::optreset = 1; + + NLastGetopt::optreset = 1; UNIT_ASSERT_VALUES_EQUAL('F', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr)); UNIT_ASSERT_VALUES_EQUAL('F', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr)); UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr)); UNIT_ASSERT_VALUES_EQUAL('o', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr)); UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr)); - } - + } + Y_UNIT_TEST(TestLongWithoutOnlySingleDashNowAllowed) { - const NLastGetopt::option longopts[] = { + const NLastGetopt::option longopts[] = { {"foo", no_argument, nullptr, 'F'}, {"zoo", no_argument, nullptr, 'z'}, {nullptr, 0, nullptr, 0}}; - - int argc = 2; + + int argc = 2; const char* argv0[] = {"program", "-foo"}; char** const argv = (char**)argv0; - - NLastGetopt::optreset = 1; + + NLastGetopt::optreset = 1; UNIT_ASSERT_VALUES_EQUAL('?', NLastGetopt::getopt_long(argc, argv, "z", longopts, nullptr)); - } -} + } +} |