Skip to content

Commit

Permalink
[TEST] Refactor basic_options and version string into test_fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Mar 1, 2024
1 parent 89436bb commit 8f8e1e1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 102 deletions.
33 changes: 33 additions & 0 deletions test/include/sharg/test/test_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ class test_fixture : public ::testing::Test
toggle_guardian();
return testing::internal::GetCapturedStdout();
}

static inline std::string basic_options_str = " Common options\n"
" -h, --help\n"
" Prints the help page.\n"
" -hh, --advanced-help\n"
" Prints the help page including advanced options.\n"
" --version\n"
" Prints the version information.\n"
" --copyright\n"
" Prints the copyright/license information.\n"
" --export-help (std::string)\n"
" Export the help page information. Value must be one of "
#if SHARG_HAS_TDL
"[html, man,\n ctd, cwl].\n";
#else
"[html, man].\n";
#endif

static inline std::string version_str(std::string_view const subcommand_with_dash)
{
return std::string{"VERSION\n"}
+ " Last update:\n"
" test_parser"
+ subcommand_with_dash.data()
+ " version:\n"
" Sharg version: "
+ sharg::sharg_version_cstring + '\n';
}

static inline std::string version_str()
{
return version_str("");
}
};

class early_exit_guardian
Expand Down
62 changes: 21 additions & 41 deletions test/unit/detail/format_help_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,6 @@ class format_help_test : public sharg::test::test_fixture
bool flag_value{false};
std::vector<std::string> pos_opt_value{};

static inline std::string basic_options_str = " Common options\n"
" -h, --help\n"
" Prints the help page.\n"
" -hh, --advanced-help\n"
" Prints the help page including advanced options.\n"
" --version\n"
" Prints the version information.\n"
" --copyright\n"
" Prints the copyright/license information.\n"
" --export-help (std::string)\n"
" Export the help page information. Value must be one of "
#if SHARG_HAS_TDL
"[html, man,\n ctd, cwl].\n";
#else
"[html, man].\n";
#endif

static inline std::string basic_version_str = "VERSION\n"
" Last update:\n"
" test_parser version:\n"
" Sharg version: "
+ std::string{sharg::sharg_version_cstring} + "\n";

static inline std::string license_text = []()
{
std::ifstream license_file{std::string{SHARG_TEST_LICENSE_DIR} + "/LICENSE.md"};
Expand Down Expand Up @@ -120,7 +97,7 @@ TEST_F(format_help_test, quote_strings)
" Default: [\"Some\", \"other\", \"string\"]\n"
" -e, --string5 (List of std::string)\n"
" Default: None\n\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + "\n" + version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}

Expand Down Expand Up @@ -155,7 +132,7 @@ TEST_F(format_help_test, quote_paths)
" Default: [\"/some\", \"/other\", \"/path\"]\n"
" -e, --path5 (List of std::filesystem::path)\n"
" Default: None\n\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + "\n" + version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}

Expand All @@ -166,7 +143,7 @@ TEST_F(format_help_test, no_information)
expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + "\n" + version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}

Expand All @@ -179,7 +156,7 @@ TEST_F(format_help_test, with_short_copyright)
expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str + "\n"
+ basic_options_str + "\n" + version_str() + "\n"
+ "LEGAL\n"
" test_parser Copyright: short\n"
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
Expand All @@ -195,7 +172,7 @@ TEST_F(format_help_test, with_long_copyright)
expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str + "\n"
+ basic_options_str + "\n" + version_str() + "\n"
+ "LEGAL\n"
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
" 3-clause BSDL.\n"
Expand All @@ -211,7 +188,7 @@ TEST_F(format_help_test, with_citation)
expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str + "\n"
+ basic_options_str + "\n" + version_str() + "\n"
+ "LEGAL\n"
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
" 3-clause BSDL.\n"
Expand All @@ -227,7 +204,7 @@ TEST_F(format_help_test, with_author)
expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str + "\n"
+ basic_options_str + "\n" + version_str() + "\n"
+ "LEGAL\n"
" Author: author\n"
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
Expand All @@ -243,7 +220,7 @@ TEST_F(format_help_test, with_email)
expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str + "\n"
+ basic_options_str + "\n" + version_str() + "\n"
+ "LEGAL\n"
" Contact: email\n"
" SeqAn Copyright: 2006-2024 Knut Reinert, FU-Berlin; released under the\n"
Expand All @@ -258,7 +235,7 @@ TEST_F(format_help_test, empty_advanced_help)
expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + "\n" + version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}

