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.

181 lines
5.4 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 m;
  31. auto cli1 = (
  32. (
  33. option("a").set(m.a),
  34. option("b").set(m.b),
  35. option("c").set(m.c)
  36. ).doc("group abc")
  37. & (
  38. option("d").set(m.d),
  39. option("e").set(m.e),
  40. option("f").set(m.f)
  41. ).doc("group def")
  42. );
  43. run_wrapped_variants({ __FILE__, lineNo }, args, cli1,
  44. [&]{ m = active{}; },
  45. [&]{ return m == matches; });
  46. auto cli2 = (
  47. (
  48. option("x"),
  49. (
  50. option("a").set(m.a),
  51. option("b").set(m.b),
  52. option("c").set(m.c)
  53. ).doc("group abc")
  54. ).doc("group x(abc)")
  55. & (
  56. option("d").set(m.d),
  57. option("e").set(m.e),
  58. option("f").set(m.f)
  59. ).doc("group def")
  60. );
  61. run_wrapped_variants({ __FILE__, lineNo }, args, cli2,
  62. [&]{ m = active{}; },
  63. [&]{ return m == matches; });
  64. auto cli3 = (
  65. (
  66. ( option("a").set(m.a),
  67. option("b").set(m.b)
  68. ).doc("group ab")
  69. ,
  70. option("c").set(m.c)
  71. ).doc("group (ab)c")
  72. & (
  73. ( option("d").set(m.d),
  74. option("e").set(m.e)
  75. ).doc("group de")
  76. ,
  77. option("f").set(m.f)
  78. ).doc("group (de)f")
  79. );
  80. run_wrapped_variants({ __FILE__, lineNo }, args, cli3,
  81. [&]{ m = active{}; },
  82. [&]{ return m == matches; });
  83. auto cli4 = (
  84. (
  85. (
  86. ( option("a").set(m.a),
  87. option("b").set(m.b)
  88. ).doc("group ab")
  89. ,
  90. option("c").set(m.c)
  91. ).doc("group (ab)c"),
  92. option("x")
  93. ).doc("group ((ab)c)x")
  94. & (
  95. (
  96. ( option("d").set(m.d),
  97. option("e").set(m.e)
  98. ).doc("group de")
  99. ,
  100. option("f").set(m.f)
  101. ).doc("group (de)f"),
  102. option("y")
  103. ).doc("group ((de)f)y")
  104. );
  105. run_wrapped_variants({ __FILE__, lineNo }, args, cli4,
  106. [&]{ m = active{}; },
  107. [&]{ return m == matches; });
  108. }
  109. //-------------------------------------------------------------------
  110. int main()
  111. {
  112. try {
  113. test(__LINE__, {""}, active{});
  114. test(__LINE__, {"a"}, active{1,0,0,0,0,0});
  115. test(__LINE__, {"b"}, active{0,1,0,0,0,0});
  116. test(__LINE__, {"c"}, active{0,0,1,0,0,0});
  117. test(__LINE__, {"d"}, active{0,0,0,0,0,0});
  118. test(__LINE__, {"e"}, active{0,0,0,0,0,0});
  119. test(__LINE__, {"f"}, active{0,0,0,0,0,0});
  120. test(__LINE__, {"a", "d"}, active{1,0,0,1,0,0});
  121. test(__LINE__, {"a", "e"}, active{1,0,0,0,1,0});
  122. test(__LINE__, {"a", "f"}, active{1,0,0,0,0,1});
  123. test(__LINE__, {"b", "d"}, active{0,1,0,1,0,0});
  124. test(__LINE__, {"b", "e"}, active{0,1,0,0,1,0});
  125. test(__LINE__, {"b", "f"}, active{0,1,0,0,0,1});
  126. test(__LINE__, {"c", "d"}, active{0,0,1,1,0,0});
  127. test(__LINE__, {"c", "e"}, active{0,0,1,0,1,0});
  128. test(__LINE__, {"c", "f"}, active{0,0,1,0,0,1});
  129. test(__LINE__, {"c", "b", "d"}, active{0,1,1,1,0,0});
  130. test(__LINE__, {"b", "a", "e"}, active{1,1,0,0,1,0});
  131. test(__LINE__, {"c", "a", "f"}, active{1,0,1,0,0,1});
  132. test(__LINE__, {"c", "a", "b", "d"}, active{1,1,1,1,0,0});
  133. test(__LINE__, {"b", "c", "a", "e"}, active{1,1,1,0,1,0});
  134. test(__LINE__, {"c", "b", "a", "f"}, active{1,1,1,0,0,1});
  135. test(__LINE__, {"c", "a", "b", "d", "e"}, active{1,1,1,1,1,0});
  136. test(__LINE__, {"b", "c", "a", "e", "d"}, active{1,1,1,1,1,0});
  137. test(__LINE__, {"c", "b", "a", "f", "d"}, active{1,1,1,1,0,1});
  138. test(__LINE__, {"c", "b", "a", "d", "f"}, active{1,1,1,1,0,1});
  139. test(__LINE__, {"c", "b", "a", "f", "e"}, active{1,1,1,0,1,1});
  140. test(__LINE__, {"c", "b", "a", "e", "f"}, active{1,1,1,0,1,1});
  141. test(__LINE__, {"a", "b", "c", "d", "e", "f"}, active{1,1,1,1,1,1});
  142. test(__LINE__, {"c", "b", "a", "f", "e", "d"}, active{1,1,1,1,1,1});
  143. test(__LINE__, {"c", "b", "a", "f", "d", "e"}, active{1,1,1,1,1,1});
  144. test(__LINE__, {"c", "b", "a", "e", "d", "f"}, active{1,1,1,1,1,1});
  145. }
  146. catch(std::exception& e) {
  147. std::cerr << e.what() << std::endl;
  148. return 1;
  149. }
  150. }