-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
122 lines (109 loc) · 3.6 KB
/
test.cpp
File metadata and controls
122 lines (109 loc) · 3.6 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
#include "args.hpp"
#include <cassert>
#include <cstring>
int ok = 0, fail = 0;
#define check(cond, msg) if(cond){ok++;}else{std::cerr<<"FAIL: "<<msg<<"\n";fail++;}
void make_argv(std::vector<const char*>& av, std::vector<std::string>& storage, std::initializer_list<const char*> args) {
storage.clear(); av.clear();
for (auto s : args) { storage.push_back(s); av.push_back(storage.back().c_str()); }
}
void test_basic_flag() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog", "--verbose"});
args::Parser p;
p.flag("verbose", "enable verbose output");
p.parse((int)av.size(), (char**)av.data());
check(p.get_flag("verbose"), "verbose flag set");
check(!p.get_flag("debug"), "debug flag not set");
}
void test_option_value() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog", "--output", "result.txt"});
args::Parser p;
p.option("output", "output file");
p.parse((int)av.size(), (char**)av.data());
check(p.get("output") == "result.txt", "option value");
}
void test_default_value() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog"});
args::Parser p;
p.option("port", "port number", "8080");
p.parse((int)av.size(), (char**)av.data());
check(p.get("port") == "8080", "default value");
}
void test_get_as_int() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog", "--count", "42"});
args::Parser p;
p.option("count", "count");
p.parse((int)av.size(), (char**)av.data());
check(p.get_as<int>("count") == 42, "get_as int");
}
void test_positional() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog", "input.txt"});
args::Parser p;
p.positional("file", "input file");
p.parse((int)av.size(), (char**)av.data());
check(p.get("file") == "input.txt", "positional arg");
}
void test_equals_syntax() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog", "--name=jasper"});
args::Parser p;
p.option("name", "name");
p.parse((int)av.size(), (char**)av.data());
check(p.get("name") == "jasper", "--key=val syntax");
}
void test_required_missing() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog"});
args::Parser p;
p.option("input", "input file", "", true);
bool threw = false;
try { p.parse((int)av.size(), (char**)av.data()); }
catch (const args::ParseError&) { threw = true; }
check(threw, "required option throws");
}
void test_unknown_flag() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog", "--unknown"});
args::Parser p;
bool threw = false;
try { p.parse((int)av.size(), (char**)av.data()); }
catch (const args::ParseError&) { threw = true; }
check(threw, "unknown flag throws");
}
void test_has() {
std::vector<const char*> av;
std::vector<std::string> st;
make_argv(av, st, {"prog", "--verbose"});
args::Parser p;
p.flag("verbose");
p.option("output");
p.parse((int)av.size(), (char**)av.data());
check(p.has("verbose"), "has set flag");
check(!p.has("output"), "has unset option");
}
int main() {
test_basic_flag();
test_option_value();
test_default_value();
test_get_as_int();
test_positional();
test_equals_syntax();
test_required_missing();
test_unknown_flag();
test_has();
std::cout << ok << "/" << (ok + fail) << " tests passed\n";
return fail == 0 ? 0 : 1;
}