Skip to content

Commit

Permalink
[FEATURE] Adapt input/output file validator to read extensions from a…
Browse files Browse the repository at this point in the history
… file.
  • Loading branch information
rrahn committed Jul 11, 2019
1 parent 7c8d29f commit f3f4b52
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
29 changes: 29 additions & 0 deletions include/seqan3/argument_parser/validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <seqan3/core/detail/to_string.hpp>
#include <seqan3/core/type_traits/basic.hpp>
#include <seqan3/core/type_traits/pre.hpp>
#include <seqan3/io/detail/misc.hpp>
#include <seqan3/io/detail/safe_filesystem_entry.hpp>
#include <seqan3/range/container/concept.hpp>
#include <seqan3/range/view/drop.hpp>
Expand Down Expand Up @@ -412,6 +413,20 @@ class input_file_validator : public file_validator_base
input_file_validator & operator=(input_file_validator &&) = default; //!< Defaulted.
virtual ~input_file_validator() = default; //!< Virtual destructor.

/*!\brief Constructs from a given file.
* \tparam file_t The type of the file; must have a member type called `valid_formats` containing the formats.
* \param[in] file The file to get the valid extensions for.
*
* \details
*
* Uses the extensions from the formats of the file to initialise the valid extensions.
*/
template <typename file_t>
requires requires { typename file_t::valid_formats; }
explicit input_file_validator(file_t const & SEQAN3_DOXYGEN_ONLY(file)) :
file_validator_base{detail::valid_file_extensions<typename file_t::valid_formats>()}
{}

// Import base class constructor.
using file_validator_base::file_validator_base;
//!\}
Expand Down Expand Up @@ -487,6 +502,20 @@ class output_file_validator : public file_validator_base
output_file_validator & operator=(output_file_validator &&) = default; //!< Defaulted.
virtual ~output_file_validator() = default; //!< Virtual Destructor.

/*!\brief Constructs from a given file.
* \tparam file_t The type of the file; must have a member type called `valid_formats` containing the formats.
* \param[in] file The file to get the valid extensions for.
*
* \details
*
* Uses the extensions from the formats of the file to initialise the valid extensions.
*/
template <typename file_t>
requires requires { typename file_t::valid_formats; }
explicit output_file_validator(file_t const & SEQAN3_DOXYGEN_ONLY(file)) :
file_validator_base{detail::valid_file_extensions<typename file_t::valid_formats>()}
{}

// Import base constructor.
using file_validator_base::file_validator_base;
//!\}
Expand Down
29 changes: 28 additions & 1 deletion test/unit/argument_parser/format_parse_validators_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@

using namespace seqan3;

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 = type_list<format1, format2>;
};

std::string const basic_options_str = "OPTIONS"
"Basic options:"
"-h, --help Prints the help page."
Expand Down Expand Up @@ -92,6 +108,12 @@ TEST(validator_test, input_file)
EXPECT_THROW(my_validator(does_not_exist), parser_invalid_argument);
}

{ // read from file
dummy_file df{};
input_file_validator my_validator{df};
EXPECT_NO_THROW(my_validator(tmp_name.get_path()));
}

std::filesystem::path file_in_path;

// option
Expand Down Expand Up @@ -166,7 +188,7 @@ TEST(validator_test, output_file)

{ // file has wrong format.
output_file_validator my_validator{std::vector{std::string{"sam"}}};
EXPECT_THROW(my_validator(tmp_name.get_path()), parser_invalid_argument);
EXPECT_THROW(my_validator(tmp_name.get_path()), parser_invalid_argument);
}

{ // file has no extension.
Expand All @@ -176,6 +198,11 @@ TEST(validator_test, output_file)
EXPECT_THROW(my_validator(no_extension), parser_invalid_argument);
}

{ // read from file
output_file_validator my_validator{dummy_file{}};
EXPECT_NO_THROW(my_validator(tmp_name.get_path()));
}

std::filesystem::path file_out_path;

// option
Expand Down

0 comments on commit f3f4b52

Please sign in to comment.