Skip to content

Commit af92d15

Browse files
committed
[TEST] Refactor basic_options and version string into test_fixture
1 parent 77fa6f2 commit af92d15

File tree

4 files changed

+70
-102
lines changed

4 files changed

+70
-102
lines changed

test/include/sharg/test/test_fixture.hpp

+33
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,39 @@ class test_fixture : public ::testing::Test
8686
toggle_guardian();
8787
return testing::internal::GetCapturedStdout();
8888
}
89+
90+
static inline std::string basic_options_str = " Common options\n"
91+
" -h, --help\n"
92+
" Prints the help page.\n"
93+
" -hh, --advanced-help\n"
94+
" Prints the help page including advanced options.\n"
95+
" --version\n"
96+
" Prints the version information.\n"
97+
" --copyright\n"
98+
" Prints the copyright/license information.\n"
99+
" --export-help (std::string)\n"
100+
" Export the help page information. Value must be one of "
101+
#if SHARG_HAS_TDL
102+
"[html, man,\n ctd, cwl].\n";
103+
#else
104+
"[html, man].\n";
105+
#endif
106+
107+
static inline std::string version_str(std::string_view const subcommand_with_dash)
108+
{
109+
return std::string{"VERSION\n"}
110+
+ " Last update:\n"
111+
" test_parser"
112+
+ subcommand_with_dash.data()
113+
+ " version:\n"
114+
" Sharg version: "
115+
+ sharg::sharg_version_cstring + '\n';
116+
}
117+
118+
static inline std::string version_str()
119+
{
120+
return version_str("");
121+
}
89122
};
90123

91124
class early_exit_guardian

test/unit/detail/format_help_test.cpp

+21-41
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,6 @@ class format_help_test : public sharg::test::test_fixture
1919
bool flag_value{false};
2020
std::vector<std::string> pos_opt_value{};
2121

22-
static inline std::string basic_options_str = " Common options\n"
23-
" -h, --help\n"
24-
" Prints the help page.\n"
25-
" -hh, --advanced-help\n"
26-
" Prints the help page including advanced options.\n"
27-
" --version\n"
28-
" Prints the version information.\n"
29-
" --copyright\n"
30-
" Prints the copyright/license information.\n"
31-
" --export-help (std::string)\n"
32-
" Export the help page information. Value must be one of "
33-
#if SHARG_HAS_TDL
34-
"[html, man,\n ctd, cwl].\n";
35-
#else
36-
"[html, man].\n";
37-
#endif
38-
39-
static inline std::string basic_version_str = "VERSION\n"
40-
" Last update:\n"
41-
" test_parser version:\n"
42-
" Sharg version: "
43-
+ std::string{sharg::sharg_version_cstring} + "\n";
44-
4522
static inline std::string license_text = []()
4623
{
4724
std::ifstream license_file{std::string{SHARG_TEST_LICENSE_DIR} + "/LICENSE.md"};
@@ -120,7 +97,7 @@ TEST_F(format_help_test, quote_strings)
12097
" Default: [\"Some\", \"other\", \"string\"]\n"
12198
" -e, --string5 (List of std::string)\n"
12299
" Default: None\n\n"
123-
+ basic_options_str + "\n" + basic_version_str;
100+
+ basic_options_str + "\n" + version_str();
124101
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
125102
}
126103

@@ -155,7 +132,7 @@ TEST_F(format_help_test, quote_paths)
155132
" Default: [\"/some\", \"/other\", \"/path\"]\n"
156133
" -e, --path5 (List of std::filesystem::path)\n"
157134
" Default: None\n\n"
158-
+ basic_options_str + "\n" + basic_version_str;
135+
+ basic_options_str + "\n" + version_str();
159136
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
160137
}
161138