Expand All @@ -269,7 +246,7 @@ TEST_F(format_help_test, empty_version_call)
expected = "test_parser\n"
"===========\n"
"\n"
+ basic_version_str;
+ version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}

Expand All @@ -285,7 +262,7 @@ TEST_F(format_help_test, version_call)
expected = "test_parser\n"
"===========\n"
"\n"
+ basic_version_str + "\n"
+ version_str() + "\n"
+ "URL\n"
" https://seqan.de\n";
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
Expand All @@ -301,7 +278,7 @@ TEST_F(format_help_test, do_not_print_hidden_options)
expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + "\n" + version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}

Expand Down Expand Up @@ -359,7 +336,7 @@ TEST_F(format_help_test, advanced_options)
" list item.\n"
" some line.\n"
"\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + "\n" + version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);

// with -hh everything is shown
Expand Down Expand Up @@ -392,7 +369,7 @@ TEST_F(format_help_test, advanced_options)
" list item.\n"
" some line.\n"
"\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + "\n" + version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}

Expand Down Expand Up @@ -486,7 +463,7 @@ TEST_F(format_help_test, full_information)
"\n"
" example2\n"
"\n"
+ basic_version_str;
+ version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}

Expand Down Expand Up @@ -540,10 +517,12 @@ TEST_F(format_help_test, copyright)
TEST_F(format_help_test, subcommand_parser)
{
bool flag_value{false};
int option_value{};

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

std::string expected = "test_parser\n"
"===========\n"
Expand All @@ -561,11 +540,12 @@ TEST_F(format_help_test, subcommand_parser)
" The following options belong to the top-level parser and need to be\n"
" specified before the subcommand key word. Every argument after the\n"
" subcommand key word is passed on to the corresponding sub-parser.\n"
"\n"
"OPTIONS\n"
"\nOPTIONS\n"
" -f, --foo\n"
" A flag.\n"
" -o, --option (signed 32 bit integer)\n"
" An option. Default: 0\n"
"\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + "\n" + version_str();
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}
34 changes: 6 additions & 28 deletions test/unit/detail/seqan3_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,19 @@
# error "seqan3/version.hpp is not available"
#endif

std::string const basic_options_str = " Common options\n"
" -h, --help\n"
" Prints the help page.\n"
" -hh, --advanced-help\n"
" Prints the help page including advanced options.\n"
" --version\n"
" Prints the version information.\n"
" --copyright\n"
" Prints the copyright/license information.\n"
" --export-help (std::string)\n"
" Export the help page information. Value must be one of "
#if SHARG_HAS_TDL
"[html, man,\n ctd, cwl].\n";
#else
"[html, man].\n";
#endif

std::string const basic_version_str = "VERSION\n"
" Last update:\n"
" test_parser version:\n"
" Sharg version: "
+ std::string{sharg::sharg_version_cstring}
+ "\n"
" SeqAn version: "
+ std::string{seqan3::seqan3_version_cstring} + "\n";

class seqan3_test : public sharg::test::test_fixture
{};
{
protected:
static inline std::string version_str_with_seqan3 =
version_str() + " SeqAn version: " + seqan3::seqan3_version_cstring + '\n';
};

TEST_F(seqan3_test, version_string)
{
auto parser = get_parser("-h");
std::string expected = "test_parser\n"
"===========\n"
"\nOPTIONS\n\n"
+ basic_options_str + "\n" + basic_version_str;
+ basic_options_str + '\n' + version_str_with_seqan3;
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
}
Loading

0 comments on commit 8f8e1e1

Please sign in to comment.