From 2415be837d125abb3df9e66f956567a3cfa53076 Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Fri, 29 Mar 2024 10:49:49 +0100 Subject: [PATCH] wip: argument_name refactor --- include/ap/argument_parser.hpp | 33 ++++----- test/include/argument_parser_test_fixture.hpp | 6 +- test/source/test_argument_name.cpp | 58 ++++++++------- .../test_argument_parser_add_argument.cpp | 12 ++-- .../test_argument_parser_parse_args.cpp | 70 +++++++++---------- test/source/test_optional_argument.cpp | 4 +- test/source/test_positional_argument.cpp | 4 +- 7 files changed, 91 insertions(+), 96 deletions(-) diff --git a/include/ap/argument_parser.hpp b/include/ap/argument_parser.hpp index dfc9416..5dc7d84 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) {} + explicit argument_name(std::string_view primary) : primary(primary) {} /** * @brief Primary and secondary name constructor. - * @param name The primary name of the argument. - * @param short_name The secondary (short) name of the argument. + * @param primary The primary name of the argument. + * @param secondary 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) {} + explicit 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. */ bool operator==(const argument_name& other) const noexcept { - 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). */ bool operator==(std::string_view name) const noexcept { - 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]] std::string str() const noexcept { - 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 (short) name of the argument. }; /// @brief Argument class interface @@ -792,7 +789,7 @@ class positional_argument : public detail::argument_interface { /// @return Reference to the vector of parsed values for the positional argument. [[nodiscard]] 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."); } /** diff --git a/test/include/argument_parser_test_fixture.hpp b/test/include/argument_parser_test_fixture.hpp index 0b7cffb..fd29801 100644 --- a/test/include/argument_parser_test_fixture.hpp +++ b/test/include/argument_parser_test_fixture.hpp @@ -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..cfad221 100644 --- a/test/source/test_argument_name.cpp +++ b/test/source/test_argument_name.cpp @@ -12,44 +12,43 @@ 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); + return argument_name(primary_name); } argument_name default_argument_name_both_names() { - return argument_name(name, short_name); + 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(); - 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(); - 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") { + "return true if primary names are equal") { const auto arg_name_a = default_argument_name_long_name(); const auto arg_name_b = default_argument_name_both_names(); @@ -57,60 +56,59 @@ TEST_CASE("argument_name::operator==(argument_name) should " } TEST_CASE("argument_name::operator==(argument_name) should " - "return false if " - "long names are not equal") { + "return false if primary 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); + const auto arg_name_b = argument_name(other_primary_name, other_secondary_name); REQUIRE_FALSE(arg_name_a == arg_name_b); } TEST_CASE("argument_name::operator==(string_view) should " "return true if at " - "least one name matches") { - SUBCASE("argument_name with long name only") { + "least one primary_name matches") { + SUBCASE("argument_name with long primary_name only") { const auto arg_name = default_argument_name_long_name(); - REQUIRE(arg_name == name); + REQUIRE(arg_name == primary_name); } SUBCASE("argument_name with both names") { const auto arg_name = default_argument_name_both_names(); - 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") { + "primary_name matches") { + SUBCASE("argument_name with long primary_name only") { const auto arg_name = default_argument_name_long_name(); - 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(); - 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") { + SUBCASE("argument_name with long primary_name only") { ss << default_argument_name_long_name(); - expected_ss << "[" << name << "]"; + expected_ss << "[" << primary_name << "]"; } SUBCASE("argument_name with both names") { ss << default_argument_name_both_names(); - expected_ss << "[" << name << "," << short_name << "]"; + 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..8a0e12d 100644 --- a/test/source/test_argument_parser_add_argument.cpp +++ b/test/source/test_argument_parser_add_argument.cpp @@ -65,11 +65,11 @@ TEST_CASE_FIXTURE( REQUIRE_NOTHROW(sut.add_positional_argument(other_name, other_short_name)); } - SUBCASE("adding argument with a previously used long name") { + SUBCASE("adding argument with a previously used primary 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 short name") { + SUBCASE("adding argument with a previously used secondary name") { REQUIRE_THROWS_AS(sut.add_positional_argument(other_name, short_name), ap::error::argument_name_used_error); } } @@ -90,11 +90,11 @@ TEST_CASE_FIXTURE( REQUIRE_NOTHROW(sut.add_optional_argument(other_name, other_short_name)); } - SUBCASE("adding argument with a previously used long name") { + SUBCASE("adding argument with a previously used primary 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 short name") { + SUBCASE("adding argument with a previously used secondary name") { REQUIRE_THROWS_AS(sut.add_optional_argument(other_name, short_name), ap::error::argument_name_used_error); } } @@ -130,11 +130,11 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "add_flag should throw only when REQUIRE_NOTHROW(sut.add_flag(other_name, other_short_name)); } - SUBCASE("adding argument with a previously used long name") { + SUBCASE("adding argument with a previously used primary name") { 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") { + SUBCASE("adding argument with a previously used secondary name") { REQUIRE_THROWS_AS(sut.add_flag(other_name, short_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 a435e64..fd81c45 100644 --- a/test/source/test_argument_parser_parse_args.cpp +++ b/test/source/test_argument_parser_parse_args.cpp @@ -118,8 +118,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())); } } @@ -129,7 +129,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); @@ -146,7 +146,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); @@ -161,7 +161,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; @@ -198,7 +198,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) @@ -226,7 +226,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); } @@ -250,7 +250,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; @@ -266,8 +266,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") { @@ -283,18 +283,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())); } } @@ -327,8 +327,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); } } @@ -337,7 +337,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; @@ -353,18 +353,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); @@ -385,8 +385,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); @@ -401,7 +401,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; @@ -414,9 +414,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); @@ -432,8 +432,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); } } @@ -511,12 +511,12 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "values() should throw when call 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") { + SUBCASE("calling with argument's primary name") { const auto& values = sut.values(optional_arg_name); REQUIRE(values.empty()); } - SUBCASE("calling with argument's short name") { + SUBCASE("calling with argument's secondary name") { const auto& values = sut.values(optional_arg_short_name); REQUIRE(values.empty()); } diff --git a/test/source/test_optional_argument.cpp b/test/source/test_optional_argument.cpp index dbca2f8..fd8fe7d 100644 --- a/test/source/test_optional_argument.cpp +++ b/test/source/test_optional_argument.cpp @@ -53,7 +53,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_optional() should return t 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 primary name") { const auto sut = prepare_argument(long_name); const auto name = sut_get_name(sut); @@ -65,7 +65,7 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "name() should return value pa 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" + "argument constructor for both primary and secondary names" ) { const auto sut = prepare_argument(long_name, short_name); diff --git a/test/source/test_positional_argument.cpp b/test/source/test_positional_argument.cpp index d03c6af..4041736 100644 --- a/test/source/test_positional_argument.cpp +++ b/test/source/test_positional_argument.cpp @@ -45,7 +45,7 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_optional() should return 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 primary name") { const auto sut = prepare_argument(long_name); const auto name = sut_get_name(sut); @@ -57,7 +57,7 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "name() should return value 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" + "argument constructor for both primary and secondary names" ) { const auto sut = prepare_argument(long_name, short_name);