-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathrepeatable_alternatives_test.cpp
More file actions
92 lines (71 loc) · 2.56 KB
/
repeatable_alternatives_test.cpp
File metadata and controls
92 lines (71 loc) · 2.56 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
/*****************************************************************************
*
* 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(bool a_, bool b_,
std::initializer_list<int> i_, std::initializer_list<int> j_)
:
a{a_}, b{b_}, i{i_}, j{j_}
{}
bool a = false, b = false;
std::vector<int> i, j;
friend bool operator == (const active& x, const active& y) noexcept {
return x.a == y.a && x.b == y.b &&
(
(x.i.size() == 0 && y.i.size() == 0) ||
(x.i.size() > 0 && y.i.size() > 0 && x.i.size() == y.i.size() &&
std::equal(x.i.begin(), x.i.end(), y.i.begin()))
) &&
(
(x.j.size() == 0 && y.j.size() == 0) ||
(x.j.size() > 0 && y.j.size() > 0 && x.j.size() == y.j.size() &&
std::equal(x.j.begin(), x.j.end(), y.j.begin()))
);
}
};
//-------------------------------------------------------------------
void test(int lineNo,
const std::initializer_list<const char*> args,
const active& matches)
{
using namespace clipp;
active m;
auto cli = repeatable(
( option("a").set(m.a) & value("i",m.i) ) |
( option("b").set(m.b) & value("j",m.j) )
);
run_wrapped_variants({ __FILE__, lineNo }, args, cli,
[&]{ m = active{}; },
[&]{ return m == matches; });
}
//-------------------------------------------------------------------
int main()
{
try {
test(__LINE__, {""}, active{});
test(__LINE__, {"a"}, active{1,0, {}, {} });
test(__LINE__, {"b"}, active{0,1, {}, {} });
test(__LINE__, {"a", "12"}, active{1,0, {12}, {} });
test(__LINE__, {"b", "34"}, active{0,1, {}, {34} });
test(__LINE__, {"a", "12", "b", "34"}, active{1,1, {12}, {34} });
test(__LINE__, {"a", "12", "a", "34"}, active{1,0, {12,34}, {} });
test(__LINE__, {"b", "12", "b", "34"}, active{0,1, {}, {12,34} });
test(__LINE__, {"a", "1", "b", "2", "a", "3"}, active{1,1, {1,3}, {2} });
test(__LINE__, {"a", "1", "b", "2", "b", "4", "a", "3"}, active{1,1, {1,3}, {2,4} });
test(__LINE__, {"a", "b", "2", "b", "a", "3"}, active{1,1, {3}, {2} });
}
catch(std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
}