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.

84 lines
2.3 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 a_, bool b_,
  16. std::initializer_list<int> i_, std::initializer_list<int> j_)
  17. :
  18. a{a_}, b{b_}, i{i_}, j{j_}
  19. {}
  20. bool a = false, b = false;
  21. std::vector<int> i, j;
  22. friend bool operator == (const active& x, const active& y) noexcept {
  23. return x.a == y.a && x.b == y.b &&
  24. std::equal(begin(x.i), end(x.i), begin(y.i)) &&
  25. std::equal(begin(x.j), end(x.j), begin(y.j));
  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 = repeatable(
  36. ( option("a").set(m.a) & value("i",m.i) ) |
  37. ( option("b").set(m.b) & value("j",m.j) )
  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__, {"a"}, active{1,0, {}, {} });
  49. test(__LINE__, {"b"}, active{0,1, {}, {} });
  50. test(__LINE__, {"a", "12"}, active{1,0, {12}, {} });
  51. test(__LINE__, {"b", "34"}, active{0,1, {}, {34} });
  52. test(__LINE__, {"a", "12", "b", "34"}, active{1,1, {12}, {34} });
  53. test(__LINE__, {"a", "12", "a", "34"}, active{1,0, {12,34}, {} });
  54. test(__LINE__, {"b", "12", "b", "34"}, active{0,1, {}, {12,34} });
  55. test(__LINE__, {"a", "1", "b", "2", "a", "3"}, active{1,1, {1,3}, {2} });
  56. test(__LINE__, {"a", "1", "b", "2", "b", "4", "a", "3"}, active{1,1, {1,3}, {2,4} });
  57. test(__LINE__, {"a", "b", "2", "b", "a", "3"}, active{1,1, {3}, {2} });
  58. }
  59. catch(std::exception& e) {
  60. std::cerr << e.what() << std::endl;
  61. return 1;
  62. }
  63. }