-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathFormatterTest.cpp
197 lines (150 loc) · 5.85 KB
/
FormatterTest.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
#ifdef CLI11_SINGLE_FILE
#include "CLI11.hpp"
#else
#include "CLI/CLI.hpp"
#endif
#include "catch.hpp"
#include <fstream>
#include <memory>
#include <string>
class SimpleFormatter : public CLI::FormatterBase {
public:
SimpleFormatter() : FormatterBase() {}
std::string make_help(const CLI::App *, std::string, CLI::AppFormatMode) const override {
return "This is really simple";
}
};
TEST_CASE("Formatter: Nothing", "[formatter]") {
CLI::App app{"My prog"};
app.formatter(std::make_shared<SimpleFormatter>());
std::string help = app.help();
CHECK("This is really simple" == help);
}
TEST_CASE("Formatter: NothingLambda", "[formatter]") {
CLI::App app{"My prog"};
app.formatter_fn(
[](const CLI::App *, std::string, CLI::AppFormatMode) { return std::string("This is really simple"); });
std::string help = app.help();
CHECK("This is really simple" == help);
}
TEST_CASE("Formatter: OptCustomize", "[formatter]") {
CLI::App app{"My prog"};
auto optfmt = std::make_shared<CLI::Formatter>();
optfmt->column_width(25);
optfmt->label("REQUIRED", "(MUST HAVE)");
app.formatter(optfmt);
int v{0};
app.add_option("--opt", v, "Something")->required();
std::string help = app.help();
CHECK_THAT(help, Contains("(MUST HAVE)"));
CHECK_THAT(help, Contains("Something"));
CHECK_THAT(help, Contains("--opt INT"));
CHECK_THAT(help, Contains("-h, --help Print"));
}
TEST_CASE("Formatter: OptCustomizeSimple", "[formatter]") {
CLI::App app{"My prog"};
app.get_formatter()->column_width(25);
app.get_formatter()->label("REQUIRED", "(MUST HAVE)");
int v{0};
app.add_option("--opt", v, "Something")->required();
std::string help = app.help();
CHECK_THAT(help, Contains("(MUST HAVE)"));
CHECK_THAT(help, Contains("(MUST HAVE)"));
CHECK_THAT(help, Contains("Something"));
CHECK_THAT(help, Contains("--opt INT"));
CHECK_THAT(help, Contains("-h, --help Print"));
}
TEST_CASE("Formatter: OptCustomizeOptionText", "[formatter]") {
CLI::App app{"My prog"};
app.get_formatter()->column_width(25);
int v{0};
app.add_option("--opt", v, "Something")->option_text("(ARG)");
std::string help = app.help();
CHECK_THAT(help, Contains("(ARG)"));
}
TEST_CASE("Formatter: FalseFlagExample", "[formatter]") {
CLI::App app{"My prog"};
app.get_formatter()->column_width(25);
app.get_formatter()->label("REQUIRED", "(MUST HAVE)");
int v{0};
app.add_flag("--opt,!--no_opt", v, "Something");
bool flag{false};
app.add_flag("!-O,--opt2,--no_opt2{false}", flag, "Something else");
std::string help = app.help();
CHECK_THAT(help, Contains("--no_opt{false}"));
CHECK_THAT(help, Contains("--no_opt2{false}"));
CHECK_THAT(help, Contains("-O{false}"));
}
TEST_CASE("Formatter: AppCustomize", "[formatter]") {
CLI::App app{"My prog"};
app.add_subcommand("subcom1", "This");
auto appfmt = std::make_shared<CLI::Formatter>();
appfmt->column_width(20);
appfmt->label("Usage", "Run");
app.formatter(appfmt);
app.add_subcommand("subcom2", "That");
std::string help = app.help();
CHECK_THAT(help, Contains("Run: [OPTIONS] [SUBCOMMAND]\n\n"));
CHECK_THAT(help, Contains("\nSUBCOMMANDS:\n"));
CHECK_THAT(help, Contains(" subcom1 This \n"));
CHECK_THAT(help, Contains(" subcom2 That \n"));
}
TEST_CASE("Formatter: AppCustomizeSimple", "[formatter]") {
CLI::App app{"My prog"};
app.add_subcommand("subcom1", "This");
app.get_formatter()->column_width(20);
app.get_formatter()->label("Usage", "Run");
app.add_subcommand("subcom2", "That");
std::string help = app.help();
CHECK_THAT(help, Contains("Run: [OPTIONS] [SUBCOMMAND]\n\n"));
CHECK_THAT(help, Contains("\nSUBCOMMANDS:\n"));
CHECK_THAT(help, Contains(" subcom1 This \n"));
CHECK_THAT(help, Contains(" subcom2 That \n"));
}
TEST_CASE("Formatter: AllSub", "[formatter]") {
CLI::App app{"My prog"};
CLI::App *sub = app.add_subcommand("subcom", "This");
sub->add_flag("--insub", "MyFlag");
std::string help = app.help("", CLI::AppFormatMode::All);
CHECK_THAT(help, Contains("--insub"));
CHECK_THAT(help, Contains("subcom"));
}
TEST_CASE("Formatter: AllSubRequired", "[formatter]") {
CLI::App app{"My prog"};
CLI::App *sub = app.add_subcommand("subcom", "This");
sub->add_flag("--insub", "MyFlag");
sub->required();
std::string help = app.help("", CLI::AppFormatMode::All);
CHECK_THAT(help, Contains("--insub"));
CHECK_THAT(help, Contains("subcom"));
CHECK_THAT(help, Contains("REQUIRED"));
}
TEST_CASE("Formatter: NamelessSub", "[formatter]") {
CLI::App app{"My prog"};
CLI::App *sub = app.add_subcommand("", "This subcommand");
sub->add_flag("--insub", "MyFlag");
std::string help = app.help("", CLI::AppFormatMode::Normal);
CHECK_THAT(help, Contains("--insub"));
CHECK_THAT(help, Contains("This subcommand"));
}
TEST_CASE("Formatter: NamelessSubInGroup", "[formatter]") {
CLI::App app{"My prog"};
CLI::App *sub = app.add_subcommand("", "This subcommand");
CLI::App *sub2 = app.add_subcommand("sub2", "subcommand2");
sub->add_flag("--insub", "MyFlag");
int val{0};
sub2->add_option("pos", val, "positional");
sub->group("group1");
sub2->group("group1");
std::string help = app.help("", CLI::AppFormatMode::Normal);
CHECK_THAT(help, Contains("--insub"));
CHECK_THAT(help, Contains("This subcommand"));
CHECK_THAT(help, Contains("group1"));
CHECK_THAT(help, Contains("sub2"));
CHECK(help.find("pos") == std::string::npos);
}