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.

105 lines
4.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. explicit
  15. active(const std::string f_,
  16. std::initializer_list<std::string> ts,
  17. bool http_, bool ftp_,
  18. std::initializer_list<std::string> ws) :
  19. http{http_}, ftp{ftp_}, f{f_}, tgts{ts}, wrong{ws}
  20. {}
  21. bool http = false, ftp = false;
  22. std::string f;
  23. std::vector<std::string> tgts;
  24. std::vector<std::string> wrong;
  25. friend bool operator == (const active& x, const active& y) noexcept {
  26. return x.http == y.http && x.f == y.f &&
  27. std::equal(x.tgts.begin(), x.tgts.end(), y.tgts.begin()) &&
  28. std::equal(x.wrong.begin(), x.wrong.end(), y.wrong.begin());
  29. }
  30. };
  31. //-------------------------------------------------------------------
  32. void test(int lineNo,
  33. const std::initializer_list<const char*> args,
  34. const active& matches)
  35. {
  36. using namespace clipp;
  37. active m;
  38. auto cli = (
  39. value("file", m.f),
  40. required("-t") & values(match::prefix_not("-"), "target", m.tgts),
  41. option("--http").set(m.http) | option("--ftp").set(m.ftp),
  42. any_other(m.wrong)
  43. );
  44. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  45. [&]{ m = active{}; },
  46. [&]{ return m == matches; });
  47. }
  48. //-------------------------------------------------------------------
  49. int main()
  50. {
  51. try {
  52. test(__LINE__, {""}, active{});
  53. test(__LINE__, {"abc"}, active{"abc", {}, false, false, {}});
  54. test(__LINE__, {"abc", "--http"}, active{"abc", {}, true, false, {}});
  55. test(__LINE__, {"abc", "--ftp"}, active{"abc", {}, false, true, {}});
  56. test(__LINE__, {"abc", "--ftp", "--http"}, active{"abc", {}, false, true, {}});
  57. test(__LINE__, {"abc", "-t", "--http"}, active{"abc", {}, true, false, {}});
  58. test(__LINE__, {"abc", "-t", "--ftp"}, active{"abc", {}, false, true, {}});
  59. test(__LINE__, {"abc", "-t", "tgt1", "--http"}, active{"abc", {"tgt1"}, true, false, {}});
  60. test(__LINE__, {"abc", "-t", "tgt1", "--ftp"}, active{"abc", {"tgt1"}, false, true, {}});
  61. test(__LINE__, {"abc", "-t", "tgt1", "t2", "--http"}, active{"abc", {"tgt1", "t2"}, true, false, {}});
  62. test(__LINE__, {"abc", "-t", "tgt1", "t2", "--ftp"}, active{"abc", {"tgt1", "t2"}, false, true, {}});
  63. test(__LINE__, {"abc", "x"}, active{"abc", {}, false, false, {"x"}});
  64. test(__LINE__, {"abc", "-x"}, active{"abc", {}, false, false, {"-x"}});
  65. test(__LINE__, {"abc", "-ftp"}, active{"abc", {}, false, false, {"-ftp"}});
  66. test(__LINE__, {"abc", "--ftpx"}, active{"abc", {}, false, false, {"--ftpx"}});
  67. test(__LINE__, {"abc", "-x", "--http"}, active{"abc", {}, true, false, {"-x"}});
  68. test(__LINE__, {"abc", "-x", "--ftp"}, active{"abc", {}, false, true, {"-x"}});
  69. test(__LINE__, {"abc", "-x", "--ftp", "--http"}, active{"abc", {}, false, true, {"-x"}});
  70. test(__LINE__, {"abc", "-t", "-x", "--http"}, active{"abc", {}, true, false, {"-x"}});
  71. test(__LINE__, {"abc", "-t", "-x", "--ftp"}, active{"abc", {}, false, true, {"-x"}});
  72. test(__LINE__, {"abc", "-t", "tgt1", "-x", "--http"}, active{"abc", {"tgt1"}, true, false, {"-x"}});
  73. test(__LINE__, {"abc", "-t", "tgt1", "-x", "--ftp"}, active{"abc", {"tgt1"}, false, true, {"-x"}});
  74. test(__LINE__, {"abc", "-t", "tgt1", "t2", "-x", "--http"}, active{"abc", {"tgt1", "t2"}, true, false, {"-x"}});
  75. test(__LINE__, {"abc", "-t", "tgt1", "t2", "-x", "--ftp"}, active{"abc", {"tgt1", "t2"}, false, true, {"-x"}});
  76. }
  77. catch(std::exception& e) {
  78. std::cerr << e.what() << std::endl;
  79. return 1;
  80. }
  81. }