diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/getopt/ut/opt_ut.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/getopt/ut/opt_ut.cpp')
-rw-r--r-- | library/cpp/getopt/ut/opt_ut.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/library/cpp/getopt/ut/opt_ut.cpp b/library/cpp/getopt/ut/opt_ut.cpp new file mode 100644 index 0000000000..441aa493a0 --- /dev/null +++ b/library/cpp/getopt/ut/opt_ut.cpp @@ -0,0 +1,52 @@ +#include <library/cpp/getopt/opt.h> + +#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[] = { + (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()); + } + + Y_UNIT_TEST(TestFreeArguments) { + Opt::Ion options[] = { + {"some-option", Opt::WithArg, nullptr, 123}, + {nullptr, Opt::WithoutArg, nullptr, 0}}; + const char* argv[] = {"cmd", "ARG1", "-some-option", "ARG2", "ARG3", nullptr}; + int argc = 5; + Opt opts(argc, argv, "", options); + + UNIT_ASSERT_VALUES_EQUAL(JoinStrings(opts.GetFreeArgs(), ", "), "ARG1, ARG3"); + } + + Y_UNIT_TEST(TestLongOption) { + const int SOME_OPTION_ID = 12345678; + Opt::Ion options[] = { + {"some-option", Opt::WithArg, nullptr, SOME_OPTION_ID}, + {nullptr, Opt::WithoutArg, nullptr, 0}}; + for (int doubleDash = 0; doubleDash <= 1; ++doubleDash) { + const char* argv[] = {"cmd", "ARG1", (doubleDash ? "--some-option" : "-some-option"), "ARG2", "ARG3", nullptr}; + int argc = 5; + Opt opts(argc, argv, "", options); + + TString optionValue = ""; + int optlet = 0; + while ((optlet = opts.Get()) != EOF) { + if (optlet == SOME_OPTION_ID) { + optionValue = opts.GetArg(); + } else { + UNIT_FAIL("don't expected any options, except -some-option"); + } + } + UNIT_ASSERT_VALUES_EQUAL(optionValue, "ARG2"); + } + } +} |