@@ -166,7 +143,7 @@ TEST_F(format_help_test, no_information)
166143
expected = "test_parser\n"
167144
"===========\n"
168145
"\nOPTIONS\n\n"
169-
+ basic_options_str + "\n" + basic_version_str;
146+
+ basic_options_str + "\n" + version_str();
170147
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
171148
}
172149

@@ -179,7 +156,7 @@ TEST_F(format_help_test, with_short_copyright)
179156
expected = "test_parser\n"
180157
"===========\n"
181158
"\nOPTIONS\n\n"
182-
+ basic_options_str + "\n" + basic_version_str + "\n"
159+
+ basic_options_str + "\n" + version_str() + "\n"
183160
+ "LEGAL\n"
184161
" test_parser Copyright: short\n"
185162
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
@@ -195,7 +172,7 @@ TEST_F(format_help_test, with_long_copyright)
195172
expected = "test_parser\n"
196173
"===========\n"
197174
"\nOPTIONS\n\n"
198-
+ basic_options_str + "\n" + basic_version_str + "\n"
175+
+ basic_options_str + "\n" + version_str() + "\n"
199176
+ "LEGAL\n"
200177
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
201178
" 3-clause BSDL.\n"
@@ -211,7 +188,7 @@ TEST_F(format_help_test, with_citation)
211188
expected = "test_parser\n"
212189
"===========\n"
213190
"\nOPTIONS\n\n"
214-
+ basic_options_str + "\n" + basic_version_str + "\n"
191+
+ basic_options_str + "\n" + version_str() + "\n"
215192
+ "LEGAL\n"
216193
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
217194
" 3-clause BSDL.\n"
@@ -227,7 +204,7 @@ TEST_F(format_help_test, with_author)
227204
expected = "test_parser\n"
228205
"===========\n"
229206
"\nOPTIONS\n\n"
230-
+ basic_options_str + "\n" + basic_version_str + "\n"
207+
+ basic_options_str + "\n" + version_str() + "\n"
231208
+ "LEGAL\n"
232209
" Author: author\n"
233210
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
@@ -243,7 +220,7 @@ TEST_F(format_help_test, with_email)
243220
expected = "test_parser\n"
244221
"===========\n"
245222
"\nOPTIONS\n\n"
246-
+ basic_options_str + "\n" + basic_version_str + "\n"
223+
+ basic_options_str + "\n" + version_str() + "\n"
247224
+ "LEGAL\n"
248225
" Contact: email\n"
249226
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
@@ -258,7 +235,7 @@ TEST_F(format_help_test, empty_advanced_help)
258235
expected = "test_parser\n"
259236
"===========\n"
260237
"\nOPTIONS\n\n"
261-
+ basic_options_str + "\n" + basic_version_str;
238+
+ basic_options_str + "\n" + version_str();
262239
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
263240
}
264241

@@ -269,7 +246,7 @@ TEST_F(format_help_test, empty_version_call)
269246
expected = "test_parser\n"
270247
"===========\n"
271248
"\n"
272-
+ basic_version_str;
249+
+ version_str();
273250
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
274251
}
275252

@@ -285,7 +262,7 @@ TEST_F(format_help_test, version_call)
285262
expected = "test_parser\n"
286263
"===========\n"
287264
"\n"
288-
+ basic_version_str + "\n"
265+
+ version_str() + "\n"
289266
+ "URL\n"
290267
" https://seqan.de\n";
291268
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
@@ -301,7 +278,7 @@ TEST_F(format_help_test, do_not_print_hidden_options)
301278
expected = "test_parser\n"
302279
"===========\n"
303280
"\nOPTIONS\n\n"
304-
+ basic_options_str + "\n" + basic_version_str;
281+
+ basic_options_str + "\n" + version_str();
305282
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
306283
}
307284

@@ -359,7 +336,7 @@ TEST_F(format_help_test, advanced_options)
359336
" list item.\n"
360337
" some line.\n"
361338
"\n"
362-
+ basic_options_str + "\n" + basic_version_str;
339+
+ basic_options_str + "\n" + version_str();
363340
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
364341

