ygetopt_ut.cpp 967 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <library/cpp/getopt/ygetopt.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. class TGetOptTest: public TTestBase {
  4. UNIT_TEST_SUITE(TGetOptTest);
  5. UNIT_TEST(TestGetOpt);
  6. UNIT_TEST_EXCEPTION(TestZeroArgC, yexception);
  7. UNIT_TEST_SUITE_END();
  8. public:
  9. void TestGetOpt();
  10. void TestZeroArgC();
  11. };
  12. UNIT_TEST_SUITE_REGISTRATION(TGetOptTest);
  13. void TGetOptTest::TestZeroArgC() {
  14. TGetOpt opt(0, nullptr, "");
  15. }
  16. void TGetOptTest::TestGetOpt() {
  17. const char* argv[] = {
  18. "/usr/bin/bash",
  19. "-f",
  20. "-p",
  21. "qwerty123",
  22. "-z",
  23. "-q",
  24. nullptr};
  25. TString res;
  26. const TString format = "qzp:f";
  27. TGetOpt opt(sizeof(argv) / sizeof(*argv) - 1, argv, format);
  28. for (TGetOpt::TIterator it = opt.Begin(); it != opt.End(); ++it) {
  29. res += it->Key();
  30. if (it->HaveArg()) {
  31. res += it->Arg();
  32. }
  33. }
  34. UNIT_ASSERT_EQUAL(res, "fpqwerty123zq");
  35. }