forked from daniele77/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.cpp
More file actions
413 lines (340 loc) · 17.3 KB
/
test_cli.cpp
File metadata and controls
413 lines (340 loc) · 17.3 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*******************************************************************************
* CLI - A simple command line interface.
* Copyright (C) 2016-2021 Daniele Pallastrelli
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include <boost/test/unit_test.hpp>
#include "cli/cli.h"
#include "cli/clifilesession.h"
using namespace std;
using namespace cli;
using namespace cli::detail;
namespace {
string ExtractFirstPrompt(const stringstream& o)
{
auto content = o.str();
std::size_t pos = content.find_first_of('>');
return content.substr(0, pos);
}
string ExtractLastPrompt(const stringstream& o)
{
auto content = o.str();
std::size_t pos = content.find_last_of('\n');
content = content.substr(pos+1);
pos = content.find_last_of('>');
return content.substr(0, pos);
}
/* takes
cli> sub> foo
bar
sub>
and gives
foo
bar
*/
string ExtractContent(const stringstream& o)
{
auto content = o.str();
// last line
auto lastNL = content.find_last_of('\n');
auto lastLine = content.substr(lastNL+1);
content = content.substr(0, lastNL);
auto pos = content.find(lastLine);
return content.substr(pos+lastLine.size());
}
void UserInput(Cli& cli, stringstream& oss, const string& input)
{
oss.str("");
oss.clear();
stringstream iss;
iss.str(input + '\n');
CliFileSession session(cli, iss, oss);
session.Start();
}
} // namespace
BOOST_AUTO_TEST_SUITE(CliSuite)
BOOST_AUTO_TEST_CASE(Basics)
{
auto rootMenu = make_unique<Menu>("cli");
rootMenu->Insert("int_cmd", [](ostream& out, int par){ out << par << "\n"; }, "int_cmd help", {"int_par"} );
rootMenu->Insert("string_cmd", [](ostream& out, const string& par){ out << par << "\n"; }, "string_cmd help", {"string_par"} );
Cli cli(move(rootMenu));
stringstream oss;
UserInput(cli, oss, "help");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK(ExtractContent(oss).find("int_cmd") != string::npos);
BOOST_CHECK(ExtractContent(oss).find("<int_par>") != string::npos);
BOOST_CHECK(ExtractContent(oss).find("string_cmd") != string::npos);
BOOST_CHECK(ExtractContent(oss).find("<string_par>") != string::npos);
UserInput(cli, oss, "int_cmd 42");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "int_cmd wrong_int_parameter");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "int_cmd 42 0");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "string_cmd foo");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "foo");
UserInput(cli, oss, R"(string_cmd "foo 'bar' \"foo\\2")");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), R"(foo 'bar' "foo\2)");
}
BOOST_AUTO_TEST_CASE(parameters)
{
auto rootMenu = make_unique<Menu>("cli");
rootMenu->Insert("char_cmd", [](ostream& out, char par){ out << par << "\n"; }, "char_cmd help", {"char_par"} );
rootMenu->Insert("unsigned_char_cmd", [](ostream& out, unsigned char par){ out << static_cast<unsigned int>(par) << "\n"; }, "unsigned_char_cmd help", {"unsigned_char_par"} );
rootMenu->Insert("signed_char_cmd", [](ostream& out, signed char par){ out << static_cast<int>(par) << "\n"; }, "signed_char_cmd help", {"signed_char_par"} );
rootMenu->Insert("short_cmd", [](ostream& out, short par){ out << par << "\n"; }, "short_cmd help", {"short_par"} );
rootMenu->Insert("unsigned_short_cmd", [](ostream& out, unsigned short par){ out << par << "\n"; }, "unsigned_short_cmd help", {"unsigned_short_par"} );
rootMenu->Insert("int_cmd", [](ostream& out, int par){ out << par << "\n"; }, "int_cmd help", {"int_par"} );
rootMenu->Insert("unsigned_int_cmd", [](ostream& out, unsigned int par){ out << par << "\n"; }, "unsigned_int_cmd help", {"unsigned_int_par"} );
rootMenu->Insert("long_cmd", [](ostream& out, long par){ out << par << "\n"; }, "long_cmd help", {"long_par"} );
rootMenu->Insert("unsigned_long_cmd", [](ostream& out, unsigned long par){ out << par << "\n"; }, "unsigned_long_cmd help", {"unsigned_long_par"} );
rootMenu->Insert("long_long_cmd", [](ostream& out, long long par){ out << par << "\n"; }, "long_long_cmd help", {"long_long_par"} );
rootMenu->Insert("unsigned_long_long_cmd", [](ostream& out, unsigned long long par){ out << par << "\n"; }, "unsigned_long_long_cmd help", {"unsigned_long_long_par"} );
rootMenu->Insert("float_cmd", [](ostream& out, float par){ out << par << "\n"; }, "float_cmd help", {"float_par"} );
rootMenu->Insert("double_cmd", [](ostream& out, double par){ out << par << "\n"; }, "double_cmd help", {"double_par"} );
rootMenu->Insert("long_double_cmd", [](ostream& out, long double par){ out << par << "\n"; }, "long_double_cmd help", {"long_double_par"} );
rootMenu->Insert("string_cmd", [](ostream& out, const string& par){ out << par << "\n"; }, "string_cmd help", {"string_par"} );
Cli cli(move(rootMenu));
stringstream oss;
UserInput(cli, oss, "char_cmd a");
BOOST_CHECK_EQUAL(ExtractContent(oss), "a");
UserInput(cli, oss, "char_cmd ' '");
BOOST_CHECK_EQUAL(ExtractContent(oss), " ");
UserInput(cli, oss, "char_cmd aa");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_char_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "unsigned_char_cmd -42");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_char_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_char_cmd 99999999999999999999999999999999999999999");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "signed_char_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "signed_char_cmd -42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "-42");
UserInput(cli, oss, "signed_char_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "signed_char_cmd 99999999999999999999999999999999999999999");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "short_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "short_cmd -42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "-42");
UserInput(cli, oss, "short_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "short_cmd 99999999999999999999999999999999999999999");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_short_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "unsigned_short_cmd -42");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_short_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_short_cmd 99999999999999999999999999999999999999999");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "int_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "int_cmd -42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "-42");
UserInput(cli, oss, "int_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "int_cmd 99999999999999999999999999999999999999999");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_int_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "unsigned_int_cmd -42");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_int_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_int_cmd 99999999999999999999999999999999999999999");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "long_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "long_cmd -42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "-42");
UserInput(cli, oss, "long_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_long_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "unsigned_long_cmd -42");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_long_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "long_long_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "long_long_cmd -42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "-42");
UserInput(cli, oss, "long_long_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_long_long_cmd 42");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "unsigned_long_long_cmd -42");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "unsigned_long_long_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "float_cmd 0.1");
BOOST_CHECK_EQUAL(ExtractContent(oss), "0.1");
UserInput(cli, oss, "float_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "double_cmd 0.1");
BOOST_CHECK_EQUAL(ExtractContent(oss), "0.1");
UserInput(cli, oss, "double_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
UserInput(cli, oss, "long_double_cmd 0.1");
BOOST_CHECK_EQUAL(ExtractContent(oss), "0.1");
UserInput(cli, oss, "long_double_cmd a");
BOOST_CHECK(ExtractContent(oss).find("wrong command:") != string::npos);
}
BOOST_AUTO_TEST_CASE(freeform)
{
auto rootMenu = make_unique<Menu>("cli");
rootMenu->Insert("cmd_printer_by_ref", [](ostream& out, const std::vector<std::string>& args){
for (const auto& entry : args) {
out << entry << "*";
}
out << "\n";
}, "cmd_printer help", {"<string values>"} );
rootMenu->Insert("cmd_printer_by_value", [](ostream& out, std::vector<std::string> args){
for (const auto& entry : args) {
out << entry << "*";
}
out << "\n";
}, "cmd_printer help", {"<string values>"} );
Cli cli(move(rootMenu));
stringstream oss;
UserInput(cli, oss, R"(cmd_printer_by_value a b 'c d e' f)");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "a*b*c d e*f*");
UserInput(cli, oss, R"(cmd_printer_by_ref a b 'c d e' f)");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "a*b*c d e*f*");
// empty parameters
UserInput(cli, oss, R"(cmd_printer_by_value)");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "");
}
BOOST_AUTO_TEST_CASE(borderLine)
{
auto rootMenu = make_unique<Menu>("cli");
Cli cli(move(rootMenu));
stringstream oss;
UserInput(cli, oss, "");
BOOST_CHECK_EQUAL(oss.str(), "cli> cli> ");
UserInput(cli, oss, "\t");
BOOST_CHECK_EQUAL(oss.str(), "cli> cli> ");
}
BOOST_AUTO_TEST_CASE(Submenus)
{
auto rootMenu = make_unique<Menu>("cli");
auto subMenu = make_unique<Menu>("sub");
subMenu->Insert("int_cmd", [](ostream& out, int par){ out << par << "\n"; }, "int_cmd help", {"int_par"} );
subMenu->Insert("string_cmd", [](ostream& out, const string& par){ out << par << "\n"; }, "string_cmd help", {"string_par"} );
auto subSubMenu = make_unique<Menu>("subsub");
subSubMenu->Insert("double_int_cmd", [](ostream& out, int par1, int par2){ out << par1 << par2 << "\n"; } );
subSubMenu->Insert("double_string_cmd", [](ostream& out, const string& par1, const string& par2){ out << par1 << par2 << "\n"; } );
subMenu->Insert(move(subSubMenu));
rootMenu->Insert(move(subMenu));
Cli cli(move(rootMenu));
stringstream oss;
UserInput(cli, oss, "help");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK(ExtractContent(oss).find("sub") != string::npos);
BOOST_CHECK(ExtractContent(oss).find("help") != string::npos);
BOOST_CHECK(ExtractContent(oss).find("exit") != string::npos);
// from the root menu
UserInput(cli, oss, "sub int_cmd 42");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "sub string_cmd foo");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "foo");
UserInput(cli, oss, "sub subsub double_int_cmd 42 0");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "420");
UserInput(cli, oss, "sub subsub double_string_cmd foo bar");
BOOST_CHECK_EQUAL(ExtractFirstPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "cli");
BOOST_CHECK_EQUAL(ExtractContent(oss), "foobar");
// enter the sub menu
UserInput(cli, oss, "sub\nint_cmd 42");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "sub");
BOOST_CHECK_EQUAL(ExtractContent(oss), "42");
UserInput(cli, oss, "sub\nstring_cmd foo");
BOOST_CHECK_EQUAL(ExtractLastPrompt(oss), "sub");
BOOST_CHECK_EQUAL(ExtractContent(oss), "foo");
}
BOOST_AUTO_TEST_CASE(ExitActions)
{
auto rootMenu = make_unique<Menu>("cli");
rootMenu->Insert("int_cmd", [](ostream& out, int par){ out << par << "\n"; }, "int_cmd help", {"int_par"} );
rootMenu->Insert("string_cmd", [](ostream& out, const string& par){ out << par << "\n"; }, "string_cmd help", {"string_par"} );
Cli cli(move(rootMenu));
bool exitActionDone = false;
cli.ExitAction([&](std::ostream&){ exitActionDone=true; });
stringstream oss;
UserInput(cli, oss, "exit");
BOOST_CHECK(exitActionDone);
}
BOOST_AUTO_TEST_CASE(Exceptions)
{
auto rootMenu = make_unique<Menu>("cli");
rootMenu->Insert("stdexception", [](ostream&){ throw std::logic_error("myerror"); } );
rootMenu->Insert("customexception", [](ostream&){ throw 42; } );
Cli cli(move(rootMenu));
stringstream oss;
// std exception type, no custom handler
BOOST_CHECK_NO_THROW( UserInput(cli, oss, "stdexception") );
BOOST_CHECK_EQUAL(ExtractContent(oss), "myerror");
// std exception type, custom handler
bool excActionDone = false;
cli.StdExceptionHandler( [&](std::ostream&, const std::string&, const std::exception&){ excActionDone = true; } );
BOOST_CHECK_NO_THROW( UserInput(cli, oss, "stdexception") );
BOOST_CHECK(excActionDone);
// custom exception
BOOST_CHECK_NO_THROW( UserInput(cli, oss, "customexception") );
}
BOOST_AUTO_TEST_SUITE_END()