You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

2 years ago
  1. /*****************************************************************************
  2. *
  3. * CLIPP - command line interfaces for modern C++
  4. *
  5. * released under MIT license
  6. *
  7. * (c) 2017-2018 André Müller; foss@andremueller-online.de
  8. *
  9. *****************************************************************************/
  10. #include "testing.h"
  11. //-------------------------------------------------------------------
  12. struct active {
  13. active() = default;
  14. explicit
  15. active(bool n_, std::initializer_list<int> nums_,
  16. bool s_, const std::string& str_)
  17. :
  18. str{str_}, nums{nums_}, n{n_}, s{s_}
  19. {}
  20. std::string str;
  21. std::vector<int> nums;
  22. bool n = false, s = false;
  23. friend bool operator == (const active& x, const active& y) noexcept {
  24. return x.n == y.n && x.s == y.s && x.str == y.str &&
  25. std::equal(x.nums.begin(), x.nums.end(), y.nums.begin());
  26. }
  27. };
  28. //-------------------------------------------------------------------
  29. void test(int lineNo,
  30. const std::initializer_list<const char*> args,
  31. const active& matches)
  32. {
  33. using namespace clipp;
  34. active m;
  35. auto cli = (
  36. option("-n", "--num").set(m.n) & integers("nums", m.nums),
  37. option("-s", "--str").set(m.s) & value("str", m.str)
  38. );
  39. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  40. [&]{ m = active{}; },
  41. [&]{ return m == matches; });
  42. }
  43. //-------------------------------------------------------------------
  44. int main()
  45. {
  46. try {
  47. test(__LINE__, {""}, active{});
  48. test(__LINE__, {"-n"}, active{true, {}, false, ""});
  49. test(__LINE__, {"-n", "1"}, active{true, {1}, false, ""});
  50. test(__LINE__, {"-n", "1", "5"}, active{true, {1,5}, false, ""});
  51. test(__LINE__, {"-n", "1", "-3", "4"}, active{true, {1,-3,4}, false, ""});
  52. test(__LINE__, {"-s"}, active{false, {}, true, ""});
  53. test(__LINE__, {"-s", "xyz"}, active{false, {}, true, "xyz"});
  54. test(__LINE__, {"-n", "1", "ab"}, active{true, {1}, false, ""});
  55. test(__LINE__, {"-n", "1", "2", "ab"}, active{true, {1,2}, false, ""});
  56. test(__LINE__, {"-n", "1", "-3", "ab"}, active{true, {1,-3}, false, ""});
  57. test(__LINE__, {"-n", "1", "-s", "ab"}, active{true, {1}, true, "ab"});
  58. test(__LINE__, {"-n", "1", "2", "-s", "ab"}, active{true, {1,2}, true, "ab"});
  59. test(__LINE__, {"-n", "1", "-3", "-s", "ab"}, active{true, {1,-3}, true, "ab"});
  60. }
  61. catch(std::exception& e) {
  62. std::cerr << e.what() << std::endl;
  63. return 1;
  64. }
  65. }