From e387b1e7b5e7b8626697ae7701bbd10ce9745a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Musia=C5=82?= <111433005+SpectraL519@users.noreply.github.com> Date: Tue, 12 Mar 2024 18:15:00 +0100 Subject: [PATCH] changed the argument_name member names to primary and secondary --- include/ap/argument_parser.hpp | 147 ++++++++---------- test/include/argument_parser_test_fixture.hpp | 8 +- test/source/test_argument_name.cpp | 78 +++++----- .../test_argument_parser_add_argument.cpp | 60 +++---- .../test_argument_parser_parse_args.cpp | 128 +++++++-------- test/source/test_optional_argument.cpp | 86 +++++----- test/source/test_positional_argument.cpp | 70 ++++----- 7 files changed, 283 insertions(+), 294 deletions(-) diff --git a/include/ap/argument_parser.hpp b/include/ap/argument_parser.hpp index 01d4fac..971c270 100644 --- a/include/ap/argument_parser.hpp +++ b/include/ap/argument_parser.hpp @@ -329,17 +329,17 @@ struct argument_name { /** * @brief Primary name constructor. - * @param name The primary name of the argument. + * @param primary The primary name of the argument. */ - explicit argument_name(std::string_view name) : name(name) {} + argument_name(std::string_view primary) : primary(primary) {} /** * @brief Primary and secondary name constructor. - * @param name The primary name of the argument. + * @param primary The primary name of the argument. * @param short_name The secondary (short) name of the argument. */ - explicit argument_name(std::string_view name, std::string_view short_name) - : name(name), short_name(short_name) {} + argument_name(std::string_view primary, std::string_view secondary) + : primary(primary), secondary(secondary) {} /// @brief Class destructor. ~argument_name() = default; @@ -350,23 +350,23 @@ struct argument_name { * @return Equality of argument names. */ inline bool operator==(const argument_name& other) const { - return this->name == other.name; + return this->primary == other.primary; } /** * @brief Equality comparison operator for string variables representing argument names. * @param name The string view to compare with. - * @return Equality of names comparison (either full or short name). + * @return Equality of names comparison (either primary or secondary 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->primary or (this->secondary and name == this->secondary.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->secondary + ? ("[" + this->primary + "," + this->secondary.value() + "]") + : ("[" + this->primary + "]"); } /** @@ -380,11 +380,8 @@ struct argument_name { return os; } - // TODO: rename - // * name -> primary - // * short_name -> secondary - const std::string name; ///< The primary name of the argument. - const std::optional short_name; ///< The optional short name of the argument. + const std::string primary; ///< The primary name of the argument. + const std::optional secondary; ///< The optional secondary (short) name of the argument. }; /// @brief Argument class interface @@ -524,20 +521,10 @@ class argument_name_used_error : public argument_parser_error { public: /** * @brief Constructor for the argument_name_used_error class. - * @param given_arg_name The name of the argument causing the collision. + * @param arg_name The name of the argument causing the collision. */ - explicit argument_name_used_error(const std::string_view& given_arg_name) - : argument_parser_error("Given name `" + std::string(given_arg_name) + "` already used") {} - - /** - * @brief Constructor for the argument_name_used_error class with a short name. - * @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 argument::detail::argument_name& arg_name) + : argument_parser_error("Given name `" + arg_name.str() + "` already used") {} }; /// @brief Exception thrown when an argument with a specific name is not found. @@ -630,18 +617,18 @@ class positional_argument : public detail::argument_interface { positional_argument() = delete; /** - * @brief Constructor for positional_argument with a name. + * @brief Constructor for positional_argument with a primary name. * @param name The primary name of the positional argument. */ positional_argument(std::string_view name) : _name(name) {} /** - * @brief Constructor for positional_argument with a name and a short name. - * @param name The primary name of the positional argument. - * @param short_name The short name of the positional argument (optional). + * @brief Constructor for positional_argument with a primary and secondary name. + * @param primary_name The primary name of the positional argument. + * @param secondary_name The secondary name of the positional argument. */ - positional_argument(std::string_view name, std::string_view short_name) - : _name(name, short_name) {} + positional_argument(std::string_view primary_name, std::string_view secondary_name) + : _name(primary_name, secondary_name) {} /// @brief Destructor for positional argument. ~positional_argument() = default; @@ -792,7 +779,7 @@ class positional_argument : public detail::argument_interface { /// @return Reference to the vector of parsed values for the positional argument. [[nodiscard]] inline const std::vector& values() const override { - throw std::logic_error("Positional argument " + this->_name.name + "has only 1 value."); + throw std::logic_error("Positional argument " + this->_name.primary + "has only 1 value."); } /** @@ -847,18 +834,18 @@ class optional_argument : public detail::argument_interface { optional_argument() = delete; /** - * @brief Constructor for optional_argument with a name. + * @brief Constructor for optional_argument with a primary name. * @param name The primary name of the optional argument. */ optional_argument(std::string_view name) : _name(name) {} /** - * @brief Constructor for optional_argument with a name and a short name. - * @param name The primary name of the optional argument. - * @param short_name The short name of the optional argument (optional). + * @brief Constructor for optional_argument with a primary and secondary name. + * @param primary_name The primary name of the positional argument. + * @param secondary_name The secondary name of the positional argument. */ - optional_argument(std::string_view name, std::string_view short_name) - : _name(name, short_name) {} + optional_argument(std::string_view primary_name, std::string_view secondary_name) + : _name(primary_name, secondary_name) {} /// @brief Destructor for optional_argument. ~optional_argument() = default; @@ -1206,7 +1193,7 @@ class argument_parser { } /** - * @brief Add a positional argument to the parser. + * @brief Add a positional argument to the parser configuration. * @tparam T Type of the argument value. * @param name The name of the argument. * @return Reference to the added positional argument. @@ -1223,25 +1210,25 @@ class argument_parser { } /** - * @brief Add a positional argument with a short name to the parser. + * @brief Add a positional argument to the parser configuration. * @tparam T Type of the argument value. - * @param name The name of the argument. - * @param short_name The short name of the argument. + * @param primary_name The name of the argument. + * @param secondary_name The secondary name of the argument. * @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 primary_name, std::string_view secondary_name) { // TODO: check forbidden characters - if (this->_is_arg_name_used(name, short_name)) - throw error::argument_name_used_error(name, short_name); + if (this->_is_arg_name_used(primary_name, secondary_name)) + throw error::argument_name_used_error({primary_name, secondary_name}); - this->_positional_args.push_back(std::make_unique>(name, short_name)); + this->_positional_args.push_back(std::make_unique>(primary_name, secondary_name)); return static_cast&>(*this->_positional_args.back()); } /** - * @brief Add an optional argument to the parser. + * @brief Add an optional argument to the parser configuration. * @tparam T Type of the argument value. * @param name The name of the argument. * @return Reference to the added optional argument. @@ -1258,25 +1245,25 @@ class argument_parser { } /** - * @brief Add an optional argument with a short name to the parser. + * @brief Add an optional argument to the parser configuration. * @tparam T Type of the argument value. - * @param name The name of the argument. - * @param short_name The short name of the argument. + * @param primary_name The name of the argument. + * @param secondary_name The secondary name of the argument. * @return Reference to the added optional argument. */ template - argument::optional_argument& add_optional_argument(std::string_view name, std::string_view short_name) { + argument::optional_argument& add_optional_argument(std::string_view primary_name, std::string_view secondary_name) { // TODO: check forbidden characters - if (this->_is_arg_name_used(name, short_name)) - throw error::argument_name_used_error(name, short_name); + if (this->_is_arg_name_used(primary_name, secondary_name)) + throw error::argument_name_used_error({primary_name, secondary_name}); - this->_optional_args.push_back(std::make_unique>(name, short_name)); + this->_optional_args.push_back(std::make_unique>(primary_name, secondary_name)); return static_cast&>(*this->_optional_args.back()); } /** - * @brief Add a boolean flag to the parser. + * @brief Add a boolean flag to the parser configuration. * @tparam StoreImplicitly Flag indicating whether to store implicitly. * @param name The name of the flag. * @return Reference to the added boolean flag argument. @@ -1290,15 +1277,15 @@ class argument_parser { } /** - * @brief Add a boolean flag with a short name to the parser. + * @brief Add a boolean flag to the parser configuration. * @tparam StoreImplicitly Flag indicating whether to store implicitly. - * @param name The name of the flag. - * @param short_name The short name of the flag. + * @param primary_name The name of the flag. + * @param secondary_name The secondary name of the flag. * @return Reference to the added boolean flag argument. */ template - argument::optional_argument& add_flag(std::string_view name, std::string_view short_name) { - return this->add_optional_argument(name, short_name) + argument::optional_argument& add_flag(std::string_view primary_name, std::string_view secondary_name) { + return this->add_optional_argument(primary_name, secondary_name) .default_value(not StoreImplicitly) .implicit_value(StoreImplicitly) .nargs(0); @@ -1517,7 +1504,7 @@ class argument_parser { using argument_predicate_type = std::function; /** - * @brief Function to create a predicate for finding arguments by name. + * @brief Function to create a predicate for finding arguments by thename. * @param name The name of the argument. * @return Argument predicate based on the provided name. */ @@ -1526,25 +1513,27 @@ class argument_parser { } /** - * @brief Function to create a predicate for finding arguments by name and short name. - * @param name The name of the argument. - * @param short_name The short name of the argument. - * @return Argument predicate based on the provided name and short name. + * @brief Function to create a predicate for finding arguments by the name. + * @param primary_name The name of the argument. + * @param secondary_name The secondary name of the argument. + * @return Argument predicate based on the provided name and secondary name. */ [[nodiscard]] inline argument_predicate_type _name_eq_predicate( - const std::string_view& name, const std::string_view& short_name + const std::string_view& primary_name, const std::string_view& secondary_name ) const { - return [&name, &short_name](const argument_ptr_type& arg) { - return name == arg->name() or short_name == arg->name(); + return [&primary_name, &secondary_name](const argument_ptr_type& arg) { + return primary_name == arg->name() or secondary_name == arg->name(); }; } /** * @brief Check if an argument name is already used. * @param name The name of the argument. - * @return True if the argument name is already used, false otherwise. + * @return True if the argument name is already used. */ [[nodiscard]] bool _is_arg_name_used(const std::string_view& name) const { + // TODO: replace with a single function which takes the argument_name structure + // same for _name_eq_predicate const auto predicate = this->_name_eq_predicate(name); if (std::ranges::find_if(this->_positional_args, predicate) != this->_positional_args.end()) @@ -1557,13 +1546,13 @@ class argument_parser { } /** - * @brief Check if an argument name and short name pair is already used. - * @param name The name of the argument. - * @param short_name The short name of the argument. - * @return True if the argument name or short name is already used, false otherwise. + * @brief Check if an argument name pair is already used. + * @param primary_name The name of the argument. + * @param secondary_name The secondary name of the argument. + * @return True if either the primary name or the secondary name is already used. */ - [[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); + [[nodiscard]] bool _is_arg_name_used(const std::string_view& primary_name, const std::string_view& secondary_name) const { + const auto predicate = this->_name_eq_predicate(primary_name, secondary_name); if (std::ranges::find_if(this->_positional_args, predicate) != this->_positional_args.end()) return true; diff --git a/test/include/argument_parser_test_fixture.hpp b/test/include/argument_parser_test_fixture.hpp index 0b7cffb..26efab3 100644 --- a/test/include/argument_parser_test_fixture.hpp +++ b/test/include/argument_parser_test_fixture.hpp @@ -27,7 +27,7 @@ struct argument_parser_test_fixture { return "--test_arg_" + std::to_string(i); } - [[nodiscard]] std::string prepare_arg_flag_short(std::size_t i) const { + [[nodiscard]] std::string prepare_arg_flag_secondary(std::size_t i) const { return "-ta_" + std::to_string(i); } @@ -87,12 +87,12 @@ struct argument_parser_test_fixture { 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()); + parser.add_positional_argument(arg_name.primary, arg_name.secondary.value()); } for (std::size_t i = args_split; i < num_args; i++) { // optional args const auto arg_name = prepare_arg_name(i); - parser.add_optional_argument(arg_name.name, arg_name.short_name.value()); + parser.add_optional_argument(arg_name.primary, arg_name.secondary.value()); } } @@ -105,7 +105,7 @@ struct argument_parser_test_fixture { } 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_argument::type_discriminator::flag, prepare_arg_name(i).primary }); cmd_args.push_back(cmd_argument{ cmd_argument::type_discriminator::value, prepare_arg_value(i) }); } diff --git a/test/source/test_argument_name.cpp b/test/source/test_argument_name.cpp index 634a96c..09929d3 100644 --- a/test/source/test_argument_name.cpp +++ b/test/source/test_argument_name.cpp @@ -12,55 +12,55 @@ using namespace ap::argument::detail; namespace { -constexpr std::string_view name = "test"; -constexpr std::string_view short_name = "t"; +constexpr std::string_view primary_name = "test"; +constexpr std::string_view secondary_name = "t"; -constexpr std::string_view other_name = "other"; -constexpr std::string_view other_short_name = "o"; +constexpr std::string_view other_primary_name = "other"; +constexpr std::string_view other_secondary_name = "o"; -argument_name default_argument_name_long_name() { - return argument_name(name); +argument_name default_argument_name_primary_only() { + return argument_name(primary_name); } -argument_name default_argument_name_both_names() { - return argument_name(name, short_name); +argument_name default_argument_name_primary_and_secondary() { + return argument_name(primary_name, secondary_name); } } // namespace TEST_SUITE_BEGIN("test_argument_name"); -TEST_CASE("argument_name.name member should be correctly " +TEST_CASE("argument_name.primary member should be correctly " "initialized") { - const auto arg_name = default_argument_name_long_name(); + const auto arg_name = default_argument_name_primary_only(); - REQUIRE_EQ(arg_name.name, name); + REQUIRE_EQ(arg_name.primary, primary_name); } TEST_CASE("argument_name members should be correctly " "initialized") { - const auto arg_name = default_argument_name_both_names(); + const auto arg_name = default_argument_name_primary_and_secondary(); - REQUIRE_EQ(arg_name.name, name); + REQUIRE_EQ(arg_name.primary, primary_name); - REQUIRE(arg_name.short_name); - REQUIRE_EQ(arg_name.short_name.value(), short_name); + REQUIRE(arg_name.secondary); + REQUIRE_EQ(arg_name.secondary.value(), secondary_name); } TEST_CASE("argument_name::operator==(argument_name) should " "return true if " - "long names are equal") { - const auto arg_name_a = default_argument_name_long_name(); - const auto arg_name_b = default_argument_name_both_names(); + "primary names are equal") { + const auto arg_name_a = default_argument_name_primary_only(); + const auto arg_name_b = default_argument_name_primary_and_secondary(); REQUIRE(arg_name_a == arg_name_b); } TEST_CASE("argument_name::operator==(argument_name) should " "return false if " - "long names are not equal") { - const auto arg_name_a = default_argument_name_long_name(); - const auto arg_name_b = argument_name(other_name, other_short_name); + "primary names are not equal") { + const auto arg_name_a = default_argument_name_primary_only(); + const auto arg_name_b = argument_name(other_primary_name, other_secondary_name); REQUIRE_FALSE(arg_name_a == arg_name_b); } @@ -68,49 +68,49 @@ TEST_CASE("argument_name::operator==(argument_name) should " TEST_CASE("argument_name::operator==(string_view) should " "return true if at " "least one name matches") { - SUBCASE("argument_name with long name only") { - const auto arg_name = default_argument_name_long_name(); + SUBCASE("argument_name with primary name only") { + const auto arg_name = default_argument_name_primary_only(); - REQUIRE(arg_name == name); + REQUIRE(arg_name == primary_name); } SUBCASE("argument_name with both names") { - const auto arg_name = default_argument_name_both_names(); + const auto arg_name = default_argument_name_primary_and_secondary(); - REQUIRE(arg_name == name); - REQUIRE(arg_name == short_name); + REQUIRE(arg_name == primary_name); + REQUIRE(arg_name == secondary_name); } } TEST_CASE("argument_name::operator==(string_view) should " "return false if no " "name matches") { - SUBCASE("argument_name with long name only") { - const auto arg_name = default_argument_name_long_name(); + SUBCASE("argument_name with primary name only") { + const auto arg_name = default_argument_name_primary_only(); - REQUIRE_FALSE(arg_name == other_name); - REQUIRE_FALSE(arg_name == other_short_name); + REQUIRE_FALSE(arg_name == other_primary_name); + REQUIRE_FALSE(arg_name == other_secondary_name); } SUBCASE("argument_name with both names") { - const auto arg_name = default_argument_name_both_names(); + const auto arg_name = default_argument_name_primary_and_secondary(); - REQUIRE_FALSE(arg_name == other_name); - REQUIRE_FALSE(arg_name == other_short_name); + REQUIRE_FALSE(arg_name == other_primary_name); + REQUIRE_FALSE(arg_name == other_secondary_name); } } TEST_CASE("operator<< should push correct data to the output stream") { std::stringstream ss, expected_ss; - SUBCASE("argument_name with long name only") { - ss << default_argument_name_long_name(); - expected_ss << "[" << name << "]"; + SUBCASE("argument_name with primary name only") { + ss << default_argument_name_primary_only(); + expected_ss << "[" << primary_name << "]"; } SUBCASE("argument_name with both names") { - ss << default_argument_name_both_names(); - expected_ss << "[" << name << "," << short_name << "]"; + ss << default_argument_name_primary_and_secondary(); + expected_ss << "[" << primary_name << "," << secondary_name << "]"; } CAPTURE(ss); diff --git a/test/source/test_argument_parser_add_argument.cpp b/test/source/test_argument_parser_add_argument.cpp index 24c16cc..3bcdd2a 100644 --- a/test/source/test_argument_parser_add_argument.cpp +++ b/test/source/test_argument_parser_add_argument.cpp @@ -13,11 +13,11 @@ using namespace ap::argument; namespace { -constexpr std::string_view name = "test"; -constexpr std::string_view short_name = "t"; +constexpr std::string_view primary_name = "test"; +constexpr std::string_view secondary_name = "t"; -constexpr std::string_view other_name = "other"; -constexpr std::string_view other_short_name = "o"; +constexpr std::string_view other_primary_name = "other"; +constexpr std::string_view other_secondary_name = "o"; } // namespace @@ -50,7 +50,7 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "default_optional_arguments shou } 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); + const auto& argument = sut.add_positional_argument(primary_name, secondary_name); REQUIRE_FALSE(argument.is_optional()); } @@ -59,23 +59,23 @@ TEST_CASE_FIXTURE( "add_positional_argument should throw only when adding an" "argument with a previously used name" ) { - sut.add_positional_argument(name, short_name); + sut.add_positional_argument(primary_name, secondary_name); SUBCASE("adding argument with a unique name") { - REQUIRE_NOTHROW(sut.add_positional_argument(other_name, other_short_name)); + REQUIRE_NOTHROW(sut.add_positional_argument(other_primary_name, other_secondary_name)); } - 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); + SUBCASE("adding argument with a previously used primary name") { + REQUIRE_THROWS_AS(sut.add_positional_argument(primary_name, other_secondary_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); + SUBCASE("adding argument with a previously used secondary name") { + REQUIRE_THROWS_AS(sut.add_positional_argument(other_primary_name, secondary_name), ap::error::argument_name_used_error); } } 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); + const auto& argument = sut.add_optional_argument(primary_name, secondary_name); REQUIRE(argument.is_optional()); } @@ -84,18 +84,18 @@ TEST_CASE_FIXTURE( "add_optional_argument should throw only when adding an" "argument with a previously used name" ) { - sut.add_optional_argument(name, short_name); + sut.add_optional_argument(primary_name, secondary_name); SUBCASE("adding argument with a unique name") { - REQUIRE_NOTHROW(sut.add_optional_argument(other_name, other_short_name)); + REQUIRE_NOTHROW(sut.add_optional_argument(other_primary_name, other_secondary_name)); } - 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); + SUBCASE("adding argument with a previously used primary name") { + REQUIRE_THROWS_AS(sut.add_optional_argument(primary_name, other_secondary_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); + SUBCASE("adding argument with a previously used secondary name") { + REQUIRE_THROWS_AS(sut.add_optional_argument(other_primary_name, secondary_name), ap::error::argument_name_used_error); } } @@ -103,39 +103,39 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "add_flag should return an optio const optional_argument_test_fixture opt_arg_fixture; SUBCASE("StoreImplicitly = true") { - auto& argument = sut.add_flag(name, short_name); + auto& argument = sut.add_flag(primary_name, secondary_name); REQUIRE(argument.is_optional()); - REQUIRE_FALSE(sut.value(name)); + REQUIRE_FALSE(sut.value(primary_name)); opt_arg_fixture.sut_set_used(argument); - REQUIRE(sut.value(name)); + REQUIRE(sut.value(primary_name)); } SUBCASE("StoreImplicitly = false") { - auto& argument = sut.add_flag(name, short_name); + auto& argument = sut.add_flag(primary_name, secondary_name); REQUIRE(argument.is_optional()); - REQUIRE(sut.value(name)); + REQUIRE(sut.value(primary_name)); opt_arg_fixture.sut_set_used(argument); - REQUIRE_FALSE(sut.value(name)); + REQUIRE_FALSE(sut.value(primary_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); + sut.add_flag(primary_name, secondary_name); SUBCASE("adding argument with a unique name") { - REQUIRE_NOTHROW(sut.add_flag(other_name, other_short_name)); + REQUIRE_NOTHROW(sut.add_flag(other_primary_name, other_secondary_name)); } - 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); + SUBCASE("adding argument with a previously used primary name") { + REQUIRE_THROWS_AS(sut.add_flag(primary_name, other_secondary_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); + SUBCASE("adding argument with a previously used secondary name") { + REQUIRE_THROWS_AS(sut.add_flag(other_primary_name, secondary_name), ap::error::argument_name_used_error); } } diff --git a/test/source/test_argument_parser_parse_args.cpp b/test/source/test_argument_parser_parse_args.cpp index 0c12150..3b910de 100644 --- a/test/source/test_argument_parser_parse_args.cpp +++ b/test/source/test_argument_parser_parse_args.cpp @@ -22,10 +22,10 @@ constexpr std::size_t non_default_args_split = non_default_num_args / 2; const std::string invalid_arg_name = "invalid_arg"; -const std::string positional_arg_name = "positional_arg"; -const std::string positional_arg_short_name = "pa"; -const std::string optional_arg_name = "optional_arg"; -const std::string optional_arg_short_name = "oa"; +const std::string positional_arg_name_primary = "positional_arg"; +const std::string positional_arg_name_secondary = "pa"; +const std::string optional_arg_name_primary = "optional_arg"; +const std::string optional_arg_name_secondary = "oa"; } // namespace @@ -121,8 +121,8 @@ TEST_CASE_FIXTURE( for (std::size_t i = 0; i < non_default_num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE(sut_get_argument(arg_name.name)); - REQUIRE(sut_get_argument(arg_name.short_name.value())); + REQUIRE(sut_get_argument(arg_name.primary)); + REQUIRE(sut_get_argument(arg_name.secondary.value())); } } @@ -132,7 +132,7 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "parse_args should throw when th 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.primary, required_arg_name.secondary.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); @@ -149,7 +149,7 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "parse_args should throw when an 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.primary, range_arg_name.secondary.value()).nargs(at_least(1)); REQUIRE_THROWS_AS(sut.parse_args(argc, argv), ap::error::invalid_nvalues_error); @@ -164,7 +164,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.primary, required_arg_name.secondary.value()).required(); int argc; char** argv; @@ -201,7 +201,7 @@ 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() + bypass_required_arg_name.primary, bypass_required_arg_name.secondary.value() ) .default_value(false) .implicit_value(true) @@ -216,11 +216,11 @@ TEST_CASE_FIXTURE( std::string arg_flag; - SUBCASE("long flag") { + SUBCASE("primary name flag") { arg_flag = prepare_arg_flag(non_default_num_args); } - SUBCASE("short flag") { - arg_flag = prepare_arg_flag_short(non_default_num_args); + SUBCASE("secondary name flag") { + arg_flag = prepare_arg_flag_secondary(non_default_num_args); } CAPTURE(arg_flag); @@ -229,7 +229,7 @@ TEST_CASE_FIXTURE( std::strcpy(argv[1], arg_flag.c_str()); REQUIRE_NOTHROW(sut.parse_args(argc, argv)); - REQUIRE(sut.value(bypass_required_arg_name.name)); + REQUIRE(sut.value(bypass_required_arg_name.primary)); free_argv(argc, argv); } @@ -253,7 +253,7 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "has_value should return false w 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.primary, required_arg_name.secondary.value()).required(); const auto num_args = non_default_num_args + 1; @@ -269,8 +269,8 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "has_value should return false w for (std::size_t i = 0; i < non_default_num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE(sut.has_value(arg_name.name)); - REQUIRE(sut.has_value(arg_name.short_name.value())); + REQUIRE(sut.has_value(arg_name.primary)); + REQUIRE(sut.has_value(arg_name.secondary.value())); } } SUBCASE("only the necessary arguments have values") { @@ -286,18 +286,18 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "has_value should return false w for (std::size_t i = 0; i < non_default_args_split; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE(sut.has_value(arg_name.name)); - REQUIRE(sut.has_value(arg_name.short_name.value())); + REQUIRE(sut.has_value(arg_name.primary)); + REQUIRE(sut.has_value(arg_name.secondary.value())); } for (std::size_t i = non_default_args_split; i < non_default_num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE_FALSE(sut.has_value(arg_name.name)); - REQUIRE_FALSE(sut.has_value(arg_name.short_name.value())); + REQUIRE_FALSE(sut.has_value(arg_name.primary)); + REQUIRE_FALSE(sut.has_value(arg_name.secondary.value())); } for (std::size_t i = non_default_num_args; i < num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE(sut.has_value(arg_name.name)); - REQUIRE(sut.has_value(arg_name.short_name.value())); + REQUIRE(sut.has_value(arg_name.primary)); + REQUIRE(sut.has_value(arg_name.secondary.value())); } } @@ -330,8 +330,8 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "value() should throw before cal for (std::size_t i = 0; i < non_default_num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE_THROWS_AS(sut.value(arg_name.name), ap::error::invalid_value_type_error); - REQUIRE_THROWS_AS(sut.value(arg_name.short_name.value()), ap::error::invalid_value_type_error); + REQUIRE_THROWS_AS(sut.value(arg_name.primary), ap::error::invalid_value_type_error); + REQUIRE_THROWS_AS(sut.value(arg_name.secondary.value()), ap::error::invalid_value_type_error); } } @@ -340,7 +340,7 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "value() should throw if the giv 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.primary, required_arg_name.secondary.value()).required(); const auto num_args = non_default_num_args + 1; @@ -356,18 +356,18 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "value() should throw if the giv for (std::size_t i = 0; i < non_default_args_split; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE_NOTHROW(sut.value(arg_name.name)); - REQUIRE_NOTHROW(sut.value(arg_name.short_name.value())); + REQUIRE_NOTHROW(sut.value(arg_name.primary)); + REQUIRE_NOTHROW(sut.value(arg_name.secondary.value())); } for (std::size_t i = non_default_args_split; i < non_default_num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE_THROWS_AS(sut.value(arg_name.name), ap::error::invalid_value_type_error); - REQUIRE_THROWS_AS(sut.value(arg_name.short_name.value()), ap::error::invalid_value_type_error); + REQUIRE_THROWS_AS(sut.value(arg_name.primary), ap::error::invalid_value_type_error); + REQUIRE_THROWS_AS(sut.value(arg_name.secondary.value()), ap::error::invalid_value_type_error); } for (std::size_t i = non_default_num_args; i < num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE_NOTHROW(sut.value(arg_name.name)); - REQUIRE_NOTHROW(sut.value(arg_name.short_name.value())); + REQUIRE_NOTHROW(sut.value(arg_name.primary)); + REQUIRE_NOTHROW(sut.value(arg_name.secondary.value())); } free_argv(argc, argv); @@ -388,8 +388,8 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "value() should throw if an argu for (std::size_t i = 0; i < non_default_num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE(sut.has_value(arg_name.name)); - REQUIRE_THROWS_AS(sut.value(arg_name.name), ap::error::invalid_value_type_error); + REQUIRE(sut.has_value(arg_name.primary)); + REQUIRE_THROWS_AS(sut.value(arg_name.primary), ap::error::invalid_value_type_error); } free_argv(argc, argv); @@ -404,7 +404,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.primary, required_arg_name.secondary.value()).required(); const auto num_args = non_default_num_args + 1; @@ -417,9 +417,9 @@ TEST_CASE_FIXTURE( const auto arg_name = prepare_arg_name(i); const auto arg_value = prepare_arg_value(i); - REQUIRE(sut.has_value(arg_name.name)); - REQUIRE_EQ(sut.value(arg_name.name), arg_value); - REQUIRE_EQ(sut.value(arg_name.short_name.value()), arg_value); + REQUIRE(sut.has_value(arg_name.primary)); + REQUIRE_EQ(sut.value(arg_name.primary), arg_value); + REQUIRE_EQ(sut.value(arg_name.secondary.value()), arg_value); } free_argv(argc, argv); @@ -435,8 +435,8 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "count should return 0 before ca for (std::size_t i = 0; i < non_default_num_args; i++) { const auto arg_name = prepare_arg_name(i); - REQUIRE_EQ(sut.count(arg_name.name), 0u); - REQUIRE_EQ(sut.count(arg_name.short_name.value()), 0u); + REQUIRE_EQ(sut.count(arg_name.primary), 0u); + REQUIRE_EQ(sut.count(arg_name.secondary.value()), 0u); } } @@ -454,8 +454,8 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "count should return 0 if there 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_positional_argument(positional_arg_name_primary, positional_arg_name_secondary); + sut.add_optional_argument(optional_arg_name_primary, optional_arg_name_secondary).nargs(ap::nargs::any()); // expected values const std::size_t positional_count = 1u; @@ -472,8 +472,8 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "count should return the number argv[1] = new char[positional_arg_value.length() + 1]; std::strcpy(argv[1], positional_arg_value.c_str()); - const std::string optional_arg_flag = "--" + optional_arg_name; - const std::string optional_arg_value = optional_arg_name + "_value"; + const std::string optional_arg_flag = "--" + optional_arg_name_primary; + const std::string optional_arg_value = optional_arg_name_primary + "_value"; for (std::size_t i = 2; i < argc; i += 2) { if (i == argc - 1) { argv[i] = new char[optional_arg_value.length() + 1]; @@ -492,8 +492,8 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "count should return the number sut.parse_args(argc, argv); // test count - REQUIRE_EQ(sut.count(positional_arg_name), positional_count); - REQUIRE_EQ(sut.count(optional_arg_name), optional_count); + REQUIRE_EQ(sut.count(positional_arg_name_primary), positional_count); + REQUIRE_EQ(sut.count(optional_arg_name_primary), optional_count); // free argv free_argv(argc, argv); @@ -505,28 +505,28 @@ 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") { - sut.add_positional_argument(positional_arg_name, positional_arg_short_name); + sut.add_positional_argument(positional_arg_name_primary, positional_arg_name_secondary); - REQUIRE_THROWS_AS(sut.values(positional_arg_name), std::logic_error); - REQUIRE_THROWS_AS(sut.values(positional_arg_short_name), std::logic_error); + REQUIRE_THROWS_AS(sut.values(positional_arg_name_primary), std::logic_error); + REQUIRE_THROWS_AS(sut.values(positional_arg_name_secondary), std::logic_error); } 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); + sut.add_optional_argument(optional_arg_name_primary, optional_arg_name_secondary); - SUBCASE("calling with argument's long name") { - const auto& values = sut.values(optional_arg_name); + SUBCASE("calling with argument's primary name") { + const auto& values = sut.values(optional_arg_name_primary); REQUIRE(values.empty()); } - SUBCASE("calling with argument's short name") { - const auto& values = sut.values(optional_arg_short_name); + SUBCASE("calling with argument's secondary name") { + const auto& values = sut.values(optional_arg_name_secondary); REQUIRE(values.empty()); } } 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)); + sut.add_optional_argument(optional_arg_name_primary, optional_arg_name_secondary).nargs(at_least(1)); // prepare argc & argv const int argc = 5; @@ -535,7 +535,7 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "values() should throw when an a argv[0] = new char[8]; std::strcpy(argv[0], "program"); - const std::string flag = "--" + optional_arg_name; + const std::string flag = "--" + optional_arg_name_primary; argv[1] = new char[flag.length() + 1]; std::strcpy(argv[1], flag.c_str()); for (int i = 2; i < argc; i++) { @@ -547,9 +547,9 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "values() should throw when an a // 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_primary), 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_name_secondary), ap::error::invalid_value_type_error ); free_argv(argc, argv); @@ -563,7 +563,7 @@ TEST_CASE_FIXTURE( const std::string default_value = "default_value"; const std::string implicit_value = "implicit_value"; - sut.add_optional_argument(optional_arg_name, optional_arg_short_name) + sut.add_optional_argument(optional_arg_name_primary, optional_arg_name_secondary) .default_value(default_value) .implicit_value(implicit_value); @@ -582,7 +582,7 @@ TEST_CASE_FIXTURE( argc = 2; argv = new char*[argc]; - const auto optional_arg_flag = "--" + optional_arg_name; + const auto optional_arg_flag = "--" + optional_arg_name_primary; argv[1] = new char[optional_arg_flag.length() + 1]; std::strcpy(argv[1], optional_arg_flag.c_str()); expected_value = implicit_value; @@ -598,7 +598,7 @@ TEST_CASE_FIXTURE( // parse args sut.parse_args(argc, argv); - const auto& stored_values = sut.values(optional_arg_name); + const auto& stored_values = sut.values(optional_arg_name_primary); REQUIRE_EQ(stored_values.size(), 1); REQUIRE_EQ(stored_values.front(), expected_value); @@ -611,7 +611,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_primary, optional_arg_name_secondary).nargs(at_least(1)); // prepare argc & argv const int argc = 5; @@ -620,7 +620,7 @@ TEST_CASE_FIXTURE( argv[0] = new char[8]; std::strcpy(argv[0], "program"); - const std::string flag = "--" + optional_arg_name; + const std::string flag = "--" + optional_arg_name_primary; argv[1] = new char[flag.length() + 1]; std::strcpy(argv[1], flag.c_str()); @@ -636,7 +636,7 @@ TEST_CASE_FIXTURE( // parse args sut.parse_args(argc, argv); - const auto& stored_values = sut.values(optional_arg_name); + const auto& stored_values = sut.values(optional_arg_name_primary); REQUIRE_EQ(stored_values.size(), values.size()); for (std::size_t i = 0; i < stored_values.size(); i++) diff --git a/test/source/test_optional_argument.cpp b/test/source/test_optional_argument.cpp index dbca2f8..2eed538 100644 --- a/test/source/test_optional_argument.cpp +++ b/test/source/test_optional_argument.cpp @@ -15,8 +15,8 @@ using ap::argument::optional_argument; namespace { -constexpr std::string_view long_name = "test"; -constexpr std::string_view short_name = "t"; +constexpr std::string_view primary_name = "test"; +constexpr std::string_view secondary_name = "t"; using test_value_type = int; using invalid_value_type = double; @@ -26,8 +26,8 @@ sut_type prepare_argument(std::string_view name) { return sut_type(name); } -sut_type prepare_argument(std::string_view name, std::string_view long_name) { - return sut_type(name, long_name); +sut_type prepare_argument(std::string_view primary_name, std::string_view secondary_name) { + return sut_type(primary_name, secondary_name); } const std::string empty_str = ""; @@ -48,41 +48,41 @@ 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") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_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") { - const auto sut = prepare_argument(long_name); +TEST_CASE_FIXTURE(optional_argument_test_fixture, "name() should return value passed to the optional argument constructor for primary name") { + const auto sut = prepare_argument(primary_name); const auto name = sut_get_name(sut); - REQUIRE_EQ(name, long_name); - REQUIRE_NE(name, short_name); + REQUIRE_EQ(name, primary_name); + REQUIRE_NE(name, secondary_name); } TEST_CASE_FIXTURE( optional_argument_test_fixture, - "name() and short_name() should return value passed to the optional" - "argument constructor for both long and short names" + "name() and secondary_name() should return value passed to the optional" + "argument constructor for both primary and secondary names" ) { - const auto sut = prepare_argument(long_name, short_name); + const auto sut = prepare_argument(primary_name, secondary_name); const auto name = sut_get_name(sut); - REQUIRE_EQ(name, long_name); - REQUIRE_EQ(name, short_name); + REQUIRE_EQ(name, primary_name); + REQUIRE_EQ(name, secondary_name); } TEST_CASE_FIXTURE(optional_argument_test_fixture, "help() should return nullopt by default") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_FALSE(sut_get_help(sut)); } TEST_CASE_FIXTURE(optional_argument_test_fixture, "help() should return message if one has been provided") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); constexpr std::string_view help_msg = "test help msg"; sut.help(help_msg); @@ -94,13 +94,13 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "help() should return message } TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_required() should return false by default") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_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") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut.required(); @@ -108,20 +108,20 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_required() should return t } TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_used() should return false by default") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_FALSE(sut_is_used(sut)); } TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_used() should return true when argument contains value") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_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") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_EQ(sut_get_nused(sut), 0u); } @@ -131,7 +131,7 @@ TEST_CASE_FIXTURE( "is_used() should return the number of times the argument's flag has been used " "[number of set_used() function calls]" ) { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); constexpr std::size_t nused = 5u; for (std::size_t n = 0; n < nused; n++) @@ -141,13 +141,13 @@ TEST_CASE_FIXTURE( } TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return false by default") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_FALSE(sut_has_value(sut)); } TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return true if value is set") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut_set_value(sut, std::to_string(value_1)); @@ -155,7 +155,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return tru } TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return true if a default value is set") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut.default_value(default_value); REQUIRE(sut_has_value(sut)); @@ -163,14 +163,14 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return tru } 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); + auto sut = prepare_argument(primary_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") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut.implicit_value(implicit_value); sut_set_used(sut); @@ -180,7 +180,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_value() should return tru } TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_parsed_values() should return false by default") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_FALSE(sut_has_parsed_values(sut)); } @@ -190,7 +190,7 @@ TEST_CASE_FIXTURE( "has_parsed_values() should return false regardles of the " "default_value and implicit_value parameters" ) { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); SUBCASE("default_value") { sut.default_value(default_value); @@ -210,7 +210,7 @@ TEST_CASE_FIXTURE( } TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_parsed_values() should true if the value is set") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut_set_value(sut, std::to_string(value_1)); @@ -218,13 +218,13 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "has_parsed_values() should tr } 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); + auto sut = prepare_argument(primary_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") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut_set_value(sut, std::to_string(value_1)); @@ -233,7 +233,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "value() should return the arg } 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); + auto sut = prepare_argument(primary_name); sut.default_value(value_1); REQUIRE(sut_has_value(sut)); @@ -241,7 +241,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "value() should return the def } 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); + auto sut = prepare_argument(primary_name); sut.implicit_value(implicit_value); sut_set_used(sut); @@ -251,7 +251,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "value() should return the imp } 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); + auto sut = prepare_argument(primary_name); SUBCASE("given string is empty") { REQUIRE_THROWS_AS(sut_set_value(sut, empty_str), ap::error::invalid_value_error); @@ -268,7 +268,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should throw w TEST_CASE_FIXTURE( 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); + auto sut = prepare_argument(primary_name); sut_set_choices(sut, default_choices); @@ -278,7 +278,7 @@ TEST_CASE_FIXTURE( } 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); + auto sut = prepare_argument(primary_name); sut_set_choices(sut, default_choices); const std::vector correct_values = default_choices; @@ -298,7 +298,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should accept } 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); + auto sut = prepare_argument(primary_name); REQUIRE_NOTHROW(sut_set_value(sut, std::to_string(value_1))); REQUIRE(sut_has_value(sut)); @@ -307,7 +307,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should throw w } 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); + auto sut = prepare_argument(primary_name); sut.nargs(non_default_range); for (const auto value : default_choices) { @@ -323,7 +323,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should accept } TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should perform the specified action") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); SUBCASE("valued action") { const auto double_valued_action = [](const test_value_type& value) { return 2 * value; }; @@ -348,13 +348,13 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "set_value(any) should perform } 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); + const auto sut = prepare_argument(primary_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") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut.nargs(non_default_range); sut.default_value(default_value); @@ -367,7 +367,7 @@ TEST_CASE_FIXTURE( "nvalues_in_range() should return equivalent only when the number of values " "is in the specified range" ) { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut.nargs(non_default_range); REQUIRE(std::is_lt(sut_nvalues_in_range(sut))); diff --git a/test/source/test_positional_argument.cpp b/test/source/test_positional_argument.cpp index d03c6af..216eae9 100644 --- a/test/source/test_positional_argument.cpp +++ b/test/source/test_positional_argument.cpp @@ -12,8 +12,8 @@ using ap::argument::positional_argument; namespace { -constexpr std::string_view long_name = "test"; -constexpr std::string_view short_name = "t"; +constexpr std::string_view primary_name = "test"; +constexpr std::string_view secondary_name = "t"; using test_value_type = int; using sut_type = positional_argument; @@ -22,8 +22,8 @@ sut_type prepare_argument(std::string_view name) { return sut_type(name); } -sut_type prepare_argument(std::string_view name, std::string_view long_name) { - return sut_type(name, long_name); +sut_type prepare_argument(std::string_view primary_name, std::string_view secondary_name) { + return sut_type(primary_name, secondary_name); } const std::string empty_str = ""; @@ -40,41 +40,41 @@ constexpr test_value_type invalid_choice = 4; TEST_SUITE_BEGIN("test_positional_argument"); TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_optional() should return false") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_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") { - const auto sut = prepare_argument(long_name); +TEST_CASE_FIXTURE(positional_argument_test_fixture, "name() should return value passed to the optional argument constructor for primary name") { + const auto sut = prepare_argument(primary_name); const auto name = sut_get_name(sut); - REQUIRE_EQ(name, long_name); - REQUIRE_NE(name, short_name); + REQUIRE_EQ(name, primary_name); + REQUIRE_NE(name, secondary_name); } TEST_CASE_FIXTURE( positional_argument_test_fixture, - "name() and short_name() should return value passed to the optional" - "argument constructor for both long and short names" + "name() and secondary_name() should return value passed to the optional" + "argument constructor for both primary and secondary names" ) { - const auto sut = prepare_argument(long_name, short_name); + const auto sut = prepare_argument(primary_name, secondary_name); const auto name = sut_get_name(sut); - REQUIRE_EQ(name, long_name); - REQUIRE_EQ(name, short_name); + REQUIRE_EQ(name, primary_name); + REQUIRE_EQ(name, secondary_name); } TEST_CASE_FIXTURE(positional_argument_test_fixture, "help() should return nullopt by default") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_FALSE(sut_get_help(sut)); } TEST_CASE_FIXTURE(positional_argument_test_fixture, "help() should return message if one has been provided") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); constexpr std::string_view help_msg = "test help msg"; sut.help(help_msg); @@ -86,45 +86,45 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "help() should return messag } TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_required() should return true") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); REQUIRE(sut_is_required(sut)); } TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_used() should return false by default") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_FALSE(sut_is_used(sut)); } TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_used() should return true when argument contains value") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_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") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_EQ(sut_get_nused(sut), 0u); } TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_used() should return 1 when argument contains value") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_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") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_name); REQUIRE_FALSE(sut_has_value(sut)); } TEST_CASE_FIXTURE(positional_argument_test_fixture, "has_value() should return true is value is set") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut_set_value(sut, std::to_string(value_1)); @@ -132,13 +132,13 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "has_value() should return t } TEST_CASE_FIXTURE(positional_argument_test_fixture, "has_parsed_values() should return false by default") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_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") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut_set_value(sut, std::to_string(value_1)); @@ -146,7 +146,7 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "has_parsed_values() should } 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); + auto sut = prepare_argument(primary_name); REQUIRE_NOTHROW(sut_set_value(sut, std::to_string(value_1))); REQUIRE(sut_has_value(sut)); @@ -155,7 +155,7 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "set_value(any) should throw } 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); + auto sut = prepare_argument(primary_name); SUBCASE("given string is empty") { REQUIRE_THROWS_AS(sut_set_value(sut, empty_str), ap::error::invalid_value_error); @@ -172,7 +172,7 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "set_value(any) should throw TEST_CASE_FIXTURE( 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); + auto sut = prepare_argument(primary_name); sut_set_choices(sut, default_choices); @@ -186,7 +186,7 @@ TEST_CASE_FIXTURE( "set_value(any) should accept the given value only when no value has been set yet " "and if the given value is present in the choices set" ) { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut_set_choices(sut, default_choices); const std::vector correct_values = default_choices; @@ -206,7 +206,7 @@ TEST_CASE_FIXTURE( } TEST_CASE_FIXTURE(positional_argument_test_fixture, "set_value(any) should perform the specified action") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); SUBCASE("valued action") { const auto double_valued_action = [](const test_value_type& value) { return 2 * value; }; @@ -231,14 +231,14 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "set_value(any) should perfo } 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); + auto sut = prepare_argument(primary_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") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut_set_value(sut, std::to_string(value_1)); @@ -247,19 +247,19 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "value() should return the a } TEST_CASE_FIXTURE(positional_argument_test_fixture, "values() should throw logic_error") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_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") { - const auto sut = prepare_argument(long_name); + const auto sut = prepare_argument(primary_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") { - auto sut = prepare_argument(long_name); + auto sut = prepare_argument(primary_name); sut_set_value(sut, std::to_string(value_1));