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.

102 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 André Müller; foss@andremueller-online.de
  8. *
  9. *****************************************************************************/
  10. #include "testing.h"
  11. //-------------------------------------------------------------------
  12. struct counters {
  13. counters() = default;
  14. explicit
  15. counters(int a_, int b_): a{a_}, b{b_} {}
  16. int a = 0, b = 0;
  17. friend bool operator == (const counters& x, const counters& y) noexcept {
  18. return (x.a == y.a && x.b == y.b);
  19. }
  20. };
  21. //-------------------------------------------------------------------
  22. void test(int lineNo,
  23. const std::initializer_list<const char*> args,
  24. const counters& matches)
  25. {
  26. using namespace clipp;
  27. counters m;
  28. auto cli1 = joinable(
  29. repeatable(
  30. option("a").call([&]{++m.a;}),
  31. option("b").call([&]{++m.b;})
  32. )
  33. );
  34. run_wrapped_variants({ __FILE__, lineNo }, args, cli1,
  35. [&]{ m = counters{}; },
  36. [&]{ return m == matches; });
  37. auto cli2 = joinable(
  38. repeatable( option("a").call([&]{++m.a;}) ),
  39. repeatable( option("b").call([&]{++m.b;}) )
  40. );
  41. run_wrapped_variants({ __FILE__, lineNo }, args, cli2,
  42. [&]{ m = counters{}; },
  43. [&]{ return m == matches; });
  44. }
  45. //-------------------------------------------------------------------
  46. int main()
  47. {
  48. try {
  49. test(__LINE__, {}, counters{});
  50. test(__LINE__, {"a"}, counters{1,0});
  51. test(__LINE__, {"b"}, counters{0,1});
  52. test(__LINE__, {"a","a"}, counters{2,0});
  53. test(__LINE__, {"b","b"}, counters{0,2});
  54. test(__LINE__, {"a","b"}, counters{1,1});
  55. test(__LINE__, {"a","a","a"}, counters{3,0});
  56. test(__LINE__, {"b","b","b"}, counters{0,3});
  57. test(__LINE__, {"a","b","a"}, counters{2,1});
  58. test(__LINE__, {"b","a","b"}, counters{1,2});
  59. test(__LINE__, {"b","a","a","b"}, counters{2,2});
  60. test(__LINE__, {"a","b","b","a"}, counters{2,2});
  61. test(__LINE__, {"aa"}, counters{2,0});
  62. test(__LINE__, {"bb"}, counters{0,2});
  63. test(__LINE__, {"aaa"}, counters{3,0});
  64. test(__LINE__, {"bbb"}, counters{0,3});
  65. test(__LINE__, {"abbb"}, counters{1,3});
  66. test(__LINE__, {"abba"}, counters{2,2});
  67. test(__LINE__, {"baba"}, counters{2,2});
  68. test(__LINE__, {"abab"}, counters{2,2});
  69. test(__LINE__, {"abaabbabbbbaaabaabababab"}, counters{12,12});
  70. test(__LINE__, {"a", "bba", "b", "aba", "bb", "aaaa"}, counters{8,6});
  71. }
  72. catch(std::exception& e) {
  73. std::cerr << e.what() << std::endl;
  74. return 1;
  75. }
  76. }