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.

84 lines
2.5 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. active(bool a_, bool b_, bool c_, bool d_):
  15. a{a_}, b{b_}, c{c_}, d{d_}
  16. {}
  17. bool a = false, b = false, c = false, d = false;
  18. friend bool operator == (const active& x, const active& y) noexcept {
  19. return (x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d);
  20. }
  21. };
  22. //-------------------------------------------------------------------
  23. void test(int lineNo,
  24. const std::initializer_list<const char*> args,
  25. const active& matches)
  26. {
  27. using namespace clipp;
  28. active m;
  29. auto cli = (
  30. joinable(option("a").set(m.a),
  31. option("b").set(m.b)),
  32. joinable(option("-a").set(m.c),
  33. option("-b").set(m.d)) );
  34. parse(args, cli);
  35. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  36. [&]{ m = active{}; },
  37. [&]{ return m == matches; });
  38. }
  39. //-------------------------------------------------------------------
  40. int main()
  41. {
  42. using std::string;
  43. try {
  44. test(__LINE__, {""}, active{});
  45. test(__LINE__, {"a"}, active{1,0,0,0});
  46. test(__LINE__, {"b"}, active{0,1,0,0});
  47. test(__LINE__, {"a","b"}, active{1,1,0,0});
  48. test(__LINE__, {"b","a"}, active{1,1,0,0});
  49. test(__LINE__, {"ab"}, active{1,1,0,0});
  50. test(__LINE__, {"ba"}, active{1,1,0,0});
  51. //second group
  52. test(__LINE__, {"-a"}, active{0,0,1,0});
  53. test(__LINE__, {"-b"}, active{0,0,0,1});
  54. test(__LINE__, {"-a","-b"}, active{0,0,1,1});
  55. test(__LINE__, {"-b","-a"}, active{0,0,1,1});
  56. test(__LINE__, {"-ab"}, active{0,0,1,1});
  57. test(__LINE__, {"-ba"}, active{0,0,1,1});
  58. test(__LINE__, {"-b-a"}, active{0,0,1,1});
  59. test(__LINE__, {"-a-b"}, active{0,0,1,1});
  60. //fail cases
  61. test(__LINE__, {"--ab"}, active{});
  62. test(__LINE__, {"ab-"}, active{});
  63. test(__LINE__, {"ba-"}, active{});
  64. test(__LINE__, {"a-b"}, active{});
  65. test(__LINE__, {"b-a"}, active{});
  66. }
  67. catch(std::exception& e) {
  68. std::cerr << e.what() << std::endl;
  69. return 1;
  70. }
  71. }