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.

100 lines
2.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-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 o_, std::string ov_ = "_", std::string pv_ = "_"):
  16. o{o_}, ov{std::move(ov_)}, pv{std::move(pv_)}
  17. {}
  18. bool o = false;
  19. std::string ov = "_";
  20. std::string pv = "_";
  21. friend bool operator == (const active& x, const active& y) noexcept {
  22. return (x.o == y.o && x.ov == y.ov && x.pv == y.pv);
  23. }
  24. };
  25. //-------------------------------------------------------------------
  26. void test_empty(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("-o", "--opt").set(m.o) & value(match::any, "O", m.ov),
  34. value("P", m.pv)
  35. );
  36. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  37. [&]{ m = active{}; },
  38. [&]{ return m == matches; });
  39. }
  40. //-------------------------------------------------------------------
  41. void test_nonempty(int lineNo,
  42. const std::initializer_list<const char*> args,
  43. const active& matches)
  44. {
  45. using namespace clipp;
  46. active m;
  47. auto cli = (
  48. option("-o", "--opt").set(m.o) & value(match::nonempty, "O", m.ov),
  49. value("P", m.pv)
  50. );
  51. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  52. [&]{ m = active{}; },
  53. [&]{ return m == matches; });
  54. }
  55. //-------------------------------------------------------------------
  56. int main()
  57. {
  58. try {
  59. test_empty(__LINE__, {}, active{});
  60. test_empty(__LINE__, {""}, active{});
  61. test_empty(__LINE__, {"-o"}, active{true});
  62. test_empty(__LINE__, {"-o", ""}, active{true, ""});
  63. test_empty(__LINE__, {"-o", "X"}, active{true, "X"});
  64. test_empty(__LINE__, {"X"}, active{false, "_", "X"});
  65. test_empty(__LINE__, {"-o", "", "X"}, active{true, "", "X"});
  66. test_nonempty(__LINE__, {}, active{});
  67. test_nonempty(__LINE__, {""}, active{});
  68. test_nonempty(__LINE__, {"-o"}, active{true});
  69. test_nonempty(__LINE__, {"-o", ""}, active{true});
  70. test_nonempty(__LINE__, {"-o", "X"}, active{true, "X"});
  71. test_nonempty(__LINE__, {"X"}, active{false, "_", "X"});
  72. //ambiguous -> cannot map to value "P", expects value "O" first
  73. test_nonempty(__LINE__, {"-o", "", "X"}, active{true});
  74. }
  75. catch(std::exception& e) {
  76. std::cerr << e.what() << std::endl;
  77. return 1;
  78. }
  79. }