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.

138 lines
5.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 && x.d == y.d &&
  21. 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)
  28. {
  29. using namespace clipp;
  30. active m1;
  31. auto cli1 = (
  32. option("a").set(m1.a),
  33. group(
  34. command("b").set(m1.b),
  35. command("c").set(m1.c),
  36. command("d").set(m1.d),
  37. command("e").set(m1.e)
  38. ),
  39. option("f").set(m1.f)
  40. );
  41. //equivalent interface
  42. active m2;
  43. auto cli2 = (
  44. option("a").set(m2.a),
  45. (
  46. required("b").set(m2.b) &
  47. required("c").set(m2.c) &
  48. required("d").set(m2.d) &
  49. required("e").set(m2.e)
  50. ),
  51. option("f").set(m2.f)
  52. );
  53. run_wrapped_variants({ __FILE__, lineNo }, args, cli1,
  54. [&]{ m1 = active{}; },
  55. [&]{ return m1 == matches; });
  56. run_wrapped_variants({ __FILE__, lineNo }, args, cli2,
  57. [&]{ m2 = active{}; },
  58. [&]{ return m2 == matches; });
  59. }
  60. //-------------------------------------------------------------------
  61. int main()
  62. {
  63. try {
  64. test(__LINE__, {""}, active{});
  65. test(__LINE__, {"a"}, active{1,0,0,0,0,0});
  66. test(__LINE__, {"f"}, active{0,0,0,0,0,1});
  67. test(__LINE__, {"b"}, active{0,1,0,0,0,0});
  68. test(__LINE__, {"c"}, active{});
  69. test(__LINE__, {"d"}, active{});
  70. test(__LINE__, {"e"}, active{});
  71. test(__LINE__, {"b", "c"}, active{0,1,1,0,0,0});
  72. test(__LINE__, {"b", "c", "d"}, active{0,1,1,1,0,0});
  73. test(__LINE__, {"b", "c", "d", "e"}, active{0,1,1,1,1,0});
  74. test(__LINE__, {"b", "c", "d", "e", "f"}, active{0,1,1,1,1,1});
  75. test(__LINE__, {"b", "c", "d", "e", "a"}, active{1,1,1,1,1,0});
  76. test(__LINE__, {"a", "b"}, active{1,1,0,0,0,0});
  77. test(__LINE__, {"a", "b", "c"}, active{1,1,1,0,0,0});
  78. test(__LINE__, {"a", "b", "c", "d"}, active{1,1,1,1,0,0});
  79. test(__LINE__, {"a", "b", "c", "d", "e"}, active{1,1,1,1,1,0});
  80. test(__LINE__, {"f", "b"}, active{0,1,0,0,0,1});
  81. test(__LINE__, {"f", "b", "c"}, active{0,1,1,0,0,1});
  82. test(__LINE__, {"f", "b", "c", "d"}, active{0,1,1,1,0,1});
  83. test(__LINE__, {"f", "b", "c", "d", "e"}, active{0,1,1,1,1,1});
  84. test(__LINE__, {"a", "b", "c", "d", "e", "f"}, active{1,1,1,1,1,1});
  85. test(__LINE__, {"f", "b", "c", "d", "e", "a"}, active{1,1,1,1,1,1});
  86. test(__LINE__, {"a", "f", "b", "c", "d", "e"}, active{1,1,1,1,1,1});
  87. test(__LINE__, {"f", "a", "b", "c", "d", "e"}, active{1,1,1,1,1,1});
  88. test(__LINE__, {"b", "c", "d", "e", "a", "f"}, active{1,1,1,1,1,1});
  89. test(__LINE__, {"b", "c", "d", "e", "f", "a"}, active{1,1,1,1,1,1});
  90. test(__LINE__, {"b", "c", "a"}, active{1,1,1,0,0,0});
  91. test(__LINE__, {"b", "c", "f"}, active{0,1,1,0,0,1});
  92. test(__LINE__, {"b", "a", "c"}, active{1,1,0,0,0,0});
  93. test(__LINE__, {"b", "f", "c"}, active{0,1,0,0,0,1});
  94. test(__LINE__, {"b", "c", "d", "a"}, active{1,1,1,1,0,0});
  95. test(__LINE__, {"b", "c", "d", "f"}, active{0,1,1,1,0,1});
  96. test(__LINE__, {"b", "c", "a", "d"}, active{1,1,1,0,0,0});
  97. test(__LINE__, {"b", "c", "f", "d"}, active{0,1,1,0,0,1});
  98. test(__LINE__, {"b", "a", "c", "d"}, active{1,1,0,0,0,0});
  99. test(__LINE__, {"b", "f", "c", "d"}, active{0,1,0,0,0,1});
  100. test(__LINE__, {"b", "a", "c", "d", "e"}, active{1,1,0,0,0,0});
  101. test(__LINE__, {"b", "f", "c", "d", "e"}, active{0,1,0,0,0,1});
  102. test(__LINE__, {"b", "c", "d", "f", "a", "e"}, active{1,1,1,1,0,1});
  103. test(__LINE__, {"b", "c", "f", "d", "a", "e"}, active{1,1,1,0,0,1});
  104. test(__LINE__, {"b", "c", "a", "d", "f", "e"}, active{1,1,1,0,0,1});
  105. test(__LINE__, {"b", "a", "c", "f", "d", "e"}, active{1,1,0,0,0,1});
  106. test(__LINE__, {"b", "f", "c", "a", "d", "e"}, active{1,1,0,0,0,1});
  107. test(__LINE__, {"b", "c", "d", "a", "e"}, active{1,1,1,1,0,0});
  108. }
  109. catch(std::exception& e) {
  110. std::cerr << e.what() << std::endl;
  111. return 1;
  112. }
  113. }