CLItest.cpp
2.23 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
#include "CLI.hpp"
#include "gtest/gtest.h"
typedef std::vector<std::string> input_t;
TEST(Basic, Empty) {
{
CLI::App app;
input_t simpleput;
app.parse(simpleput);
}
{
CLI::App app;
input_t spare = {"spare"};
EXPECT_THROW(app.parse(spare), CLI::ExtraPositionalsError);
}
{
CLI::App app;
input_t simpleput;
app.parse(simpleput);
}
}
struct TApp : public ::testing::Test {
CLI::App app{"My Test Program"};
input_t args;
void run() {
input_t newargs = args;
std::reverse(std::begin(newargs), std::end(newargs));
app.parse(newargs);
}
};
TEST_F(TApp, OneFlagShort) {
app.add_flag("c,count");
args = {"-c"};
run();
EXPECT_EQ(1, app.count("c"));
EXPECT_EQ(1, app.count("count"));
}
TEST_F(TApp, OneFlagLong) {
app.add_flag("c,count");
args = {"--count"};
run();
EXPECT_EQ(1, app.count("c"));
EXPECT_EQ(1, app.count("count"));
}
TEST_F(TApp, OneFlagRef) {
int ref;
app.add_flag("c,count", ref);
args = {"--count"};
run();
EXPECT_EQ(1, app.count("c"));
EXPECT_EQ(1, app.count("count"));
EXPECT_EQ(1, ref);
}
TEST_F(TApp, OneString) {
std::string str;
app.add_option("s,string", str);
args = {"--string", "mystring"};
run();
EXPECT_EQ(1, app.count("s"));
EXPECT_EQ(1, app.count("string"));
EXPECT_EQ(str, "mystring");
}
TEST_F(TApp, TogetherInt) {
int i;
app.add_option("i,int", i);
args = {"-i4"};
run();
EXPECT_EQ(1, app.count("int"));
EXPECT_EQ(1, app.count("i"));
EXPECT_EQ(i, 4);
}
TEST_F(TApp, SepInt) {
int i;
app.add_option("i,int", i);
args = {"-i","4"};
run();
EXPECT_EQ(1, app.count("int"));
EXPECT_EQ(1, app.count("i"));
EXPECT_EQ(i, 4);
}
TEST_F(TApp, OneStringAgain) {
std::string str;
app.add_option("s,string", str);
args = {"--string", "mystring"};
run();
EXPECT_EQ(1, app.count("s"));
EXPECT_EQ(1, app.count("string"));
EXPECT_EQ(str, "mystring");
}
TEST_F(TApp, DefaultStringAgain) {
std::string str = "previous";
app.add_option("s,string", str);
run();
EXPECT_EQ(0, app.count("s"));
EXPECT_EQ(0, app.count("string"));
EXPECT_EQ(str, "previous");
}