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.

187 lines
9.7 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. class active {
  13. public:
  14. active() { for(auto& x : b) x = false; }
  15. active(std::initializer_list<int> il) {
  16. if(il.size() != 17)
  17. throw std::logic_error{"test active not properly initialized"};
  18. auto i = il.begin();
  19. for(auto& x : b) {
  20. x = *i;
  21. ++i;
  22. }
  23. }
  24. bool& operator [] (int i) noexcept { return b[i]; }
  25. bool operator [] (int i) const noexcept { return b[i]; }
  26. friend bool operator == (const active& x, const active& y) noexcept {
  27. using std::begin; using std::end;
  28. return std::equal(begin(x.b), end(x.b), begin(y.b));
  29. }
  30. private:
  31. bool b[17];
  32. };
  33. //-------------------------------------------------------------------
  34. void test(int lineNo,
  35. const std::initializer_list<const char*> args,
  36. const active& matches,
  37. const active& conflict = active{})
  38. {
  39. using namespace clipp;
  40. active m;
  41. active c;
  42. auto cmd_a = command ("a" ).set(m[ 0]).if_conflicted(set(c[ 0]));
  43. auto cmd_b = command ("b" ).set(m[ 1]).if_conflicted(set(c[ 1]));
  44. auto cmd_c = command ("c" ).set(m[ 2]).if_conflicted(set(c[ 2]));
  45. auto opt_d = option ("-d" ).set(m[ 3]).if_conflicted(set(c[ 3]));
  46. auto opt_e = option ("-e" ).set(m[ 4]).if_conflicted(set(c[ 4]));
  47. auto cmd_f = command ("f" ).set(m[ 5]).if_conflicted(set(c[ 5]));
  48. auto opt_g = option ("--gee").set(m[ 6]).if_conflicted(set(c[ 6]));
  49. auto cmd_h = command ("h" ).set(m[ 7]).if_conflicted(set(c[ 7]));
  50. auto cmd_i = command ("i" ).set(m[ 8]).if_conflicted(set(c[ 8]));
  51. auto cmd_j = command ("j" ).set(m[ 9]).if_conflicted(set(c[ 9]));
  52. auto opt_k = option ("k" ).set(m[10]).if_conflicted(set(c[10]));
  53. auto cmd_l = command ("l" ).set(m[11]).if_conflicted(set(c[11]));
  54. auto cmd_m = command ("m" ).set(m[12]).if_conflicted(set(c[12]));
  55. auto req_n = required(">n" ).set(m[13]).if_conflicted(set(c[13]));
  56. auto opt_o = option ("-o" ).set(m[14]).if_conflicted(set(c[14]));
  57. auto opt_x = option ("-x" ).set(m[15]).if_conflicted(set(c[15]));
  58. auto opt_y = option ("y" ).set(m[16]).if_conflicted(set(c[16]));
  59. auto cli = (
  60. cmd_a
  61. | ( (cmd_b | cmd_c), opt_d, opt_e)
  62. | ( cmd_f, opt_g)
  63. | ( cmd_h, ( cmd_i
  64. | (cmd_j, opt_k)
  65. | ((cmd_l | cmd_m), req_n, opt_o)
  66. )
  67. ),
  68. opt_x, opt_y
  69. );
  70. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  71. [&]{ m = active{}; c = active{}; },
  72. [&]{ return m == matches && c == conflict; });
  73. }
  74. //-------------------------------------------------------------------
  75. int main()
  76. {
  77. try {
  78. test(__LINE__, {""}, active{});
  79. test(__LINE__, {"-x"}, active{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0});
  80. test(__LINE__, {"y"}, active{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1});
  81. test(__LINE__, {"-x", "y"}, active{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1});
  82. test(__LINE__, {"y", "-x"}, active{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1});
  83. test(__LINE__, {"a"}, active{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0});
  84. test(__LINE__, {"b"}, active{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0});
  85. test(__LINE__, {"b", "-d", "-e"}, active{0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0});
  86. test(__LINE__, {"b", "-e", "-d"}, active{0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0});
  87. test(__LINE__, {"c"}, active{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0});
  88. test(__LINE__, {"c", "-d", "-e"}, active{0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0});
  89. test(__LINE__, {"c", "-e", "-d"}, active{0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0});
  90. test(__LINE__, {"f"}, active{0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0});
  91. test(__LINE__, {"f", "--gee"}, active{0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0});
  92. test(__LINE__, {"h"}, active{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0});
  93. test(__LINE__, {"h", "i"}, active{0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0});
  94. test(__LINE__, {"h", "j"}, active{0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0});
  95. test(__LINE__, {"h", "j", "k"}, active{0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0});
  96. test(__LINE__, {"h", "l"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0});
  97. test(__LINE__, {"h", "l", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0});
  98. test(__LINE__, {"h", "l", "-o", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0});
  99. test(__LINE__, {"h", "l", ">n", "-o"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0});
  100. test(__LINE__, {"h", "m"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0});
  101. test(__LINE__, {"h", "m", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0});
  102. test(__LINE__, {"h", "m", "-o", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,0,0});
  103. test(__LINE__, {"h", "m", ">n", "-o"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,0,0});
  104. test(__LINE__, {"a", "y", "-x"}, active{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1});
  105. test(__LINE__, {"b", "y", "-x"}, active{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1});
  106. test(__LINE__, {"b", "-e", "-d", "y", "-x"}, active{0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1});
  107. test(__LINE__, {"b", "-e", "-d", "-x", "y"}, active{0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1});
  108. test(__LINE__, {"c", "y", "-x"}, active{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1});
  109. test(__LINE__, {"c", "-e", "-d", "y", "-x"}, active{0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1});
  110. test(__LINE__, {"c", "-e", "-d", "-x", "y"}, active{0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1});
  111. test(__LINE__, {"f", "y", "-x"}, active{0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1});
  112. test(__LINE__, {"f", "--gee", "y", "-x"}, active{0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1});
  113. test(__LINE__, {"f", "--gee", "-x", "y"}, active{0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1});
  114. test(__LINE__, {"h"}, active{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0});
  115. test(__LINE__, {"h", "i", "y", "-x"}, active{0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1});
  116. test(__LINE__, {"h", "j"}, active{0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0});
  117. test(__LINE__, {"h", "j", "k", "y", "-x"}, active{0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,1,1});
  118. test(__LINE__, {"h", "j", "k", "-x", "y"}, active{0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,1,1});
  119. test(__LINE__, {"h", "l"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0});
  120. test(__LINE__, {"h", "l", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0});
  121. test(__LINE__, {"h", "l", ">n", "y", "-x"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,1,1});
  122. test(__LINE__, {"h", "l", ">n", "-x", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,1,1});
  123. test(__LINE__, {"h", "l", ">n", "-x", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,1,1});
  124. test(__LINE__, {"h", "l", "-x", ">n", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1});
  125. test(__LINE__, {"h", "l", "-x", "y", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1});
  126. test(__LINE__, {"h", "l", "y", "-x", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1});
  127. test(__LINE__, {"h", "l", "-o", ">n", "y", "-x"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,1,1});
  128. test(__LINE__, {"h", "l", ">n", "-o", "y", "-x"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,1,1});
  129. test(__LINE__, {"h", "l", "-o", ">n", "-x", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,1,1});
  130. test(__LINE__, {"h", "l", ">n", "-x", "-o", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,1,1});
  131. test(__LINE__, {"h", "l", "-x", "-o", ">n", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1});
  132. test(__LINE__, {"h", "l", "-x", "y", ">n", "-o"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1});
  133. test(__LINE__, {"h", "l", "y", "-o", "-x", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1});
  134. test(__LINE__, {"h", "m"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0});
  135. test(__LINE__, {"h", "m", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0});
  136. test(__LINE__, {"h", "m", ">n", "y", "-x"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,1});
  137. test(__LINE__, {"h", "m", ">n", "-x", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,1});
  138. test(__LINE__, {"h", "m", "-x", ">n", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1});
  139. test(__LINE__, {"h", "m", ">n", "-x", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,1});
  140. test(__LINE__, {"h", "m", "-x", "y", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1});
  141. test(__LINE__, {"h", "m", "y", "-x", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1});
  142. test(__LINE__, {"h", "m", "-o", ">n", "y", "-x"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1});
  143. test(__LINE__, {"h", "m", ">n", "-o", "y", "-x"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1});
  144. test(__LINE__, {"h", "m", "-o", ">n", "-x", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1});
  145. test(__LINE__, {"h", "m", "-x", "-o", ">n", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1});
  146. test(__LINE__, {"h", "m", ">n", "-x", "-o", "y"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,1});
  147. test(__LINE__, {"h", "m", "-x", "y", ">n", "-o"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1});
  148. test(__LINE__, {"h", "m", "y", "-o", "-x", ">n"}, active{0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1});
  149. }
  150. catch(std::exception& e) {
  151. std::cerr << e.what() << std::endl;
  152. return 1;
  153. }
  154. }