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.

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