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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
|
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------
#include <gtest/gtest.h>
#include <fstream>
#include <seqan3/argument_parser/argument_parser.hpp>
#include <seqan3/std/ranges>
#include <seqan3/std/filesystem>
#include <seqan3/test/tmp_filename.hpp>
struct dummy_file
{
struct format1
{
static inline std::vector<std::string> file_extensions{ {"fa"}, {"fasta"}};
};
struct format2
{
static inline std::vector<std::string> file_extensions{ {"sam"}, {"bam"}};
};
using valid_formats = seqan3::type_list<format1, format2>;
};
std::string const basic_options_str = "OPTIONS\n"
"\n"
" Basic 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 [html, man].\n"
"\n"
" \n";
std::string const basic_version_str = "VERSION\n"
" Last update: \n"
" test_parser version: \n"
" SeqAn version: " + seqan3::seqan3_version + "\n";
namespace seqan3::detail
{
struct test_accessor
{
static void set_terminal_width(seqan3::argument_parser & parser, unsigned terminal_width)
{
std::visit([terminal_width](auto & f)
{
if constexpr(std::is_same_v<decltype(f), seqan3::detail::format_help &>)
f.layout = seqan3::detail::format_help::console_layout_struct{terminal_width};
}, parser.format);
}
};
} // seqan3::detail
using seqan3::detail::test_accessor;
TEST(validator_test, fullfill_concept)
{
EXPECT_FALSE(seqan3::validator<int>);
EXPECT_TRUE(seqan3::validator<seqan3::detail::default_validator<int>>);
EXPECT_TRUE(seqan3::validator<seqan3::detail::default_validator<int> const>);
EXPECT_TRUE(seqan3::validator<seqan3::detail::default_validator<int> &>);
EXPECT_TRUE(seqan3::validator<seqan3::detail::default_validator<std::vector<int>>>);
EXPECT_TRUE(seqan3::validator<seqan3::arithmetic_range_validator>);
EXPECT_TRUE(seqan3::validator<seqan3::value_list_validator<double>>);
EXPECT_TRUE(seqan3::validator<seqan3::value_list_validator<std::string>>);
EXPECT_TRUE(seqan3::validator<seqan3::input_file_validator<>>);
EXPECT_TRUE(seqan3::validator<seqan3::output_file_validator<>>);
EXPECT_TRUE(seqan3::validator<seqan3::input_directory_validator>);
EXPECT_TRUE(seqan3::validator<seqan3::output_directory_validator>);
EXPECT_TRUE(seqan3::validator<seqan3::regex_validator>);
EXPECT_TRUE(seqan3::validator<decltype(seqan3::input_file_validator{{"t"}} | seqan3::regex_validator{".*"})>);
}
TEST(validator_test, input_file)
{
seqan3::test::tmp_filename tmp_name{"testbox.fasta"};
seqan3::test::tmp_filename tmp_name_2{"testbox_2.fasta"};
std::vector formats{std::string{"fa"}, std::string{"sam"}, std::string{"fasta"}};
std::ofstream tmp_file(tmp_name.get_path());
std::ofstream tmp_file_2(tmp_name_2.get_path());
{ // single file
{ // empty list of file.
seqan3::input_file_validator my_validator{};
EXPECT_NO_THROW(my_validator(tmp_name.get_path()));
}
{ // file already exists.
std::filesystem::path does_not_exist{tmp_name.get_path()};
does_not_exist.replace_extension(".bam");
seqan3::input_file_validator my_validator{formats};
EXPECT_THROW(my_validator(does_not_exist), seqan3::validation_error);
}
{ // file has wrong format.
seqan3::input_file_validator my_validator{std::vector{std::string{"sam"}}};
EXPECT_THROW(my_validator(tmp_name.get_path()), seqan3::validation_error);
}
{ // file has no extension.
std::filesystem::path does_not_exist{tmp_name.get_path()};
does_not_exist.replace_extension();
seqan3::input_file_validator my_validator{formats};
EXPECT_THROW(my_validator(does_not_exist), seqan3::validation_error);
}
{ // read from file
seqan3::input_file_validator<dummy_file> my_validator{};
EXPECT_NO_THROW(my_validator(tmp_name.get_path()));
}
std::filesystem::path file_in_path;
// option
std::string const & path = tmp_name.get_path().string();
const char * argv[] = {"./argument_parser_test", "-i", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(file_in_path, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::input_file_validator{formats});
EXPECT_NO_THROW(parser.parse());
EXPECT_EQ(file_in_path.string(), path);
}
{ // file list.
std::vector<std::filesystem::path> input_files;
// option
std::string const & path = tmp_name.get_path().string();
std::string const & path_2 = tmp_name_2.get_path().string();
const char * argv[] = {"./argument_parser_test", path.c_str(), path_2.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_positional_option(input_files, "desc", seqan3::input_file_validator{formats});
EXPECT_NO_THROW(parser.parse());
EXPECT_EQ(input_files.size(), 2u);
EXPECT_EQ(input_files[0].string(), path);
EXPECT_EQ(input_files[1].string(), path_2);
}
{ // get help page message
std::filesystem::path path;
const char * argv[] = {"./argument_parser_test", "-h"};
seqan3::argument_parser parser{"test_parser", 2, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_positional_option(path, "desc", seqan3::input_file_validator{formats});
testing::internal::CaptureStdout();
EXPECT_EXIT(parser.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
std::string my_stdout = testing::internal::GetCapturedStdout();
std::string expected = std::string{"test_parser\n"
"===========\n"
"\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (std::filesystem::path)\n"
" desc The input file must exist and read permissions must be granted.\n"
" Valid file extensions are: [fa, sam, fasta].\n"
"\n"} +
basic_options_str +
"\n" +
basic_version_str;
EXPECT_EQ(my_stdout, expected);
}
}
TEST(validator_test, input_file_ext_from_file)
{
// Give as a template argument the seqan3 file type to get all valid extensions for this file.
seqan3::input_file_validator<dummy_file> validator{};
EXPECT_EQ(validator.get_help_page_message(), "The input file must exist and read permissions must be granted. "
"Valid file extensions are: [fa, fasta, sam, bam].");
seqan3::input_file_validator validator2{};
EXPECT_EQ(validator2.get_help_page_message(), "The input file must exist and read permissions must be granted.");
}
TEST(validator_test, output_file)
{
seqan3::test::tmp_filename tmp_name{"testbox.fasta"};
seqan3::test::tmp_filename tmp_name_2{"testbox_2.fasta"};
seqan3::test::tmp_filename tmp_name_3{"testbox_3.fa"};
std::vector formats{std::string{"fa"}, std::string{"sam"}, std::string{"fasta"}};
{ // single file
{ // empty list of file.
seqan3::output_file_validator my_validator{};
EXPECT_NO_THROW(my_validator(tmp_name.get_path()));
}
{ // file does not exist.
std::ofstream tmp_file_2(tmp_name_2.get_path());
std::filesystem::path does_not_exist{tmp_name_2.get_path()};
seqan3::output_file_validator my_validator{formats};
EXPECT_THROW(my_validator(does_not_exist), seqan3::validation_error);
}
{ // file has wrong format.
seqan3::output_file_validator my_validator{std::vector{std::string{"sam"}}};
EXPECT_THROW(my_validator(tmp_name.get_path()), seqan3::validation_error);
}
{ // file has no extension.
std::filesystem::path no_extension{tmp_name.get_path()};
no_extension.replace_extension();
seqan3::output_file_validator my_validator{formats};
EXPECT_THROW(my_validator(no_extension), seqan3::validation_error);
}
{ // read from file
seqan3::output_file_validator<dummy_file> my_validator{};
EXPECT_NO_THROW(my_validator(tmp_name.get_path()));
}
std::filesystem::path file_out_path;
// option
std::string const & path = tmp_name.get_path().string();
const char * argv[] = {"./argument_parser_test", "-o", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(file_out_path, 'o', "out-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::output_file_validator{formats});
EXPECT_NO_THROW(parser.parse());
EXPECT_EQ(file_out_path.string(), path);
}
{ // file list.
std::vector<std::filesystem::path> output_files;
// option
std::string const & path = tmp_name.get_path().string();
std::string const & path_3 = tmp_name_3.get_path().string();
const char * argv[] = {"./argument_parser_test", path.c_str(), path_3.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_positional_option(output_files, "desc", seqan3::output_file_validator{formats});
EXPECT_NO_THROW(parser.parse());
EXPECT_EQ(output_files.size(), 2u);
EXPECT_EQ(output_files[0].string(), path);
EXPECT_EQ(output_files[1].string(), path_3);
}
// get help page message
{
std::filesystem::path path;
const char * argv[] = {"./argument_parser_test", "-h"};
seqan3::argument_parser parser{"test_parser", 2, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_positional_option(path, "desc", seqan3::output_file_validator{formats});
testing::internal::CaptureStdout();
EXPECT_EXIT(parser.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
std::string my_stdout = testing::internal::GetCapturedStdout();
std::string expected = std::string{"test_parser\n"
"===========\n"
"\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (std::filesystem::path)\n"
" desc The output file must not exist already and write permissions\n"
" must be granted. Valid file extensions are: [fa, sam, fasta].\n"
"\n"} +
basic_options_str +
"\n" +
basic_version_str;
EXPECT_EQ(my_stdout, expected);
}
}
TEST(validator_test, output_file_ext_from_file)
{
// Give as a template argument the seqan3 file type to get all valid extensions for this file.
seqan3::output_file_validator<dummy_file> validator{};
EXPECT_EQ(validator.get_help_page_message(), "The output file must not exist already and write permissions must "
"be granted. Valid file extensions are: [fa, fasta, sam, bam].");
seqan3::output_file_validator validator2{};
EXPECT_EQ(validator2.get_help_page_message(), "The output file must not exist already and write permissions must "
"be granted.");
}
TEST(validator_test, input_directory)
{
seqan3::test::tmp_filename tmp_name{"testbox.fasta"};
{ // directory
{ // has filename
std::ofstream tmp_dir(tmp_name.get_path());
seqan3::input_directory_validator my_validator{};
EXPECT_THROW(my_validator(tmp_name.get_path()), seqan3::validation_error);
}
{ // read directory
std::filesystem::path p = tmp_name.get_path();
p.remove_filename();
std::ofstream tmp_dir(p);
seqan3::input_directory_validator my_validator{};
my_validator(p);
EXPECT_NO_THROW(my_validator(p));
std::filesystem::path dir_in_path;
// option
std::string const & path = p.string();
const char * argv[] = {"./argument_parser_test", "-i", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(dir_in_path, 'i', "input-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::input_directory_validator{});
EXPECT_NO_THROW(parser.parse());
EXPECT_EQ(path, dir_in_path.string());
}
}
{
// get help page message
std::filesystem::path path;
const char * argv[] = {"./argument_parser_test", "-h"};
seqan3::argument_parser parser{"test_parser", 2, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_positional_option(path, "desc", seqan3::input_directory_validator{});
testing::internal::CaptureStdout();
EXPECT_EXIT(parser.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
std::string my_stdout = testing::internal::GetCapturedStdout();
std::string expected = std::string{"test_parser\n"
"===========\n"
"\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (std::filesystem::path)\n"
" desc An existing, readable path for the input directory.\n"
"\n"} +
basic_options_str +
"\n" +
basic_version_str;
EXPECT_EQ(my_stdout, expected);
}
}
TEST(validator_test, output_directory)
{
seqan3::test::tmp_filename tmp_name{"testbox.fasta"};
{ // read directory
std::filesystem::path p = tmp_name.get_path();
p.remove_filename();
seqan3::output_directory_validator my_validator{};
my_validator(p);
EXPECT_NO_THROW();
std::filesystem::path dir_out_path;
// option
std::string const & path = p.string();
const char * argv[] = {"./argument_parser_test", "-o", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(dir_out_path, 'o', "output-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::output_directory_validator{});
EXPECT_NO_THROW(parser.parse());
EXPECT_EQ(path, dir_out_path.string());
}
{
// get help page message
std::filesystem::path path;
const char * argv[] = {"./argument_parser_test", "-h"};
seqan3::argument_parser parser{"test_parser", 2, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_positional_option(path, "desc", seqan3::output_directory_validator{});
testing::internal::CaptureStdout();
EXPECT_EXIT(parser.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
std::string my_stdout = testing::internal::GetCapturedStdout();
std::string expected = std::string{"test_parser\n"
"===========\n"
"\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (std::filesystem::path)\n"
" desc A valid path for the output directory.\n"
"\n"} +
basic_options_str +
"\n" +
basic_version_str;
EXPECT_EQ(my_stdout, expected);
}
}
#if __has_include(<filesystem>)
// Setting the permissions with perm_options is not available in the experimental/filesystem branch.
// In case this test is built as `root`, we want to exclude tests that check if certain missing permissions cause
// specific exceptions. For this, we check if read/write permissions are still available after the permissions were
// revoked. Note that `root` can always read/write even if user/group/all permissions are not set.
inline bool read_access(std::filesystem::path const & file)
{
std::fstream stream;
stream.open(file, std::ios::in);
return !stream.fail();
}
inline bool write_access(std::filesystem::path const & file)
{
if (std::filesystem::is_directory(file))
{
std::fstream stream;
std::filesystem::path test_file{file};
test_file /= "test";
stream.open(test_file, std::ios::out);
return !stream.fail();
}
else
{
std::fstream stream;
stream.open(file, std::ios::out);
return !stream.fail();
}
}
TEST(validator_test, inputfile_not_readable)
{
seqan3::test::tmp_filename tmp_name{"my_file.test"};
std::filesystem::path tmp_file{tmp_name.get_path()};
std::ofstream str{tmp_name.get_path()};
EXPECT_NO_THROW(seqan3::input_file_validator{}(tmp_file));
std::filesystem::permissions(tmp_file,
std::filesystem::perms::owner_read | std::filesystem::perms::group_read |
std::filesystem::perms::others_read,
std::filesystem::perm_options::remove);
if (!read_access(tmp_file))
{
EXPECT_THROW(seqan3::input_file_validator{}(tmp_file), seqan3::validation_error);
}
std::filesystem::permissions(tmp_file,
std::filesystem::perms::owner_read | std::filesystem::perms::group_read |
std::filesystem::perms::others_read,
std::filesystem::perm_options::add);
}
TEST(validator_test, inputdir_not_readable)
{
seqan3::test::tmp_filename tmp_name{"dir"};
std::filesystem::path tmp_dir{tmp_name.get_path()};
std::filesystem::create_directory(tmp_dir);
EXPECT_NO_THROW(seqan3::input_directory_validator{}(tmp_dir));
std::filesystem::permissions(tmp_dir,
std::filesystem::perms::owner_read | std::filesystem::perms::group_read |
std::filesystem::perms::others_read,
std::filesystem::perm_options::remove);
if (!read_access(tmp_dir))
{
EXPECT_THROW(seqan3::input_directory_validator{}(tmp_dir), seqan3::validation_error);
}
std::filesystem::permissions(tmp_dir,
std::filesystem::perms::owner_read | std::filesystem::perms::group_read |
std::filesystem::perms::others_read,
std::filesystem::perm_options::add);
}
TEST(validator_test, outputfile_not_writable)
{
seqan3::test::tmp_filename tmp_name{"my_file.test"};
std::filesystem::path tmp_file{tmp_name.get_path()};
EXPECT_NO_THROW(seqan3::output_file_validator{}(tmp_file));
// Parent path is not writable.
std::filesystem::permissions(tmp_file.parent_path(),
std::filesystem::perms::owner_write | std::filesystem::perms::group_write |
std::filesystem::perms::others_write,
std::filesystem::perm_options::remove);
if (!write_access(tmp_file))
{
EXPECT_THROW(seqan3::output_file_validator{}(tmp_file), seqan3::validation_error);
}
// make sure we can remove the directory.
std::filesystem::permissions(tmp_file.parent_path(),
std::filesystem::perms::owner_write | std::filesystem::perms::group_write |
std::filesystem::perms::others_write,
std::filesystem::perm_options::add);
}
TEST(validator_test, outputdir_not_writable)
{
{ // parent dir is not writable.
seqan3::test::tmp_filename tmp_name{"dir"};
std::filesystem::path tmp_dir{tmp_name.get_path()};
EXPECT_NO_THROW(seqan3::output_directory_validator{}(tmp_dir));
EXPECT_FALSE(std::filesystem::exists(tmp_dir));
// Parent path is not writable.
std::filesystem::permissions(tmp_dir.parent_path(),
std::filesystem::perms::owner_write | std::filesystem::perms::group_write |
std::filesystem::perms::others_write,
std::filesystem::perm_options::remove);
if (!write_access(tmp_dir))
{
EXPECT_THROW(seqan3::output_directory_validator{}(tmp_dir), seqan3::validation_error);
}
// make sure we can remove the directory.
std::filesystem::permissions(tmp_dir.parent_path(),
std::filesystem::perms::owner_write | std::filesystem::perms::group_write |
std::filesystem::perms::others_write,
std::filesystem::perm_options::add);
}
{ // this dir is not writable
seqan3::test::tmp_filename tmp_name{"dir"};
std::filesystem::path tmp_dir{tmp_name.get_path()};
std::filesystem::create_directory(tmp_dir);
EXPECT_NO_THROW(seqan3::output_directory_validator{}(tmp_dir));
// This path exists but is not writable.
std::filesystem::permissions(tmp_dir,
std::filesystem::perms::owner_write | std::filesystem::perms::group_write |
std::filesystem::perms::others_write,
std::filesystem::perm_options::remove);
if (!write_access(tmp_dir))
{
EXPECT_THROW(seqan3::output_directory_validator{}(tmp_dir), seqan3::validation_error);
}
// make sure we can remove the directory.
std::filesystem::permissions(tmp_dir,
std::filesystem::perms::owner_write | std::filesystem::perms::group_write |
std::filesystem::perms::others_write,
std::filesystem::perm_options::add);
}
}
#endif // __has_include(<filesystem>)
TEST(validator_test, arithmetic_range_validator_success)
{
int option_value{0};
std::vector<int> option_vector{};
// option
const char * argv[] = {"./argument_parser_test", "-i", "10"};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::arithmetic_range_validator{1, 20});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, 10);
// option - negative values
const char * argv2[] = {"./argument_parser_test", "-i", "-10"};
seqan3::argument_parser parser2{"test_parser", 3, argv2, false};
test_accessor::set_terminal_width(parser2, 80);
parser2.add_option(option_value, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::arithmetic_range_validator{-20, 20});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser2.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, -10);
// positional option
const char * argv3[] = {"./argument_parser_test", "10"};
seqan3::argument_parser parser3{"test_parser", 2, argv3, false};
test_accessor::set_terminal_width(parser3, 80);
parser3.add_positional_option(option_value, "desc", seqan3::arithmetic_range_validator{1, 20});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser3.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, 10);
// positional option - negative values
const char * argv4[] = {"./argument_parser_test", "--", "-10"};
seqan3::argument_parser parser4{"test_parser", 3, argv4, false};
test_accessor::set_terminal_width(parser4, 80);
parser4.add_positional_option(option_value, "desc", seqan3::arithmetic_range_validator{-20, 20});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser4.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, -10);
// option - vector
const char * argv5[] = {"./argument_parser_test", "-i", "-10", "-i", "48"};
seqan3::argument_parser parser5{"test_parser", 5, argv5, false};
test_accessor::set_terminal_width(parser5, 80);
parser5.add_option(option_vector, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::arithmetic_range_validator{-50,50});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser5.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_vector[0], -10);
EXPECT_EQ(option_vector[1], 48);
// positional option - vector
option_vector.clear();
const char * argv6[] = {"./argument_parser_test", "--", "-10", "1"};
seqan3::argument_parser parser6{"test_parser", 4, argv6, false};
test_accessor::set_terminal_width(parser6, 80);
parser6.add_positional_option(option_vector, "desc", seqan3::arithmetic_range_validator{-20,20});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser6.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_vector[0], -10);
EXPECT_EQ(option_vector[1], 1);
// get help page message
option_vector.clear();
const char * argv7[] = {"./argument_parser_test", "-h"};
seqan3::argument_parser parser7{"test_parser", 2, argv7, false};
test_accessor::set_terminal_width(parser7, 80);
parser7.add_positional_option(option_vector, "desc", seqan3::arithmetic_range_validator{-20,20});
testing::internal::CaptureStdout();
EXPECT_EXIT(parser7.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
std::string my_stdout = testing::internal::GetCapturedStdout();
std::string expected = std::string("test_parser\n"
"===========\n"
"\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (List of signed 32 bit integer's)\n"
" desc Default: []. Value must be in range [-20,20].\n"
"\n" +
basic_options_str +
"\n" +
basic_version_str);
EXPECT_EQ(my_stdout, expected);
// option - double value
double double_option_value;
const char * argv8[] = {"./argument_parser_test", "-i", "10.9"};
seqan3::argument_parser parser8{"test_parser", 3, argv8, false};
test_accessor::set_terminal_width(parser8, 80);
parser8.add_option(double_option_value, 'i', "double-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::arithmetic_range_validator{1, 20});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser8.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_FLOAT_EQ(double_option_value, 10.9);
}
TEST(validator_test, arithmetic_range_validator_error)
{
int option_value;
std::vector<int> option_vector;
// option - above max
const char * argv[] = {"./argument_parser_test", "-i", "30"};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::arithmetic_range_validator{1, 20});
EXPECT_THROW(parser.parse(), seqan3::validation_error);
// option - below min
const char * argv2[] = {"./argument_parser_test", "-i", "-21"};
seqan3::argument_parser parser2{"test_parser", 3, argv2, false};
test_accessor::set_terminal_width(parser2, 80);
parser2.add_option(option_value, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::arithmetic_range_validator{-20, 20});
EXPECT_THROW(parser2.parse(), seqan3::validation_error);
// positional option - above max
const char * argv3[] = {"./argument_parser_test", "30"};
seqan3::argument_parser parser3{"test_parser", 2, argv3, false};
test_accessor::set_terminal_width(parser3, 80);
parser3.add_positional_option(option_value, "desc", seqan3::arithmetic_range_validator{1, 20});
EXPECT_THROW(parser3.parse(), seqan3::validation_error);
// positional option - below min
const char * argv4[] = {"./argument_parser_test", "--", "-21"};
seqan3::argument_parser parser4{"test_parser", 3, argv4, false};
test_accessor::set_terminal_width(parser4, 80);
parser4.add_positional_option(option_value, "desc", seqan3::arithmetic_range_validator{-20, 20});
EXPECT_THROW(parser4.parse(), seqan3::validation_error);
// option - vector
const char * argv5[] = {"./argument_parser_test", "-i", "-100"};
seqan3::argument_parser parser5{"test_parser", 3, argv5, false};
test_accessor::set_terminal_width(parser5, 80);
parser5.add_option(option_vector, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::arithmetic_range_validator{-50, 50});
EXPECT_THROW(parser5.parse(), seqan3::validation_error);
// positional option - vector
option_vector.clear();
const char * argv6[] = {"./argument_parser_test", "--", "-10", "100"};
seqan3::argument_parser parser6{"test_parser", 4, argv6, false};
test_accessor::set_terminal_width(parser6, 80);
parser6.add_positional_option(option_vector, "desc", seqan3::arithmetic_range_validator{-20, 20});
EXPECT_THROW(parser6.parse(), seqan3::validation_error);
// option - double value
double double_option_value;
const char * argv7[] = {"./argument_parser_test", "-i", "0.9"};
seqan3::argument_parser parser7{"test_parser", 3, argv7, false};
test_accessor::set_terminal_width(parser7, 80);
parser7.add_option(double_option_value, 'i', "double-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::arithmetic_range_validator{1, 20});
EXPECT_THROW(parser7.parse(), seqan3::validation_error);
}
enum class foo
{
one,
two,
three
};
TEST(validator_test, value_list_validator_success)
{
// type deduction
// --------------
// all arithmetic types are deduced to double in order to easily allow chaining of arithmetic validators
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<double>,
decltype(seqan3::value_list_validator{1})>));
// except char
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<char>,
decltype(seqan3::value_list_validator{'c'})>));
// The same holds for a range of arithmetic types
std::vector v{1, 2, 3};
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<double>,
decltype(seqan3::value_list_validator{v})>));
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<double>,
decltype(seqan3::value_list_validator{v | std::views::take(2)})>));
std::vector v_char{'1', '2', '3'};
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<char>,
decltype(seqan3::value_list_validator{v_char})>));
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<char>,
decltype(seqan3::value_list_validator{v_char | std::views::take(2)})>));
// const char * is deduced to std::string
std::vector v2{"ha", "ba", "ma"};
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<std::string>,
decltype(seqan3::value_list_validator{"ha"})>));
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<std::string>,
decltype(seqan3::value_list_validator{"ha", "ba", "ma"})>));
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<std::string>,
decltype(seqan3::value_list_validator{v2})>));
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<std::string>,
decltype(seqan3::value_list_validator{v2 | std::views::take(2)})>));
// custom types are used as is
EXPECT_TRUE((std::same_as<seqan3::value_list_validator<foo>,
decltype(seqan3::value_list_validator{foo::one, foo::two})>));
// usage
// -----
std::string option_value;
int option_value_int;
std::vector<std::string> option_vector;
std::vector<int> option_vector_int;
// option
std::vector<std::string> valid_str_values{"ha", "ba", "ma"};
const char * argv[] = {"./argument_parser_test", "-s", "ba"};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT,
seqan3::value_list_validator{valid_str_values | std::views::take(2)});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, "ba");
// option with integers
const char * argv2[] = {"./argument_parser_test", "-i", "-21"};
seqan3::argument_parser parser2{"test_parser", 3, argv2, false};
test_accessor::set_terminal_width(parser2, 80);
parser2.add_option(option_value_int, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::value_list_validator<int>{0, -21, 10});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser2.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value_int, -21);
// positional option
const char * argv3[] = {"./argument_parser_test", "ma"};
seqan3::argument_parser parser3{"test_parser", 2, argv3, false};
test_accessor::set_terminal_width(parser3, 80);
parser3.add_positional_option(option_value, "desc", seqan3::value_list_validator{valid_str_values});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser3.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, "ma");
// positional option - vector
const char * argv4[] = {"./argument_parser_test", "ha", "ma"};
seqan3::argument_parser parser4{"test_parser", 3, argv4, false};
test_accessor::set_terminal_width(parser4, 80);
parser4.add_positional_option(option_vector, "desc", seqan3::value_list_validator{"ha", "ba", "ma"});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser4.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_vector[0], "ha");
EXPECT_EQ(option_vector[1], "ma");
// option - vector
const char * argv5[] = {"./argument_parser_test", "-i", "-10", "-i", "48"};
seqan3::argument_parser parser5{"test_parser", 5, argv5, false};
test_accessor::set_terminal_width(parser5, 80);
parser5.add_option(option_vector_int, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::value_list_validator<int>{-10, 48, 50});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser5.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_vector_int[0], -10);
EXPECT_EQ(option_vector_int[1], 48);
// get help page message
option_vector_int.clear();
const char * argv7[] = {"./argument_parser_test", "-h"};
seqan3::argument_parser parser7{"test_parser", 2, argv7, false};
test_accessor::set_terminal_width(parser7, 80);
parser7.add_option(option_vector_int, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::value_list_validator<int>{-10, 48, 50});
option_vector_int.clear();
testing::internal::CaptureStdout();
EXPECT_EXIT(parser7.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
std::string my_stdout = testing::internal::GetCapturedStdout();
std::string expected = std::string("test_parser\n"
"===========\n"
"\n" +
basic_options_str +
" -i, --int-option (List of signed 32 bit integer's)\n"
" desc Default: []. Value must be one of [-10,48,50].\n"
"\n" +
basic_version_str);
EXPECT_EQ(my_stdout, expected);
}
TEST(validator_test, value_list_validator_error)
{
std::string option_value;
int option_value_int;
std::vector<std::string> option_vector;
std::vector<int> option_vector_int;
// option
const char * argv[] = {"./argument_parser_test", "-s", "sa"};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::value_list_validator{"ha", "ba", "ma"});
EXPECT_THROW(parser.parse(), seqan3::validation_error);
// positional option
const char * argv3[] = {"./argument_parser_test", "30"};
seqan3::argument_parser parser3{"test_parser", 2, argv3, false};
test_accessor::set_terminal_width(parser3, 80);
parser3.add_positional_option(option_value_int, "desc", seqan3::value_list_validator{0, 5, 10});
EXPECT_THROW(parser3.parse(), seqan3::validation_error);
// positional option - vector
const char * argv4[] = {"./argument_parser_test", "fo", "ma"};
seqan3::argument_parser parser4{"test_parser", 3, argv4, false};
test_accessor::set_terminal_width(parser4, 80);
parser4.add_positional_option(option_vector, "desc",
seqan3::value_list_validator{"ha", "ba", "ma"});
EXPECT_THROW(parser4.parse(), seqan3::validation_error);
// option - vector
const char * argv5[] = {"./argument_parser_test", "-i", "-10", "-i", "488"};
seqan3::argument_parser parser5{"test_parser", 5, argv5, false};
test_accessor::set_terminal_width(parser5, 80);
parser5.add_option(option_vector_int, 'i', "int-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::value_list_validator<int>{-10, 48, 50});
EXPECT_THROW(parser5.parse(), seqan3::validation_error);
}
TEST(validator_test, regex_validator_success)
{
std::string option_value;
std::vector<std::string> option_vector;
seqan3::regex_validator email_validator("[a-zA-Z]+@[a-zA-Z]+\\.com");
seqan3::regex_validator email_vector_validator("[a-zA-Z]+@[a-zA-Z]+\\.com");
// option
const char * argv[] = {"./argument_parser_test", "-s", "ballo@rollo.com"};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT, email_validator);
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, "ballo@rollo.com");
// positional option
const char * argv2[] = {"./argument_parser_test", "chr1"};
seqan3::argument_parser parser2{"test_parser", 2, argv2, false};
test_accessor::set_terminal_width(parser2, 80);
parser2.add_positional_option(option_value, "desc",
seqan3::regex_validator{"^chr[0-9]+"});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser2.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, "chr1");
// positional option - vector
const char * argv3[] = {"./argument_parser_test", "rollo", "bollo", "lollo"};
seqan3::argument_parser parser3{"test_parser", 4, argv3, false};
test_accessor::set_terminal_width(parser3, 80);
parser3.add_positional_option(option_vector, "desc",
seqan3::regex_validator{".*oll.*"});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser3.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_vector[0], "rollo");
EXPECT_EQ(option_vector[1], "bollo");
EXPECT_EQ(option_vector[2], "lollo");
// option - vector
option_vector.clear();
const char * argv4[] = {"./argument_parser_test", "-s", "rita@rambo.com", "-s", "tina@rambo.com"};
seqan3::argument_parser parser4{"test_parser", 5, argv4, false};
test_accessor::set_terminal_width(parser4, 80);
parser4.add_option(option_vector, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT, email_vector_validator);
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser4.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_vector[0], "rita@rambo.com");
EXPECT_EQ(option_vector[1], "tina@rambo.com");
// get help page message
option_vector.clear();
const char * argv7[] = {"./argument_parser_test", "-h"};
seqan3::argument_parser parser7{"test_parser", 2, argv7, false};
test_accessor::set_terminal_width(parser7, 80);
parser7.add_option(option_vector, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT, email_vector_validator);
option_vector.clear();
testing::internal::CaptureStdout();
EXPECT_EXIT(parser7.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
std::string my_stdout = testing::internal::GetCapturedStdout();
std::string expected = std::string("test_parser\n"
"===========\n"
"\n" +
basic_options_str +
" -s, --string-option (List of std::string's)\n"
" desc Default: []. Value must match the pattern\n"
" '[a-zA-Z]+@[a-zA-Z]+\\.com'.\n"
"\n" +
basic_version_str);
EXPECT_EQ(my_stdout, expected);
}
TEST(validator_test, regex_validator_error)
{
std::string option_value;
std::vector<std::string> option_vector;
// option
const char * argv[] = {"./argument_parser_test", "--string-option", "sally"};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, '\0', "string-option", "desc",
seqan3::option_spec::DEFAULT, seqan3::regex_validator{"tt"});
EXPECT_THROW(parser.parse(), seqan3::validation_error);
// positional option
const char * argv2[] = {"./argument_parser_test", "jessy"};
seqan3::argument_parser parser2{"test_parser", 2, argv2, false};
test_accessor::set_terminal_width(parser2, 80);
parser2.add_positional_option(option_value, "desc",
seqan3::regex_validator{"[0-9]"});
EXPECT_THROW(parser2.parse(), seqan3::validation_error);
// positional option - vector
const char * argv3[] = {"./argument_parser_test", "rollo", "bttllo", "lollo"};
seqan3::argument_parser parser3{"test_parser", 4, argv3, false};
test_accessor::set_terminal_width(parser3, 80);
parser3.add_positional_option(option_vector, "desc",
seqan3::regex_validator{".*oll.*"});
EXPECT_THROW(parser3.parse(), seqan3::validation_error);
// option - vector
option_vector.clear();
const char * argv4[] = {"./argument_parser_test", "-s", "gh", "-s", "tt"};
seqan3::argument_parser parser4{"test_parser", 5, argv4, false};
test_accessor::set_terminal_width(parser4, 80);
parser4.add_option(option_vector, 's', "", "desc",
seqan3::option_spec::DEFAULT, seqan3::regex_validator{"tt"});
EXPECT_THROW(parser4.parse(), seqan3::validation_error);
}
TEST(validator_test, chaining_validators)
{
std::string option_value{};
std::vector<std::string> option_vector{};
seqan3::regex_validator absolute_path_validator{"(/[^/]+)+/.*\\.[^/\\.]+$"};
seqan3::output_file_validator my_file_ext_validator{{"sa", "so"}};
seqan3::test::tmp_filename tmp_name{"file.sa"};
std::filesystem::path invalid_extension{tmp_name.get_path()};
invalid_extension.replace_extension(".invalid");
// option
{
std::string const & path = tmp_name.get_path().string();
const char * argv[] = {"./argument_parser_test", "-s", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT, absolute_path_validator | my_file_ext_validator);
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, path);
}
{
auto rel_path = tmp_name.get_path().relative_path().string();
const char * argv[] = {"./argument_parser_test", "-s", rel_path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT, absolute_path_validator | my_file_ext_validator);
EXPECT_THROW(parser.parse(), seqan3::validation_error);
}
{
std::string const & path = invalid_extension.string();
const char * argv[] = {"./argument_parser_test", "-s", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT, absolute_path_validator | my_file_ext_validator);
EXPECT_THROW(parser.parse(), seqan3::validation_error);
}
// with temporary validators
{
std::string const & path = tmp_name.get_path().string();
const char * argv[] = {"./argument_parser_test", "-s", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT,
seqan3::regex_validator{"(/[^/]+)+/.*\\.[^/\\.]+$"} |
seqan3::output_file_validator{{"sa", "so"}});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, path);
}
// three validators
{
std::string const & path = tmp_name.get_path().string();
const char * argv[] = {"./argument_parser_test", "-s", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT,
seqan3::regex_validator{"(/[^/]+)+/.*\\.[^/\\.]+$"} |
seqan3::output_file_validator{{"sa", "so"}} |
seqan3::regex_validator{".*"});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_value, path);
}
// help page message
{
option_value.clear();
const char * argv[] = {"./argument_parser_test", "-h"};
seqan3::argument_parser parser{"test_parser", 2, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT,
seqan3::regex_validator{"(/[^/]+)+/.*\\.[^/\\.]+$"} |
seqan3::output_file_validator{{"sa", "so"}} |
seqan3::regex_validator{".*"});
testing::internal::CaptureStdout();
EXPECT_EXIT(parser.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
std::string my_stdout = testing::internal::GetCapturedStdout();
std::string expected = std::string{"test_parser\n"
"===========\n"
"\n" +
basic_options_str +
" -s, --string-option (std::string)\n"
" desc Default: . Value must match the pattern '(/[^/]+)+/.*\\.[^/\\.]+$'.\n"
" The output file must not exist already and write permissions must be\n"
" granted. Valid file extensions are: [sa, so]. Value must match the\n"
" pattern '.*'.\n"
"\n"} +
basic_version_str;
EXPECT_EQ(my_stdout, expected);
}
// chaining with a container option value type
{
std::vector<std::string> option_list_value{};
std::string const & path = tmp_name.get_path().string();
const char * argv[] = {"./argument_parser_test", "-s", path.c_str()};
seqan3::argument_parser parser{"test_parser", 3, argv, false};
test_accessor::set_terminal_width(parser, 80);
parser.add_option(option_list_value, 's', "string-option", "desc",
seqan3::option_spec::DEFAULT,
seqan3::regex_validator{"(/[^/]+)+/.*\\.[^/\\.]+$"} | seqan3::output_file_validator{{"sa",
"so"}});
testing::internal::CaptureStderr();
EXPECT_NO_THROW(parser.parse());
EXPECT_TRUE((testing::internal::GetCapturedStderr()).empty());
EXPECT_EQ(option_list_value[0], path);
}
}
|