diff --git a/test/include/sharg/test/test_fixture.hpp b/test/include/sharg/test/test_fixture.hpp index c41e324a..b1607bc1 100644 --- a/test/include/sharg/test/test_fixture.hpp +++ b/test/include/sharg/test/test_fixture.hpp @@ -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 diff --git a/test/unit/detail/format_help_test.cpp b/test/unit/detail/format_help_test.cpp index 596934a4..253cd6db 100644 --- a/test/unit/detail/format_help_test.cpp +++ b/test/unit/detail/format_help_test.cpp @@ -19,29 +19,6 @@ class format_help_test : public sharg::test::test_fixture bool flag_value{false}; std::vector 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"}; @@ -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); } @@ -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); } @@ -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); } @@ -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" @@ -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" @@ -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" @@ -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" @@ -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" @@ -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); } @@ -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); } @@ -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); @@ -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); } @@ -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 @@ -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); } @@ -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); } @@ -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" @@ -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); } diff --git a/test/unit/detail/seqan3_test.cpp b/test/unit/detail/seqan3_test.cpp index 765d9475..187ac3e0 100644 --- a/test/unit/detail/seqan3_test.cpp +++ b/test/unit/detail/seqan3_test.cpp @@ -11,34 +11,12 @@ # 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) { @@ -46,6 +24,6 @@ TEST_F(seqan3_test, version_string) 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); } diff --git a/test/unit/parser/format_parse_validators_test.cpp b/test/unit/parser/format_parse_validators_test.cpp index 87d275a6..66871904 100644 --- a/test/unit/parser/format_parse_validators_test.cpp +++ b/test/unit/parser/format_parse_validators_test.cpp @@ -11,29 +11,6 @@ #include #include -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"; - class validator_test : public sharg::test::test_fixture {}; @@ -136,7 +113,7 @@ TEST_F(validator_test, input_file) " desc. The input file must exist and read permissions must be\n" " granted. Valid file extensions are: [fa, sam, fasta, fasta.txt].\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); // get help page message (file extensions) @@ -256,7 +233,7 @@ TEST_F(validator_test, output_file) " must be granted. Valid file extensions are: [fa, sam, fasta,\n" " fasta.txt].\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); // get help page message (overwriting allowed) @@ -271,7 +248,7 @@ TEST_F(validator_test, output_file) " desc. Write permissions must be granted. Valid file extensions are:\n" " [fa, sam, fasta, fasta.txt].\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); // get help page message (file extensions) @@ -337,7 +314,7 @@ TEST_F(validator_test, input_directory) " ARGUMENT-1 (std::filesystem::path)\n" " desc. An existing, readable path for the input directory.\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); } @@ -391,7 +368,7 @@ TEST_F(validator_test, output_directory) " ARGUMENT-1 (std::filesystem::path)\n" " desc. A valid path for the output directory.\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); } @@ -623,7 +600,7 @@ TEST_F(validator_test, arithmetic_range_validator_success) " ARGUMENT-1 (List of signed 32 bit integer)\n" " desc Default: []. Value must be in range [-20,20].\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); // option - double value @@ -787,7 +764,7 @@ TEST_F(validator_test, value_list_validator_success) "\nOPTIONS\n" " -i, --int-option (List of signed 32 bit integer)\n" " desc Default: []. Value must be one of [-10, 48, 50].\n\n" - + basic_options_str + "\n" + basic_version_str); + + basic_options_str + '\n' + version_str()); EXPECT_EQ(get_parse_cout_on_exit(parser), expected); } @@ -909,7 +886,7 @@ TEST_F(validator_test, regex_validator_success) " desc Default: []. Value must match the pattern\n" " '[a-zA-Z]+@[a-zA-Z]+\\.com'.\n" "\n" - + basic_options_str + "\n" + basic_version_str); + + basic_options_str + '\n' + version_str()); EXPECT_EQ(get_parse_cout_on_exit(parser), expected); } @@ -1096,7 +1073,7 @@ TEST_F(validator_test, chaining_validators) " write permissions must be granted. Valid file extensions are: [sa,\n" " so]. Value must match the pattern '.*'.\n" "\n"} - + basic_options_str + "\n" + basic_version_str; + + basic_options_str + '\n' + version_str(); EXPECT_EQ(get_parse_cout_on_exit(parser), expected); // help page message (allow overwriting) @@ -1118,7 +1095,7 @@ TEST_F(validator_test, chaining_validators) " '(/[^/]+)+/.*\\.[^/\\.]+$'. Write permissions must be granted. Valid\n" " file extensions are: [sa, so]. Value must match the pattern '.*'.\n" "\n"} - + basic_options_str + "\n" + basic_version_str; + + basic_options_str + '\n' + version_str(); EXPECT_EQ(get_parse_cout_on_exit(parser), expected); // chaining with a container option value type