-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjoined_sequence_test.cpp
More file actions
132 lines (104 loc) · 4.74 KB
/
Copy pathjoined_sequence_test.cpp
File metadata and controls
132 lines (104 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*****************************************************************************
*
* CLIPP - command line interfaces for modern C++
*
* released under MIT license
*
* (c) 2017-2018 André Müller; foss@andremueller-online.de
*
*****************************************************************************/
#include "testing.h"
//-------------------------------------------------------------------
struct active {
active() = default;
explicit
active(int i_, char a_, char b_, char c_) : i{i_}, a{a_}, b{b_}, c{c_} {}
active(int i_, const std::string& s_) : i{i_}, s{s_} {}
int i = 0;
char a = ' ', b = ' ', c = ' ';
std::string s = "";
friend bool operator == (const active& x, const active& y) noexcept {
return (x.i == y.i &&
x.a == y.a && x.b == y.b && x.c == y.c &&
x.s == y.s);
}
};
//-------------------------------------------------------------------
void test(int lineNo,
const std::initializer_list<const char*> args,
const active& matches)
{
using namespace clipp;
auto one_char = [](const std::string& arg) {
if(arg.empty() || !std::isalpha(arg[0])) return subrange{};
return subrange{0,1};
};
active m;
auto cli = group(
option("-i").set(m.i,1) & value(one_char, "A", m.a),
option("-j").set(m.i,2) & value(one_char, "A", m.a) & value(one_char, "B", m.b),
option("-k").set(m.i,3) & value(one_char, "A", m.a) & value(one_char, "B", m.b) & value(one_char, "C", m.c)
,
option("-m").set(m.i,4) & values(one_char, "char", [&](const std::string& a){m.s += a;})
,
option("-o").set(m.i,9) & value("str", m.s)
);
run_wrapped_variants({ __FILE__, lineNo }, args, cli,
[&]{ m = active{}; },
[&]{ return m == matches; });
}
//-------------------------------------------------------------------
int main()
{
try {
test(__LINE__, {""}, active{});
test(__LINE__, {"-i" }, active{1, ' ', ' ', ' '});
test(__LINE__, {"-i", "x" }, active{1, 'x', ' ', ' '});
test(__LINE__, {"-i", "x", "y" }, active{1, 'x', ' ', ' '});
test(__LINE__, {"-i", "x", "y", "z"}, active{1, 'x', ' ', ' '});
test(__LINE__, {"-j" }, active{2, ' ', ' ', ' '});
test(__LINE__, {"-j", "x" }, active{2, 'x', ' ', ' '});
test(__LINE__, {"-j", "x", "y" }, active{2, 'x', 'y', ' '});
test(__LINE__, {"-j", "x", "y", "z"}, active{2, 'x', 'y', ' '});
test(__LINE__, {"-k" }, active{3, ' ', ' ', ' '});
test(__LINE__, {"-k", "x" }, active{3, 'x', ' ', ' '});
test(__LINE__, {"-k", "x", "y" }, active{3, 'x', 'y', ' '});
test(__LINE__, {"-k", "x", "y", "z"}, active{3, 'x', 'y', 'z'});
test(__LINE__, {"-m" }, active{4, ""});
test(__LINE__, {"-m", "x" }, active{4, "x"});
test(__LINE__, {"-m", "x", "y" }, active{4, "xy"});
test(__LINE__, {"-m", "x", "y", "z"}, active{4, "xyz"});
test(__LINE__, {"-o" }, active{9, ""});
test(__LINE__, {"-o", "x" }, active{9, "x"});
test(__LINE__, {"-o", "x", "y" }, active{9, "x"});
test(__LINE__, {"-o", "x", "y", "z"}, active{9, "x"});
//joined sequence
test(__LINE__, {"-i" }, active{1, ' ', ' ', ' '});
test(__LINE__, {"-ix" }, active{1, 'x', ' ', ' '});
test(__LINE__, {"-ixy" }, active{0, ' ', ' ', ' '});
test(__LINE__, {"-ixyz"}, active{0, ' ', ' ', ' '});
test(__LINE__, {"-j" }, active{2, ' ', ' ', ' '});
test(__LINE__, {"-jx" }, active{2, 'x', ' ', ' '});
test(__LINE__, {"-jxy" }, active{2, 'x', 'y', ' '});
test(__LINE__, {"-jxyz"}, active{0, ' ', ' ', ' '});
test(__LINE__, {"-k" }, active{3, ' ', ' ', ' '});
test(__LINE__, {"-kx" }, active{3, 'x', ' ', ' '});
test(__LINE__, {"-kxy" }, active{3, 'x', 'y', ' '});
test(__LINE__, {"-kxyz" }, active{3, 'x', 'y', 'z'});
test(__LINE__, {"-kxyza"}, active{0, ' ', ' ', ' '});
test(__LINE__, {"-m" }, active{4, ""});
test(__LINE__, {"-mx" }, active{4, "x"});
test(__LINE__, {"-mxy" }, active{4, "xy"});
test(__LINE__, {"-mxyz" }, active{4, "xyz"});
test(__LINE__, {"-mxyza"}, active{4, "xyza"});
test(__LINE__, {"-o" }, active{9, ""});
test(__LINE__, {"-ox" }, active{9, "x"});
test(__LINE__, {"-oxy" }, active{9, "xy"});
test(__LINE__, {"-oxyz" }, active{9, "xyz"});
test(__LINE__, {"-oxyza"}, active{9, "xyza"});
}
catch(std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
}