From f7c88e94f8ebb6eed9f32b4f90b2f0866a8523aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Musia=C5=82?= <111433005+SpectraL519@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:10:41 +0200 Subject: [PATCH] resolved comments --- change_log.md | 1 + include/ap/argument_parser.hpp | 9 ----- test/source/test_argument_name.cpp | 38 +++++++++++++++---- .../test_argument_parser_parse_args.cpp | 2 +- test/source/test_optional_argument.cpp | 10 ++--- test/source/test_positional_argument.cpp | 10 ++--- 6 files changed, 40 insertions(+), 30 deletions(-) diff --git a/change_log.md b/change_log.md index e7eda6d..74727e9 100644 --- a/change_log.md +++ b/change_log.md @@ -32,6 +32,7 @@ * Added the `format` workflow * Switched from the `` to the `` library for all current container operations * Modified the `argument_name` structure - renamed members: `name` to `primary`, `short_name` to `secondary` +* Added `argument_name::match(string_view)` and `argument_name::match(argument_name)` functions * Added aliases for default argument enum classes: * `ap::default_argument::positional` = `ap::default_posarg` * `ap::default_argument::optional` = `ap::default_optarg` diff --git a/include/ap/argument_parser.hpp b/include/ap/argument_parser.hpp index ff39f4f..2ed25a3 100644 --- a/include/ap/argument_parser.hpp +++ b/include/ap/argument_parser.hpp @@ -384,15 +384,6 @@ struct argument_name { return false; } - /** - * @brief Equality comparison operator for string variables representing argument names. - * @param name The string view to compare with. - * @return Equality of names comparison (either primary or secondary name). - */ - bool operator==(std::string_view name) const noexcept { - 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->secondary diff --git a/test/source/test_argument_name.cpp b/test/source/test_argument_name.cpp index 570b288..9610acf 100644 --- a/test/source/test_argument_name.cpp +++ b/test/source/test_argument_name.cpp @@ -30,15 +30,13 @@ argument_name default_argument_name_primary_and_secondary() { TEST_SUITE_BEGIN("test_argument_name"); -TEST_CASE("argument_name.primary member should be correctly " - "initialized") { +TEST_CASE("argument_name.primary member should be correctly initialized") { const auto arg_name = default_argument_name_primary(); REQUIRE_EQ(arg_name.primary, primary_name); } -TEST_CASE("argument_name members should be correctly " - "initialized") { +TEST_CASE("argument_name members should be correctly initialized") { const auto arg_name = default_argument_name_primary_and_secondary(); REQUIRE_EQ(arg_name.primary, primary_name); @@ -47,6 +45,15 @@ TEST_CASE("argument_name members should be correctly " REQUIRE_EQ(arg_name.secondary.value(), secondary_name); } +TEST_CASE("argument_name::operator==(argument_name) should return false if only one argument has " + "both primary and secondary values") { + const auto arg_name_a = default_argument_name_primary(); + const auto arg_name_b = default_argument_name_primary_and_secondary(); + + REQUIRE_NE(arg_name_a, arg_name_b); + REQUIRE_NE(arg_name_b, arg_name_a); +} + TEST_CASE("argument_name::operator==(argument_name) should return false if primary names are not " "equal") { const auto arg_name_a = default_argument_name_primary(); @@ -55,13 +62,28 @@ TEST_CASE("argument_name::operator==(argument_name) should return false if prima REQUIRE_NE(arg_name_a, arg_name_b); } -TEST_CASE("argument_name::operator==(argument_name) should return false if only one argument has " - "both primary and secondary values") { +TEST_CASE("argument_name::operator==(argument_name) should return false if secondary names are not " + "equal") { + const auto arg_name_a = default_argument_name_primary_and_secondary(); + const auto arg_name_b = argument_name{ primary_name, other_primary_name }; + + REQUIRE_NE(arg_name_a, arg_name_b); +} + +TEST_CASE("argument_name::operator==(argument_name) should return true if primary names are equal " + "and secondary names are null") { const auto arg_name_a = default_argument_name_primary(); + const auto arg_name_b = default_argument_name_primary(); + + REQUIRE_EQ(arg_name_a, arg_name_b); +} + +TEST_CASE("argument_name::operator==(argument_name) should return true if both primary and " + "secondary names are equal") { + const auto arg_name_a = default_argument_name_primary_and_secondary(); const auto arg_name_b = default_argument_name_primary_and_secondary(); - REQUIRE_NE(arg_name_a, arg_name_b); - REQUIRE_NE(arg_name_b, arg_name_a); + REQUIRE_EQ(arg_name_a, arg_name_b); } TEST_CASE("argument_name::match(string_view) should return true if the given string matches at " diff --git a/test/source/test_argument_parser_parse_args.cpp b/test/source/test_argument_parser_parse_args.cpp index c8506ca..6e273f0 100644 --- a/test/source/test_argument_parser_parse_args.cpp +++ b/test/source/test_argument_parser_parse_args.cpp @@ -59,7 +59,7 @@ TEST_CASE_FIXTURE(argument_parser_test_fixture, "_preprocess_input should return 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).value, prepare_arg_name(opt_arg_idx)); + REQUIRE(prepare_arg_name(opt_arg_idx).match(cmd_args.at(i).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++; diff --git a/test/source/test_optional_argument.cpp b/test/source/test_optional_argument.cpp index 2de0a56..792d3fe 100644 --- a/test/source/test_optional_argument.cpp +++ b/test/source/test_optional_argument.cpp @@ -56,11 +56,10 @@ TEST_CASE_FIXTURE(optional_argument_test_fixture, "is_optional() should return t 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, primary_name); - REQUIRE_NE(name, secondary_name); + REQUIRE(name.match(primary_name)); + REQUIRE_FALSE(name.match(secondary_name)); } TEST_CASE_FIXTURE( @@ -69,11 +68,10 @@ TEST_CASE_FIXTURE( "argument constructor for both primary and secondary names" ) { const auto sut = prepare_argument(primary_name, secondary_name); - const auto name = sut_get_name(sut); - REQUIRE_EQ(name, primary_name); - REQUIRE_EQ(name, secondary_name); + REQUIRE(name.match(primary_name)); + REQUIRE(name.match(secondary_name)); } TEST_CASE_FIXTURE(optional_argument_test_fixture, "help() should return nullopt by default") { diff --git a/test/source/test_positional_argument.cpp b/test/source/test_positional_argument.cpp index 171dbe3..378dee3 100644 --- a/test/source/test_positional_argument.cpp +++ b/test/source/test_positional_argument.cpp @@ -49,11 +49,10 @@ TEST_CASE_FIXTURE(positional_argument_test_fixture, "is_optional() should return 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, primary_name); - REQUIRE_NE(name, secondary_name); + REQUIRE(name.match(primary_name)); + REQUIRE_FALSE(name.match(secondary_name)); } TEST_CASE_FIXTURE( @@ -62,11 +61,10 @@ TEST_CASE_FIXTURE( "argument constructor for both primary and secondary names" ) { const auto sut = prepare_argument(primary_name, secondary_name); - const auto name = sut_get_name(sut); - REQUIRE_EQ(name, primary_name); - REQUIRE_EQ(name, secondary_name); + REQUIRE(name.match(primary_name)); + REQUIRE(name.match(secondary_name)); } TEST_CASE_FIXTURE(positional_argument_test_fixture, "help() should return nullopt by default") {