|
| 1 | +/***************************************************************************** |
| 2 | + * |
| 3 | + * CLIPP - command line interfaces for modern C++ |
| 4 | + * |
| 5 | + * released under MIT license |
| 6 | + * |
| 7 | + * (c) 2017-2019 André Müller; [email protected] |
| 8 | + * |
| 9 | + *****************************************************************************/ |
| 10 | + |
| 11 | +#include "testing.h" |
| 12 | + |
| 13 | + |
| 14 | +//------------------------------------------------------------------- |
| 15 | +struct active { |
| 16 | + active() = default; |
| 17 | + active(bool a_, int i_): a{a_}, i{i_} {} |
| 18 | + bool a = false; |
| 19 | + int i = 0; |
| 20 | + |
| 21 | + friend bool operator == (const active& x, const active& y) noexcept { |
| 22 | + return (x.a == y.a && x.i == y.i); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | + |
| 27 | +//------------------------------------------------------------------- |
| 28 | +static void |
| 29 | +test(int lineNo, |
| 30 | + const std::initializer_list<const char*> args, |
| 31 | + const active& matches ) |
| 32 | +{ |
| 33 | + using namespace clipp; |
| 34 | + active m; |
| 35 | + |
| 36 | + auto cli = ( |
| 37 | + option("-a").set(m.a), |
| 38 | + option("-ab", "-a-b", "-a-b=") & value("i", m.i) |
| 39 | + ); |
| 40 | + |
| 41 | + run_wrapped_variants({ __FILE__, lineNo }, args, cli, |
| 42 | + [&]{ m = active{}; }, |
| 43 | + [&]{ return m == matches; }); |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +//------------------------------------------------------------------- |
| 48 | +int main() |
| 49 | +{ |
| 50 | + using std::string; |
| 51 | + |
| 52 | + try { |
| 53 | + test(__LINE__, {""}, active{0,0}); |
| 54 | + test(__LINE__, {"-a"}, active{1,0}); |
| 55 | + |
| 56 | + test(__LINE__, {"-ab"}, active{0,0}); |
| 57 | + test(__LINE__, {"-a-b"}, active{0,0}); |
| 58 | + test(__LINE__, {"-a-b="}, active{0,0}); |
| 59 | + |
| 60 | + test(__LINE__, {"-ab", "2"}, active{0,2}); |
| 61 | + test(__LINE__, {"-a-b", "3"}, active{0,3}); |
| 62 | + test(__LINE__, {"-a-b=", "4"}, active{0,4}); |
| 63 | + |
| 64 | + test(__LINE__, {"-ab2" }, active{0,2}); |
| 65 | + test(__LINE__, {"-a-b3" }, active{0,3}); |
| 66 | + test(__LINE__, {"-a-b=4"}, active{0,4}); |
| 67 | + |
| 68 | + test(__LINE__, {"-a", "-ab", "2"}, active{1,2}); |
| 69 | + test(__LINE__, {"-a", "-a-b", "3"}, active{1,3}); |
| 70 | + test(__LINE__, {"-a", "-a-b=", "4"}, active{1,4}); |
| 71 | + |
| 72 | + test(__LINE__, {"-a", "-ab2" }, active{1,2}); |
| 73 | + test(__LINE__, {"-a", "-a-b3" }, active{1,3}); |
| 74 | + test(__LINE__, {"-a", "-a-b=4"}, active{1,4}); |
| 75 | + |
| 76 | + test(__LINE__, {"-ab", "2", "-a"}, active{1,2}); |
| 77 | + test(__LINE__, {"-a-b", "3", "-a"}, active{1,3}); |
| 78 | + test(__LINE__, {"-a-b=", "4", "-a"}, active{1,4}); |
| 79 | + |
| 80 | + test(__LINE__, {"-a", "-ab" }, active{1,0}); |
| 81 | + test(__LINE__, {"-a", "-a-b" }, active{1,0}); |
| 82 | + test(__LINE__, {"-a", "-a-b="}, active{1,0}); |
| 83 | + |
| 84 | + test(__LINE__, {"-ab", "-a"}, active{1,0}); |
| 85 | + test(__LINE__, {"-a-b", "-a"}, active{1,0}); |
| 86 | + test(__LINE__, {"-a-b=", "-a"}, active{1,0}); |
| 87 | + } |
| 88 | + catch(std::exception& e) { |
| 89 | + std::cerr << e.what() << std::endl; |
| 90 | + return 1; |
| 91 | + } |
| 92 | +} |
0 commit comments