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.

81 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 = joinable(
  30. option("a").set(m.a),
  31. option("b").set(m.b),
  32. option("c").set(m.c),
  33. option("d").set(m.d));
  34. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  35. [&]{ m = active{}; },
  36. [&]{ return m == matches; });
  37. }
  38. //-------------------------------------------------------------------
  39. int main()
  40. {
  41. using std::string;
  42. try {
  43. test(__LINE__, {""}, active{});
  44. test(__LINE__, {"a"}, active{1,0,0,0});
  45. test(__LINE__, {"b"}, active{0,1,0,0});
  46. test(__LINE__, {"c"}, active{0,0,1,0});
  47. test(__LINE__, {"d"}, active{0,0,0,1});
  48. test(__LINE__, {"a","b"}, active{1,1,0,0});
  49. test(__LINE__, {"c","b"}, active{0,1,1,0});
  50. test(__LINE__, {"d","a","c"}, active{1,0,1,1});
  51. test(__LINE__, {"ab"}, active{1,1,0,0});
  52. test(__LINE__, {"ac"}, active{1,0,1,0});
  53. test(__LINE__, {"ad"}, active{1,0,0,1});
  54. test(__LINE__, {"abd"}, active{1,1,0,1});
  55. test(__LINE__, {"cab"}, active{1,1,1,0});
  56. test(__LINE__, {"abcd"}, active{1,1,1,1});
  57. test(__LINE__, {"cadb"}, active{1,1,1,1});
  58. //fail cases
  59. test(__LINE__, {"cxadb"}, active{});
  60. test(__LINE__, {"-abc"}, active{});
  61. test(__LINE__, {"-bca"}, active{});
  62. test(__LINE__, {"cab-"}, active{});
  63. test(__LINE__, {"c-ad-"}, active{});
  64. }
  65. catch(std::exception& e) {
  66. std::cerr << e.what() << std::endl;
  67. return 1;
  68. }
  69. }