posix_getopt_ut.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <library/cpp/getopt/posix_getopt.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NLastGetopt;
  4. Y_UNIT_TEST_SUITE(TPosixGetoptTest) {
  5. Y_UNIT_TEST(TestSimple) {
  6. int argc = 6;
  7. const char* argv0[] = {"program", "-b", "-f1", "-f", "2", "zzzz"};
  8. char** const argv = (char**)argv0;
  9. NLastGetopt::optreset = 1;
  10. UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt(argc, argv, "bf:"));
  11. UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt(argc, argv, "bf:"));
  12. UNIT_ASSERT_VALUES_EQUAL(NLastGetopt::optarg, TString("1"));
  13. UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt(argc, argv, "bf:"));
  14. UNIT_ASSERT_VALUES_EQUAL(NLastGetopt::optarg, TString("2"));
  15. UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt(argc, argv, "bf:"));
  16. UNIT_ASSERT_VALUES_EQUAL(5, NLastGetopt::optind);
  17. }
  18. Y_UNIT_TEST(TestLong) {
  19. int daggerset = 0;
  20. /* options descriptor */
  21. const NLastGetopt::option longopts[] = {
  22. {"buffy", no_argument, nullptr, 'b'},
  23. {"fluoride", required_argument, nullptr, 'f'},
  24. {"daggerset", no_argument, &daggerset, 1},
  25. {nullptr, 0, nullptr, 0}};
  26. int argc = 7;
  27. const char* argv0[] = {"program", "-b", "--buffy", "-f1", "--fluoride=2", "--daggerset", "zzzz"};
  28. char** const argv = (char**)argv0;
  29. int longIndex;
  30. NLastGetopt::optreset = 1;
  31. UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, &longIndex));
  32. UNIT_ASSERT_VALUES_EQUAL(0, longIndex);
  33. UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
  34. UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, &longIndex));
  35. UNIT_ASSERT_VALUES_EQUAL(1, longIndex);
  36. UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
  37. UNIT_ASSERT_VALUES_EQUAL(0, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
  38. UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
  39. UNIT_ASSERT_VALUES_EQUAL(6, NLastGetopt::optind);
  40. }
  41. Y_UNIT_TEST(TestLongPermutation) {
  42. int daggerset = 0;
  43. /* options descriptor */
  44. const NLastGetopt::option longopts[] = {
  45. {"buffy", no_argument, nullptr, 'b'},
  46. {"fluoride", required_argument, nullptr, 'f'},
  47. {"daggerset", no_argument, &daggerset, 1},
  48. {nullptr, 0, nullptr, 0}};
  49. int argc = 7;
  50. const char* argv0[] = {"program", "aa", "-b", "bb", "cc", "--buffy", "dd"};
  51. char** const argv = (char**)argv0;
  52. NLastGetopt::optreset = 1;
  53. UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
  54. UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
  55. UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
  56. UNIT_ASSERT_VALUES_EQUAL(3, NLastGetopt::optind);
  57. }
  58. Y_UNIT_TEST(TestNoOptionsOptionsWithDoubleDash) {
  59. const NLastGetopt::option longopts[] = {
  60. {"buffy", no_argument, nullptr, 'b'},
  61. {"fluoride", no_argument, nullptr, 'f'},
  62. {nullptr, 0, nullptr, 0}};
  63. int argc = 2;
  64. const char* argv0[] = {"program", "--bf"};
  65. char** const argv = (char**)argv0;
  66. NLastGetopt::optreset = 1;
  67. UNIT_ASSERT_VALUES_EQUAL('?', NLastGetopt::getopt_long(argc, argv, "bf", longopts, nullptr));
  68. }
  69. Y_UNIT_TEST(TestLongOnly) {
  70. const NLastGetopt::option longopts[] = {
  71. {"foo", no_argument, nullptr, 'F'},
  72. {"fluoride", no_argument, nullptr, 'f'},
  73. {"ogogo", no_argument, nullptr, 'o'},
  74. {nullptr, 0, nullptr, 0}};
  75. int argc = 4;
  76. const char* argv0[] = {"program", "--foo", "-foo", "-fo"};
  77. char** const argv = (char**)argv0;
  78. NLastGetopt::optreset = 1;
  79. UNIT_ASSERT_VALUES_EQUAL('F', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
  80. UNIT_ASSERT_VALUES_EQUAL('F', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
  81. UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
  82. UNIT_ASSERT_VALUES_EQUAL('o', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
  83. UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
  84. }
  85. Y_UNIT_TEST(TestLongWithoutOnlySingleDashNowAllowed) {
  86. const NLastGetopt::option longopts[] = {
  87. {"foo", no_argument, nullptr, 'F'},
  88. {"zoo", no_argument, nullptr, 'z'},
  89. {nullptr, 0, nullptr, 0}};
  90. int argc = 2;
  91. const char* argv0[] = {"program", "-foo"};
  92. char** const argv = (char**)argv0;
  93. NLastGetopt::optreset = 1;
  94. UNIT_ASSERT_VALUES_EQUAL('?', NLastGetopt::getopt_long(argc, argv, "z", longopts, nullptr));
  95. }
  96. }