365342
// with -hh everything is shown
@@ -392,7 +369,7 @@ TEST_F(format_help_test, advanced_options)
392369
" list item.\n"
393370
" some line.\n"
394371
"\n"
395-
+ basic_options_str + "\n" + basic_version_str;
372+
+ basic_options_str + "\n" + version_str();
396373
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
397374
}
398375

@@ -486,7 +463,7 @@ TEST_F(format_help_test, full_information)
486463
"\n"
487464
" example2\n"
488465
"\n"
489-
+ basic_version_str;
466+
+ version_str();
490467
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
491468
}
492469

@@ -540,10 +517,12 @@ TEST_F(format_help_test, copyright)
540517
TEST_F(format_help_test, subcommand_parser)
541518
{
542519
bool flag_value{false};
520+
int option_value{};
543521

544522
auto parser = get_subcommand_parser({"-h"}, {"sub1", "sub2"});
545523
parser.info.description.push_back("description");
546524
parser.add_flag(flag_value, sharg::config{.short_id = 'f', .long_id = "foo", .description = "A flag."});
525+
parser.add_option(option_value, sharg::config{.short_id = 'o', .long_id = "option", .description = "An option."});
547526

548527
std::string expected = "test_parser\n"
549528
"===========\n"
@@ -561,11 +540,12 @@ TEST_F(format_help_test, subcommand_parser)
561540
" The following options belong to the top-level parser and need to be\n"
562541
" specified before the subcommand key word. Every argument after the\n"
563542
" subcommand key word is passed on to the corresponding sub-parser.\n"
564-
"\n"
565-
"OPTIONS\n"
543+
"\nOPTIONS\n"
566544
" -f, --foo\n"
567545
" A flag.\n"
546+
" -o, --option (signed 32 bit integer)\n"
547+
" An option. Default: 0\n"
568548
"\n"
569-
+ basic_options_str + "\n" + basic_version_str;
549+
+ basic_options_str + "\n" + version_str();
570550
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
571551
}

test/unit/detail/seqan3_test.cpp

+6-28
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,19 @@
1111
# error "seqan3/version.hpp is not available"
1212
#endif
1313

14-
std::string const basic_options_str = " Common options\n"
15-
" -h, --help\n"
16-
" Prints the help page.\n"
17-
" -hh, --advanced-help\n"
18-
" Prints the help page including advanced options.\n"
19-
" --version\n"
20-
" Prints the version information.\n"
21-
" --copyright\n"
22-
" Prints the copyright/license information.\n"
23-
" --export-help (std::string)\n"
24-
" Export the help page information. Value must be one of "
25-
#if SHARG_HAS_TDL
26-
"[html, man,\n ctd, cwl].\n";
27-
#else
28-
"[html, man].\n";
29-
#endif
30-
31-
std::string const basic_version_str = "VERSION\n"
32-
" Last update:\n"
33-
" test_parser version:\n"
34-
" Sharg version: "
35-
+ std::string{sharg::sharg_version_cstring}
36-
+ "\n"
37-
" SeqAn version: "
38-
+ std::string{seqan3::seqan3_version_cstring} + "\n";
39-
4014
class seqan3_test : public sharg::test::test_fixture
41-
{};
15+
{
16+
protected:
17+
static inline std::string version_str_with_seqan3 =
18+
version_str() + " SeqAn version: " + seqan3::seqan3_version_cstring + '\n';
19+
};
4220

4321
TEST_F(seqan3_test, version_string)
4422
{
4523
auto parser = get_parser("-h");
4624
std::string expected = "test_parser\n"
4725
"===========\n"
4826
"\nOPTIONS\n\n"
49-
+ basic_options_str + "\n" + basic_version_str;
27+
+ basic_options_str + '\n' + version_str_with_seqan3;
5028
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
5129
}

0 commit comments

Comments
 (0)