modchooser_ut.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <library/cpp/getopt/modchooser.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/stream/str.h>
  4. void ValidateArgcArgv(int argc, const char** argv) {
  5. UNIT_ASSERT_EQUAL(argc, 1);
  6. UNIT_ASSERT_EQUAL(argv[argc], nullptr);
  7. }
  8. int One(int argc, const char** argv) {
  9. ValidateArgcArgv(argc, argv);
  10. return 1;
  11. }
  12. int Two(int argc, const char** argv) {
  13. ValidateArgcArgv(argc, argv);
  14. return 2;
  15. }
  16. int Three(int argc, const char** argv) {
  17. ValidateArgcArgv(argc, argv);
  18. return 3;
  19. }
  20. int Four(int argc, const char** argv) {
  21. ValidateArgcArgv(argc, argv);
  22. return 4;
  23. }
  24. int Five(int argc, const char** argv) {
  25. ValidateArgcArgv(argc, argv);
  26. return 5;
  27. }
  28. typedef int (*F_PTR)(int, const char**);
  29. static const F_PTR FUNCTIONS[] = {One, Two, Three, Four, Five};
  30. static const char* NAMES[] = {"one", "two", "three", "four", "five"};
  31. static_assert(Y_ARRAY_SIZE(FUNCTIONS) == Y_ARRAY_SIZE(NAMES), "Incorrect input tests data");
  32. Y_UNIT_TEST_SUITE(TModChooserTest) {
  33. Y_UNIT_TEST(TestModesSimpleRunner) {
  34. TModChooser chooser;
  35. for (size_t idx = 0; idx < Y_ARRAY_SIZE(NAMES); ++idx) {
  36. chooser.AddMode(NAMES[idx], FUNCTIONS[idx], NAMES[idx]);
  37. }
  38. // test argc, argv
  39. for (size_t idx = 0; idx < Y_ARRAY_SIZE(NAMES); ++idx) {
  40. int argc = 2;
  41. const char* argv[] = {"UNITTEST", NAMES[idx], nullptr};
  42. UNIT_ASSERT_EQUAL(static_cast<int>(idx) + 1, chooser.Run(argc, argv));
  43. }
  44. // test TVector<TString> argv
  45. for (size_t idx = 0; idx < Y_ARRAY_SIZE(NAMES); ++idx) {
  46. const TVector<TString> argv = {"UNITTEST", NAMES[idx]};
  47. UNIT_ASSERT_EQUAL(static_cast<int>(idx) + 1, chooser.Run(argv));
  48. }
  49. }
  50. Y_UNIT_TEST(TestHelpMessage) {
  51. TModChooser chooser;
  52. int argc = 2;
  53. const char* argv[] = {"UNITTEST", "-?", nullptr};
  54. chooser.Run(argc, argv);
  55. }
  56. }