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.

194 lines
6.3 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. #include <cmath>
  12. //-------------------------------------------------------------------
  13. struct active {
  14. int av = 0;
  15. int bv = 1;
  16. float cv = 0.0f, dv = 1.0f;
  17. double ev = 0.0, fv = 1.0;
  18. std::string gv;
  19. bool a = false, b = false, c = false, d = false, e = false, f = false,
  20. g = false, h = false, i = false;
  21. friend bool operator == (const active& x, const active& y) noexcept {
  22. if(x.a != y.a || x.b != y.b || x.c != y.c ||
  23. x.d != y.d || x.e != y.e || x.f != y.f ||
  24. x.g != y.g || x.h != y.h || x.i != y.i ||
  25. x.av != y.av || x.bv != y.bv || x.gv != y.gv) return false;
  26. using std::abs;
  27. if(abs(x.cv - y.cv) > 1e-4f || abs(x.dv - y.dv) > 1e-4f ||
  28. abs(x.ev - y.ev) > 1e-4 || abs(x.fv - y.fv) > 1e-4) return false;
  29. return true;
  30. }
  31. };
  32. //-------------------------------------------------------------------
  33. void test(int lineNo,
  34. const std::initializer_list<const char*> args,
  35. const active& matches)
  36. {
  37. using namespace clipp;
  38. active m;
  39. auto cli = (
  40. required("-a").set(m.a) & value("A",m.av),
  41. required("-b", "--bb").set(m.b) & value("B",m.bv),
  42. option("-c", "--cc").set(m.c) & value("C",m.cv) & opt_value("D",m.dv),
  43. option("-d", "--dd").set(m.d) & opt_value("D",m.dv),
  44. required("-e", "--ee").set(m.e) & value("E",m.ev),
  45. option("-f", "--ff").set(m.f) & opt_value("F",m.fv),
  46. value("G", m.gv).set(m.g),
  47. option("-h", "--hh", "---hhh").set(m.h),
  48. required("-i", "--ii").set(m.i)
  49. );
  50. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  51. [&]{ m = active{}; },
  52. [&]{ return m == matches; });
  53. }
  54. //-------------------------------------------------------------------
  55. int main()
  56. {
  57. try {
  58. active m;
  59. test(__LINE__, {""}, m);
  60. m = active{}; m.a = true;
  61. test(__LINE__, {"-a"}, m);
  62. m = active{}; m.a = true; m.av = 65;
  63. test(__LINE__, {"-a", "65"}, m);
  64. test(__LINE__, {"-a65"}, m);
  65. m = active{}; m.b = true;
  66. test(__LINE__, {"-b"}, m);
  67. m = active{}; m.b = true; m.bv = 12;
  68. test(__LINE__, {"-b", "12"}, m);
  69. test(__LINE__, {"-b12"}, m);
  70. m = active{}; m.c = true;
  71. test(__LINE__, {"-c"}, m);
  72. m = active{}; m.c = true; m.cv = 2.3f;
  73. test(__LINE__, {"-c", "2.3"}, m);
  74. test(__LINE__, {"-c2.3"}, m);
  75. test(__LINE__, {"-c2", ".3"}, m);
  76. test(__LINE__, {"-c", "+2.3"}, m);
  77. test(__LINE__, {"-c+2.3"}, m);
  78. test(__LINE__, {"-c+2", ".3"}, m);
  79. m = active{}; m.c = true; m.cv = -2.3f;
  80. test(__LINE__, {"-c", "-2.3"}, m);
  81. test(__LINE__, {"-c-2.3"}, m);
  82. test(__LINE__, {"-c-2", ".3"}, m);
  83. m = active{}; m.c = true; m.cv = 1; m.dv = 2;
  84. test(__LINE__, {"-c", "1", "2"}, m);
  85. test(__LINE__, {"-c1", "2"}, m);
  86. test(__LINE__, {"-c1", "2"}, m);
  87. m = active{}; m.d = true; m.c = true; m.cv = 2;
  88. test(__LINE__, {"-c", "2", "-d"}, m);
  89. m = active{}; m.a = true; m.av = 1; m.c = true; m.cv = 2;
  90. test(__LINE__, {"-c", "2", "-a", "1"}, m);
  91. m = active{}; m.d = true;
  92. test(__LINE__, {"-d"}, m);
  93. m = active{}; m.d = true; m.dv = 2.3f;
  94. test(__LINE__, {"-d", "2.3"}, m);
  95. test(__LINE__, {"-d2.3"}, m);
  96. test(__LINE__, {"-d2", ".3"}, m);
  97. m = active{}; m.e = true;
  98. test(__LINE__, {"-e"}, m);
  99. m = active{}; m.e = true; m.ev = 2.3;
  100. test(__LINE__, {"-e", "2.3"}, m);
  101. test(__LINE__, {"-e2.3"}, m);
  102. test(__LINE__, {"-e2", ".3"}, m);
  103. m = active{}; m.f = true;
  104. test(__LINE__, {"-f"}, m);
  105. test(__LINE__, {"--ff"}, m);
  106. m = active{}; m.f = true; m.fv = 2.3;
  107. test(__LINE__, {"-f", "2.3"}, m);
  108. test(__LINE__, {"--ff", "2.3"}, m);
  109. test(__LINE__, {"-f2.3"}, m);
  110. test(__LINE__, {"--ff2.3"}, m);
  111. test(__LINE__, {"-f2", ".3"}, m);
  112. test(__LINE__, {"--ff2", ".3"}, m);
  113. m = active{}; m.g = true; m.gv = "xyz";
  114. test(__LINE__, {"xyz"}, m);
  115. m = active{}; m.g = true; m.gv = "-h";
  116. test(__LINE__, {"-h"}, m);
  117. m = active{}; m.g = true; m.gv = "--hh";
  118. test(__LINE__, {"--hh"}, m);
  119. m = active{}; m.g = true; m.gv = "---hhh";
  120. test(__LINE__, {"---hhh"}, m);
  121. m = active{}; m.g = true; m.gv = "--h";
  122. test(__LINE__, {"--h"}, m);
  123. m = active{}; m.g = true; m.gv = "--hh";
  124. test(__LINE__, {"--hh"}, m);
  125. m = active{}; m.g = true; m.gv = "-hh";
  126. test(__LINE__, {"-hh"}, m);
  127. m = active{}; m.g = true; m.gv = "-hhh";
  128. test(__LINE__, {"-hhh"}, m);
  129. m = active{}; m.h = true; m.g = true; m.gv = "x-y.z";
  130. test(__LINE__, {"x-y.z", "-h"}, m);
  131. test(__LINE__, {"x-y.z", "--hh"}, m);
  132. test(__LINE__, {"x-y.z", "---hhh"}, m);
  133. m = active{}; m.i = true; m.g = true; m.gv = "xYz";
  134. test(__LINE__, {"xYz", "-i"}, m);
  135. test(__LINE__, {"xYz", "--ii"}, m);
  136. m = active{}; m.g = true; m.gv = "-ii";
  137. test(__LINE__, {"-ii"}, m);
  138. m = active{}; m.g = true; m.gv = "--i";
  139. test(__LINE__, {"--i"}, m);
  140. m = active{};
  141. m.a = true; m.av = 65;
  142. m.b = true; m.bv = 12;
  143. m.c = true; m.cv = -0.12f;
  144. m.d = true; m.dv = 2.3f;
  145. m.e = true; m.ev = 3.4;
  146. m.f = true; m.fv = 5.6;
  147. m.g = true; m.gv = "x-y.z";
  148. m.h = true;
  149. m.i = true;
  150. test(__LINE__, {"-a", "65", "-b12", "-c", "-0.12f", "-d2.3",
  151. "-e3", ".4", "--ff", "5.6", "x-y.z", "-h", "--ii"}, m);
  152. test(__LINE__, {"-b12", "-c", "-0.12f", "-d2.3", "-e3", ".4", "-a", "65",
  153. "--ff", "5.6", "x-y.z", "-h", "--ii"}, m);
  154. test(__LINE__, {"-d2.3", "-e3", ".4", "-b12", "-c", "-0.12f", "-a", "65",
  155. "--ff", "5.6", "x-y.z", "-h", "--ii"}, m);
  156. }
  157. catch(std::exception& e) {
  158. std::cerr << e.what() << std::endl;
  159. return 1;
  160. }
  161. }