opt2_ut.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <library/cpp/getopt/opt2.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. //using namespace NLastGetopt;
  4. Y_UNIT_TEST_SUITE(Opt2Test) {
  5. Y_UNIT_TEST(TestSimple) {
  6. int argc = 8;
  7. char* argv[] = {
  8. (char*)"cmd",
  9. (char*)"--aaaa=aaaa",
  10. (char*)"zz",
  11. (char*)"-x1",
  12. (char*)"-x2",
  13. (char*)"-c",
  14. (char*)"-d8",
  15. (char*)"ww",
  16. };
  17. Opt2 opt(argc, argv, "A:b:cd:e:x:", 2, "aaaa=A");
  18. const char* edef = "edef";
  19. const char* a = opt.Arg('A', "<var_name> - usage of -A");
  20. int b = opt.Int('b', "<var_name> - usage of -b", 2);
  21. bool c = opt.Has('c', "usage of -c");
  22. int d = opt.Int('d', "<var_name> - usage of -d", 13);
  23. const char* e = opt.Arg('e', "<unused> - only default is really used", edef);
  24. const TVector<const char*>& x = opt.MArg('x', "<var_name> - usage of -x");
  25. UNIT_ASSERT(!opt.AutoUsage("<L> <M>"));
  26. UNIT_ASSERT_VALUES_EQUAL("aaaa", a);
  27. UNIT_ASSERT_VALUES_EQUAL(2, b);
  28. UNIT_ASSERT(c);
  29. UNIT_ASSERT_VALUES_EQUAL(8, d);
  30. UNIT_ASSERT_VALUES_EQUAL((void*)edef, e);
  31. UNIT_ASSERT_VALUES_EQUAL(2u, opt.Pos.size());
  32. UNIT_ASSERT_STRINGS_EQUAL("zz", opt.Pos.at(0));
  33. UNIT_ASSERT_VALUES_EQUAL((void*)argv[2], opt.Pos.at(0));
  34. UNIT_ASSERT_STRINGS_EQUAL("ww", opt.Pos.at(1));
  35. UNIT_ASSERT_STRINGS_EQUAL("1", x.at(0));
  36. UNIT_ASSERT_STRINGS_EQUAL("2", x.at(1));
  37. }
  38. Y_UNIT_TEST(TestErrors1) {
  39. int argc = 4;
  40. char* argv[] = {
  41. (char*)"cmd",
  42. (char*)"zz",
  43. (char*)"-c",
  44. (char*)"-e",
  45. };
  46. Opt2 opt(argc, argv, "ce:", 2);
  47. const char* edef = "edef";
  48. bool c = opt.Has('c', "usage of -c");
  49. const char* e = opt.Arg('e', "<unused> - only default is really used", edef);
  50. UNIT_ASSERT(c);
  51. UNIT_ASSERT_VALUES_EQUAL((void*)edef, e);
  52. }
  53. }