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.

130 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_, int i_ = 0) :
  16. a{a_}, b{b_}, c{c_}, d{d_}, e{e_}, f{f_}, i{i_}
  17. {}
  18. bool a = false, b = false, c = false, d = false, e = false, f = false;
  19. int i = 0;
  20. friend bool operator == (const active& x, const active& y) noexcept {
  21. return (x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d &&
  22. x.e == y.e && x.f == y.f && x.i == y.i);
  23. }
  24. };
  25. //-------------------------------------------------------------------
  26. void test(int lineNo,
  27. const std::initializer_list<const char*> args,
  28. const active& matches)
  29. {
  30. using namespace clipp;
  31. active m;
  32. auto cli = (
  33. option("-a").set(m.a),
  34. command("-b").set(m.b),
  35. option("-c").set(m.c),
  36. option("-i") & value("i", m.i),
  37. option("-d").set(m.d),
  38. command("-e").set(m.e),
  39. option("-f").set(m.f)
  40. );
  41. run_test({ __FILE__, lineNo }, args, cli, [&]{ return m == matches; });
  42. }
  43. //-------------------------------------------------------------------
  44. int main()
  45. {
  46. try {
  47. test(__LINE__, {""}, active{});
  48. test(__LINE__, {"-a"}, active{1,0,0,0,0,0, 0});
  49. test(__LINE__, {"-a", "-c"}, active{1,0,0,0,0,0, 0});
  50. test(__LINE__, {"-a", "-d"}, active{1,0,0,0,0,0, 0});
  51. test(__LINE__, {"-a", "-e"}, active{1,0,0,0,0,0, 0});
  52. test(__LINE__, {"-a", "-f"}, active{1,0,0,0,0,0, 0});
  53. test(__LINE__, {"-a", "-i", "123"}, active{1,0,0,0,0,0, 0});
  54. test(__LINE__, {"-b"}, active{0,1,0,0,0,0, 0});
  55. test(__LINE__, {"-b", "-a"}, active{0,1,0,0,0,0, 0});
  56. test(__LINE__, {"-a", "-b"}, active{1,1,0,0,0,0, 0});
  57. test(__LINE__, {"-b", "-c"}, active{0,1,1,0,0,0, 0});
  58. test(__LINE__, {"-a", "-b", "-c"}, active{1,1,1,0,0,0, 0});
  59. test(__LINE__, {"-a", "-b", "-c", "-i"}, active{1,1,1,0,0,0, 0});
  60. test(__LINE__, {"-a", "-b", "-c", "-i", "123"}, active{1,1,1,0,0,0, 123});
  61. test(__LINE__, {"-b", "-i", "123"}, active{0,1,0,0,0,0, 123});
  62. test(__LINE__, {"-a", "-b", "-i", "123"}, active{1,1,0,0,0,0, 123});
  63. test(__LINE__, {"-a", "-b", "-c", "-i", "123"}, active{1,1,1,0,0,0, 123});
  64. test(__LINE__, {"-a", "-b", "-d", "-i", "123"}, active{1,1,0,1,0,0, 123});
  65. test(__LINE__, {"-a", "-b", "-i", "123", "-c"}, active{1,1,1,0,0,0, 123});
  66. test(__LINE__, {"-a", "-b", "-i", "123", "-d"}, active{1,1,0,1,0,0, 123});
  67. test(__LINE__, {"-b", "-i", "123", "-a"}, active{0,1,0,0,0,0, 123});
  68. test(__LINE__, {"-b", "-i", "123", "-a"}, active{0,1,0,0,0,0, 123});
  69. test(__LINE__, {"-b", "-i", "123", "-d", "-c"}, active{0,1,1,1,0,0, 123});
  70. test(__LINE__, {"-b", "-i", "123", "-c", "-d"}, active{0,1,1,1,0,0, 123});
  71. test(__LINE__, {"-b", "-c", "-i", "123", "-d"}, active{0,1,1,1,0,0, 123});
  72. test(__LINE__, {"-b", "-c", "-d", "-i", "123"}, active{0,1,1,1,0,0, 123});
  73. test(__LINE__, {"-b", "-d", "-c", "-i", "123"}, active{0,1,1,1,0,0, 123});
  74. test(__LINE__, {"-b", "-d", "-i", "123", "-c"}, active{0,1,1,1,0,0, 123});
  75. test(__LINE__, {"-a", "-b", "-i", "123", "-d", "-c"}, active{1,1,1,1,0,0, 123});
  76. test(__LINE__, {"-a", "-b", "-i", "123", "-c", "-d"}, active{1,1,1,1,0,0, 123});
  77. test(__LINE__, {"-a", "-b", "-c", "-i", "123", "-d"}, active{1,1,1,1,0,0, 123});
  78. test(__LINE__, {"-a", "-b", "-c", "-d", "-i", "123"}, active{1,1,1,1,0,0, 123});
  79. test(__LINE__, {"-a", "-b", "-d", "-c", "-i", "123"}, active{1,1,1,1,0,0, 123});
  80. test(__LINE__, {"-a", "-b", "-d", "-i", "123", "-c"}, active{1,1,1,1,0,0, 123});
  81. test(__LINE__, {"-b", "-c", "-d"}, active{0,1,1,1,0,0, 0});
  82. test(__LINE__, {"-b", "-c", "-d", "-e"}, active{0,1,1,1,1,0, 0});
  83. test(__LINE__, {"-b", "-c", "-d", "-e", "-f"}, active{0,1,1,1,1,1, 0});
  84. test(__LINE__, {"-a", "-b", "-c", "-d"}, active{1,1,1,1,0,0, 0});
  85. test(__LINE__, {"-a", "-b", "-c", "-d", "-e"}, active{1,1,1,1,1,0, 0});
  86. test(__LINE__, {"-a", "-b", "-c", "-d", "-e", "-f"}, active{1,1,1,1,1,1, 0});
  87. test(__LINE__, {"-b", "-f"}, active{0,1,0,0,0,0, 0});
  88. test(__LINE__, {"-b", "-e", "-f"}, active{0,1,0,0,1,1, 0});
  89. test(__LINE__, {"-a", "-b", "-f"}, active{1,1,0,0,0,0, 0});
  90. test(__LINE__, {"-a", "-b", "-e", "-f"}, active{1,1,0,0,1,1, 0});
  91. test(__LINE__, {"-b", "-e", "-c"}, active{0,1,0,0,1,0, 0});
  92. test(__LINE__, {"-b", "-e", "-d"}, active{0,1,0,0,1,0, 0});
  93. test(__LINE__, {"-b", "-e", "-i", "123"}, active{0,1,0,0,1,0, 0});
  94. test(__LINE__, {"-a", "-b", "-e", "-c"}, active{1,1,0,0,1,0, 0});
  95. test(__LINE__, {"-a", "-b", "-e", "-d"}, active{1,1,0,0,1,0, 0});
  96. test(__LINE__, {"-a", "-b", "-e", "-i", "123"}, active{1,1,0,0,1,0, 0});
  97. }
  98. catch(std::exception& e) {
  99. std::cerr << e.what() << std::endl;
  100. return 1;
  101. }
  102. }