From 4fb6df669529caf6ac889ab487cd71bb47b541d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Musia=C5=82?= <111433005+SpectraL519@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:33:41 +0100 Subject: [PATCH] aligned clang-format --- .clang-format | 62 +-- change_log.md | 1 + example/src/convert_numbers.cpp | 17 +- example/src/merge_files.cpp | 15 +- example/src/power.cpp | 11 +- example/src/verbosity.cpp | 14 +- format/{unix_like.sh => unix.sh} | 0 include/ap/argument_parser.hpp | 398 +++++++----------- test/include/argument_parser_test_fixture.hpp | 20 +- .../optional_argument_test_fixture.hpp | 26 +- .../positional_argument_test_fixture.hpp | 21 +- test/src/test_argument_name.cpp | 2 - .../src/test_argument_parser_add_argument.cpp | 78 +--- test/src/test_argument_parser_info.cpp | 4 +- test/src/test_argument_parser_parse_args.cpp | 168 +++----- test/src/test_nargs_range.cpp | 2 - test/src/test_optional_argument.cpp | 181 ++------ test/src/test_positional_argument.cpp | 153 ++----- 18 files changed, 364 insertions(+), 809 deletions(-) rename format/{unix_like.sh => unix.sh} (100%) mode change 100755 => 100644 diff --git a/.clang-format b/.clang-format index b7614ae..626e8fc 100644 --- a/.clang-format +++ b/.clang-format @@ -1,6 +1,6 @@ --- Language: Cpp -Standard: Latest +Standard: c++20 DisableFormat: false @@ -8,69 +8,41 @@ DisableFormat: false IndentWidth: 4 TabWidth: 4 UseTab: Never -ColumnLimit: 80 +ColumnLimit: 100 ContinuationIndentWidth: 4 # Alignment options AlignAfterOpenBracket: BlockIndent AlignArrayOfStructures: Right -AlignConsecutiveAssignments: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - PadOperators: false -AlignConsecutiveBitFields: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - PadOperators: false -AlignConsecutiveDeclarations: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - PadOperators: false -AlignConsecutiveMacros: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - PadOperators: false +AlignConsecutiveAssignments: false +AlignConsecutiveBitFields: false +AlignConsecutiveDeclarations: false +AlignConsecutiveMacros: false AlignConsecutiveShortCaseStatements: - Enabled: true - AcrossEmptyLines: true - AcrossComments: true - AlignCaseColons: false + Enabled: false AlignEscapedNewlines: Left AlignOperands: AlignAfterOperator -AlignTrailingComments: - Kind: Always - OverEmptyLines: 0 +AlignTrailingComments: false AllowAllArgumentsOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortBlocksOnASingleLine: Empty AllowShortCaseLabelsOnASingleLine: false AllowShortEnumsOnASingleLine: true -AllowShortFunctionsOnASingleLine: Inline +AllowShortFunctionsOnASingleLine: Empty AllowShortIfStatementsOnASingleLine: Never -AllowShortLambdasOnASingleLine: Inline +AllowShortLambdasOnASingleLine: All AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false AlwaysBreakTemplateDeclarations: Yes -BreakAfterAttributes: Leave -BreakAfterJavaFieldAnnotations: true -BreakArrays: false +BreakAfterAttributes: Never BreakBeforeBinaryOperators: NonAssignment BreakBeforeBraces: Custom BreakBeforeConceptDeclarations: Always -BreakBeforeInlineASMColon: OnlyMultiline BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon @@ -123,14 +95,14 @@ ShortNamespaceLines: 0 IndentAccessModifiers: false AccessModifierOffset: -4 -ConstructorInitializerIndentWidth: 4 +ConstructorInitializerIndentWidth: 0 EmptyLineAfterAccessModifier: Never EmptyLineBeforeAccessModifier: LogicalBlock PackConstructorInitializers: NextLine -SeparateDefinitionBlocks: Leave +SeparateDefinitionBlocks: Always -IndentCaseBlocks: true -IndentCaseLabels: true +IndentCaseBlocks: false +IndentCaseLabels: false IndentWrappedFunctionNames: false LambdaBodyIndentation: Signature @@ -154,7 +126,7 @@ SpaceAfterLogicalNot: true SpaceAfterTemplateKeyword: true SpaceAroundPointerQualifiers: Default SpaceBeforeAssignmentOperators: true -SpaceBeforeCaseColon: true +SpaceBeforeCaseColon: false SpaceBeforeCpp11BracedList: false SpaceBeforeCtorInitializerColon: true SpaceBeforeParens: Custom @@ -164,7 +136,7 @@ SpaceBeforeParensOptions: AfterFunctionDeclarationName: false AfterFunctionDefinitionName: false AfterIfMacros: false - AfterOverloadedOperator: true + AfterOverloadedOperator: false AfterRequiresInClause: false AfterRequiresInExpression: false BeforeNonEmptyParentheses: false diff --git a/change_log.md b/change_log.md index c90b4b6..f0b8ec5 100644 --- a/change_log.md +++ b/change_log.md @@ -18,3 +18,4 @@ ### Version 1.1 * Added `change_log.md` +* Aligned the `.clang-format` configuration file and renamed `format/unix_like.sh` to `format/unix.sh` diff --git a/example/src/convert_numbers.cpp b/example/src/convert_numbers.cpp index ebf5fb6..21ee5c9 100644 --- a/example/src/convert_numbers.cpp +++ b/example/src/convert_numbers.cpp @@ -3,21 +3,18 @@ #include #include - int main(int argc, char* argv[]) { ap::argument_parser parser; parser.program_name("convert numbers") - .program_description("shows the correct way of using dthe choices parameter") - .default_optional_arguments({ap::default_argument::optional::help}); + .program_description("shows the correct way of using dthe choices parameter") + .default_optional_arguments({ ap::default_argument::optional::help }); - parser.add_optional_argument("number", "n") - .nargs(ap::nargs::any()) - .help("positive integer value"); + parser.add_optional_argument("number", "n").nargs(ap::nargs::any()).help("positive integer value"); parser.add_optional_argument("base", "b") - .required() - .default_value("dec") - .choices({"bin", "dec", "hex"}) - .help("output number format base"); + .required() + .default_value("dec") + .choices({ "bin", "dec", "hex" }) + .help("output number format base"); try { parser.parse_args(argc, argv); diff --git a/example/src/merge_files.cpp b/example/src/merge_files.cpp index 48b846b..71e07c0 100644 --- a/example/src/merge_files.cpp +++ b/example/src/merge_files.cpp @@ -1,17 +1,18 @@ #include -#include #include +#include #include - int main(int argc, char* argv[]) { ap::argument_parser parser; parser.program_name("merge files") - .program_description("shows the correct way of using default arguments") - .default_optional_arguments({ap::default_argument::optional::help, - ap::default_argument::optional::multi_input, - ap::default_argument::optional::output}); + .program_description("shows the correct way of using default arguments") + .default_optional_arguments( + { ap::default_argument::optional::help, + ap::default_argument::optional::multi_input, + ap::default_argument::optional::output } + ); try { parser.parse_args(argc, argv); @@ -30,7 +31,7 @@ int main(int argc, char* argv[]) { const auto output_file_name = parser.value("output"); std::ofstream output_file(output_file_name); - if (!output_file.is_open()) + if (! output_file.is_open()) throw std::runtime_error("Cannot open file: " + output_file_name); for (const auto& input_file_name : input_file_name_list) { diff --git a/example/src/power.cpp b/example/src/power.cpp index 2d9c4f6..2cc3499 100644 --- a/example/src/power.cpp +++ b/example/src/power.cpp @@ -1,21 +1,18 @@ #include -#include #include +#include int main(int argc, char* argv[]) { // create the parser class instance ap::argument_parser parser; - parser.program_name("power calculator") - .program_description("calculates the value of an expression: base & exponent"); + parser.program_name("power calculator").program_description("calculates the value of an expression: base & exponent"); // add arguments parser.add_positional_argument("base").help("the exponentation base value"); - parser.add_optional_argument("exponent", "e") - .nargs(ap::nargs::any()) - .help("the exponent value"); + parser.add_optional_argument("exponent", "e").nargs(ap::nargs::any()).help("the exponent value"); - parser.default_optional_arguments({ap::default_argument::optional::help}); + parser.default_optional_arguments({ ap::default_argument::optional::help }); // parse command-line arguments try { diff --git a/example/src/verbosity.cpp b/example/src/verbosity.cpp index 9a63aa6..c100a0e 100644 --- a/example/src/verbosity.cpp +++ b/example/src/verbosity.cpp @@ -3,12 +3,9 @@ #include #include - namespace { -enum class verbosity_level : uint16_t { - low, mid, high -}; +enum class verbosity_level : uint16_t { low, mid, high }; std::istream& operator>>(std::istream& input, verbosity_level& v) { uint16_t value; @@ -53,16 +50,13 @@ void print_msg(const verbosity_level verbosity) { } // namespace - int main(int argc, char* argv[]) { ap::argument_parser parser; - parser - .program_name("verbosity level") + parser.program_name("verbosity level") .program_description("shows the correct way of using enums as a parser argument type") - .default_optional_arguments({ap::default_argument::optional::help}); + .default_optional_arguments({ ap::default_argument::optional::help }); - parser - .add_optional_argument("verbosity_level", "v") + parser.add_optional_argument("verbosity_level", "v") .default_value(verbosity_level::low) .implicit_value(verbosity_level::mid) .nargs(1); diff --git a/format/unix_like.sh b/format/unix.sh old mode 100755 new mode 100644 similarity index 100% rename from format/unix_like.sh rename to format/unix.sh diff --git a/include/ap/argument_parser.hpp b/include/ap/argument_parser.hpp index 37492bf..15cb0f0 100644 --- a/include/ap/argument_parser.hpp +++ b/include/ap/argument_parser.hpp @@ -49,9 +49,9 @@ SOFTWARE. #include #include #include +#include #include #include -#include #ifdef AP_TESTING @@ -68,7 +68,6 @@ namespace ap { class argument_parser; - /// @brief Template type validation utility. namespace utility { @@ -77,8 +76,7 @@ namespace utility { * @tparam T Type to check. */ template -concept readable = - requires(T value, std::istream& input_stream) { input_stream >> value; }; +concept readable = requires(T value, std::istream& input_stream) { input_stream >> value; }; /** * @brief The concept is satisfied when `T` is readable, copy constructible and assignable. @@ -86,8 +84,7 @@ concept readable = */ template concept valid_argument_value_type = - readable and - std::copy_constructible and std::assignable_from; + readable and std::copy_constructible and std::assignable_from; /** * @brief The concept is satisfied when `T` is comparable using the equality operator `==`. @@ -108,7 +105,6 @@ inline constexpr bool is_valid_type_v = std::disjunction_v_nlow.has_value()) - return (n < this->_nlow.value()) ? std::weak_ordering::less - : std::weak_ordering::equivalent; + return (n < this->_nlow.value()) ? std::weak_ordering::less : std::weak_ordering::equivalent; - return (n > this->_nhigh.value()) ? std::weak_ordering::greater - : std::weak_ordering::equivalent; + return (n > this->_nhigh.value()) ? std::weak_ordering::greater : std::weak_ordering::equivalent; } friend range at_least(const count_type); @@ -189,7 +188,7 @@ class range { * @param nhigh The optional upper bound of the range. */ range(const std::optional nlow, const std::optional nhigh) - : _nlow(nlow), _nhigh(nhigh) {} + : _nlow(nlow), _nhigh(nhigh) {} std::optional _nlow; std::optional _nhigh; @@ -244,7 +243,6 @@ class range { } // namespace nargs - /// @brief Defines valued argument action traits. struct valued_action { template @@ -294,35 +292,34 @@ template } // namespace detail -/// @brief Default argument action. +/// @brief Returns a default argument action. template -detail::callable_type default_action{ [](T&) {} }; +detail::callable_type default_action() { + return [](T&) {}; +} -/// @brief Predefined action for file name handling arguments. Checks whether a file with the given name exists. -inline detail::callable_type check_file_exists_action{ - [](std::string& file_path) { +/// @brief Returns a predefined action for file name handling arguments. Checks whether a file with the given name exists. +inline detail::callable_type check_file_exists_action() { + return [](std::string& file_path) { if (not std::filesystem::exists(file_path)) { std::cerr << "[ERROR] : File " + file_path + " does not exists!"; std::exit(EXIT_FAILURE); } - } -}; + }; +} // TODO: on_flag_action } // namespace action - /// @brief Internal argument handling utility. namespace argument::detail { /// @brief Structure holding the argument name. struct argument_name { - /// @brief Default constructor (deleted). argument_name() = delete; - - /// @brief Assignment operator for argument_name (deleted). argument_name& operator=(const argument_name&) = delete; + argument_name& operator=(argument_name&&) = delete; /// @brief Copy constructor argument_name(const argument_name&) = default; @@ -362,14 +359,14 @@ struct argument_name { * @return Equality of names comparison (either full or short name). */ inline bool operator==(std::string_view name) const { - return name == this->name or - (this->short_name and name == this->short_name.value()); + return name == this->name or (this->short_name and name == this->short_name.value()); } /// @brief Get a string representation of the argument_name. [[nodiscard]] inline std::string str() const { - return this->short_name ? ("[" + this->name + "," + this->short_name.value() + "]") - : ("[" + this->name + "]"); + return this->short_name + ? ("[" + this->name + "," + this->short_name.value() + "]") + : ("[" + this->name + "]"); } /** @@ -468,7 +465,6 @@ class argument_interface { } // namespace argument::detail - /** * @brief Base class for exceptions thrown by the argument parser. * @@ -481,11 +477,9 @@ class argument_parser_error : public std::runtime_error { * @brief Constructor for the argument_parser_error class. * @param message A descriptive error message providing information about the exception. */ - explicit argument_parser_error(const std::string& message) - : std::runtime_error(message) {} + explicit argument_parser_error(const std::string& message) : std::runtime_error(message) {} }; - /// @brief Namespace containing custom exception classes for argument parser errors. namespace error { @@ -501,17 +495,15 @@ class value_already_set_error : public argument_parser_error { }; /// @brief Exception thrown when the value provided for an argument cannot be parsed. -class invalid_value_error : public argument_parser_error{ +class invalid_value_error : public argument_parser_error { public: /** * @brief Constructor for the invalid_value_error class. * @param arg_name The name of the argument for which the value parsing failed. * @param value The value that failed to parse. */ - explicit invalid_value_error( - const argument::detail::argument_name& arg_name, const std::string& value - ) : argument_parser_error( - "Cannot parse value `" + value + "` for argument " + arg_name.str()) {} + explicit invalid_value_error(const argument::detail::argument_name& arg_name, const std::string& value) + : argument_parser_error("Cannot parse value `" + value + "` for argument " + arg_name.str()) {} }; /// @brief Exception thrown when the provided value is not in the choices for an argument. @@ -522,10 +514,9 @@ class invalid_choice_error : public argument_parser_error { * @param arg_name The name of the argument for which the value is not in choices. * @param value The value that is not in the allowed choices. */ - explicit invalid_choice_error( - const argument::detail::argument_name& arg_name, const std::string& value - ) : argument_parser_error( - "Value `" + value + "` is not a valid choice for argument " + arg_name.str()) {} + explicit invalid_choice_error(const argument::detail::argument_name& arg_name, const std::string& value) + : argument_parser_error("Value `" + value + "` is not a valid choice for argument " + arg_name.str()) { + } }; /// @brief Exception thrown when there is a collision in argument names. @@ -543,11 +534,10 @@ class argument_name_used_error : public argument_parser_error { * @param given_arg_name The name of the argument causing the collision. * @param given_arg_name_short The short name of the argument causing the collision. */ - explicit argument_name_used_error( - const std::string_view& given_arg_name, const std::string_view& given_arg_name_short - ) : argument_parser_error( - "Given name " + - argument::detail::argument_name(given_arg_name, given_arg_name_short).str() + " already used") {} + explicit argument_name_used_error(const std::string_view& given_arg_name, const std::string_view& given_arg_name_short) + : argument_parser_error( + "Given name " + argument::detail::argument_name(given_arg_name, given_arg_name_short).str() + " already used" + ) {} }; /// @brief Exception thrown when an argument with a specific name is not found. @@ -569,10 +559,10 @@ class invalid_value_type_error : public argument_parser_error { * @param arg_name The name of the argument that had invalid value type. * @param given_arg_type The type information that failed to cast. */ - explicit invalid_value_type_error( - const argument::detail::argument_name& arg_name, const std::type_info& given_arg_type - ) : argument_parser_error( - "Invalid value type specified for argument " + arg_name.str() + " - " + given_arg_type.name()) {} + explicit invalid_value_type_error(const argument::detail::argument_name& arg_name, const std::type_info& given_arg_type) + : argument_parser_error( + "Invalid value type specified for argument " + arg_name.str() + " - " + given_arg_type.name() + ) {} }; /// @brief Exception thrown when a required argument is not parsed. @@ -606,10 +596,8 @@ class invalid_nvalues_error : public argument_parser_error { * @param arg_name The name of the argument for which the error occurred. * @return The error message. */ - [[nodiscard]] static std::string msg( - const std::weak_ordering ordering, const argument::detail::argument_name& arg_name - ) { - if(std::is_lt(ordering)) + [[nodiscard]] static std::string msg(const std::weak_ordering ordering, const argument::detail::argument_name& arg_name) { + if (std::is_lt(ordering)) return "Too few values provided for optional argument " + arg_name.str(); else return "Too many values provided for optional argument " + arg_name.str(); @@ -620,15 +608,12 @@ class invalid_nvalues_error : public argument_parser_error { * @param ordering The result of the weak_ordering comparison. * @param arg_name The name of the argument for which the error occurred. */ - explicit invalid_nvalues_error( - const std::weak_ordering ordering, const argument::detail::argument_name& arg_name - ) : argument_parser_error(invalid_nvalues_error::msg(ordering, arg_name)) {} + explicit invalid_nvalues_error(const std::weak_ordering ordering, const argument::detail::argument_name& arg_name) + : argument_parser_error(invalid_nvalues_error::msg(ordering, arg_name)) {} }; } // namespace error - - /// @brief Namespace containing classes and utilities for handling command-line arguments. namespace argument { @@ -656,7 +641,7 @@ class positional_argument : public detail::argument_interface { * @param short_name The short name of the positional argument (optional). */ positional_argument(std::string_view name, std::string_view short_name) - : _name(name, short_name) {} + : _name(name, short_name) {} /// @brief Destructor for positional argument. ~positional_argument() = default; @@ -687,7 +672,8 @@ class positional_argument : public detail::argument_interface { * @note Requires T to be equality comparable. */ inline positional_argument& choices(const std::vector& choices) - requires(utility::equality_comparable) { + requires(utility::equality_comparable) + { this->_choices = choices; return *this; } @@ -707,7 +693,9 @@ class positional_argument : public detail::argument_interface { } /// @return True if the positional argument is optional., false if required. - [[nodiscard]] inline bool is_optional() const override { return this->_optional; } + [[nodiscard]] inline bool is_optional() const override { + return this->_optional; + } /// @brief Friend class declaration for access by argument_parser. @@ -794,8 +782,7 @@ class positional_argument : public detail::argument_interface { /// @return Ordering relationship of positional argument range. [[nodiscard]] inline std::weak_ordering nvalues_in_range() const override { - return this->_value.has_value() ? std::weak_ordering::equivalent - : std::weak_ordering::less; + return this->_value.has_value() ? std::weak_ordering::equivalent : std::weak_ordering::less; } /// @brief Get the stored value of the positional argument. @@ -814,8 +801,8 @@ class positional_argument : public detail::argument_interface { * @return True if the choice valid, false otherwise. */ [[nodiscard]] inline bool _is_valid_choice(const value_type& choice) const { - return this->_choices.empty() or - std::find(this->_choices.begin(), this->_choices.end(), choice) != this->_choices.end(); + return this->_choices.empty() + or std::find(this->_choices.begin(), this->_choices.end(), choice) != this->_choices.end(); } /** @@ -832,14 +819,15 @@ class positional_argument : public detail::argument_interface { using action_type = ap::action::detail::action_variant_type; - static constexpr bool _optional{false}; + static constexpr bool _optional = false; const detail::argument_name _name; std::optional _help_msg; - static constexpr bool _required{true}; ///< Positional arguments are required by default. - static constexpr bool _bypass_required{false}; ///< Bypassing required status is defaultly not allowed for positional arguments. + static constexpr bool _required = true; ///< Positional arguments are required by default. + static constexpr bool _bypass_required = + false; ///< Bypassing required status is defaultly not allowed for positional arguments. std::vector _choices; ///< Vector of valid choices for the positional argument. - action_type _action{ap::action::default_action}; ///< Action associated with the positional argument. + action_type _action = ap::action::default_action(); ///< Action associated with the positional argument. std::any _value; ///< Stored value of the positional argument. @@ -871,7 +859,7 @@ class optional_argument : public detail::argument_interface { * @param short_name The short name of the optional argument (optional). */ optional_argument(std::string_view name, std::string_view short_name) - : _name(name, short_name) {} + : _name(name, short_name) {} /// @brief Destructor for optional_argument. ~optional_argument() = default; @@ -965,7 +953,8 @@ class optional_argument : public detail::argument_interface { * @note Requires T to be equality comparable. */ inline optional_argument& choices(const std::vector& choices) - requires(utility::equality_comparable) { + requires(utility::equality_comparable) + { this->_choices = choices; return *this; } @@ -991,7 +980,9 @@ class optional_argument : public detail::argument_interface { } /// @return True if argument is optional, false otherwise. - [[nodiscard]] inline bool is_optional() const override { return this->_optional; } + [[nodiscard]] inline bool is_optional() const override { + return this->_optional; + } /// @brief Friend class declaration for access by argument_parser. friend class ::ap::argument_parser; @@ -1097,8 +1088,7 @@ class optional_argument : public detail::argument_interface { /// @return True if the optional argument has a predefined value, false otherwise. [[nodiscard]] inline bool _has_predefined_value() const { - return this->_default_value.has_value() or - (this->is_used() and this->_implicit_value.has_value()); + return this->_default_value.has_value() or (this->is_used() and this->_implicit_value.has_value()); } /// @return Reference to the predefined value of the optional argument. @@ -1112,8 +1102,8 @@ class optional_argument : public detail::argument_interface { * @return True if choice is valid, false otherwise. */ [[nodiscard]] inline bool _is_valid_choice(const value_type& choice) const { - return this->_choices.empty() or - std::find(this->_choices.begin(), this->_choices.end(), choice) != this->_choices.end(); + return this->_choices.empty() + or std::find(this->_choices.begin(), this->_choices.end(), choice) != this->_choices.end(); } /** @@ -1130,19 +1120,19 @@ class optional_argument : public detail::argument_interface { using action_type = ap::action::detail::action_variant_type; - static constexpr bool _optional{true}; + static constexpr bool _optional = true; const detail::argument_name _name; std::optional _help_msg; - bool _required{false}; - bool _bypass_required{false}; + bool _required = false; + bool _bypass_required = false; std::optional _nargs_range; - action_type _action{ap::action::default_action}; ///< Action associated with the opitonal argument. + action_type _action = ap::action::default_action(); ///< Action associated with the opitonal argument. std::vector _choices; ///< Vector of valid choices for the optional argument. std::any _default_value; std::any _implicit_value; - std::size_t _nused{0u}; ///< Number of used optional arguments. + std::size_t _nused = 0u; ///< Number of used optional arguments. std::vector _values; ///< Vector holding parsed values for the optional argument. std::stringstream _ss; ///< Stringstream used for parsing values. @@ -1150,42 +1140,27 @@ class optional_argument : public detail::argument_interface { } // namespace argument - /// @brief Namespace containing default argument types. namespace default_argument { /// @brief Enum class representing positional arguments. -enum class positional : uint8_t { - input, - output -}; +enum class positional : uint8_t { input, output }; /// @brief Enum class representing optional arguments. -enum class optional : uint8_t { - help, - input, - output, - multi_input, - multi_output -}; +enum class optional : uint8_t { help, input, output, multi_input, multi_output }; } // namespace default_argument - /// @brief Main argument parser class. class argument_parser { public: /// @brief Default constructor. argument_parser() = default; - /// @brief Deleted copy constructor. argument_parser(const argument_parser&) = delete; - - /// @brief Deleted move constructor. argument_parser(argument_parser&&) = delete; - - /// @brief Deleted copy assignment operator. argument_parser& operator=(const argument_parser&) = delete; + argument_parser& operator=(argument_parser&&) = delete; /// @brief Destructor for the argument parser. ~argument_parser() = default; @@ -1215,9 +1190,7 @@ class argument_parser { * @param args Vector of default positional argument categories. * @return Reference to the argument parser. */ - inline argument_parser& default_positional_arguments( - const std::vector& args - ) { + inline argument_parser& default_positional_arguments(const std::vector& args) { for (const auto arg : args) this->_add_default_positional_argument(arg); return *this; @@ -1228,9 +1201,7 @@ class argument_parser { * @param args Vector of default optional argument categories. * @return Reference to the argument parser. */ - inline argument_parser& default_optional_arguments( - const std::vector& args - ) { + inline argument_parser& default_optional_arguments(const std::vector& args) { for (const auto arg : args) this->_add_default_optional_argument(arg); return *this; @@ -1249,10 +1220,8 @@ class argument_parser { if (this->_is_arg_name_used(name)) throw error::argument_name_used_error(name); - this->_positional_args.push_back( - std::make_unique>(name)); - return static_cast&>( - *this->_positional_args.back()); + this->_positional_args.push_back(std::make_unique>(name)); + return static_cast&>(*this->_positional_args.back()); } /** @@ -1263,18 +1232,14 @@ class argument_parser { * @return Reference to the added positional argument. */ template - argument::positional_argument& add_positional_argument( - std::string_view name, std::string_view short_name - ) { + argument::positional_argument& add_positional_argument(std::string_view name, std::string_view short_name) { // TODO: check forbidden characters if (this->_is_arg_name_used(name, short_name)) throw error::argument_name_used_error(name, short_name); - this->_positional_args.push_back( - std::make_unique>(name, short_name)); - return static_cast&>( - *this->_positional_args.back()); + this->_positional_args.push_back(std::make_unique>(name, short_name)); + return static_cast&>(*this->_positional_args.back()); } /** @@ -1308,8 +1273,7 @@ class argument_parser { if (this->_is_arg_name_used(name, short_name)) throw error::argument_name_used_error(name, short_name); - this->_optional_args.push_back( - std::make_unique>(name, short_name)); + this->_optional_args.push_back(std::make_unique>(name, short_name)); return static_cast&>(*this->_optional_args.back()); } @@ -1322,9 +1286,9 @@ class argument_parser { template argument::optional_argument& add_flag(std::string_view name) { return this->add_optional_argument(name) - .default_value(not StoreImplicitly) - .implicit_value(StoreImplicitly) - .nargs(0); + .default_value(not StoreImplicitly) + .implicit_value(StoreImplicitly) + .nargs(0); } /** @@ -1335,13 +1299,11 @@ class argument_parser { * @return Reference to the added boolean flag argument. */ template - argument::optional_argument& add_flag( - std::string_view name, std::string_view short_name - ) { + argument::optional_argument& add_flag(std::string_view name, std::string_view short_name) { return this->add_optional_argument(name, short_name) - .default_value(not StoreImplicitly) - .implicit_value(StoreImplicitly) - .nargs(0); + .default_value(not StoreImplicitly) + .implicit_value(StoreImplicitly) + .nargs(0); } /** @@ -1392,7 +1354,7 @@ class argument_parser { throw error::argument_not_found_error(arg_name); try { - T value{std::any_cast(arg_opt->get().value())}; + T value = std::any_cast(arg_opt->get().value()); return value; } catch (const std::bad_any_cast& err) { @@ -1416,7 +1378,7 @@ class argument_parser { try { if (not arg.has_parsed_values() and arg.has_value()) - return std::vector{std::any_cast(arg.value())}; + return std::vector{ std::any_cast(arg.value()) }; const auto& arg_values = arg.values(); @@ -1425,7 +1387,7 @@ class argument_parser { std::begin(arg_values), std::end(arg_values), std::back_inserter(values), - [] (const std::any& value) { return std::any_cast(value); } + [](const std::any& value) { return std::any_cast(value); } ); return values; } @@ -1468,16 +1430,15 @@ class argument_parser { */ void _add_default_positional_argument(const default_argument::positional arg) { switch (arg) { - case default_argument::positional::input: - this->add_positional_argument("input") - .action(ap::action::check_file_exists_action) - .help("Input file path"); - break; - - case default_argument::positional::output: - this->add_positional_argument("output") - .help("Output file path"); - break; + case default_argument::positional::input: + this->add_positional_argument("input") + .action(ap::action::check_file_exists_action()) + .help("Input file path"); + break; + + case default_argument::positional::output: + this->add_positional_argument("output").help("Output file path"); + break; } } @@ -1487,41 +1448,33 @@ class argument_parser { */ void _add_default_optional_argument(const default_argument::optional arg) { switch (arg) { - case default_argument::optional::help: - this->add_flag("help", "h") - .bypass_required() - .help("Display help message"); - break; - - case default_argument::optional::input: - this->add_optional_argument("input", "i") - .required() - .nargs(1) - .action(ap::action::check_file_exists_action) - .help("Input file path"); - break; - - case default_argument::optional::output: - this->add_optional_argument("output", "o") - .required() - .nargs(1) - .help("Output file path"); - break; - - case default_argument::optional::multi_input: - this->add_optional_argument("input", "i") - .required() - .nargs(ap::nargs::at_least(1)) - .action(ap::action::check_file_exists_action) - .help("Input files paths"); - break; - - case default_argument::optional::multi_output: - this->add_optional_argument("output", "o") - .required() - .nargs(ap::nargs::at_least(1)) - .help("Output files paths"); - break; + case default_argument::optional::help: + this->add_flag("help", "h").bypass_required().help("Display help message"); + break; + + case default_argument::optional::input: + this->add_optional_argument("input", "i") + .required() + .nargs(1) + .action(ap::action::check_file_exists_action()) + .help("Input file path"); + break; + + case default_argument::optional::output: + this->add_optional_argument("output", "o").required().nargs(1).help("Output file path"); + break; + + case default_argument::optional::multi_input: + this->add_optional_argument("input", "i") + .required() + .nargs(ap::nargs::at_least(1)) + .action(ap::action::check_file_exists_action()) + .help("Input files paths"); + break; + + case default_argument::optional::multi_output: + this->add_optional_argument("output", "o").required().nargs(ap::nargs::at_least(1)).help("Output files paths"); + break; } } @@ -1539,9 +1492,8 @@ class argument_parser { * @param discriminator Type discriminator (flag or value). * @param value The value of the argument. */ - cmd_argument( - const type_discriminator discriminator, const std::string& value - ) : discriminator(discriminator), value(value) {} + cmd_argument(const type_discriminator discriminator, const std::string& value) + : discriminator(discriminator), value(value) {} ~cmd_argument() = default; @@ -1551,8 +1503,7 @@ class argument_parser { * @return Boolean statement of equality comparison. */ inline bool operator==(const cmd_argument& other) const { - return this->discriminator == other.discriminator and - this->value == other.value; + return this->discriminator == other.discriminator and this->value == other.value; } type_discriminator discriminator; @@ -1563,8 +1514,7 @@ class argument_parser { using cmd_argument_list_iterator = typename cmd_argument_list::const_iterator; using argument_ptr_type = std::unique_ptr; - using argument_opt_type = - std::optional>; + using argument_opt_type = std::optional>; using argument_list_type = std::vector; using argument_list_iterator_type = typename argument_list_type::iterator; using argument_list_const_iterator_type = typename argument_list_type::const_iterator; @@ -1575,12 +1525,8 @@ class argument_parser { * @param name The name of the argument. * @return Argument predicate based on the provided name. */ - [[nodiscard]] inline argument_predicate_type _name_eq_predicate( - const std::string_view& name - ) const { - return [&name](const argument_ptr_type& arg) { - return name == arg->name(); - }; + [[nodiscard]] inline argument_predicate_type _name_eq_predicate(const std::string_view& name) const { + return [&name](const argument_ptr_type& arg) { return name == arg->name(); }; } /** @@ -1620,9 +1566,7 @@ class argument_parser { * @param short_name The short name of the argument. * @return True if the argument name or short name is already used, false otherwise. */ - [[nodiscard]] bool _is_arg_name_used( - const std::string_view& name, const std::string_view& short_name - ) const { + [[nodiscard]] bool _is_arg_name_used(const std::string_view& name, const std::string_view& short_name) const { const auto predicate = this->_name_eq_predicate(name, short_name); if (this->_const_find_positional(predicate) != this->_positional_args.end()) @@ -1651,10 +1595,10 @@ class argument_parser { std::string value = argv[i]; if (this->_is_flag(value)) { this->_strip_flag_prefix(value); - args.push_back(cmd_argument{cmd_argument::type_discriminator::flag, value}); + args.push_back(cmd_argument{ cmd_argument::type_discriminator::flag, value }); } else { - args.push_back(cmd_argument{cmd_argument::type_discriminator::value, value}); + args.push_back(cmd_argument{ cmd_argument::type_discriminator::value, value }); } } @@ -1702,9 +1646,7 @@ class argument_parser { * @param cmd_args The list of command-line arguments. * @param cmd_it Iterator for iterating through command-line arguments. */ - void _parse_positional_args( - const cmd_argument_list& cmd_args, cmd_argument_list_iterator& cmd_it - ) { + void _parse_positional_args(const cmd_argument_list& cmd_args, cmd_argument_list_iterator& cmd_it) { // TODO: align tests for (const auto& pos_arg : this->_positional_args) { if (cmd_it == cmd_args.end()) @@ -1723,15 +1665,12 @@ class argument_parser { * @param cmd_args The list of command-line arguments. * @param cmd_it Iterator for iterating through command-line arguments. */ - void _parse_optional_args( - const cmd_argument_list& cmd_args, cmd_argument_list_iterator& cmd_it - ) { + void _parse_optional_args(const cmd_argument_list& cmd_args, cmd_argument_list_iterator& cmd_it) { std::optional> curr_opt_arg; while (cmd_it != cmd_args.end()) { if (cmd_it->discriminator == cmd_argument::type_discriminator::flag) { - auto opt_arg_it = - this->_find_optional(this->_name_eq_predicate(cmd_it->value)); + auto opt_arg_it = this->_find_optional(this->_name_eq_predicate(cmd_it->value)); if (opt_arg_it == this->_optional_args.end()) throw error::argument_not_found_error(cmd_it->value); @@ -1755,12 +1694,9 @@ class argument_parser { * @return True if optional arguments can bypass required arguments, false otherwise. */ [[nodiscard]] inline bool _bypass_required_args() const { - return std::any_of( - std::cbegin(this->_optional_args), std::cend(this->_optional_args), - [](const argument_ptr_type& arg) { - return arg->is_used() and arg->bypass_required_enabled(); - } - ); + return std::any_of(std::cbegin(this->_optional_args), std::cend(this->_optional_args), [](const argument_ptr_type& arg) { + return arg->is_used() and arg->bypass_required_enabled(); + }); } /// @brief Check if all required positional and optional arguments are used. @@ -1815,11 +1751,8 @@ class argument_parser { * @param predicate The predicate for finding the argument. * @return Iterator to the found positional argument. */ - [[nodiscard]] inline argument_list_iterator_type _find_positional( - const argument_predicate_type& predicate - ) { - return std::find_if( - std::begin(this->_positional_args), std::end(this->_positional_args), predicate); + [[nodiscard]] inline argument_list_iterator_type _find_positional(const argument_predicate_type& predicate) { + return std::find_if(std::begin(this->_positional_args), std::end(this->_positional_args), predicate); } /** @@ -1827,11 +1760,8 @@ class argument_parser { * @param predicate The predicate for finding the argument. * @return Iterator to the found optional argument. */ - [[nodiscard]] inline argument_list_iterator_type _find_optional( - const argument_predicate_type& predicate - ) { - return std::find_if( - std::begin(this->_optional_args), std::end(this->_optional_args), predicate); + [[nodiscard]] inline argument_list_iterator_type _find_optional(const argument_predicate_type& predicate) { + return std::find_if(std::begin(this->_optional_args), std::end(this->_optional_args), predicate); } /** @@ -1839,11 +1769,9 @@ class argument_parser { * @param predicate The predicate for finding the argument. * @return Iterator to the found positional argument. */ - [[nodiscard]] inline argument_list_const_iterator_type _const_find_positional( - const argument_predicate_type& predicate + [[nodiscard]] inline argument_list_const_iterator_type _const_find_positional(const argument_predicate_type& predicate ) const { - return std::find_if( - std::cbegin(this->_positional_args), std::cend(this->_positional_args), predicate); + return std::find_if(std::cbegin(this->_positional_args), std::cend(this->_positional_args), predicate); } /** @@ -1851,11 +1779,9 @@ class argument_parser { * @param predicate The predicate for finding the argument. * @return Iterator to the found optional argument. */ - [[nodiscard]] inline argument_list_const_iterator_type _const_find_optional( - const argument_predicate_type& predicate + [[nodiscard]] inline argument_list_const_iterator_type _const_find_optional(const argument_predicate_type& predicate ) const { - return std::find_if( - std::cbegin(this->_optional_args), std::cend(this->_optional_args), predicate); + return std::find_if(std::cbegin(this->_optional_args), std::cend(this->_optional_args), predicate); } std::optional _program_name; @@ -1864,10 +1790,10 @@ class argument_parser { argument_list_type _positional_args; argument_list_type _optional_args; - static constexpr uint8_t _flag_prefix_char_length{1u}; - static constexpr uint8_t _flag_prefix_length{2u}; - static constexpr char _flag_prefix_char{'-'}; - const std::string _flag_prefix{"--"}; // not static constexpr because of ubuntu :( + static constexpr uint8_t _flag_prefix_char_length = 1u; + static constexpr uint8_t _flag_prefix_length = 2u; + static constexpr char _flag_prefix_char = '-'; + const std::string _flag_prefix = "--"; // not static constexpr because of ubuntu gcc implementation :( }; } // namespace ap diff --git a/test/include/argument_parser_test_fixture.hpp b/test/include/argument_parser_test_fixture.hpp index 0c24899..0b7cffb 100644 --- a/test/include/argument_parser_test_fixture.hpp +++ b/test/include/argument_parser_test_fixture.hpp @@ -35,9 +35,7 @@ struct argument_parser_test_fixture { return "test_value_" + std::to_string(i); } - [[nodiscard]] std::size_t get_args_length( - std::size_t num_args, std::size_t args_split - ) const { + [[nodiscard]] std::size_t get_args_length(std::size_t num_args, std::size_t args_split) const { return args_split + 2 * (num_args - args_split); } @@ -86,9 +84,7 @@ struct argument_parser_test_fixture { return argument_name("test_arg_" + std::to_string(i), "ta_" + std::to_string(i)); } - void add_arguments( - ap::argument_parser& parser, std::size_t num_args, std::size_t args_split - ) const { + void add_arguments(ap::argument_parser& parser, std::size_t num_args, std::size_t args_split) const { for (std::size_t i = 0; i < args_split; i++) { // positional args const auto arg_name = prepare_arg_name(i); parser.add_positional_argument(arg_name.name, arg_name.short_name.value()); @@ -100,21 +96,17 @@ struct argument_parser_test_fixture { } } - [[nodiscard]] cmd_argument_list prepare_cmd_arg_list( - std::size_t num_args, std::size_t args_split - ) const { + [[nodiscard]] cmd_argument_list prepare_cmd_arg_list(std::size_t num_args, std::size_t args_split) const { cmd_argument_list cmd_args; cmd_args.reserve(get_args_length(num_args, args_split)); for (std::size_t i = 0; i < args_split; i++) { // positional args - cmd_args.push_back(cmd_argument{ - cmd_argument::type_discriminator::value, prepare_arg_value(i)}); + cmd_args.push_back(cmd_argument{ cmd_argument::type_discriminator::value, prepare_arg_value(i) }); } for (std::size_t i = args_split; i < num_args; i++) { // optional args cmd_args.push_back(cmd_argument{ - cmd_argument::type_discriminator::flag, prepare_arg_name(i).name}); - cmd_args.push_back(cmd_argument{ - cmd_argument::type_discriminator::value, prepare_arg_value(i)}); + cmd_argument::type_discriminator::flag, prepare_arg_name(i).name }); + cmd_args.push_back(cmd_argument{ cmd_argument::type_discriminator::value, prepare_arg_value(i) }); } return cmd_args; diff --git a/test/include/optional_argument_test_fixture.hpp b/test/include/optional_argument_test_fixture.hpp index 84a2f7e..30004e8 100644 --- a/test/include/optional_argument_test_fixture.hpp +++ b/test/include/optional_argument_test_fixture.hpp @@ -6,8 +6,8 @@ #include -using ap::argument::detail::argument_name; using ap::argument::optional_argument; +using ap::argument::detail::argument_name; using ap::utility::valid_argument_value_type; namespace ap_testing { @@ -35,9 +35,7 @@ struct optional_argument_test_fixture { } template - inline optional_argument& sut_set_value( - optional_argument& sut, const std::string& str_value - ) const { + inline optional_argument& sut_set_value(optional_argument& sut, const std::string& str_value) const { sut.set_used(); return sut.set_value(str_value); } @@ -60,30 +58,22 @@ struct optional_argument_test_fixture { } template - [[nodiscard]] inline std::weak_ordering sut_nvalues_in_range( - const optional_argument& sut - ) const { + [[nodiscard]] inline std::weak_ordering sut_nvalues_in_range(const optional_argument& sut) const { return sut.nvalues_in_range(); } template - [[nodiscard]] inline const std::any& sut_get_value( - const optional_argument& sut - ) const { + [[nodiscard]] inline const std::any& sut_get_value(const optional_argument& sut) const { return sut.value(); } template - [[nodiscard]] inline const std::vector& sut_get_values( - const optional_argument& sut - ) const { + [[nodiscard]] inline const std::vector& sut_get_values(const optional_argument& sut) const { return sut.values(); } template - [[nodiscard]] inline const argument_name& sut_get_name( - const optional_argument& sut - ) const { + [[nodiscard]] inline const argument_name& sut_get_name(const optional_argument& sut) const { return sut.name(); } @@ -93,9 +83,7 @@ struct optional_argument_test_fixture { } template - [[nodiscard]] inline const std::optional& sut_get_help( - const optional_argument& sut - ) const { + [[nodiscard]] inline const std::optional& sut_get_help(const optional_argument& sut) const { return sut.help(); } }; diff --git a/test/include/positional_argument_test_fixture.hpp b/test/include/positional_argument_test_fixture.hpp index 354c7f2..91e2a3a 100644 --- a/test/include/positional_argument_test_fixture.hpp +++ b/test/include/positional_argument_test_fixture.hpp @@ -6,8 +6,8 @@ #include -using ap::argument::detail::argument_name; using ap::argument::positional_argument; +using ap::argument::detail::argument_name; using ap::utility::valid_argument_value_type; namespace ap_testing { @@ -20,9 +20,7 @@ struct positional_argument_test_fixture { using value_type = typename positional_argument::value_type; template - [[nodiscard]] inline const argument_name& sut_get_name( - const positional_argument& sut - ) const { + [[nodiscard]] inline const argument_name& sut_get_name(const positional_argument& sut) const { return sut.name(); } @@ -32,8 +30,7 @@ struct positional_argument_test_fixture { } template - [[nodiscard]] inline const std::optional& sut_get_help( - const positional_argument& sut + [[nodiscard]] inline const std::optional& sut_get_help(const positional_argument& sut ) const { return sut.help(); } @@ -59,9 +56,7 @@ struct positional_argument_test_fixture { } template - inline positional_argument& sut_set_value( - positional_argument& sut, const std::string& str_value - ) const { + inline positional_argument& sut_set_value(positional_argument& sut, const std::string& str_value) const { return sut.set_value(str_value); } @@ -73,16 +68,12 @@ struct positional_argument_test_fixture { } template - [[nodiscard]] inline std::weak_ordering sut_nvalues_in_range( - const positional_argument& sut - ) const { + [[nodiscard]] inline std::weak_ordering sut_nvalues_in_range(const positional_argument& sut) const { return sut.nvalues_in_range(); } template - [[nodiscard]] inline const std::any& sut_get_value( - const positional_argument& sut - ) const { + [[nodiscard]] inline const std::any& sut_get_value(const positional_argument& sut) const { return sut.value(); } diff --git a/test/src/test_argument_name.cpp b/test/src/test_argument_name.cpp index e15a2ba..9f9d8d0 100644 --- a/test/src/test_argument_name.cpp +++ b/test/src/test_argument_name.cpp @@ -9,7 +9,6 @@ using namespace ap::argument::detail; - namespace { constexpr std::string_view name = "test"; @@ -28,7 +27,6 @@ argument_name default_argument_name_both_names() { } // namespace - namespace ap_testing { TEST_SUITE_BEGIN("test_argument_name"); diff --git a/test/src/test_argument_parser_add_argument.cpp b/test/src/test_argument_parser_add_argument.cpp index 7224da9..4a1b89e 100644 --- a/test/src/test_argument_parser_add_argument.cpp +++ b/test/src/test_argument_parser_add_argument.cpp @@ -1,7 +1,7 @@ #define AP_TESTING -#include "doctest.h" #include "argument_parser_test_fixture.hpp" +#include "doctest.h" #include "optional_argument_test_fixture.hpp" #include @@ -10,7 +10,6 @@ using namespace ap::argument; - namespace { constexpr std::string_view name = "test"; @@ -21,33 +20,25 @@ constexpr std::string_view other_short_name = "o"; } // namespace - namespace ap_testing { TEST_SUITE_BEGIN("test_argument_parser_add_argument"); -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "default_positional_arguments should add the specified arguments" -) { - sut.default_positional_arguments({ - ap::default_argument::positional::input, - ap::default_argument::positional::output - }); +TEST_CASE_FIXTURE(argument_parser_test_fixture, "default_positional_arguments should add the specified arguments") { + sut.default_positional_arguments( + { ap::default_argument::positional::input, ap::default_argument::positional::output } + ); REQUIRE_FALSE(sut_get_argument("input")->get().is_optional()); REQUIRE_FALSE(sut_get_argument("output")->get().is_optional()); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "default_optional_arguments should add the specified arguments" -) { - sut.default_optional_arguments({ - ap::default_argument::optional::help, - ap::default_argument::optional::input, - ap::default_argument::optional::output - }); +TEST_CASE_FIXTURE(argument_parser_test_fixture, "default_optional_arguments should add the specified arguments") { + sut.default_optional_arguments( + { ap::default_argument::optional::help, + ap::default_argument::optional::input, + ap::default_argument::optional::output } + ); REQUIRE(sut_get_argument("help")->get().is_optional()); REQUIRE(sut_get_argument("h")->get().is_optional()); @@ -59,11 +50,7 @@ TEST_CASE_FIXTURE( REQUIRE(sut_get_argument("o")->get().is_optional()); } - -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "add_positional_argument should return a positional argument reference" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "add_positional_argument should return a positional argument reference") { const auto& argument = sut.add_positional_argument(name, short_name); REQUIRE_FALSE(argument.is_optional()); } @@ -80,23 +67,15 @@ TEST_CASE_FIXTURE( } SUBCASE("adding argument with a previously used long name") { - REQUIRE_THROWS_AS( - sut.add_positional_argument(name, other_short_name), - ap::error::argument_name_used_error); + REQUIRE_THROWS_AS(sut.add_positional_argument(name, other_short_name), ap::error::argument_name_used_error); } SUBCASE("adding argument with a previously used short name") { - REQUIRE_THROWS_AS( - sut.add_positional_argument(other_name, short_name), - ap::error::argument_name_used_error); + REQUIRE_THROWS_AS(sut.add_positional_argument(other_name, short_name), ap::error::argument_name_used_error); } } - -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "add_optional_argument should return an optional argument reference" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "add_optional_argument should return an optional argument reference") { const auto& argument = sut.add_optional_argument(name, short_name); REQUIRE(argument.is_optional()); } @@ -113,23 +92,15 @@ TEST_CASE_FIXTURE( } SUBCASE("adding argument with a previously used long name") { - REQUIRE_THROWS_AS( - sut.add_optional_argument(name, other_short_name), - ap::error::argument_name_used_error); + REQUIRE_THROWS_AS(sut.add_optional_argument(name, other_short_name), ap::error::argument_name_used_error); } SUBCASE("adding argument with a previously used short name") { - REQUIRE_THROWS_AS( - sut.add_optional_argument(other_name, short_name), - ap::error::argument_name_used_error); + REQUIRE_THROWS_AS(sut.add_optional_argument(other_name, short_name), ap::error::argument_name_used_error); } } - -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "add_flag should return an optional argument reference with flag parameters" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "add_flag should return an optional argument reference with flag parameters") { const optional_argument_test_fixture opt_arg_fixture; SUBCASE("StoreImplicitly = true") { @@ -153,10 +124,7 @@ TEST_CASE_FIXTURE( } } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "add_flag should throw only when adding and argument with a previously used name" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "add_flag should throw only when adding and argument with a previously used name") { sut.add_flag(name, short_name); SUBCASE("adding argument with a unique name") { @@ -164,15 +132,11 @@ TEST_CASE_FIXTURE( } SUBCASE("adding argument with a previously used long name") { - REQUIRE_THROWS_AS( - sut.add_flag(name, other_short_name), - ap::error::argument_name_used_error); + REQUIRE_THROWS_AS(sut.add_flag(name, other_short_name), ap::error::argument_name_used_error); } SUBCASE("adding argument with a previously used short name") { - REQUIRE_THROWS_AS( - sut.add_flag(other_name, short_name), - ap::error::argument_name_used_error); + REQUIRE_THROWS_AS(sut.add_flag(other_name, short_name), ap::error::argument_name_used_error); } } diff --git a/test/src/test_argument_parser_info.cpp b/test/src/test_argument_parser_info.cpp index f56e246..b009057 100644 --- a/test/src/test_argument_parser_info.cpp +++ b/test/src/test_argument_parser_info.cpp @@ -1,17 +1,15 @@ #define AP_TESTING -#include "doctest.h" #include "argument_parser_test_fixture.hpp" +#include "doctest.h" #include - namespace { const std::string test_name = "test program name"; const std::string test_description = "test program description"; } // namespace - namespace ap_testing { TEST_SUITE_BEGIN("test_argument_parser_info"); diff --git a/test/src/test_argument_parser_parse_args.cpp b/test/src/test_argument_parser_parse_args.cpp index fa898b4..e7ef0bc 100644 --- a/test/src/test_argument_parser_parse_args.cpp +++ b/test/src/test_argument_parser_parse_args.cpp @@ -3,8 +3,8 @@ #define AP_TESTING -#include "doctest.h" #include "argument_parser_test_fixture.hpp" +#include "doctest.h" #include @@ -34,10 +34,7 @@ TEST_SUITE_BEGIN("test_argument_parser_parse_args"); TEST_SUITE_BEGIN("test_argument_parser_parse_args::_preprocess_input"); -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "_preprocess_input should return an empty vector for no command-line arguments" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "_preprocess_input should return an empty vector for no command-line arguments") { const auto argc = get_argc(default_num_args, default_num_args); auto argv = prepare_argv(default_num_args, default_num_args); @@ -48,10 +45,7 @@ TEST_CASE_FIXTURE( free_argv(argc, argv); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "_preprocess_input should return a vector of correct arguments" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "_preprocess_input should return a vector of correct arguments") { add_arguments(sut, non_default_num_args, non_default_args_split); const auto argc = get_argc(non_default_num_args, non_default_args_split); @@ -59,24 +53,18 @@ TEST_CASE_FIXTURE( const auto cmd_args = sut_process_input(argc, argv); - REQUIRE_EQ( - cmd_args.size(), - get_args_length(non_default_num_args, non_default_args_split) - ); + REQUIRE_EQ(cmd_args.size(), get_args_length(non_default_num_args, non_default_args_split)); for (std::size_t i = 0; i < non_default_args_split; i++) { // positional args - REQUIRE_EQ( - cmd_args.at(i).discriminator, cmd_argument::type_discriminator::value); + REQUIRE_EQ(cmd_args.at(i).discriminator, cmd_argument::type_discriminator::value); REQUIRE_EQ(cmd_args.at(i).value, prepare_arg_value(i)); } std::size_t opt_arg_idx = non_default_args_split; for (std::size_t i = non_default_args_split; i < cmd_args.size(); i += 2) { // optional args - REQUIRE_EQ( - cmd_args.at(i).discriminator, cmd_argument::type_discriminator::flag); + REQUIRE_EQ(cmd_args.at(i).discriminator, cmd_argument::type_discriminator::flag); REQUIRE_EQ(cmd_args.at(i).value, prepare_arg_name(opt_arg_idx)); - REQUIRE_EQ( - cmd_args.at(i + 1).discriminator, cmd_argument::type_discriminator::value); + REQUIRE_EQ(cmd_args.at(i + 1).discriminator, cmd_argument::type_discriminator::value); REQUIRE_EQ(cmd_args.at(i + 1).value, prepare_arg_value(opt_arg_idx)); opt_arg_idx++; } @@ -102,14 +90,10 @@ TEST_CASE_FIXTURE( REQUIRE_THROWS_AS(sut_parse_args_impl(cmd_args), ap::error::free_value_error); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "_parse_args_impl should not throw when the arguments are correct" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "_parse_args_impl should not throw when the arguments are correct") { add_arguments(sut, non_default_num_args, non_default_args_split); - const auto cmd_args = - prepare_cmd_arg_list(non_default_num_args, non_default_args_split); + const auto cmd_args = prepare_cmd_arg_list(non_default_num_args, non_default_args_split); REQUIRE_NOTHROW(sut_parse_args_impl(cmd_args)); } @@ -145,17 +129,11 @@ TEST_CASE_FIXTURE( TEST_SUITE_END(); // test_argument_parser_parse_args::_get_argument - -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "parse_args should throw when there is no value specified for a required optional argument" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "parse_args should throw when there is no value specified for a required optional argument") { add_arguments(sut, non_default_num_args, non_default_args_split); const auto required_arg_name = prepare_arg_name(non_default_num_args); - sut.add_optional_argument( - required_arg_name.name, required_arg_name.short_name.value() - ).required(); + sut.add_optional_argument(required_arg_name.name, required_arg_name.short_name.value()).required(); const auto argc = get_argc(non_default_num_args, non_default_args_split); auto argv = prepare_argv(non_default_num_args, non_default_args_split); @@ -165,19 +143,14 @@ TEST_CASE_FIXTURE( free_argv(argc, argv); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "parse_args should throw when an optional argument's nvalues is not in a specified range" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "parse_args should throw when an optional argument's nvalues is not in a specified range") { add_arguments(sut, non_default_num_args, non_default_args_split); auto argc = get_argc(non_default_num_args, non_default_args_split); auto argv = prepare_argv(non_default_num_args, non_default_args_split); const auto range_arg_name = prepare_arg_name(non_default_num_args); - sut.add_optional_argument( - range_arg_name.name, range_arg_name.short_name.value() - ).nargs(at_least(1)); + sut.add_optional_argument(range_arg_name.name, range_arg_name.short_name.value()).nargs(at_least(1)); REQUIRE_THROWS_AS(sut.parse_args(argc, argv), ap::error::invalid_nvalues_error); @@ -192,9 +165,7 @@ TEST_CASE_FIXTURE( add_arguments(sut, non_default_num_args, non_default_args_split); const auto required_arg_name = prepare_arg_name(non_default_num_args); - sut.add_optional_argument( - required_arg_name.name, required_arg_name.short_name.value() - ).required(); + sut.add_optional_argument(required_arg_name.name, required_arg_name.short_name.value()).required(); int argc; char** argv; @@ -231,10 +202,11 @@ TEST_CASE_FIXTURE( const auto bypass_required_arg_name = prepare_arg_name(non_default_num_args); sut.add_optional_argument( - bypass_required_arg_name.name, bypass_required_arg_name.short_name.value() - ).default_value(false) - .implicit_value(true) - .bypass_required(); + bypass_required_arg_name.name, bypass_required_arg_name.short_name.value() + ) + .default_value(false) + .implicit_value(true) + .bypass_required(); const int argc = 2; @@ -245,8 +217,12 @@ TEST_CASE_FIXTURE( std::string arg_flag; - SUBCASE("long flag") { arg_flag = prepare_arg_flag(non_default_num_args); } - SUBCASE("short flag") { arg_flag = prepare_arg_flag_short(non_default_num_args); } + SUBCASE("long flag") { + arg_flag = prepare_arg_flag(non_default_num_args); + } + SUBCASE("short flag") { + arg_flag = prepare_arg_flag_short(non_default_num_args); + } CAPTURE(arg_flag); @@ -259,13 +235,9 @@ TEST_CASE_FIXTURE( free_argv(argc, argv); } - TEST_SUITE_BEGIN("test_argument_parser_parse_args::has_value"); -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "has_value should return false if there is no argument with given name present" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "has_value should return false if there is no argument with given name present") { add_arguments(sut, non_default_num_args, non_default_args_split); const auto argc = get_argc(non_default_num_args, non_default_args_split); @@ -277,17 +249,12 @@ TEST_CASE_FIXTURE( free_argv(argc, argv); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "has_value should return false when an argument has no values" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "has_value should return false when an argument has no values") { add_arguments(sut, non_default_num_args, non_default_args_split); const auto required_arg_name = prepare_arg_name(non_default_num_args); - sut.add_optional_argument( - required_arg_name.name, required_arg_name.short_name.value() - ).required(); + sut.add_optional_argument(required_arg_name.name, required_arg_name.short_name.value()).required(); const auto num_args = non_default_num_args + 1; @@ -346,10 +313,7 @@ TEST_SUITE_END(); // test_argument_parser_parse_args::has_value TEST_SUITE_BEGIN("test_argument_parser_parse_args::value"); -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "value() should throw if there is no argument with given name present" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "value() should throw if there is no argument with given name present") { add_arguments(sut, non_default_num_args, non_default_args_split); const auto argc = get_argc(non_default_num_args, non_default_args_split); @@ -362,10 +326,7 @@ TEST_CASE_FIXTURE( free_argv(argc, argv); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "value() should throw before calling parse_args" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "value() should throw before calling parse_args") { add_arguments(sut, non_default_num_args, non_default_args_split); for (std::size_t i = 0; i < non_default_num_args; i++) { @@ -375,17 +336,12 @@ TEST_CASE_FIXTURE( } } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "value() should throw if the given argument doesn't have a value" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "value() should throw if the given argument doesn't have a value") { add_arguments(sut, non_default_num_args, non_default_args_split); const auto required_arg_name = prepare_arg_name(non_default_num_args); - sut.add_optional_argument( - required_arg_name.name, required_arg_name.short_name.value() - ).required(); + sut.add_optional_argument(required_arg_name.name, required_arg_name.short_name.value()).required(); const auto num_args = non_default_num_args + 1; @@ -418,10 +374,7 @@ TEST_CASE_FIXTURE( free_argv(argc, argv); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "value() should throw if an argument has a value but the given type is invalid" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "value() should throw if an argument has a value but the given type is invalid") { add_arguments(sut, non_default_num_args, non_default_args_split); const auto required_arg_name = prepare_arg_name(non_default_num_args); @@ -452,9 +405,7 @@ TEST_CASE_FIXTURE( const auto required_arg_name = prepare_arg_name(non_default_num_args); - sut.add_optional_argument( - required_arg_name.name, required_arg_name.short_name.value() - ).required(); + sut.add_optional_argument(required_arg_name.name, required_arg_name.short_name.value()).required(); const auto num_args = non_default_num_args + 1; @@ -480,10 +431,7 @@ TEST_SUITE_END(); // test_argument_parser_parse_args::value TEST_SUITE_BEGIN("test_argument_parser_parse_args::count"); -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "count should return 0 before calling parse_args" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "count should return 0 before calling parse_args") { add_arguments(sut, non_default_num_args, non_default_args_split); for (std::size_t i = 0; i < non_default_num_args; i++) { @@ -493,10 +441,7 @@ TEST_CASE_FIXTURE( } } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "count should return 0 if there is no argument with given name present" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "count should return 0 if there is no argument with given name present") { add_arguments(sut, non_default_num_args, non_default_args_split); const auto argc = get_argc(non_default_num_args, non_default_args_split); @@ -508,14 +453,10 @@ TEST_CASE_FIXTURE( free_argv(argc, argv); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "count should return the number of argument's flag usage" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "count should return the number of argument's flag usage") { // prepare sut sut.add_positional_argument(positional_arg_name, positional_arg_short_name); - sut.add_optional_argument(optional_arg_name, optional_arg_short_name) - .nargs(ap::nargs::any()); + sut.add_optional_argument(optional_arg_name, optional_arg_short_name).nargs(ap::nargs::any()); // expected values const std::size_t positional_count = 1u; @@ -564,20 +505,14 @@ TEST_SUITE_END(); // test_argument_parser_parse_args::count TEST_SUITE_BEGIN("test_argument_parser_parse_args::values"); -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "values() should throw when calling with a positional argument's name" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "values() should throw when calling with a positional argument's name") { sut.add_positional_argument(positional_arg_name, positional_arg_short_name); REQUIRE_THROWS_AS(sut.values(positional_arg_name), std::logic_error); REQUIRE_THROWS_AS(sut.values(positional_arg_short_name), std::logic_error); } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "values() should return an empty vector if an argument has no values" -) { +TEST_CASE_FIXTURE(argument_parser_test_fixture, "values() should return an empty vector if an argument has no values") { sut.add_optional_argument(optional_arg_name, optional_arg_short_name); SUBCASE("calling with argument's long name") { @@ -591,12 +526,8 @@ TEST_CASE_FIXTURE( } } -TEST_CASE_FIXTURE( - argument_parser_test_fixture, - "values() should throw when an argument has values but the given type is invalid" -) { - sut.add_optional_argument(optional_arg_name, optional_arg_short_name) - .nargs(at_least(1)); +TEST_CASE_FIXTURE(argument_parser_test_fixture, "values() should throw when an argument has values but the given type is invalid") { + sut.add_optional_argument(optional_arg_name, optional_arg_short_name).nargs(at_least(1)); // prepare argc & argv const int argc = 5; @@ -617,12 +548,10 @@ TEST_CASE_FIXTURE( // parse args sut.parse_args(argc, argv); + REQUIRE_THROWS_AS(sut.values(optional_arg_name), ap::error::invalid_value_type_error); REQUIRE_THROWS_AS( - sut.values(optional_arg_name), - ap::error::invalid_value_type_error); - REQUIRE_THROWS_AS( - sut.values(optional_arg_short_name), - ap::error::invalid_value_type_error); + sut.values(optional_arg_short_name), ap::error::invalid_value_type_error + ); free_argv(argc, argv); } @@ -636,8 +565,8 @@ TEST_CASE_FIXTURE( const std::string implicit_value = "implicit_value"; sut.add_optional_argument(optional_arg_name, optional_arg_short_name) - .default_value(default_value) - .implicit_value(implicit_value); + .default_value(default_value) + .implicit_value(implicit_value); // prepare argc & argv int argc; @@ -683,8 +612,7 @@ TEST_CASE_FIXTURE( "values() should return a correct vector of values when there is an argument with " "a given name and parsed values present" ) { - sut.add_optional_argument(optional_arg_name, optional_arg_short_name) - .nargs(at_least(1)); + sut.add_optional_argument(optional_arg_name, optional_arg_short_name).nargs(at_least(1)); // prepare argc & argv const int argc = 5; diff --git a/test/src/test_nargs_range.cpp b/test/src/test_nargs_range.cpp index 3cce0a3..78bfefd 100644 --- a/test/src/test_nargs_range.cpp +++ b/test/src/test_nargs_range.cpp @@ -8,7 +8,6 @@ using namespace ap::nargs; - namespace { constexpr range::count_type ndefault = 1; @@ -21,7 +20,6 @@ constexpr range::count_type nmax = std::numeric_limits::max() } // namespace - namespace ap_testing { TEST_SUITE_BEGIN("test_nargs_range"); diff --git a/test/src/test_optional_argument.cpp b/test/src/test_optional_argument.cpp index ff2e5f5..dbca2f8 100644 --- a/test/src/test_optional_argument.cpp +++ b/test/src/test_optional_argument.cpp @@ -38,7 +38,7 @@ constexpr test_value_type value_2 = 2; constexpr test_value_type default_value = 0; constexpr test_value_type implicit_value = -1; -const std::vector default_choices{1, 2, 3}; +const std::vector default_choices{ 1, 2, 3 }; constexpr test_value_type invalid_choice = 4; const range non_default_range = range(1u, default_choices.size()); @@ -47,20 +47,13 @@ const range non_default_range = range(1u, default_choices.size()); TEST_SUITE_BEGIN("test_optional_argument"); -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "is_optional() should return true" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_optional() should return true") { const auto sut = prepare_argument(long_name); REQUIRE(sut.is_optional()); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "name() should return value passed to the optional argument constructor for long name" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "name() should return value passed to the optional argument constructor for long name") { const auto sut = prepare_argument(long_name); const auto name = sut_get_name(sut); @@ -82,20 +75,13 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(name, short_name); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "help() should return nullopt by default" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "help() should return nullopt by default") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_get_help(sut)); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "help() should return message if one has been provided" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "help() should return message if one has been provided") { auto sut = prepare_argument(long_name); constexpr std::string_view help_msg = "test help msg"; @@ -107,20 +93,13 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(stored_help_msg, help_msg); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "is_required() should return false by default" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_required() should return false by default") { auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_is_required(sut)); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "is_required() should return true is argument is set to be required" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_required() should return true is argument is set to be required") { auto sut = prepare_argument(long_name); sut.required(); @@ -128,31 +107,20 @@ TEST_CASE_FIXTURE( REQUIRE(sut_is_required(sut)); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "is_used() should return false by default" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_used() should return false by default") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_is_used(sut)); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "is_used() should return true when argument contains value" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_used() should return true when argument contains value") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); REQUIRE(sut_is_used(sut)); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "nused() should return 0 by default" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "nused() should return 0 by default") { const auto sut = prepare_argument(long_name); REQUIRE_EQ(sut_get_nused(sut), 0u); @@ -172,20 +140,13 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(sut_get_nused(sut), nused); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "has_value() should return false by default" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return false by default") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_has_value(sut)); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "has_value() should return true if value is set" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return true if value is set") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); @@ -193,10 +154,7 @@ TEST_CASE_FIXTURE( REQUIRE(sut_has_value(sut)); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "has_value() should return true if a default value is set" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return true if a default value is set") { auto sut = prepare_argument(long_name); sut.default_value(default_value); @@ -204,20 +162,14 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(std::any_cast(sut_get_value(sut)), default_value); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "has_value() should return false if an implicit value is set but the argument is not used" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return false if an implicit value is set but the argument is not used") { auto sut = prepare_argument(long_name); sut.implicit_value(implicit_value); REQUIRE_FALSE(sut_has_value(sut)); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "has_value() should return true if an implicit value is set and the agument is used" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return true if an implicit value is set and the agument is used") { auto sut = prepare_argument(long_name); sut.implicit_value(implicit_value); @@ -227,11 +179,7 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(std::any_cast(sut_get_value(sut)), implicit_value); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "has_parsed_values() should return false by default" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_parsed_values() should return false by default") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_has_parsed_values(sut)); @@ -261,10 +209,7 @@ TEST_CASE_FIXTURE( } } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "has_parsed_values() should true if the value is set" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_parsed_values() should true if the value is set") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); @@ -272,20 +217,13 @@ TEST_CASE_FIXTURE( REQUIRE(sut_has_parsed_values(sut)); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "value() should return default any object if argument's value has not been set" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "value() should return default any object if argument's value has not been set") { auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_get_value(sut).has_value()); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "value() should return the argument's value if it has been set" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "value() should return the argument's value if it has been set") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); @@ -294,10 +232,7 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(std::any_cast(sut_get_value(sut)), value_1); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "value() should return the default value if one has been provided and argument is not used" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "value() should return the default value if one has been provided and argument is not used") { auto sut = prepare_argument(long_name); sut.default_value(value_1); @@ -305,10 +240,7 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(std::any_cast(sut_get_value(sut)), value_1); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "value() should return the implicit value if one has been provided and argument is used" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "value() should return the implicit value if one has been provided and argument is used") { auto sut = prepare_argument(long_name); sut.implicit_value(implicit_value); @@ -318,48 +250,34 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(std::any_cast(sut_get_value(sut)), implicit_value); } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "set_value(any) should throw when value_type cannot be obtained from given string" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should throw when value_type cannot be obtained from given string") { auto sut = prepare_argument(long_name); SUBCASE("given string is empty") { - REQUIRE_THROWS_AS( - sut_set_value(sut, empty_str), - ap::error::invalid_value_error); + REQUIRE_THROWS_AS(sut_set_value(sut, empty_str), ap::error::invalid_value_error); REQUIRE_FALSE(sut_has_value(sut)); } SUBCASE("given string is non-convertible to value_type") { - REQUIRE_THROWS_AS( - sut_set_value(sut, invalid_value_str), - ap::error::invalid_value_error); + REQUIRE_THROWS_AS(sut_set_value(sut, invalid_value_str), ap::error::invalid_value_error); REQUIRE_FALSE(sut_has_value(sut)); } } TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "set_value(any) should throw when parameter passed to value() is not present in the choices set" + optional_argument_test_fixture, "set_value(any) should throw when parameter passed to value() is not present in the choices set" ) { auto sut = prepare_argument(long_name); sut_set_choices(sut, default_choices); - REQUIRE_THROWS_AS( - sut_set_value(sut, std::to_string(invalid_choice)), - ap::error::invalid_choice_error); + REQUIRE_THROWS_AS(sut_set_value(sut, std::to_string(invalid_choice)), ap::error::invalid_choice_error); REQUIRE_FALSE(sut_has_value(sut)); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "set_value(any) should accept the given value when it's present in the choices set" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should accept the given value when it's present in the choices set") { auto sut = prepare_argument(long_name); sut_set_choices(sut, default_choices); @@ -367,7 +285,9 @@ TEST_CASE_FIXTURE( test_value_type value; for (const auto& v : correct_values) { - SUBCASE("correct value") { value = v; } + SUBCASE("correct value") { + value = v; + } } CAPTURE(value); @@ -377,24 +297,16 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(std::any_cast(sut_get_value(sut)), value); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "set_value(any) should throw when a value has already been set when nargs is default" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should throw when a value has already been set when nargs is default") { auto sut = prepare_argument(long_name); REQUIRE_NOTHROW(sut_set_value(sut, std::to_string(value_1))); REQUIRE(sut_has_value(sut)); - REQUIRE_THROWS_AS( - sut_set_value(sut, std::to_string(value_2)), - ap::error::value_already_set_error); + REQUIRE_THROWS_AS(sut_set_value(sut, std::to_string(value_2)), ap::error::value_already_set_error); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "set_value(any) should accept multiple values if nargs is not detault" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should accept multiple values if nargs is not detault") { auto sut = prepare_argument(long_name); sut.nargs(non_default_range); @@ -410,28 +322,20 @@ TEST_CASE_FIXTURE( } } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "set_value(any) should perform the specified action" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should perform the specified action") { auto sut = prepare_argument(long_name); SUBCASE("valued action") { - const auto double_valued_action = - [] (const test_value_type& value) { return 2 * value; }; + const auto double_valued_action = [](const test_value_type& value) { return 2 * value; }; sut.action(double_valued_action); sut_set_value(sut, std::to_string(value_1)); - REQUIRE_EQ( - std::any_cast(sut_get_value(sut)), - double_valued_action(value_1) - ); + REQUIRE_EQ(std::any_cast(sut_get_value(sut)), double_valued_action(value_1)); } SUBCASE("void action") { - const auto double_void_action = [] (test_value_type& value) { value *= 2; }; + const auto double_void_action = [](test_value_type& value) { value *= 2; }; sut.action(double_void_action); auto test_value = value_1; @@ -443,20 +347,13 @@ TEST_CASE_FIXTURE( } } - -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "nvalues_in_range() should return equivalent if nargs has not been set" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "nvalues_in_range() should return equivalent if nargs has not been set") { const auto sut = prepare_argument(long_name); REQUIRE(std::is_eq(sut_nvalues_in_range(sut))); } -TEST_CASE_FIXTURE( - optional_argument_test_fixture, - "nvalues_in_range() should return equivalent if a default value has been set" -) { +TEST_CASE_FIXTURE(optional_argument_test_fixture, "nvalues_in_range() should return equivalent if a default value has been set") { auto sut = prepare_argument(long_name); sut.nargs(non_default_range); diff --git a/test/src/test_positional_argument.cpp b/test/src/test_positional_argument.cpp index 6eb69e8..d03c6af 100644 --- a/test/src/test_positional_argument.cpp +++ b/test/src/test_positional_argument.cpp @@ -32,27 +32,20 @@ const std::string invalid_value_str = "invalid_value"; constexpr test_value_type value_1 = 1; constexpr test_value_type value_2 = 2; -const std::vector default_choices{1, 2, 3}; +const std::vector default_choices{ 1, 2, 3 }; constexpr test_value_type invalid_choice = 4; } // namespace TEST_SUITE_BEGIN("test_positional_argument"); -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "is_optional() should return false" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_optional() should return false") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut.is_optional()); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "name() should return value passed to the optional argument constructor for long name" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "name() should return value passed to the optional argument constructor for long name") { const auto sut = prepare_argument(long_name); const auto name = sut_get_name(sut); @@ -74,20 +67,13 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(name, short_name); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "help() should return nullopt by default" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "help() should return nullopt by default") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_get_help(sut)); } -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "help() should return message if one has been provided" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "help() should return message if one has been provided") { auto sut = prepare_argument(long_name); constexpr std::string_view help_msg = "test help msg"; @@ -99,70 +85,45 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(stored_help_msg, help_msg); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "is_required() should return true" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_required() should return true") { auto sut = prepare_argument(long_name); REQUIRE(sut_is_required(sut)); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "is_used() should return false by default" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_used() should return false by default") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_is_used(sut)); } -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "is_used() should return true when argument contains value" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_used() should return true when argument contains value") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); REQUIRE(sut_is_used(sut)); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "nused() should return 0 by default" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "nused() should return 0 by default") { const auto sut = prepare_argument(long_name); REQUIRE_EQ(sut_get_nused(sut), 0u); } -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "is_used() should return 1 when argument contains value" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_used() should return 1 when argument contains value") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); REQUIRE_EQ(sut_get_nused(sut), 1u); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "has_value() should return false by default" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "has_value() should return false by default") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_has_value(sut)); } -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "has_value() should return true is value is set" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "has_value() should return true is value is set") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); @@ -170,20 +131,13 @@ TEST_CASE_FIXTURE( REQUIRE(sut_has_value(sut)); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "has_parsed_values() should return false by default" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "has_parsed_values() should return false by default") { const auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_has_parsed_values(sut)); } -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "has_parsed_values() should true if the value is set" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "has_parsed_values() should true if the value is set") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); @@ -191,55 +145,38 @@ TEST_CASE_FIXTURE( REQUIRE(sut_has_parsed_values(sut)); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "set_value(any) should throw when a value has already been set" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "set_value(any) should throw when a value has already been set") { auto sut = prepare_argument(long_name); REQUIRE_NOTHROW(sut_set_value(sut, std::to_string(value_1))); REQUIRE(sut_has_value(sut)); - REQUIRE_THROWS_AS( - sut_set_value( - sut, std::to_string(value_2)), - ap::error::value_already_set_error); + REQUIRE_THROWS_AS(sut_set_value(sut, std::to_string(value_2)), ap::error::value_already_set_error); } -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "set_value(any) should throw when value_type cannot be obtained from given string" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "set_value(any) should throw when value_type cannot be obtained from given string") { auto sut = prepare_argument(long_name); SUBCASE("given string is empty") { - REQUIRE_THROWS_AS( - sut_set_value(sut, empty_str), - ap::error::invalid_value_error); + REQUIRE_THROWS_AS(sut_set_value(sut, empty_str), ap::error::invalid_value_error); REQUIRE_FALSE(sut_has_value(sut)); } SUBCASE("given string is non-convertible to value_type") { - REQUIRE_THROWS_AS( - sut_set_value(sut, invalid_value_str), - ap::error::invalid_value_error); + REQUIRE_THROWS_AS(sut_set_value(sut, invalid_value_str), ap::error::invalid_value_error); REQUIRE_FALSE(sut_has_value(sut)); } } TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "set_value(any) should throw when parameter passed to value() is not present in the choices set" + positional_argument_test_fixture, "set_value(any) should throw when parameter passed to value() is not present in the choices set" ) { auto sut = prepare_argument(long_name); sut_set_choices(sut, default_choices); - REQUIRE_THROWS_AS( - sut_set_value(sut, std::to_string(invalid_choice)), - ap::error::invalid_choice_error); + REQUIRE_THROWS_AS(sut_set_value(sut, std::to_string(invalid_choice)), ap::error::invalid_choice_error); REQUIRE_FALSE(sut_has_value(sut)); } @@ -256,7 +193,9 @@ TEST_CASE_FIXTURE( test_value_type value; for (const auto& v : correct_values) { - SUBCASE("correct value") { value = v; } + SUBCASE("correct value") { + value = v; + } } CAPTURE(value); @@ -266,28 +205,20 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(std::any_cast(sut_get_value(sut)), value); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "set_value(any) should perform the specified action" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "set_value(any) should perform the specified action") { auto sut = prepare_argument(long_name); SUBCASE("valued action") { - const auto double_valued_action = - [] (const test_value_type& value) { return 2 * value; }; + const auto double_valued_action = [](const test_value_type& value) { return 2 * value; }; sut.action(double_valued_action); sut_set_value(sut, std::to_string(value_1)); - REQUIRE_EQ( - std::any_cast(sut_get_value(sut)), - double_valued_action(value_1) - ); + REQUIRE_EQ(std::any_cast(sut_get_value(sut)), double_valued_action(value_1)); } SUBCASE("void action") { - const auto double_void_action = [] (test_value_type& value) { value *= 2; }; + const auto double_void_action = [](test_value_type& value) { value *= 2; }; sut.action(double_void_action); auto test_value = value_1; @@ -299,21 +230,14 @@ TEST_CASE_FIXTURE( } } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "value() should return default any object if argument's value has not been set" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "value() should return default any object if argument's value has not been set") { auto sut = prepare_argument(long_name); REQUIRE_FALSE(sut_has_value(sut)); REQUIRE_THROWS_AS(std::any_cast(sut_get_value(sut)), std::bad_any_cast); } -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "value() should return the argument's value if it has been set" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "value() should return the argument's value if it has been set") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1)); @@ -322,30 +246,19 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(std::any_cast(sut_get_value(sut)), value_1); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "values() should throw logic_error" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "values() should throw logic_error") { auto sut = prepare_argument(long_name); REQUIRE_THROWS_AS(sut_get_values(sut), std::logic_error); } - -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "nvalues_in_range() should return less by default" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "nvalues_in_range() should return less by default") { const auto sut = prepare_argument(long_name); REQUIRE(std::is_lt(sut_nvalues_in_range(sut))); } -TEST_CASE_FIXTURE( - positional_argument_test_fixture, - "nvalues_in_range() should return equivalent if a value has been set" -) { +TEST_CASE_FIXTURE(positional_argument_test_fixture, "nvalues_in_range() should return equivalent if a value has been set") { auto sut = prepare_argument(long_name); sut_set_value(sut, std::to_string(value_1));