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.

132 lines
4.8 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 m1;
  32. auto cli1 = (
  33. command("a").set(m1.a),
  34. command("b").set(m1.b),
  35. command("c").set(m1.c),
  36. command("d").set(m1.d),
  37. opt_value("i").set(m1.i),
  38. option("-e").set(m1.e),
  39. option("-f").set(m1.f)
  40. );
  41. //equivalent interface
  42. active m2;
  43. auto cli2 = (
  44. required("a").set(m2.a) &
  45. required("b").set(m2.b) &
  46. required("c").set(m2.c) &
  47. required("d").set(m2.d) &
  48. (
  49. opt_value("i").set(m2.i),
  50. option("-e").set(m2.e),
  51. option("-f").set(m2.f)
  52. )
  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, 0});
  67. test(__LINE__, {"b"}, active{});
  68. test(__LINE__, {"c"}, active{});
  69. test(__LINE__, {"d"}, active{});
  70. test(__LINE__, {"12"}, active{});
  71. test(__LINE__, {"-e"}, active{});
  72. test(__LINE__, {"-f"}, active{});
  73. test(__LINE__, {"a", "b"}, active{1,1,0,0,0,0, 0});
  74. test(__LINE__, {"a", "c"}, active{1,0,0,0,0,0, 0});
  75. test(__LINE__, {"a", "d"}, active{1,0,0,0,0,0, 0});
  76. test(__LINE__, {"a", "12"}, active{1,0,0,0,0,0, 0});
  77. test(__LINE__, {"a", "-e"}, active{1,0,0,0,0,0, 0});
  78. test(__LINE__, {"a", "-f"}, active{1,0,0,0,0,0, 0});
  79. test(__LINE__, {"a", "b", "c"}, active{1,1,1,0,0,0, 0});
  80. test(__LINE__, {"a", "b", "d"}, active{1,1,0,0,0,0, 0});
  81. test(__LINE__, {"a", "b", "12"}, active{1,1,0,0,0,0, 0});
  82. test(__LINE__, {"a", "b", "-e"}, active{1,1,0,0,0,0, 0});
  83. test(__LINE__, {"a", "b", "-f"}, active{1,1,0,0,0,0, 0});
  84. test(__LINE__, {"a", "b", "c", "d"}, active{1,1,1,1,0,0, 0});
  85. test(__LINE__, {"a", "b", "c", "12"}, active{1,1,1,0,0,0, 0});
  86. test(__LINE__, {"a", "b", "c", "-e"}, active{1,1,1,0,0,0, 0});
  87. test(__LINE__, {"a", "b", "c", "-f"}, active{1,1,1,0,0,0, 0});
  88. test(__LINE__, {"a", "b", "c", "d", "-e"}, active{1,1,1,1,1,0, 0});
  89. test(__LINE__, {"a", "b", "c", "d", "-f"}, active{1,1,1,1,0,1, 0});
  90. test(__LINE__, {"a", "b", "c", "d", "12"}, active{1,1,1,1,0,0, 12});
  91. test(__LINE__, {"a", "b", "c", "d", "-3"}, active{1,1,1,1,0,0, -3});
  92. test(__LINE__, {"a", "b", "c", "d", "12", "-e"}, active{1,1,1,1,1,0, 12});
  93. test(__LINE__, {"a", "b", "c", "d", "12", "-f"}, active{1,1,1,1,0,1, 12});
  94. test(__LINE__, {"a", "b", "c", "d", "-e", "12"}, active{1,1,1,1,1,0, 12});
  95. test(__LINE__, {"a", "b", "c", "d", "-f", "12"}, active{1,1,1,1,0,1, 12});
  96. test(__LINE__, {"a", "b", "c", "d", "-e", "-f"}, active{1,1,1,1,1,1, 0});
  97. test(__LINE__, {"a", "b", "c", "d", "-f", "-e"}, active{1,1,1,1,1,1, 0});
  98. test(__LINE__, {"a", "b", "c", "d", "12", "-e", "-f"}, active{1,1,1,1,1,1, 12});
  99. test(__LINE__, {"a", "b", "c", "d", "-e", "12", "-f"}, active{1,1,1,1,1,1, 12});
  100. test(__LINE__, {"a", "b", "c", "d", "-e", "-f", "12"}, active{1,1,1,1,1,1, 12});
  101. test(__LINE__, {"a", "b", "c", "d", "12", "-f", "-e"}, active{1,1,1,1,1,1, 12});
  102. test(__LINE__, {"a", "b", "c", "d", "-f", "12", "-e"}, active{1,1,1,1,1,1, 12});
  103. test(__LINE__, {"a", "b", "c", "d", "-f", "-e", "12"}, active{1,1,1,1,1,1, 12});
  104. }
  105. catch(std::exception& e) {
  106. std::cerr << e.what() << std::endl;
  107. return 1;
  108. }
  109. }