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.

89 lines
3.0 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_, bool c_, bool d_, bool e_, bool f_) :
  16. a{a_}, b{b_}, c{c_}, d{d_}, e{e_}, f{f_}
  17. {}
  18. bool a = false, b = false, c = false, d = false, e = false, f = false;
  19. friend bool operator == (const active& x, const active& y) noexcept {
  20. return (x.a == y.a && x.b == y.b && x.c == y.c &&
  21. x.d == y.d && x.e == y.e && x.f == y.f );
  22. }
  23. };
  24. //-------------------------------------------------------------------
  25. void test(int lineNo,
  26. const std::initializer_list<const char*> args,
  27. const active& matches, const active& missing)
  28. {
  29. using namespace clipp;
  30. active v;
  31. active m;
  32. auto cli = (
  33. option("a").set(v.a).if_missing(set(m.a)),
  34. (
  35. option("b").set(v.b).if_missing(set(m.b)) |
  36. required("c").set(v.c).if_missing(set(m.c))
  37. ),
  38. (
  39. required("d").set(v.d).if_missing(set(m.d)) |
  40. required("e").set(v.e).if_missing(set(m.e))
  41. ),
  42. required("f").set(v.f).if_missing(set(m.f))
  43. );
  44. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  45. [&]{ m = active{}; v = active{}; },
  46. [&]{ return v == matches && m == missing; });
  47. }
  48. //-------------------------------------------------------------------
  49. int main()
  50. {
  51. try {
  52. test(__LINE__, {""}, active{0,0,0,0,0,0}, active{0,0,0,1,1,1});
  53. test(__LINE__, {"a"}, active{1,0,0,0,0,0}, active{0,0,0,1,1,1});
  54. test(__LINE__, {"b"}, active{0,1,0,0,0,0}, active{0,0,0,1,1,1});
  55. test(__LINE__, {"c"}, active{0,0,1,0,0,0}, active{0,0,0,1,1,1});
  56. test(__LINE__, {"d"}, active{0,0,0,1,0,0}, active{0,0,0,0,0,1});
  57. test(__LINE__, {"e"}, active{0,0,0,0,1,0}, active{0,0,0,0,0,1});
  58. test(__LINE__, {"f"}, active{0,0,0,0,0,1}, active{0,0,0,1,1,0});
  59. test(__LINE__, {"d", "f"}, active{0,0,0,1,0,1}, active{0,0,0,0,0,0});
  60. test(__LINE__, {"e", "f"}, active{0,0,0,0,1,1}, active{0,0,0,0,0,0});
  61. test(__LINE__, {"b", "f"}, active{0,1,0,0,0,1}, active{0,0,0,1,1,0});
  62. test(__LINE__, {"c", "f"}, active{0,0,1,0,0,1}, active{0,0,0,1,1,0});
  63. test(__LINE__, {"b", "d", "f"}, active{0,1,0,1,0,1}, active{0,0,0,0,0,0});
  64. test(__LINE__, {"c", "d", "f"}, active{0,0,1,1,0,1}, active{0,0,0,0,0,0});
  65. test(__LINE__, {"b", "e", "f"}, active{0,1,0,0,1,1}, active{0,0,0,0,0,0});
  66. test(__LINE__, {"c", "e", "f"}, active{0,0,1,0,1,1}, active{0,0,0,0,0,0});
  67. }
  68. catch(std::exception& e) {
  69. std::cerr << e.what() << std::endl;
  70. return 1;
  71. }
  72. }