forked from CLIUtils/CLI11
/
HelpersTest.cpp
358 lines (284 loc) · 10.7 KB
/
HelpersTest.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "app_helper.hpp"
#include <cstdio>
#include <fstream>
#include <cstdint>
#include <string>
TEST(Split, SimpleByToken) {
auto out = CLI::detail::split("one.two.three", '.');
ASSERT_EQ((size_t)3, out.size());
EXPECT_EQ("one", out.at(0));
EXPECT_EQ("two", out.at(1));
EXPECT_EQ("three", out.at(2));
}
TEST(Split, Single) {
auto out = CLI::detail::split("one", '.');
ASSERT_EQ((size_t)1, out.size());
EXPECT_EQ("one", out.at(0));
}
TEST(Split, Empty) {
auto out = CLI::detail::split("", '.');
ASSERT_EQ((size_t)1, out.size());
EXPECT_EQ("", out.at(0));
}
TEST(String, InvalidName) {
EXPECT_TRUE(CLI::detail::valid_name_string("valid"));
EXPECT_FALSE(CLI::detail::valid_name_string("-invalid"));
EXPECT_TRUE(CLI::detail::valid_name_string("va-li-d"));
EXPECT_FALSE(CLI::detail::valid_name_string("vali&d"));
EXPECT_TRUE(CLI::detail::valid_name_string("_valid"));
}
TEST(Trim, Various) {
std::string s1{" sdlfkj sdflk sd s "};
std::string a1{"sdlfkj sdflk sd s"};
CLI::detail::trim(s1);
EXPECT_EQ(a1, s1);
std::string s2{" a \t"};
CLI::detail::trim(s2);
EXPECT_EQ("a", s2);
std::string s3{" a \n"};
CLI::detail::trim(s3);
EXPECT_EQ("a", s3);
std::string s4{" a b "};
EXPECT_EQ("a b", CLI::detail::trim(s4));
}
TEST(Trim, VariousFilters) {
std::string s1{" sdlfkj sdflk sd s "};
std::string a1{"sdlfkj sdflk sd s"};
CLI::detail::trim(s1, " ");
EXPECT_EQ(a1, s1);
std::string s2{" a \t"};
CLI::detail::trim(s2, " ");
EXPECT_EQ("a \t", s2);
std::string s3{"abdavda"};
CLI::detail::trim(s3, "a");
EXPECT_EQ("bdavd", s3);
std::string s4{"abcabcabc"};
EXPECT_EQ("cabcabc", CLI::detail::trim(s4, "ab"));
}
TEST(Trim, TrimCopy) {
std::string orig{" cabc "};
std::string trimmed = CLI::detail::trim_copy(orig);
EXPECT_EQ("cabc", trimmed);
EXPECT_NE(orig, trimmed);
CLI::detail::trim(orig);
EXPECT_EQ(trimmed, orig);
orig = "abcabcabc";
trimmed = CLI::detail::trim_copy(orig, "ab");
EXPECT_EQ("cabcabc", trimmed);
EXPECT_NE(orig, trimmed);
CLI::detail::trim(orig, "ab");
EXPECT_EQ(trimmed, orig);
}
TEST(Validators, FileExists) {
std::string myfile{"TestFileNotUsed.txt"};
EXPECT_FALSE(CLI::ExistingFile(myfile));
bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
EXPECT_TRUE(ok);
EXPECT_TRUE(CLI::ExistingFile(myfile));
std::remove(myfile.c_str());
EXPECT_FALSE(CLI::ExistingFile(myfile));
}
TEST(Validators, FileNotExists) {
std::string myfile{"TestFileNotUsed.txt"};
EXPECT_TRUE(CLI::NonexistentPath(myfile));
bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
EXPECT_TRUE(ok);
EXPECT_FALSE(CLI::NonexistentPath(myfile));
std::remove(myfile.c_str());
EXPECT_TRUE(CLI::NonexistentPath(myfile));
}
TEST(Validators, FileIsDir) {
std::string mydir{"../tests"};
EXPECT_FALSE(CLI::ExistingFile(mydir));
}
TEST(Validators, DirectoryExists) {
std::string mydir{"../tests"};
EXPECT_TRUE(CLI::ExistingDirectory(mydir));
}
TEST(Validators, DirectoryNotExists) {
std::string mydir{"nondirectory"};
EXPECT_FALSE(CLI::ExistingDirectory(mydir));
}
TEST(Validators, DirectoryIsFile) {
std::string myfile{"TestFileNotUsed.txt"};
EXPECT_TRUE(CLI::NonexistentPath(myfile));
bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
EXPECT_TRUE(ok);
EXPECT_FALSE(CLI::ExistingDirectory(myfile));
std::remove(myfile.c_str());
EXPECT_TRUE(CLI::NonexistentPath(myfile));
}
// Yes, this is testing an app_helper :)
TEST(AppHelper, TempfileCreated) {
std::string name = "TestFileNotUsed.txt";
{
TempFile myfile{name};
EXPECT_FALSE(CLI::ExistingFile(myfile));
bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
EXPECT_TRUE(ok);
EXPECT_TRUE(CLI::ExistingFile(name));
EXPECT_THROW({ TempFile otherfile(name); }, std::runtime_error);
}
EXPECT_FALSE(CLI::ExistingFile(name));
}
TEST(AppHelper, TempfileNotCreated) {
std::string name = "TestFileNotUsed.txt";
{
TempFile myfile{name};
EXPECT_FALSE(CLI::ExistingFile(myfile));
}
EXPECT_FALSE(CLI::ExistingFile(name));
}
TEST(AppHelper, Ofstream) {
std::string name = "TestFileNotUsed.txt";
{
TempFile myfile(name);
{
std::ofstream out{myfile};
out << "this is output" << std::endl;
}
EXPECT_TRUE(CLI::ExistingFile(myfile));
}
EXPECT_FALSE(CLI::ExistingFile(name));
}
TEST(Split, StringList) {
std::vector<std::string> results{"a", "long", "--lone", "-q"};
EXPECT_EQ(results, CLI::detail::split_names("a,long,--lone,-q"));
EXPECT_EQ(std::vector<std::string>({"one"}), CLI::detail::split_names("one"));
}
TEST(RegEx, Shorts) {
std::string name, value;
EXPECT_TRUE(CLI::detail::split_short("-a", name, value));
EXPECT_EQ("a", name);
EXPECT_EQ("", value);
EXPECT_TRUE(CLI::detail::split_short("-B", name, value));
EXPECT_EQ("B", name);
EXPECT_EQ("", value);
EXPECT_TRUE(CLI::detail::split_short("-cc", name, value));
EXPECT_EQ("c", name);
EXPECT_EQ("c", value);
EXPECT_TRUE(CLI::detail::split_short("-simple", name, value));
EXPECT_EQ("s", name);
EXPECT_EQ("imple", value);
EXPECT_FALSE(CLI::detail::split_short("--a", name, value));
EXPECT_FALSE(CLI::detail::split_short("--thing", name, value));
EXPECT_FALSE(CLI::detail::split_short("--", name, value));
EXPECT_FALSE(CLI::detail::split_short("something", name, value));
EXPECT_FALSE(CLI::detail::split_short("s", name, value));
}
TEST(RegEx, Longs) {
std::string name, value;
EXPECT_TRUE(CLI::detail::split_long("--a", name, value));
EXPECT_EQ("a", name);
EXPECT_EQ("", value);
EXPECT_TRUE(CLI::detail::split_long("--thing", name, value));
EXPECT_EQ("thing", name);
EXPECT_EQ("", value);
EXPECT_TRUE(CLI::detail::split_long("--some=thing", name, value));
EXPECT_EQ("some", name);
EXPECT_EQ("thing", value);
EXPECT_FALSE(CLI::detail::split_long("-a", name, value));
EXPECT_FALSE(CLI::detail::split_long("-things", name, value));
EXPECT_FALSE(CLI::detail::split_long("Q", name, value));
EXPECT_FALSE(CLI::detail::split_long("--", name, value));
}
TEST(RegEx, SplittingNew) {
std::vector<std::string> shorts;
std::vector<std::string> longs;
std::string pname;
EXPECT_NO_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"--long", "-s", "-q", "--also-long"}));
EXPECT_EQ(std::vector<std::string>({"long", "also-long"}), longs);
EXPECT_EQ(std::vector<std::string>({"s", "q"}), shorts);
EXPECT_EQ("", pname);
EXPECT_NO_THROW(std::tie(shorts, longs, pname) =
CLI::detail::get_names({"--long", "", "-s", "-q", "", "--also-long"}));
EXPECT_EQ(std::vector<std::string>({"long", "also-long"}), longs);
EXPECT_EQ(std::vector<std::string>({"s", "q"}), shorts);
EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"-"}), CLI::BadNameString);
EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"--"}), CLI::BadNameString);
EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"-hi"}), CLI::BadNameString);
EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"---hi"}), CLI::BadNameString);
EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"one", "two"}), CLI::BadNameString);
}
TEST(String, ToLower) { EXPECT_EQ("one and two", CLI::detail::to_lower("one And TWO")); }
TEST(Join, Forward) {
std::vector<std::string> val{{"one", "two", "three"}};
EXPECT_EQ("one,two,three", CLI::detail::join(val));
EXPECT_EQ("one;two;three", CLI::detail::join(val, ";"));
}
TEST(Join, Backward) {
std::vector<std::string> val{{"three", "two", "one"}};
EXPECT_EQ("one,two,three", CLI::detail::rjoin(val));
EXPECT_EQ("one;two;three", CLI::detail::rjoin(val, ";"));
}
TEST(SplitUp, Simple) {
std::vector<std::string> oput = {"one", "two three"};
std::string orig{R"(one "two three")"};
std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result);
}
TEST(SplitUp, Layered) {
std::vector<std::string> output = {R"(one 'two three')"};
std::string orig{R"("one 'two three'")"};
std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(output, result);
}
TEST(SplitUp, Spaces) {
std::vector<std::string> oput = {"one", " two three"};
std::string orig{R"( one " two three" )"};
std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result);
}
TEST(SplitUp, BadStrings) {
std::vector<std::string> oput = {"one", " two three"};
std::string orig{R"( one " two three )"};
std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result);
oput = {"one", " two three"};
orig = R"( one ' two three )";
result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result);
}
TEST(Types, TypeName) {
std::string int_name = CLI::detail::type_name<int>();
EXPECT_EQ("INT", int_name);
std::string int2_name = CLI::detail::type_name<short>();
EXPECT_EQ("INT", int2_name);
std::string uint_name = CLI::detail::type_name<unsigned char>();
EXPECT_EQ("UINT", uint_name);
std::string float_name = CLI::detail::type_name<double>();
EXPECT_EQ("FLOAT", float_name);
std::string vector_name = CLI::detail::type_name<std::vector<int>>();
EXPECT_EQ("VECTOR", vector_name);
std::string text_name = CLI::detail::type_name<std::string>();
EXPECT_EQ("TEXT", text_name);
std::string text2_name = CLI::detail::type_name<char *>();
EXPECT_EQ("TEXT", text2_name);
}
TEST(Types, LexicalCastInt) {
std::string input = "912";
int x;
EXPECT_TRUE(CLI::detail::lexical_cast(input, x));
EXPECT_EQ(912, x);
unsigned char y;
std::string overflow_input = std::to_string(UINT64_MAX) + "0";
EXPECT_FALSE(CLI::detail::lexical_cast(overflow_input, y));
std::string bad_input = "hello";
EXPECT_FALSE(CLI::detail::lexical_cast(bad_input, y));
}
TEST(Types, LexicalCastDouble) {
std::string input = "9.12";
long double x;
EXPECT_TRUE(CLI::detail::lexical_cast(input, x));
EXPECT_FLOAT_EQ((float)9.12, (float)x);
std::string bad_input = "hello";
EXPECT_FALSE(CLI::detail::lexical_cast(bad_input, x));
std::string overflow_input = "1" + std::to_string(LDBL_MAX);
EXPECT_FALSE(CLI::detail::lexical_cast(overflow_input, x));
}
TEST(Types, LexicalCastString) {
std::string input = "one";
std::string output;
CLI::detail::lexical_cast(input, output);
EXPECT_EQ(input, output);
}