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.

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